add bar fprintf

This commit is contained in:
ubuntu201711081 2020-12-14 11:09:12 +00:00
parent 7ffcbafaa2
commit aeb60a7537
6 changed files with 77 additions and 152 deletions

View File

@ -16,7 +16,7 @@ display_bar.o: display_bar.c display_bar.h
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)
$(CC) -o p-client p-client.c $(ClientObject) $(CFLAGS)
p-mulclient: p-client.c $(ClientObject)
$(CC) -o p-slowclient p-client.c $(ClientObject) $(CFLAGS) -D MUL_CLIENT=10
slowclient: client.c $(ClientObject)

View File

@ -30,12 +30,17 @@ enum{
static bool DisplayProgress = true;
static inline void server_perror(const char * msg){
static inline void client_perror(const char * msg){
if(DisplayProgress)
myd_perror(msg);
else
perror(msg);
}
static inline int client_fprintf(int line,FILE * file,const char * msg,...){
va_list va;
va_start(va,msg);
return DisplayProgress ? myd_vfprintf(line,file,msg,va) : vfprintf(file,msg,va);
}
/*========
*Operation
*========*/
@ -45,14 +50,14 @@ int sendReadOp(int sock,const char * filename){
op.file_url_size = strlen(filename);
op.padding0 = 0;
if(send(sock,&op,sizeof(op),0)<0){
server_perror("readop send fail");
client_perror("readop send fail");
return -1;
}
#ifdef MUL_CLIENT
sleep(MUL_CLIENT);
#endif
if(send(sock,filename,op.file_url_size,0)<0){
server_perror("readop filename send fail");
client_perror("readop filename send fail");
return -1;
}
return 0;
@ -71,7 +76,7 @@ int recvFile(int sock, const char * filename,size_t file_size){
}
fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH);
if (fd < 0){
server_perror("file open fail");
client_perror("file open fail");
return -1;
}
if(DisplayProgress){
@ -86,12 +91,12 @@ int recvFile(int sock, const char * filename,size_t file_size){
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
if(i == -2)
fprintf(stderr,"recv file failed : timeout connetion lost\n");
else server_perror("recv file failed");
else client_perror("recv file failed");
return_value = -1;
goto END;
}
if(write(fd,buf,readed)<0){
server_perror("file write failed");
client_perror("file write failed");
return_value = -1;
goto END;
}
@ -113,55 +118,31 @@ int recvData(int sock,const char * filename){
int i=0;
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
if (i == -2) {
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"timeout\n");
unlock_scrolled();
client_fprintf(1,stderr,"timeout\n");
}
else fprintf(stderr,"timeout\n");
}
else server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
char error_meesage_buf[256] = "";
switch(res.res){
case RES_ERR:
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);
client_fprintf(1,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 server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
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);
client_fprintf(1,stderr,"Error Message From Server: %s\n",error_meesage_buf);
return -1;
case RES_OK:
return recvFile(sock,filename,res.file_size);
break;
default:
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"unknown value!\n");
unlock_scrolled();
}
else fprintf(stderr,"unknown value!\n");
client_fprintf(1,stderr,"unknown value!\n");
return -1;
}
@ -173,22 +154,16 @@ int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
int sock;
int ret = -1;
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);
client_fprintf(1,stdout,"request %s\n",filename);
}
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0){
server_perror("sock create fail");
client_perror("sock create fail");
return -1;
}
if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){
server_perror("connect failed");
client_perror("connect failed");
return -1;
}
if(sendReadOp(sock,filename) == 0){
@ -233,7 +208,7 @@ typedef struct SimpleCharQueue{
const char ** filename_end;
FILE * fs;
} simple_queue_t;
//
// q have lifetime of arg b or arg e.
// do not deallocate before queue closed.
static bool init_queue_from_chararray(simple_queue_t * restrict q,const char ** b, const char ** e){
@ -242,7 +217,7 @@ static bool init_queue_from_chararray(simple_queue_t * restrict q,const char **
q->filename_end = e;
return true;
}
//
// q have lifetime of arg f.
// do not close file f before queue closed.
static bool init_queue_from_file(simple_queue_t * restrict q, FILE * f){
@ -424,13 +399,7 @@ 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;
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));
client_fprintf(1,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);
@ -470,6 +439,7 @@ int main(int argc, const char *argv[]){
destroy_worker_return(ret);
}
}
quit_progress_bar();
if (bench.benchmode){
struct timespec result;
double avg;
@ -481,6 +451,5 @@ int main(int argc, const char *argv[]){
fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
}
quit_progress_bar();
return retval;
}

