From 4eaed57a0c3a95069037096319638f7ccf583d58 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Tue, 8 Dec 2020 15:14:42 +0000 Subject: [PATCH] use client display bar --- .gitignore | 3 +- Makefile | 31 ++++++----- client.c | 142 ++++++++++++++++++++++++++++---------------------- display_bar.c | 2 +- display_bar.h | 2 +- server.c | 4 +- 6 files changed, 103 insertions(+), 81 deletions(-) diff --git a/.gitignore b/.gitignore index 533743c..950c1b4 100644 --- a/.gitignore +++ b/.gitignore @@ -60,4 +60,5 @@ p-client p-server server client -slowclient \ No newline at end of file +slowclient +slowserver \ No newline at end of file diff --git a/Makefile b/Makefile index fe4e98f..69f95b5 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ CFLAGS = -lm -Wall -O2 -pthread ServerBin = server p-server ClientBin = client p-client Bin = $(ServerBin) $(ClientBin) - +ClientObject = display_bar.o socket_wrapper.o +ServerObject = display_bar.o socket_wrapper.o all: make $(Bin) @@ -11,19 +12,23 @@ socket_wrapper.o: socket_wrapper.c socket_wrapper.h $(CC) -c socket_wrapper.c -o socket_wrapper.o $(CFLAGS) display_bar.o: display_bar.c display_bar.h $(CC) -c display_bar.c -o display_bar.o $(CFLAGS) - -client: socket_wrapper.o client.c - $(CC) -o client client.c socket_wrapper.o $(CFLAGS) -server: socket_wrapper.o server.c display_bar.o - $(CC) -o server server.c socket_wrapper.o display_bar.o $(CFLAGS) -p-server: socket_wrapper.o p-server.c - $(CC) -o p-server p-server.c socket_wrapper.o $(CFLAGS) -p-client: socket_wrapper.o p-client.c +#client +client: client.c $(ClientObject) + $(CC) -o client client.c $(ClientObject) $(CFLAGS) +p-client: p-client.c $(ClientObject) $(CC) -o p-client p-client.c socket_wrapper.o $(CFLAGS) -p-mulclient: socket_wrapper.o p-client.c - $(CC) -o p-slowclient p-client.c socket_wrapper.o $(CFLAGS) -D MUL_CLIENT=10 -slowclient: socket_wrapper.o client.c - $(CC) -o slowclient client.c socket_wrapper.o $(CFLAGS) -D SLOW_CLIENT=1000 +p-mulclient: p-client.c $(ClientObject) + $(CC) -o p-slowclient p-client.c $(ClientObject) $(CFLAGS) -D MUL_CLIENT=10 +slowclient: client.c $(ClientObject) + $(CC) -o slowclient client.c $(ClientObject) $(CFLAGS) -D SLOW_CLIENT=1000 +#server +server: server.c $(ServerObject) + $(CC) -o server server.c $(ServerObject) $(CFLAGS) +p-server: p-server.c $(ServerObject) + $(CC) -o p-server p-server.c $(ServerObject) $(CFLAGS) +slowserver: server.c $(ServerObject) + $(CC) -o slowserver server.c $(ServerObject) $(CFLAGS) -D SLOW_SERVER=100000 + .PHONY: clean test clean: diff --git a/client.c b/client.c index 8d69f66..0c94da2 100644 --- a/client.c +++ b/client.c @@ -16,21 +16,26 @@ #include #include "socket_wrapper.h" #include "timerhelper.h" +#include "display_bar.h" #ifndef DEFAULT_TIMEOUT static const int TIMEOUT = 5; #else static const int TIMEOUT = DEFAULT_TIMEOUT; #endif -#ifndef DEFAULT_PROGRESS_BAR_WIDTH -static const int PROGRESS_BAR_WIDTH = 30; -#else -static const int PROGRESS_BAR_WIDTH = DEFAULT_PROGRESS_BAR_WIDTH; -#endif enum{ FILENAME_BUF_SIZE = 1024 }; + +static bool DisplayProgress = true; + +static inline void server_perror(const char * msg){ + if(DisplayProgress) + myd_perror(msg); + else + perror(msg); +} /*======== *Operation *========*/ @@ -40,93 +45,63 @@ int sendReadOp(int sock,const char * filename){ op.file_url_size = strlen(filename); op.padding0 = 0; if(send(sock,&op,sizeof(op),0)<0){ - perror("readop send fail"); + server_perror("readop send fail"); return -1; } #ifdef MUL_CLIENT sleep(MUL_CLIENT); #endif if(send(sock,filename,op.file_url_size,0)<0){ - perror("readop filename send fail"); + server_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+1]; - for (i = 0; i < PROGRESS_BAR_WIDTH; i++){ - buf[i] = '='; - } - buf[PROGRESS_BAR_WIDTH] = '\0'; - printf("\r[%s]: 100%% bytes: %ld/%ld bytes\n",buf,total,total); -} -static bool DisplayProgress = true; + int recvFile(int sock, const char * filename,size_t file_size){ int fd; size_t count = 0; int i; - double cur_progress = 1; int return_value = 0; int buf_sz = getBufferSizeFrom(sock); + progress_bar_t pbar; uint8_t * buf = malloc(buf_sz*sizeof(*buf)); if (buf == NULL){ return -1; } fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH); - if (fd < 0) - { - perror("file open fail"); + if (fd < 0){ + server_perror("file open fail"); return -1; } - + if(DisplayProgress){ + init_progress_bar(&pbar,10); + } while (file_size - count > 0) { int readed = buf_sz < file_size - count ? buf_sz : file_size - count; + if(DisplayProgress){ + DisplayProgressBar(&pbar,count,file_size,filename,false); + } if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){ if(i == -2) fprintf(stderr,"recv file failed : timeout connetion lost\n"); - else perror("recv file failed"); + else server_perror("recv file failed"); return_value = -1; goto END; } if(write(fd,buf,readed)<0){ - perror("file write failed"); + server_perror("file write failed"); return_value = -1; goto END; } - if( DisplayProgress && 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; #ifdef SLOW_CLIENT usleep(SLOW_CLIENT); #endif } if(DisplayProgress) - DisplayProgressBar100Percent(file_size); + DisplayProgressBar(&pbar,count,file_size,filename,true); END: free(buf); close(fd); @@ -137,29 +112,56 @@ int recvData(int sock,const char * filename){ struct TransferResult res; int i=0; if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){ - if (i == -2) fprintf(stderr,"timeout\n"); - else perror("recv fail"); + if (i == -2) { + if(DisplayProgress && isatty_file(stderr)){ + lock_scrolled(); + add_scrolled_unlocked(1); + fprintf(stderr,"timeout\n"); + unlock_scrolled(); + } + else fprintf(stderr,"timeout\n"); + } + else server_perror("recv fail"); return -1; } - char error_meesage_buf[80] = ""; + char error_meesage_buf[256] = ""; switch(res.res){ case RES_ERR: - fprintf(stderr,"Server Fail: %s\n", strerror(res.err_number)); + strerror_r(res.err_number,error_meesage_buf,sizeof(error_meesage_buf)); + if(DisplayProgress && isatty_file(stderr)){ + lock_scrolled(); + add_scrolled_unlocked(1); + fprintf(stderr,"Server Fail: %s\n", error_meesage_buf); + unlock_scrolled(); + } + else fprintf(stderr,"Server Fail: %s\n", error_meesage_buf); return -1; case RES_USR_ERR: assert(res.error_msg_size < 80);/*todo : fix*/ if((i=recv_until_byte(sock,error_meesage_buf,res.error_msg_size,TIMEOUT)) < 0){ if (i == -2) fprintf(stderr,"timeout\n"); - else perror("recv fail"); + else server_perror("recv fail"); return -1; } - fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf); + if(DisplayProgress && isatty_file(stderr)){ + lock_scrolled(); + add_scrolled_unlocked(1); + fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf); + unlock_scrolled(); + } + else fprintf(stderr,"Error Message From Server: %s\n",error_meesage_buf); return -1; case RES_OK: return recvFile(sock,filename,res.file_size); break; default: - fprintf(stderr,"unknown value!\n"); + if(DisplayProgress && isatty_file(stderr)){ + lock_scrolled(); + add_scrolled_unlocked(1); + fprintf(stderr,"unknown value!\n"); + unlock_scrolled(); + } + else fprintf(stderr,"unknown value!\n"); return -1; } @@ -170,17 +172,23 @@ static bool quiet_mode = false; int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){ int sock; int ret = -1; - if(!quiet_mode) - fprintf(stdout,"request %s\n",filename); - + if(!quiet_mode){ + if(DisplayProgress && isatty_file(stdout)){ + lock_scrolled(); + add_scrolled_unlocked(1); + fprintf(stdout,"request %s\n",filename); + unlock_scrolled(); + } + else fprintf(stdout,"request %s\n",filename); + } sock = socket(AF_INET,SOCK_STREAM,0); if(sock < 0){ - perror("sock create fail"); + server_perror("sock create fail"); return -1; } if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){ - perror("connect failed"); + server_perror("connect failed"); return -1; } if(sendReadOp(sock,filename) == 0){ @@ -343,6 +351,7 @@ static const char * show_help_message(FILE * file,const char * argv0){ "Options and arguments: \n" "-b or --benchmark\t:benchmark mode\n" "-nv or --no-verbose\t:no progress bar\n" + "-q or --quiet\t:show no message\n" "-h\t:print help message.\n"; fprintf(file,msg,argv0); return msg; @@ -389,6 +398,7 @@ static int parse_arg(int argc,const char *argv[]){ else if(strcmp("-q",argv[cur]) == 0 || strcmp("--quiet",argv[cur]) == 0){ cmd_args.quiet_mode = true; quiet_mode = true; + DisplayProgress = false; } else break; } @@ -413,7 +423,13 @@ int main(int argc, const char *argv[]){ err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr); if (err != 0){ int check; - fprintf(stderr,"netdb fail: %s\n",gai_strerror(err)); + if(DisplayProgress && isatty_file(stderr)){ + lock_scrolled(); + add_scrolled_unlocked(1); + fprintf(stderr,"netdb fail: %s\n",gai_strerror(err)); + unlock_scrolled(); + } + else fprintf(stderr,"netdb fail: %s\n",gai_strerror(err)); //assume that sernmae is *.*.*.* and try to parse addr check = inet_pton(AF_INET,cmd_args.server_name,&addr.sin_addr); assert(check != -1); diff --git a/display_bar.c b/display_bar.c index 2af79dd..1237c8d 100644 --- a/display_bar.c +++ b/display_bar.c @@ -108,7 +108,7 @@ void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const ch clock_gettime(CLOCK_REALTIME,&ts); if (!sync){ dif = timespec_sub(ts,bar->last_update); - diff = dif.tv_nsec / 1000000 + dif.tv_sec; + diff = dif.tv_nsec / 1000000 + dif.tv_sec*1000; if (diff < bar->update_rate){ return; } diff --git a/display_bar.h b/display_bar.h index b83fea6..2909de9 100644 --- a/display_bar.h +++ b/display_bar.h @@ -29,7 +29,7 @@ typedef struct { int update_rate; struct timespec last_update; } progress_bar_t; - +//update rate is millsecond unit void init_progress_bar(progress_bar_t * bar,int update_rate); void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const char * filename, bool sync); diff --git a/server.c b/server.c index 44ae211..40915c4 100644 --- a/server.c +++ b/server.c @@ -229,6 +229,8 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ #else while (offset < r.file_size) { + if(use_gui) + DisplayProgressBar(&pbar,offset,r.file_size,"",false); readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset; if(read(fd,buf,readed)<0){ server_perror("send response read fail"); @@ -239,8 +241,6 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ return -1; } offset += readed; - if(use_gui) - DisplayProgressBar(&pbar,offset,r.file_size,"",false); #ifdef SLOW_SERVER usleep(SLOW_SERVER_TIME); #endif