summaryrefslogtreecommitdiffstats
path: root/PIPET-1/main.c
blob: 45c9b4363d9559823bb1692681b9386c5b405fe5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//
//  main.c
//  PIPET-1
//
//  Created by dianshi on 3/28/20.
//  Copyright © 2020 dianshi. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <strings.h>
#include <getopt.h>
#include <signal.h>
#include <fcntl.h>
#include <pthread.h>

#include <SDL2/SDL.h>
#include <SDL2_ttf/SDL_ttf.h>

#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;
}