View File

@ -127,17 +127,23 @@ void myd_perror(const char * msg){
}
int myd_vfprintf(int line,FILE * f,const char * msg, va_list arg){
int ret;
if(isatty_file(f)){
lock_scrolled();
add_scrolled_unlocked(line);
vfprintf(f,msg,arg);
ret = vfprintf(f,msg,arg);
unlock_scrolled();
}
else{
vfprintf(f,msg,arg);
ret = vfprintf(f,msg,arg);
}
return ret;
}
int myd_fprintf(int line,FILE * f,const char * msg, ...){
va_list va;
va_start(va,msg);
return myd_vfprintf(line,f,msg,va);
}
void init_progress_bar(progress_bar_t * bar,int update_rate){
memset(bar,0,sizeof(*bar));
bar->bar = create_raw_progress_bar();

View File

@ -1,7 +1,7 @@
#include<stdio.h>
#include<time.h>
#include<stdbool.h>
#include<stddef.h>
#include<stdarg.h>
#ifndef _DISPLAY_BAR_H_
#define _DISPLAY_BAR_H_
@ -22,9 +22,13 @@ void quit_progress_bar();
//same as isatty
bool isatty_file(FILE * file);
//
//After calling ready_progress_bar, you must use following functions on print.
void myd_perror(const char * msg);
//argument `line` is number of newline created by argument `msg`.
int myd_vfprintf(int line,FILE * f,const char * msg,va_list arg);
//argument `line` is number of newline created by argument `msg`.
int myd_fprintf(int line,FILE * f,const char * msg, ...);
typedef struct {
raw_progress_bar_t bar;
@ -34,7 +38,8 @@ typedef struct {
} progress_bar_t;
//update rate is millsecond unit
void init_progress_bar(progress_bar_t * bar,int update_rate);
/// display progress bar
/// if `sync`, update unconditionally.
void DisplayProgressBar(progress_bar_t * bar,size_t offset,size_t total,const char * filename, bool sync);
#endif

View File

@ -30,12 +30,17 @@ enum{
static bool DisplayProgress = true;
static inline void server_perror(const char * msg){
static inline void client_perror(const char * msg){
if(DisplayProgress)
myd_perror(msg);
else
perror(msg);
}
static inline int client_fprintf(int line,FILE * file,const char * msg,...){
va_list va;
va_start(va,msg);
return DisplayProgress ? myd_vfprintf(line,file,msg,va) : vfprintf(file,msg,va);
}
/*========
*Operation
*========*/
@ -45,14 +50,14 @@ int sendReadOp(int sock,const char * filename){
op.file_url_size = strlen(filename);
op.padding0 = 0;
if(send(sock,&op,sizeof(op),0)<0){
server_perror("readop send fail");
client_perror("readop send fail");
return -1;
}
#ifdef MUL_CLIENT
sleep(MUL_CLIENT);
#endif
if(send(sock,filename,op.file_url_size,0)<0){
server_perror("readop filename send fail");
client_perror("readop filename send fail");
return -1;
}
return 0;
@ -71,7 +76,7 @@ int recvFile(int sock, const char * filename,size_t file_size){
}
fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU|S_IRWXG|S_IROTH);
if (fd < 0){
server_perror("file open fail");
client_perror("file open fail");
return -1;
}
if(DisplayProgress){
@ -86,12 +91,12 @@ int recvFile(int sock, const char * filename,size_t file_size){
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
if(i == -2)
fprintf(stderr,"recv file failed : timeout connetion lost\n");
else server_perror("recv file failed");
else client_perror("recv file failed");
return_value = -1;
goto END;
}
if(write(fd,buf,readed)<0){
server_perror("file write failed");
client_perror("file write failed");
return_value = -1;
goto END;
}
@ -113,55 +118,31 @@ int recvData(int sock,const char * filename){
int i=0;
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
if (i == -2) {
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"timeout\n");
unlock_scrolled();
client_fprintf(1,stderr,"timeout\n");
}
else fprintf(stderr,"timeout\n");
}
else server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
char error_meesage_buf[256] = "";
switch(res.res){
case RES_ERR:
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);
client_fprintf(1,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 server_perror("recv fail");
else client_perror("recv fail");
return -1;
}
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);
client_fprintf(1,stderr,"Error Message From Server: %s\n",error_meesage_buf);
return -1;
case RES_OK:
return recvFile(sock,filename,res.file_size);
break;
default:
if(DisplayProgress && isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"unknown value!\n");
unlock_scrolled();
}
else fprintf(stderr,"unknown value!\n");
client_fprintf(1,stderr,"unknown value!\n");
return -1;
}
@ -173,22 +154,16 @@ int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
int sock;
int ret = -1;
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);
client_fprintf(1,stdout,"request %s\n",filename);
}
sock = socket(AF_INET,SOCK_STREAM,0);
if(sock < 0){
server_perror("sock create fail");
client_perror("sock create fail");
return -1;
}
if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){
server_perror("connect failed");
client_perror("connect failed");
return -1;
}
if(sendReadOp(sock,filename) == 0){
@ -233,7 +208,7 @@ typedef struct SimpleCharQueue{
const char ** filename_end;
FILE * fs;
} simple_queue_t;
//
// q have lifetime of arg b or arg e.
// do not deallocate before queue closed.
static bool init_queue_from_chararray(simple_queue_t * restrict q,const char ** b, const char ** e){
@ -242,7 +217,7 @@ static bool init_queue_from_chararray(simple_queue_t * restrict q,const char **
q->filename_end = e;
return true;
}
//
// q have lifetime of arg f.
// do not close file f before queue closed.
static bool init_queue_from_file(simple_queue_t * restrict q, FILE * f){
@ -424,13 +399,7 @@ 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;
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));
client_fprintf(1,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);
@ -470,6 +439,7 @@ int main(int argc, const char *argv[]){
destroy_worker_return(ret);
}
}
quit_progress_bar();
if (bench.benchmode){
struct timespec result;
double avg;
@ -481,6 +451,5 @@ int main(int argc, const char *argv[]){
fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
}
quit_progress_bar();
return retval;
}

