From 54d371e4e897fa58c316fcc3b6dc7bde5af6868c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 11 Oct 2017 22:28:00 +0200 Subject: Third --- controller/main.c | 76 +++++++++++++++++++++++++++++++++++++++++++++------- server/data_stream.c | 23 ++++++++++++---- server/main.c | 7 +---- util.c | 9 +++++++ util.h | 6 +++++ 5 files changed, 100 insertions(+), 21 deletions(-) create mode 100644 util.c create mode 100644 util.h diff --git a/controller/main.c b/controller/main.c index d289ce9..be13773 100644 --- a/controller/main.c +++ b/controller/main.c @@ -172,16 +172,7 @@ static void frame_data_sink(u8 *newdata,i64 newdatalen,void *payload){ bufferlen-=cursor; } -int main(int argc,char **argv){ - if(argc<3||argc>4){ - fprintf(stderr,"Usage: %s [port=57575]\n",argv[0]); - return 1; - } - - const char *logfname=argv[1]; - const char *ipaddr=argv[2]; - const char *port=argc==4?argv[4]:"57575"; - +static int connect_server(const char *logfname,const char *ipaddr,const char *port){ logfile=fopen(logfname,"w"); if(logfile==NULL){ fprintf(stderr,"Cannot open log file '%s'\n",logfname); @@ -284,3 +275,68 @@ cleanup: if(haderror)return 1; return 0; } + +static int read_zz_file(const char *zzfname,const char *logfname){ + logfile=fopen(logfname,"w"); + if(logfile==NULL){ + fprintf(stderr,"Cannot open log file '%s'\n",logfname); + return 1; + } + + FILE *f=fopen(zzfname,"r"); + if(f==NULL){ + fprintf(stderr,"Cannot open zz file '%s'\n",zzfname); + return 1; + } + + struct unzbuffer *z=unzbuffer_init(frame_data_sink,NULL); + u8 buf[4096]; + while(true){ + i64 nr=fread(buf,1,sizeof buf,f); + if(nr<=0){ + if(ferror(f)){ + fprintf(stderr,"An error occurred reading the zz file '%s'\n",zzfname); + return 1; + } + break; + } + if(!unzbuffer_write(z,buf,nr)){ + return 1; + } + } + if(!unzbuffer_finish_destroy(z))return 1; + return 0; +} + +int main(int argc,char **argv){ + if(argc<=1){ + fprintf(stderr,"Usage:\n"); + fprintf(stderr," %s connect [port=57575]\n",argv[0]); + fprintf(stderr," %s read \n",argv[0]); + fprintf(stderr," will probably contain JSON.\n"); + fprintf(stderr," should contain zlib-compressed protocol data.\n"); + return 1; + } + + if(strcmp(argv[1],"connect")==0){ + if(argc<4||argc>5){ + fprintf(stderr,"Invalid number of arguments, run without arguments for usage\n"); + return 1; + } + const char *logfname=argv[2]; + const char *ipaddr=argv[3]; + const char *port=argc==5?argv[4]:"57575"; + return connect_server(logfname,ipaddr,port); + } else if(strcmp(argv[1],"read")==0){ + if(argc!=4){ + fprintf(stderr,"Invalid number of arguments, run without arguments for usage\n"); + return 1; + } + const char *zzfname=argv[2]; + const char *logfname=argv[3]; + return read_zz_file(zzfname,logfname); + } else { + fprintf(stderr,"Invalid command '%s'; run without arguments for usage\n",argv[1]); + return 1; + } +} diff --git a/server/data_stream.c b/server/data_stream.c index 3d25104..2719d2a 100644 --- a/server/data_stream.c +++ b/server/data_stream.c @@ -5,10 +5,13 @@ #include #include #include "data_stream.h" +#include "util.h" struct data_stream { int fd; + FILE *mirrorfile; + i64 pidbufsize; pid_t *pids; i64 namebufsize; @@ -21,11 +24,11 @@ struct data_stream { static bool callback_error=false; -static void write_data_fd(u8 *data,i64 len,void *fdp){ - int fd=*(int*)fdp; +static void write_data_callback(u8 *data,i64 len,void *streamp){ + struct data_stream *s=(struct data_stream*)streamp; i64 cursor=0; while(cursorfd,data+cursor,len-cursor); if(nwr<0){ perror("write"); callback_error=true; @@ -33,7 +36,8 @@ static void write_data_fd(u8 *data,i64 len,void *fdp){ } cursor+=nwr; } - fprintf(stderr,"write_data_fd(len=%" PRIi64 ")\n",len); + fwrite(data,1,len,s->mirrorfile); + fprintf(stderr,"write_data_callback(len=%" PRIi64 ")\n",len); } static void bufappend(u8 **buf,i64 *cursor,i64 *cap,u8 *data,i64 datalen){ @@ -57,6 +61,14 @@ static void serialise4(u8 *buf,i32 value){ struct data_stream* data_stream_init(int fd){ struct data_stream *s=malloc(1,struct data_stream); s->fd=fd; + char fnamebuf[128]; + snprintf(fnamebuf,sizeof fnamebuf,"logmirror_%" PRIi64 ".zz",make_timestamp()); + printf("Using log mirror file '%s'\n",fnamebuf); + s->mirrorfile=fopen(fnamebuf,"w"); + if(s->mirrorfile==NULL){ + fprintf(stderr,"Cannot open log mirror!\n"); + exit(1); + } s->pidbufsize=1024; s->pids=malloc(s->pidbufsize,pid_t); s->namebufsize=64; @@ -64,7 +76,7 @@ struct data_stream* data_stream_init(int fd){ s->writebufsize=4096; s->writebuf=malloc(s->writebufsize,u8); s->cursor=0; - s->z=zbuffer_init(write_data_fd,&s->fd); + s->z=zbuffer_init(write_data_callback,s); callback_error=false; @@ -74,6 +86,7 @@ struct data_stream* data_stream_init(int fd){ int data_stream_finish_destroy(struct data_stream *s){ callback_error=false; zbuffer_finish_destroy(s->z); + fclose(s->mirrorfile); free(s->pids); free(s->namebuf); free(s->writebuf); diff --git a/server/main.c b/server/main.c index ffa16cd..cb4e2b4 100644 --- a/server/main.c +++ b/server/main.c @@ -12,6 +12,7 @@ #include "data_stream.h" #include "line_reader.h" #include "zbuffer.h" +#include "util.h" #define PORT 57575 @@ -38,12 +39,6 @@ int32_t pti_priority; task priority };*/ -static i64 make_timestamp(void){ - struct timeval tv; - gettimeofday(&tv,NULL); - return tv.tv_sec*(i64)1000000+tv.tv_usec; -} - static void connection_handler(int sock){ struct data_stream *stream=data_stream_init(sock); struct line_reader *reader=line_reader_init(sock); diff --git a/util.c b/util.c new file mode 100644 index 0000000..9fadd9f --- /dev/null +++ b/util.c @@ -0,0 +1,9 @@ +#include +#include "util.h" + + +i64 make_timestamp(void){ + struct timeval tv; + gettimeofday(&tv,NULL); + return tv.tv_sec*(i64)1000000+tv.tv_usec; +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..e161efe --- /dev/null +++ b/util.h @@ -0,0 +1,6 @@ +#pragma once + +#include "global.h" + + +i64 make_timestamp(void); -- cgit v1.2.3-70-g09d2