add client progress bar
This commit is contained in:
parent
6d6b446640
commit
1c0c6d923c
46
client.c
46
client.c
@ -9,7 +9,7 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include "socket_wrapper.h"
|
||||
@ -28,6 +28,7 @@ static const int TIMEOUT = 5;
|
||||
static const int TIMEOUT = DEFAULT_TIMEOUT;
|
||||
#endif
|
||||
|
||||
static const int PROGRESS_BAR_WIDTH = 30;
|
||||
/*========
|
||||
*Operation
|
||||
*========*/
|
||||
@ -40,13 +41,43 @@ int sendReadOp(int sock,const char * filename){
|
||||
perror("readop send fail");
|
||||
return -1;
|
||||
}
|
||||
#ifdef SLOW_CLIENT
|
||||
sleep(SLOW_CLIENT);
|
||||
#endif
|
||||
if(send(sock,filename,op.file_url_size,0)<0){
|
||||
perror("readop filename send fail");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @arg cur_progress : it is percentage.
|
||||
*/
|
||||
bool isProgressBarNeedUpdate(size_t offset,size_t total,double cur_progress){
|
||||
return ((double)offset / (double)total) > (cur_progress / 100.0);
|
||||
}
|
||||
void DisplayProgressBar(size_t offset,size_t total,double cur_progress){
|
||||
char buf[PROGRESS_BAR_WIDTH];
|
||||
size_t i;
|
||||
size_t cur_pos = (cur_progress / 100.0 * PROGRESS_BAR_WIDTH); //must be less than SIZE_MAX. other value is undefined behavior.
|
||||
for (i = 0; i < PROGRESS_BAR_WIDTH; i++)
|
||||
{
|
||||
if (i < cur_pos)
|
||||
buf[i] = '=';
|
||||
else if(i == cur_pos)
|
||||
buf[i] = '>';
|
||||
else buf[i] = ' ';
|
||||
}
|
||||
printf("\r[%s]: %.2f%% bytes: %ld/%ld bytes",buf,cur_progress,total,offset);
|
||||
}
|
||||
void DisplayProgressBar100Percent(size_t total){
|
||||
size_t i;
|
||||
char buf[PROGRESS_BAR_WIDTH];
|
||||
for (i = 0; i < PROGRESS_BAR_WIDTH; i++){
|
||||
buf[i] = '=';
|
||||
}
|
||||
printf("\r[%s]: 100%% bytes: %ld/%ld bytes\n",buf,total,total);
|
||||
}
|
||||
int recvFile(int sock, const char * filename,size_t file_size){
|
||||
int fd;
|
||||
size_t count = 0;
|
||||
@ -80,14 +111,14 @@ int recvFile(int sock, const char * filename,size_t file_size){
|
||||
return_value = -1;
|
||||
goto END;
|
||||
}
|
||||
if( ((double)count / (double)file_size) * 100.0 > ((double)cur_progress) ){
|
||||
printf("\rprogress : %d%% current bytes: %ld bytes",cur_progress,count);
|
||||
if( isProgressBarNeedUpdate(count,file_size,cur_progress) ){
|
||||
DisplayProgressBar(count,file_size,cur_progress);
|
||||
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
|
||||
fflush(stdout);
|
||||
}
|
||||
count += readed;
|
||||
}
|
||||
printf("\rprogress : 100.00%% current bytes: %ld bytes\n",count);
|
||||
DisplayProgressBar100Percent(file_size);
|
||||
END:
|
||||
free(buf);
|
||||
return return_value;
|
||||
@ -130,7 +161,7 @@ int main(int argc, const char *argv[]){
|
||||
struct sockaddr_in addr;
|
||||
const char * filename;
|
||||
const char * server_name;
|
||||
in_port_t server_port;
|
||||
in_port_t server_port = 0;
|
||||
int sock, err;
|
||||
if (argc != 4){
|
||||
fprintf(stderr,"USAUE: %s SERVERNAME PORT FILENAME\n",argv[0]);
|
||||
@ -149,7 +180,7 @@ int main(int argc, const char *argv[]){
|
||||
perror("sock create fail");
|
||||
return 1;
|
||||
}
|
||||
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,server_name,((struct sockaddr *)&addr));
|
||||
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,server_name,(struct sockaddr *)&addr);
|
||||
if (err != 0){
|
||||
int check;
|
||||
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err));
|
||||
@ -166,7 +197,6 @@ int main(int argc, const char *argv[]){
|
||||
addr.sin_port = htons(server_port);
|
||||
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){
|
||||
perror("connect failed");
|
||||
close(sock);
|
||||
return 1;
|
||||
}
|
||||
if(sendReadOp(sock,filename) == 0){
|
||||
|
42
p-client.c
42
p-client.c
@ -9,9 +9,10 @@
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "socket_wrapper.h"
|
||||
|
||||
#ifdef USE_SENDFILE
|
||||
@ -27,6 +28,8 @@ static const int TIMEOUT = 5;
|
||||
#else
|
||||
static const int TIMEOUT = DEFAULT_TIMEOUT;
|
||||
#endif
|
||||
|
||||
static const int PROGRESS_BAR_WIDTH = 30;
|
||||
/*========
|
||||
*Operation
|
||||
*========*/
|
||||
@ -48,12 +51,39 @@ int sendReadOp(int sock,const char * filename){
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @arg cur_progress : it is percentage.
|
||||
*/
|
||||
bool isProgressBarNeedUpdate(size_t offset,size_t total,double cur_progress){
|
||||
return ((double)offset / (double)total) > (cur_progress / 100.0);
|
||||
}
|
||||
void DisplayProgressBar(size_t offset,size_t total,double cur_progress){
|
||||
char buf[PROGRESS_BAR_WIDTH];
|
||||
size_t i;
|
||||
size_t cur_pos = (cur_progress / 100.0 * PROGRESS_BAR_WIDTH); //must be less than SIZE_MAX. other value is undefined behavior.
|
||||
for (i = 0; i < PROGRESS_BAR_WIDTH; i++)
|
||||
{
|
||||
if (i < cur_pos)
|
||||
buf[i] = '=';
|
||||
else if(i == cur_pos)
|
||||
buf[i] = '>';
|
||||
else buf[i] = ' ';
|
||||
}
|
||||
printf("\r[%s]: %.2f%% bytes: %ld/%ld bytes",buf,cur_progress,total,offset);
|
||||
}
|
||||
void DisplayProgressBar100Percent(size_t total){
|
||||
size_t i;
|
||||
char buf[PROGRESS_BAR_WIDTH];
|
||||
for (i = 0; i < PROGRESS_BAR_WIDTH; i++){
|
||||
buf[i] = '=';
|
||||
}
|
||||
printf("\r[%s]: 100%% bytes: %ld/%ld bytes\n",buf,total,total);
|
||||
}
|
||||
int recvFile(int sock, const char * filename,size_t file_size){
|
||||
int fd;
|
||||
size_t count = 0;
|
||||
int i;
|
||||
int cur_progress = 1;
|
||||
double cur_progress = 1;
|
||||
int return_value = 0;
|
||||
int buf_sz = getBufferSizeFrom(sock);
|
||||
uint8_t * buf = malloc(buf_sz*sizeof(*buf));
|
||||
@ -82,14 +112,14 @@ int recvFile(int sock, const char * filename,size_t file_size){
|
||||
return_value = -1;
|
||||
goto END;
|
||||
}
|
||||
if( ((double)count / (double)file_size) * 100.0 > ((double)cur_progress) ){
|
||||
printf("\rprogress : %d%% current bytes: %ld bytes",cur_progress,count);
|
||||
if( isProgressBarNeedUpdate(count,file_size,cur_progress) ){
|
||||
DisplayProgressBar(count,file_size,cur_progress);
|
||||
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
|
||||
fflush(stdout);
|
||||
}
|
||||
count += readed;
|
||||
}
|
||||
printf("\rprogress : 100.00%% current bytes: %ld bytes\n",count);
|
||||
DisplayProgressBar100Percent(file_size);
|
||||
END:
|
||||
free(buf);
|
||||
return return_value;
|
||||
|
Loading…
Reference in New Issue
Block a user