add client progress bar

This commit is contained in:
ubuntu201711081 2020-12-04 11:11:35 +00:00
parent 6d6b446640
commit 1c0c6d923c
2 changed files with 74 additions and 14 deletions

View File

@ -9,7 +9,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
@ -28,6 +28,7 @@ static const int TIMEOUT = 5;
static const int TIMEOUT = DEFAULT_TIMEOUT; static const int TIMEOUT = DEFAULT_TIMEOUT;
#endif #endif
static const int PROGRESS_BAR_WIDTH = 30;
/*======== /*========
*Operation *Operation
*========*/ *========*/
@ -40,13 +41,43 @@ int sendReadOp(int sock,const char * filename){
perror("readop send fail"); perror("readop send fail");
return -1; return -1;
} }
#ifdef SLOW_CLIENT
sleep(SLOW_CLIENT);
#endif
if(send(sock,filename,op.file_url_size,0)<0){ if(send(sock,filename,op.file_url_size,0)<0){
perror("readop filename send fail"); perror("readop filename send fail");
return -1; return -1;
} }
return 0; 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 recvFile(int sock, const char * filename,size_t file_size){
int fd; int fd;
size_t count = 0; size_t count = 0;
@ -80,14 +111,14 @@ int recvFile(int sock, const char * filename,size_t file_size){
return_value = -1; return_value = -1;
goto END; goto END;
} }
if( ((double)count / (double)file_size) * 100.0 > ((double)cur_progress) ){ if( isProgressBarNeedUpdate(count,file_size,cur_progress) ){
printf("\rprogress : %d%% current bytes: %ld bytes",cur_progress,count); DisplayProgressBar(count,file_size,cur_progress);
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0); cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
fflush(stdout); fflush(stdout);
} }
count += readed; count += readed;
} }
printf("\rprogress : 100.00%% current bytes: %ld bytes\n",count); DisplayProgressBar100Percent(file_size);
END: END:
free(buf); free(buf);
return return_value; return return_value;
@ -130,7 +161,7 @@ int main(int argc, const char *argv[]){
struct sockaddr_in addr; struct sockaddr_in addr;
const char * filename; const char * filename;
const char * server_name; const char * server_name;
in_port_t server_port; in_port_t server_port = 0;
int sock, err; int sock, err;
if (argc != 4){ if (argc != 4){
fprintf(stderr,"USAUE: %s SERVERNAME PORT FILENAME\n",argv[0]); 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"); perror("sock create fail");
return 1; 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){ if (err != 0){
int check; int check;
fprintf(stderr,"netdb fail: %s\n",gai_strerror(err)); 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); addr.sin_port = htons(server_port);
if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){ if(connect(sock,(struct sockaddr *)&addr,sizeof(addr)) < 0){
perror("connect failed"); perror("connect failed");
close(sock);
return 1; return 1;
} }
if(sendReadOp(sock,filename) == 0){ if(sendReadOp(sock,filename) == 0){

View File

@ -9,9 +9,10 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdbool.h>
#include <assert.h> #include <assert.h>
#include <fcntl.h> #include <fcntl.h>
#include "socket_wrapper.h" #include "socket_wrapper.h"
#ifdef USE_SENDFILE #ifdef USE_SENDFILE
@ -27,6 +28,8 @@ static const int TIMEOUT = 5;
#else #else
static const int TIMEOUT = DEFAULT_TIMEOUT; static const int TIMEOUT = DEFAULT_TIMEOUT;
#endif #endif
static const int PROGRESS_BAR_WIDTH = 30;
/*======== /*========
*Operation *Operation
*========*/ *========*/
@ -48,12 +51,39 @@ int sendReadOp(int sock,const char * filename){
} }
return 0; 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 recvFile(int sock, const char * filename,size_t file_size){
int fd; int fd;
size_t count = 0; size_t count = 0;
int i; int i;
int cur_progress = 1; double cur_progress = 1;
int return_value = 0; int return_value = 0;
int buf_sz = getBufferSizeFrom(sock); int buf_sz = getBufferSizeFrom(sock);
uint8_t * buf = malloc(buf_sz*sizeof(*buf)); 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; return_value = -1;
goto END; goto END;
} }
if( ((double)count / (double)file_size) * 100.0 > ((double)cur_progress) ){ if( isProgressBarNeedUpdate(count,file_size,cur_progress) ){
printf("\rprogress : %d%% current bytes: %ld bytes",cur_progress,count); DisplayProgressBar(count,file_size,cur_progress);
cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0); cur_progress = (int)((((double)count / (double)file_size)) * 100.0 + 1.0);
fflush(stdout); fflush(stdout);
} }
count += readed; count += readed;
} }
printf("\rprogress : 100.00%% current bytes: %ld bytes\n",count); DisplayProgressBar100Percent(file_size);
END: END:
free(buf); free(buf);
return return_value; return return_value;