View File

@ -13,6 +13,7 @@
#include <assert.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdarg.h>
#include "socket_wrapper.h"
#include "simple_circular_buffer.h"
@ -97,6 +98,11 @@ static inline void server_perror(const char * msg){
else
perror(msg);
}
static inline int server_fprintf(int line,FILE * file,const char * msg,...){
va_list va;
va_start(va,msg);
return use_gui ? myd_vfprintf(line,file,msg,va) : vfprintf(file,msg,va);
}
/*========
*Operation
@ -167,13 +173,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(use_gui&&isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
unlock_scrolled();
}
else fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
server_fprintf(1,stdout,"str size: %d, request %s\n",p.file_url_size,buf);
if(strchr((char *)buf,'/') != NULL){
send_fail(sock,"Illegal character /");
return -1;
@ -387,13 +387,7 @@ void * worker_proc(void * data){
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhalf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
unlock_scrolled();
}
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
server_fprintf(1,stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
#endif
if(close(csock) < 0)
server_perror("csock close error");
@ -421,13 +415,7 @@ void * worker_proc(void * data){
clock_gettime(Bottom_Trace_Timer_ID,&ts_bottom_end);
struct timespec tophalf = timespec_sub(ts_top_end,ts_top_begin);
struct timespec bottomhalf = timespec_sub(ts_bottom_end,ts_bottom_begin);
if(use_gui&&isatty_file(stderr)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
unlock_scrolled();
}
else fprintf(stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
server_fprintf(1,stderr,"top : %ld ns, bottom : %ld ns\n",tophalf.tv_nsec,bottomhalf.tv_nsec);
#endif
if(close(csock) < 0)
server_perror("csock close error");
@ -493,13 +481,7 @@ int main(int argc, const char *argv[]){
char ip_buf[INET_ADDRSTRLEN];
const char * msg = inet_ntop(AF_INET,&addr.sin_addr,ip_buf,sizeof(ip_buf));
assert(msg != NULL);
if(use_gui&&isatty_file(stdout)){
lock_scrolled();
add_scrolled_unlocked(1);
fprintf(stdout,"server bind on %s:%d\n",msg ,binding_port_number);
unlock_scrolled();
}
else fprintf(stdout,"server bind on %s:%d\n",msg ,binding_port_number);
server_fprintf(1,stdout,"server bind on %s:%d\n",msg ,binding_port_number);
}
if(listen(sock,MAX_LISTEN_SOCKET) < 0){
@ -511,13 +493,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(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));
unlock_scrolled();
}
else fprintf(stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
server_fprintf(1,stdout,"Connected on : %s:%d\n",msg == NULL ? "(null)" : msg , ntohs(addr.sin_port));
#ifdef USE_TRACE
struct timespec ts_top_begin;
clock_gettime(Top_Trace_Timer_ID, &ts_top_begin);