2020-12-04 16:48:03 +09:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2020-12-04 20:11:35 +09:00
|
|
|
#include <stdbool.h>
|
2020-12-04 16:48:03 +09:00
|
|
|
#include <assert.h>
|
|
|
|
#include <fcntl.h>
|
2020-12-06 03:38:01 +09:00
|
|
|
#include <time.h>
|
2020-12-08 23:37:19 +09:00
|
|
|
#include <pthread.h>
|
2020-12-04 16:48:03 +09:00
|
|
|
#include "socket_wrapper.h"
|
2020-12-08 23:37:19 +09:00
|
|
|
#include "timerhelper.h"
|
2020-12-09 02:34:13 +09:00
|
|
|
#include "display_bar.h"
|
2020-12-04 16:48:03 +09:00
|
|
|
|
2020-12-04 17:33:41 +09:00
|
|
|
#ifndef DEFAULT_TIMEOUT
|
|
|
|
static const int TIMEOUT = 5;
|
|
|
|
#else
|
|
|
|
static const int TIMEOUT = DEFAULT_TIMEOUT;
|
2020-12-04 16:48:03 +09:00
|
|
|
#endif
|
2020-12-08 23:37:19 +09:00
|
|
|
|
|
|
|
enum{
|
|
|
|
FILENAME_BUF_SIZE = 1024
|
|
|
|
};
|
2020-12-09 02:34:13 +09:00
|
|
|
|
|
|
|
static bool DisplayProgress = true;
|
|
|
|
|
|
|
|
static inline void server_perror(const char * msg){
|
|
|
|
if(DisplayProgress)
|
|
|
|
myd_perror(msg);
|
|
|
|
else
|
|
|
|
perror(msg);
|
|
|
|
}
|
2020-12-04 16:48:03 +09:00
|
|
|
/*========
|
|
|
|
*Operation
|
|
|
|
*========*/
|
2020-12-08 23:37:19 +09:00
|
|
|
//if success, return zero
|
2020-12-04 16:48:03 +09:00
|
|
|
int sendReadOp(int sock,const char * filename){
|
|
|
|
struct ReadOp op;
|
|
|
|
op.file_url_size = strlen(filename);
|
|
|
|
op.padding0 = 0;
|
|
|
|
if(send(sock,&op,sizeof(op),0)<0){
|
2020-12-09 02:34:13 +09:00
|
|
|
server_perror("readop send fail");
|
2020-12-04 16:48:03 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-05 11:13:13 +09:00
|
|
|
#ifdef MUL_CLIENT
|
|
|
|
sleep(MUL_CLIENT);
|
2020-12-04 19:10:40 +09:00
|
|
|
#endif
|
2020-12-04 16:48:03 +09:00
|
|
|
if(send(sock,filename,op.file_url_size,0)<0){
|
2020-12-09 02:34:13 +09:00
|
|
|
server_perror("readop filename send fail");
|
2020-12-04 16:48:03 +09:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-09 02:34:13 +09:00
|
|
|
|
2020-12-04 16:48:03 +09:00
|
|
|
int recvFile(int sock, const char * filename,size_t file_size){
|
|
|
|
int fd;
|
|
|
|
size_t count = 0;
|
|
|
|
int i;
|
|
|
|
int return_value = 0;
|
|
|
|
int buf_sz = getBufferSizeFrom(sock);
|
2020-12-09 02:34:13 +09:00
|
|
|
progress_bar_t pbar;
|
2020-12-04 16:48:03 +09:00
|
|
|
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);
|
2020-12-09 02:34:13 +09:00
|
|
|
if (fd < 0){
|
|
|
|
server_perror("file open fail");
|
2020-12-04 16:48:03 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-09 02:34:13 +09:00
|
|
|
if(DisplayProgress){
|
|
|
|
init_progress_bar(&pbar,10);
|
|
|
|
}
|
2020-12-04 16:48:03 +09:00
|
|
|
while (file_size - count > 0)
|
|
|
|
{
|
|
|
|
int readed = buf_sz < file_size - count ? buf_sz : file_size - count;
|
2020-12-09 02:34:13 +09:00
|
|
|
if(DisplayProgress){
|
|
|
|
DisplayProgressBar(&pbar,count,file_size,filename,false);
|
|
|
|
}
|
2020-12-04 16:48:03 +09:00
|
|
|
if((i = recv_until_byte(sock,buf,readed,TIMEOUT)) < 0){
|
|
|
|
if(i == -2)
|
|
|
|
fprintf(stderr,"recv file failed : timeout connetion lost\n");
|
2020-12-09 02:34:13 +09:00
|
|
|
else server_perror("recv file failed");
|
2020-12-04 16:48:03 +09:00
|
|
|
return_value = -1;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
if(write(fd,buf,readed)<0){
|
2020-12-09 02:34:13 +09:00
|
|
|
server_perror("file write failed");
|
2020-12-04 16:48:03 +09:00
|
|
|
return_value = -1;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
count += readed;
|
2020-12-05 11:13:13 +09:00
|
|
|
#ifdef SLOW_CLIENT
|
|
|
|
usleep(SLOW_CLIENT);
|
|
|
|
#endif
|
2020-12-04 16:48:03 +09:00
|
|
|
}
|
2020-12-06 03:38:01 +09:00
|
|
|
if(DisplayProgress)
|
2020-12-09 02:34:13 +09:00
|
|
|
DisplayProgressBar(&pbar,count,file_size,filename,true);
|
2020-12-04 16:48:03 +09:00
|
|
|
END:
|
|
|
|
free(buf);
|
2020-12-08 23:37:19 +09:00
|
|
|
close(fd);
|
2020-12-04 16:48:03 +09:00
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int recvData(int sock,const char * filename){
|
|
|
|
struct TransferResult res;
|
|
|
|
int i=0;
|
|
|
|
if((i=recv_until_byte(sock,&res,sizeof(res),TIMEOUT)) < 0){
|
2020-12-09 02:34:13 +09:00
|
|
|
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");
|
2020-12-04 16:48:03 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-09 02:34:13 +09:00
|
|
|
char error_meesage_buf[256] = "";
|
2020-12-04 16:48:03 +09:00
|
|
|
switch(res.res){
|
|
|
|
case RES_ERR:
|
2020-12-09 02:34:13 +09:00
|
|
|
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);
|
2020-12-04 16:48:03 +09:00
|
|
|
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){
|
2020-12-06 03:38:01 +09:00
|
|
|
if (i == -2) fprintf(stderr,"timeout\n");
|
2020-12-09 02:34:13 +09:00
|
|
|
else server_perror("recv fail");
|
2020-12-04 16:48:03 +09:00
|
|
|
return -1;
|
|
|
|
}
|
2020-12-09 02:34:13 +09:00
|
|
|
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);
|
2020-12-04 16:48:03 +09:00
|
|
|
return -1;
|
|
|
|
case RES_OK:
|
|
|
|
return recvFile(sock,filename,res.file_size);
|
|
|
|
break;
|
|
|
|
default:
|
2020-12-09 02:34:13 +09:00
|
|
|
if(DisplayProgress && isatty_file(stderr)){
|
|
|
|
lock_scrolled();
|
|
|
|
add_scrolled_unlocked(1);
|
|
|
|
fprintf(stderr,"unknown value!\n");
|
|
|
|
unlock_scrolled();
|
|
|
|
}
|
|
|
|
else fprintf(stderr,"unknown value!\n");
|
2020-12-04 16:48:03 +09:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
|
|
|
|
static bool quiet_mode = false;
|
|
|
|
int SendOpAndReceiveFile(const char * filename, struct sockaddr const * addr){
|
|
|
|
int sock;
|
|
|
|
int ret = -1;
|
2020-12-09 02:34:13 +09:00
|
|
|
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);
|
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
sock = socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if(sock < 0){
|
2020-12-09 02:34:13 +09:00
|
|
|
server_perror("sock create fail");
|
2020-12-08 23:37:19 +09:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(connect(sock,(struct sockaddr *)addr,sizeof(*addr)) < 0){
|
2020-12-09 02:34:13 +09:00
|
|
|
server_perror("connect failed");
|
2020-12-08 23:37:19 +09:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if(sendReadOp(sock,filename) == 0){
|
|
|
|
ret = recvData(sock,filename);
|
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//====
|
|
|
|
//bench
|
|
|
|
//====
|
2020-12-06 03:38:01 +09:00
|
|
|
struct benchmark_data{
|
|
|
|
bool benchmode;
|
|
|
|
struct timespec begin;
|
|
|
|
struct timespec end;
|
|
|
|
clockid_t clock_id;
|
|
|
|
struct timespec resolution;
|
|
|
|
int op_count;
|
|
|
|
} bench = {0,};
|
|
|
|
|
|
|
|
void init_bench_data(){
|
|
|
|
int i;
|
|
|
|
memset(&bench,0,sizeof(bench));
|
|
|
|
bench.clock_id = CLOCK_PROCESS_CPUTIME_ID;
|
|
|
|
i = clock_getres(bench.clock_id,&bench.resolution);
|
|
|
|
if (i < 0){
|
|
|
|
bench.clock_id = CLOCK_REALTIME;
|
|
|
|
clock_getres(bench.clock_id,&bench.resolution);
|
|
|
|
}
|
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
//====
|
|
|
|
//simple queue
|
|
|
|
//====
|
|
|
|
typedef enum{
|
|
|
|
From_CharArray,
|
|
|
|
From_FileStream
|
|
|
|
} queueing_method_t;
|
|
|
|
|
|
|
|
typedef struct SimpleCharQueue{
|
|
|
|
queueing_method_t method;
|
|
|
|
const char ** filename_begin;
|
|
|
|
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){
|
|
|
|
q->method = From_CharArray;
|
|
|
|
q->filename_begin = b;
|
|
|
|
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){
|
|
|
|
q->method = From_FileStream;
|
|
|
|
q->fs = f;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * dequeue_from_simplecharqueue(simple_queue_t * restrict q,char * restrict buf, size_t size){
|
|
|
|
const char * filename;
|
|
|
|
if (q->method == From_CharArray){
|
|
|
|
if (__glibc_unlikely(q->filename_begin == q->filename_end)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
filename = *(q->filename_begin++);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
//unsafe.
|
|
|
|
int t = fscanf(q->fs,"%s",buf);
|
|
|
|
if (__glibc_unlikely(t != 1)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
filename = buf;
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
//====
|
|
|
|
//thread
|
|
|
|
//====
|
|
|
|
struct SimpleThreadGlobal{
|
|
|
|
size_t thread_arr_size;
|
|
|
|
pthread_t * thread_arr;
|
|
|
|
pthread_mutex_t queueing_mutex;
|
|
|
|
simple_queue_t queue;
|
|
|
|
} global_state;
|
|
|
|
//if initialization is success, return true
|
|
|
|
bool init_global_state(struct SimpleThreadGlobal * t, size_t size){
|
|
|
|
pthread_mutex_init(&t->queueing_mutex,NULL);
|
|
|
|
t->thread_arr_size = size;
|
|
|
|
t->thread_arr = (pthread_t *)malloc(sizeof(*global_state.thread_arr) * global_state.thread_arr_size);
|
|
|
|
if (t->thread_arr == NULL){
|
|
|
|
return false;
|
2020-12-06 03:38:01 +09:00
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct SimpleThreadReturn{
|
|
|
|
int retval;
|
|
|
|
int op_count;
|
|
|
|
} worker_return_t;
|
|
|
|
|
|
|
|
__attribute_malloc__ worker_return_t * create_worker_return(){
|
|
|
|
return (worker_return_t *)malloc(sizeof(worker_return_t));
|
|
|
|
}
|
|
|
|
void destroy_worker_return(worker_return_t * r){
|
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct SimpleThreadArg{
|
|
|
|
struct sockaddr addr;
|
|
|
|
} worker_arg_t;
|
|
|
|
|
|
|
|
__attribute_malloc__ worker_arg_t * create_thread_arg(struct sockaddr * r){
|
|
|
|
worker_arg_t * ret = (worker_arg_t *)malloc(sizeof(*ret));
|
|
|
|
memcpy(&ret->addr,r,sizeof(*r));
|
2020-12-06 03:38:01 +09:00
|
|
|
return ret;
|
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
void destroy_thread_arg(worker_arg_t * arg){
|
|
|
|
free(arg);
|
|
|
|
}
|
2020-12-04 16:48:03 +09:00
|
|
|
|
2020-12-08 23:37:19 +09:00
|
|
|
void * WorkerProc(void * args){
|
|
|
|
worker_arg_t * arg = (worker_arg_t *)(args);
|
|
|
|
worker_return_t * ret = create_worker_return();
|
|
|
|
char filename_buf[FILENAME_BUF_SIZE];
|
2020-12-04 16:48:03 +09:00
|
|
|
const char * filename;
|
2020-12-08 23:37:19 +09:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&global_state.queueing_mutex);
|
|
|
|
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
|
|
|
|
pthread_mutex_unlock(&global_state.queueing_mutex);
|
|
|
|
if(filename == NULL) break;
|
|
|
|
ret->retval += SendOpAndReceiveFile(filename,&arg->addr);
|
|
|
|
ret->op_count++;
|
|
|
|
}
|
|
|
|
destroy_thread_arg(arg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
//====
|
|
|
|
// cmd parse
|
|
|
|
//====
|
|
|
|
static struct {
|
2020-12-04 16:48:03 +09:00
|
|
|
const char * server_name;
|
2020-12-08 23:37:19 +09:00
|
|
|
in_port_t server_port;
|
|
|
|
bool stdinisatty;
|
|
|
|
int thread_number_option;
|
|
|
|
bool quiet_mode;
|
|
|
|
} cmd_args = {
|
|
|
|
.server_name = "",
|
|
|
|
.server_port = 0
|
|
|
|
};
|
2020-12-06 03:38:01 +09:00
|
|
|
|
2020-12-08 23:37:19 +09:00
|
|
|
static const char * show_help_message(FILE * file,const char * argv0){
|
|
|
|
static const char * msg =
|
|
|
|
"USAUE: %s SERVERNAME PORT [Option]... [FILENAME]...\n"
|
|
|
|
"Options and arguments: \n"
|
|
|
|
"-b or --benchmark\t:benchmark mode\n"
|
|
|
|
"-nv or --no-verbose\t:no progress bar\n"
|
2020-12-09 02:34:13 +09:00
|
|
|
"-q or --quiet\t:show no message\n"
|
2020-12-08 23:37:19 +09:00
|
|
|
"-h\t:print help message.\n";
|
|
|
|
fprintf(file,msg,argv0);
|
|
|
|
return msg;
|
|
|
|
}
|
2020-12-06 03:38:01 +09:00
|
|
|
|
2020-12-08 23:37:19 +09:00
|
|
|
static int parse_arg(int argc,const char *argv[]){
|
|
|
|
int cur;
|
|
|
|
cmd_args.stdinisatty = isatty(STDIN_FILENO);
|
|
|
|
if (argc < (cmd_args.stdinisatty ? 4 : 3)){
|
|
|
|
show_help_message(stderr,argv[0]);
|
2020-12-04 16:48:03 +09:00
|
|
|
return 1;
|
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
cmd_args.server_name = argv[1];
|
|
|
|
cmd_args.server_port = atoi(argv[2]);
|
|
|
|
if (cmd_args.server_port == 0){
|
|
|
|
fprintf(stderr,"port invalid\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cmd_args.quiet_mode = false;
|
|
|
|
cmd_args.thread_number_option = 1;
|
|
|
|
for(cur = 3;cur < argc; cur++){
|
|
|
|
if (strcmp("-b",argv[cur])==0||strcmp("--benchmark",argv[cur])==0){
|
2020-12-06 03:38:01 +09:00
|
|
|
bench.benchmode = true;
|
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
else if(strcmp("-nv",argv[cur]) == 0||strcmp("--no-verbose",argv[cur])==0){
|
2020-12-06 03:38:01 +09:00
|
|
|
DisplayProgress = false;
|
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
else if(strcmp("-t",argv[cur]) == 0 || strcmp("--thread",argv[cur]) == 0){
|
|
|
|
cur++;
|
|
|
|
if (cur >= argc){
|
|
|
|
fprintf(stderr,"need number");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
cmd_args.thread_number_option = atoi(argv[cur]);
|
|
|
|
if(cmd_args.thread_number_option == 0){
|
|
|
|
fprintf(stderr,"not number or zero");
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(strcmp("-h",argv[cur]) == 0 || strcmp("--help",argv[cur]) == 0){
|
|
|
|
show_help_message(stdout,argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if(strcmp("-q",argv[cur]) == 0 || strcmp("--quiet",argv[cur]) == 0){
|
|
|
|
cmd_args.quiet_mode = true;
|
|
|
|
quiet_mode = true;
|
2020-12-09 02:34:13 +09:00
|
|
|
DisplayProgress = false;
|
2020-12-08 23:37:19 +09:00
|
|
|
}
|
2020-12-06 03:38:01 +09:00
|
|
|
else break;
|
2020-12-04 16:48:03 +09:00
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
if (cmd_args.stdinisatty){
|
|
|
|
init_queue_from_chararray(&global_state.queue, &argv[cur],&argv[argc]);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
init_queue_from_file(&global_state.queue,stdin);
|
2020-12-04 16:48:03 +09:00
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
return 0;
|
|
|
|
}
|
2020-12-06 03:38:01 +09:00
|
|
|
|
2020-12-08 23:37:19 +09:00
|
|
|
int main(int argc, const char *argv[]){
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
const char * filename;
|
|
|
|
int err;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
init_bench_data();
|
|
|
|
if(parse_arg(argc,argv) < 0) return EXIT_FAILURE;
|
2020-12-09 02:34:13 +09:00
|
|
|
ready_progress_bar();
|
2020-12-08 23:37:19 +09:00
|
|
|
|
|
|
|
err = getsockaddrbyname(AF_INET,SOCK_STREAM,0,cmd_args.server_name,(struct sockaddr *)&addr);
|
2020-12-04 16:48:03 +09:00
|
|
|
if (err != 0){
|
|
|
|
int check;
|
2020-12-09 02:34:13 +09:00
|
|
|
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));
|
2020-12-04 16:48:03 +09:00
|
|
|
//assume that sernmae is *.*.*.* and try to parse addr
|
2020-12-08 23:37:19 +09:00
|
|
|
check = inet_pton(AF_INET,cmd_args.server_name,&addr.sin_addr);
|
2020-12-04 16:48:03 +09:00
|
|
|
assert(check != -1);
|
|
|
|
if (check == 0){
|
|
|
|
fprintf(stderr,"parsing fail : invaild format\n");
|
2020-12-08 23:37:19 +09:00
|
|
|
return -1;
|
2020-12-04 16:48:03 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addr.sin_family = AF_INET;
|
2020-12-08 23:37:19 +09:00
|
|
|
addr.sin_port = htons(cmd_args.server_port);
|
2020-12-06 03:38:01 +09:00
|
|
|
|
|
|
|
if (bench.benchmode){
|
|
|
|
clock_gettime(bench.clock_id,&bench.begin);
|
2020-12-04 16:48:03 +09:00
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
if(cmd_args.thread_number_option == 1){
|
|
|
|
char filename_buf[FILENAME_BUF_SIZE];
|
|
|
|
for (;;){
|
|
|
|
filename = dequeue_from_simplecharqueue(&global_state.queue,filename_buf,FILENAME_BUF_SIZE);
|
|
|
|
if (filename == NULL) break;
|
|
|
|
|
|
|
|
retval += SendOpAndReceiveFile(filename,(struct sockaddr *)&addr);
|
|
|
|
bench.op_count++;
|
2020-12-06 03:38:01 +09:00
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
int i = 0;
|
|
|
|
init_global_state(&global_state,cmd_args.thread_number_option);
|
|
|
|
for (i = 0; i < global_state.thread_arr_size; i++){
|
|
|
|
worker_arg_t * arg = create_thread_arg((struct sockaddr *)&addr);
|
|
|
|
pthread_create(&global_state.thread_arr[i],NULL,WorkerProc,arg);
|
2020-12-06 03:38:01 +09:00
|
|
|
}
|
2020-12-08 23:37:19 +09:00
|
|
|
for (i = 0; i < global_state.thread_arr_size; i++){
|
|
|
|
worker_return_t * ret;
|
|
|
|
pthread_join(global_state.thread_arr[i],(void **)&ret);
|
|
|
|
bench.op_count += ret->op_count;
|
|
|
|
retval += ret->retval;
|
|
|
|
destroy_worker_return(ret);
|
2020-12-06 03:38:01 +09:00
|
|
|
}
|
2020-12-04 16:48:03 +09:00
|
|
|
}
|
2020-12-06 03:38:01 +09:00
|
|
|
if (bench.benchmode){
|
|
|
|
struct timespec result;
|
|
|
|
double avg;
|
|
|
|
clock_gettime(bench.clock_id,&bench.end);
|
|
|
|
result = timespec_sub(bench.end,bench.begin);
|
|
|
|
if (result.tv_sec == 0) avg = result.tv_nsec;
|
|
|
|
else avg = result.tv_sec * 1e9 + result.tv_nsec;
|
|
|
|
avg /= bench.op_count;
|
2020-12-08 23:37:19 +09:00
|
|
|
fprintf(stdout,"operation: %lf us/op\n",avg / 1000.0);
|
2020-12-06 03:38:01 +09:00
|
|
|
fprintf(stdout,"resolution: %ld sec %ld ns\n",bench.resolution.tv_sec,bench.resolution.tv_nsec);
|
|
|
|
}
|
2020-12-09 02:34:13 +09:00
|
|
|
quit_progress_bar();
|
2020-12-06 03:38:01 +09:00
|
|
|
return retval;
|
2020-12-04 16:48:03 +09:00
|
|
|
}
|