From c23db73d710d4758303b761cf414fe261984dd31 Mon Sep 17 00:00:00 2001 From: ubuntu201711081 <201711081@jbnu.ac.kr> Date: Tue, 8 Dec 2020 08:38:35 +0000 Subject: [PATCH] add gui option --- server.c | 79 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/server.c b/server.c index 6411a39..2dc399c 100644 --- a/server.c +++ b/server.c @@ -66,6 +66,15 @@ enum{ static const int SLOW_SERVER_TIME = SLOW_SERVER; #endif +static bool use_gui = false; + +static inline void server_perror(const char * msg){ + if(use_gui) + myd_perror(msg); + else + perror(msg); +} + /*======== *Operation *========*/ @@ -83,11 +92,11 @@ int send_fail(int sock,const char * msg){ res.error_msg_size = strlen(msg); //os will be combining if tcp_autocorking emabled. if(send(sock,&res,sizeof(res),0) < 0){ - myd_perror("error msg send"); + server_perror("error msg send"); return -1; } if (send(sock,msg,res.error_msg_size,0) < 0){ - myd_perror("error msg send"); + server_perror("error msg send"); return -1; } return 0; @@ -103,7 +112,7 @@ int send_errno(int sock){ r.file_size = 0; r.error_msg_size = 0; if(send(sock,&r,sizeof(r),0)){ - myd_perror("errno send"); + server_perror("errno send"); return -1; } return 0; @@ -118,7 +127,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){ ssize_t n = recv_until_byte(sock,&p,sizeof(p),TIMEOUT); if (n < 0){ if (n == -2) fprintf(stderr,"timeout!"); - else myd_perror("receive fail"); + else server_perror("receive fail"); return -1; } if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){ @@ -135,7 +144,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){ } n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT); buf[p.file_url_size] = '\0'; //truncate - if (isatty_file(stdout)){ + if(use_gui&&isatty_file(stdout)){ lock_scrolled(); add_scrolled_unlocked(1); fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf); @@ -175,19 +184,21 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ } r.file_size = st.st_size; if(send(sock,&r,sizeof(r),0)<0){ - myd_perror("send fail"); + server_perror("send fail"); return -1; } - init_progress_bar(&pbar,10); + if(use_gui) + init_progress_bar(&pbar,10); #ifdef USE_SENDFILE while (r.file_size != offset) { size_t count = SEND_FILE_CHUNK_SIZE < (r.file_size - offset) ? SEND_FILE_CHUNK_SIZE : (r.file_size - offset); if((readed = sendfile(sock,fd,&offset,count)) < 0){ - myd_perror("send file fail"); + server_perror("send file fail"); return -1; } - DisplayProgressBar(&pbar,offset,r.file_size,"",false); + if(use_gui) + DisplayProgressBar(&pbar,offset,r.file_size,"",false); #ifdef SLOW_SERVER usleep(SLOW_SERVER_TIME); #endif @@ -197,20 +208,22 @@ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ { readed = bufsize < (r.file_size - offset) ? bufsize : r.file_size - offset; if(read(fd,buf,readed)<0){ - myd_perror("send response read fail"); + server_perror("send response read fail"); return -1; } if(send(sock,buf,readed,0)<0){ - myd_perror("send response send fail"); + server_perror("send response send fail"); return -1; } offset += readed; - DisplayProgressBar(&pbar,offset,r.file_size,"",false); + if(use_gui) + DisplayProgressBar(&pbar,offset,r.file_size,"",false); #ifdef SLOW_SERVER usleep(SLOW_SERVER_TIME); #endif } - DisplayProgressBar(&pbar,offset,r.file_size,"",true); + if(use_gui) + DisplayProgressBar(&pbar,offset,r.file_size,"",true); #endif return 0; } @@ -218,7 +231,8 @@ const char * help(const char * n){ const char * msg = "USASE : %s [Option] ...\n" "Options and arguments: \n" "-p port\t:set to port binding. couldn't set to 0\n" - "-h\t:print help message.\n"; + "-h\t:print help message.\n" + "--progress_bar\t: show pretty progress bar"; printf(msg,n); return msg; } @@ -226,11 +240,9 @@ const char * help(const char * n){ int parse_args(int argc,const char * argv[] , in_port_t * port){ int pos = 1; const char * opt; - while (pos < argc) - { + while (pos < argc){ opt = argv[pos++]; - if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0) - { + if (strcmp(opt,"-h") == 0 || strcmp(opt,"--help") == 0){ help(argv[0]); return 0; } @@ -248,6 +260,14 @@ int parse_args(int argc,const char * argv[] , in_port_t * port){ return 2; //failed to find argument. } } + else if(strcmp(opt,"--progress_bar") == 0){ + use_gui = true; + } + else{ + fprintf(stderr,"unknown option\n"); + help(argv[0]); + return 2; + } } return 0; } @@ -347,7 +367,7 @@ void * worker_proc(void * data){ clock_gettime(Trace_Timer_ID,&ts_end); struct timespec tophalf = timespec_sub(ts_middle,ts); struct timespec bottomhelf = timespec_sub(ts_end,ts_middle); - if(isatty_file(stderr)){ + if(use_gui&&isatty_file(stderr)){ lock_scrolled(); add_scrolled_unlocked(1); fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); @@ -355,7 +375,7 @@ void * worker_proc(void * data){ } #endif if(close(csock) < 0) - myd_perror("csock close error"); + server_perror("csock close error"); } destory_worker_argument(args); return NULL; @@ -379,7 +399,7 @@ void * worker_proc(void * data){ clock_gettime(Trace_Timer_ID,&ts_end); struct timespec tophalf = timespec_sub(ts_middle,ts); struct timespec bottomhelf = timespec_sub(ts_end,ts_middle); - if(isatty_file(stderr)){ + if(use_gui&&isatty_file(stderr)){ lock_scrolled(); add_scrolled_unlocked(1); fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); @@ -388,7 +408,7 @@ void * worker_proc(void * data){ else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhelf.tv_nsec); #endif if(close(csock) < 0) - myd_perror("csock close error"); + server_perror("csock close error"); destory_worker_argument(args); return NULL; } @@ -411,17 +431,18 @@ int main(int argc, const char *argv[]){ int d = parse_args(argc,argv,&binding_port_number); if(d != 0 ) return d; } - ready_progress_bar(); + if(use_gui) + ready_progress_bar(); sock = socket(AF_INET,SOCK_STREAM,0); atexit(safe_exit); if(sock < 0){ - myd_perror("sock create fail"); + server_perror("sock create fail"); return 1; } else { int option = 1; if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option)) < 0){ - myd_perror("setsockopt"); + server_perror("setsockopt"); } } bufsize = getBufferSizeFrom(sock); @@ -441,13 +462,13 @@ int main(int argc, const char *argv[]){ addr.sin_port = htons(binding_port_number); if(bind(sock, (struct sockaddr *)&addr,sizeof(addr)) < 0){ - myd_perror("bind failed"); + server_perror("bind failed"); return 1; } else { char ip_buf[INET_ADDRSTRLEN]; const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf)); assert(msg != NULL); - if(isatty_file(stdout)){ + if(use_gui&&isatty_file(stdout)){ lock_scrolled(); add_scrolled_unlocked(1); fprintf(stdout,"server bind on %s:%d\n",msg ,binding_port_number); @@ -457,7 +478,7 @@ int main(int argc, const char *argv[]){ } if(listen(sock,MAX_LISTEN_SOCKET) < 0){ - myd_perror("listen failed"); + server_perror("listen failed"); return 1; } @@ -465,7 +486,7 @@ int main(int argc, const char *argv[]){ { char ip_buf[INET_ADDRSTRLEN]; const char * msg = inet_ntop(AF_INET,&client_addr.sin_addr,ip_buf,sizeof(ip_buf)); - if(isatty_file(stdout)){ + if(use_gui&&isatty_file(stdout)){ lock_scrolled(); add_scrolled_unlocked(1); fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));