// // main.c // PIPET-1 // // Created by dianshi on 3/28/20. // Copyright © 2020 dianshi. All rights reserved. // #include #include #include #include #include #include #include #include #include #include #include #include "pipet_view.h" #define SCREEN_WIDTH 200 #define SCREEN_HEIGHT 100 typedef struct { int f_output; //show statistics of pipe int f_stopzero; //stop if 0 bytes in input int f_nowriteout; // dont write to output int f_graphmode; //show graph } pipet_params; typedef struct { uint64_t cnt_in; uint64_t prev_cnt_in; uint64_t cnt_out; } pipet_stats; typedef struct { int fd_in; int fd_out; pipet_stats *ps; } thread_params; //global_params static pipet_params g_params; static pipet_stats g_stats; static pipet_view g_view; static int quit=0; static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; void helper(char *progname) { printf("Usage: %s [OPTS]\n\n" "-o show output stats\n" "-s stop if there is 0 bytes in read\n" "-n dont ouput input data\n" "-g graphical interface\n" "Version: 0.0.2 \n" "\n" , progname); } void sig_handler(int signo) { switch(signo) { case SIGINT: printf("Catch SIGINT Exit\n"); if (g_params.f_output) { printf("Data in %d bytes Data out %d bytes",g_stats.cnt_in,g_stats.cnt_out); } default: printf("Unknown signal %d\n",signo); } exit(0); } void *thread_pipe(void *param) { thread_params *tp = (thread_params *)param; int cur_read=-1; int cur_write=-1; const uint64_t BUF_SZ=1024*4; uint8_t buf[BUF_SZ]; printf("Start thread\n"); while (quit == 0) { cur_read = read(tp->fd_in,buf, BUF_SZ); if (g_params.f_stopzero) { if (cur_read < 1) { quit = 1; break; } } g_stats.cnt_in += cur_read;// printf("Read %d\n", cur_read); if (!g_params.f_nowriteout) { cur_write = write(tp->fd_out, buf, cur_read); g_stats.cnt_out += cur_write; } } //exit(0); pthread_exit(NULL); } int main(int argc, const char * argv[]) { int c; int rc; int fd_stdin=-1,fd_stdout=-1; int cur_read,cur_write; int prev_val=0; pthread_t thread; SDL_Event event; const uint64_t BUF_SZ=1024*4; uint8_t buf[BUF_SZ]; thread_params tp; // insert code here... memset(&g_params,0,sizeof(pipet_params)); memset(&g_stats,0,sizeof(pipet_stats)); memset(&g_view,0,sizeof(pipet_view)); memset(&tp,0,sizeof(thread_params)); pv_init(&g_view, 200, SCREEN_WIDTH, SCREEN_HEIGHT); fd_stdin = fileno(stdin); fd_stdout = fileno(stdout); //int flags = fcntl(fd_stdin, F_GETFL, 0); //fcntl(fd_stdin, F_SETFL, flags | O_NONBLOCK); if (signal(SIGINT,sig_handler) == SIG_ERR) { printf("cannot register signal handler\n"); exit(1); } //process arguments while ((c = getopt(argc, argv, "osng")) != -1) { switch (c) { case 'o': g_params.f_output = 1; break; case 's': g_params.f_stopzero = 1; break; case 'n': g_params.f_nowriteout = 1; break; case 'g': g_params.f_graphmode = 1; break; default: helper(argv[0]); exit(1); } } if ( argc < 2 ) { helper(argv[0]); exit(1); } //start graph if (g_params.f_graphmode) { printf("Start thread\n"); SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindow("PIPET-1", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawColor(renderer, 0xff, 0x99, 0x99, 0x99); //int rc = pthread_create(&thread, NULL, thread_graph, NULL); //if (rc) { // printf("Cannot start thread\n"); } } tp.fd_in = dup(fd_stdin); tp.fd_out = dup(fd_stdout); rc = pthread_create(&thread, NULL, thread_pipe, &tp); if (rc) { printf("Cannot lunch pipe thread\n"); } //main loop int i=0; while (quit == 0) { i+=10; /* //printf("!!!\n"); cur_read = read(fd_stdin,buf, BUF_SZ); if (g_params.f_stopzero) { if (cur_read < 1) { quit = 1; break; } } g_stats.cnt_in += cur_read;// printf("Read %d\n", cur_read); if (!g_params.f_nowriteout) { cur_write = write(fd_stdout, buf, cur_read); g_stats.cnt_out += cur_write; } */ //sdl thread for now its here if (g_params.f_graphmode) { //printf("SDL loop\n"); while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: { quit = 1; break; } //case SDL_KE } //sleep(1); } SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); pv_render(&g_view, renderer); SDL_RenderPresent(renderer); pv_add_dot(&g_view, g_stats.cnt_in-g_stats.prev_cnt_in); g_stats.prev_cnt_in = g_stats.cnt_in; } sleep(1); } if (g_params.f_output) { printf("Data in %d bytes Data out %d bytes\n", g_stats.cnt_in, g_stats.cnt_out); } if (g_params.f_graphmode) { SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); } //pthread_join(&thread,NULL); //pthread_exit(NULL); return 0; }