-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_test.c
366 lines (342 loc) · 8.85 KB
/
echo_test.c
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include"lineclient.h"
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<ctype.h>
#include<string.h>
#include<sys/time.h>
#include<time.h>
#include<errno.h>
#define def_num_lines 32
#define def_min_size 0
#define def_max_size 256
#define def_min_wait 0
#define def_max_wait 8
int num_lines=def_num_lines;
int min_size=def_min_size;
int max_size=def_max_size;
int min_wait=def_min_wait;
int max_wait=def_max_wait;
const char *host;
u_int16_t port;
#define tcpbuf_len 4096
const int sendbuf_len=tcpbuf_len;
const int recvbuf_len=tcpbuf_len;
const char usage_fmt[]="\
Usage: echo_test <-n number of lines (default %d)\n\
<-s min,max size of lines (default %d,%d)\n\
<-w min,max wait between lines (default %d,%d)\n\
[host] [port]\n";
int get_min_max(char *buf, int *min, int *max);
int read_args(int argc, char *argv[]);
void print_usage_str();
inline int rand_range(int min, int max);
void generate_line(char *buf, int len);
time_t start_time;
typedef struct test_string
{
int len;
char *str;
int time;
} test_string;
typedef struct test_strings
{
int num_strings;
test_string *strings;
char *buf;
int next_send;
int next_receive;
} test_strings;
void test_strings_construct(test_strings *ts, int num_strings,
int min_len, int max_len,
int min_wait, int max_wait);
void test_strings_destruct(test_strings *ts);
int test_strings_send(test_strings *ts, const char **buf, int *len);
void test_strings_sent(test_strings *ts);
int test_strings_received(test_strings *ts, const char *buf, int len);
void print_test_strings(test_strings *ts);
void print_string(const char *buf, int len);
int
main(int argc, char *argv[])
{
lineclient client;
lineclient_event e;
const pollconn *c;
struct pollfd pfd;
test_strings ts;
const char *buf;
int len, ret;
struct timeval init_time;
gettimeofday(&init_time, NULL);
srand(init_time.tv_sec^init_time.tv_usec);
if(!read_args(argc, argv))
return 1;
lineclient_construct(&client, &pfd, sendbuf_len, recvbuf_len);
if(lineclient_resolve(&client, host, port)!=0)
{
fprintf(stderr, "Error resolving host.\n");
lineclient_destruct(&client);
return 2;
}
while(1)
{
if(poll(&pfd, 1, 250)<0)
{
perror("echo_test poll");
lineclient_destruct(&client);
return 3;
}
lineclient_operate(&client);
c=lineclient_conn(&client);
if((pollconn_err_connect(c)!=EINPROGRESS &&
pollconn_err_connect(c)!=0) ||
pollconn_err_send(c)!=0)
{
fprintf(stderr, "Error connecting socket.\n");
fprintf(stderr, "err_connect=%s\n",
strerror(pollconn_err_connect(c)));
fprintf(stderr, "err_send=%s\n",
strerror(pollconn_err_send(c)));
lineclient_destruct(&client);
return 4;
}
if(pollconn_ready(c))
break;
}
start_time=time(NULL);
test_strings_construct(&ts, num_lines,
min_size, max_size, min_wait, max_wait);
//print_test_strings(&ts);
while(num_lines>0)
{
if(poll(&pfd, 1, 250)<0)
{
perror("echo_test poll");
lineclient_destruct(&client);
test_strings_destruct(&ts);
return 5;
}
lineclient_operate(&client);
if((ret=test_strings_send(&ts, &buf, &len))==1)
{
if(lineclient_enqueue(&client, buf, len)<0)
{
fprintf(stderr, "Error enqueuing to send buffers.\n");
lineclient_destruct(&client);
test_strings_destruct(&ts);
return 6;
}
test_strings_sent(&ts);
//printf("sent: ");
//print_string(buf, len);
//printf("\n");
}
else if(ret<0)
lineclient_shutdown(&client);
while((e=lineclient_getevent(&client))!=LINECLIENT_NULLEVENT)
{
if(e==LINECLIENT_LINE)
{
lineclient_getline(&client, &buf, &len);
//printf("received: ");
//print_string(buf, len);
//printf("\n");
if(!test_strings_received(&ts, buf, len))
{
lineclient_destruct(&client);
test_strings_destruct(&ts);
return 7;
}
num_lines--;
//printf("num_lines=%d\n", num_lines);
}
else if(e==LINECLIENT_CLOSED)
{
if(num_lines>0)
{
test_strings_destruct(&ts);
lineclient_destruct(&client);
return 8;
}
}
lineclient_dequeue(&client);
}
}
test_strings_destruct(&ts);
lineclient_destruct(&client);
return 0;
}
void
test_strings_construct(test_strings *ts, int num_strings,
int min_len, int max_len,
int min_wait, int max_wait)
{
int i;
int last_wait=0;
ts->num_strings=num_strings;
ts->strings=(test_string*)malloc(sizeof(test_string)*num_strings);
ts->buf=(char*)malloc(max_len*num_strings);
ts->next_send=0;
ts->next_receive=0;
for(i=0; i<ts->num_strings; i++)
{
ts->strings[i].len=rand_range(min_len, max_len);
last_wait+=rand_range(min_wait, max_wait);
ts->strings[i].time=start_time+last_wait;
ts->strings[i].str=ts->buf+(max_len*i);
generate_line(ts->strings[i].str, ts->strings[i].len);
}
}
void
test_strings_destruct(test_strings *ts)
{
free(ts->strings);
free(ts->buf);
}
int
test_strings_send(test_strings *ts, const char **buf, int *len)
{
test_string *t;
if(ts->next_send>=ts->num_strings)
return -1; //done sending
t=ts->strings+ts->next_send;
//printf("time=%d\n", time(NULL));
if(time(NULL)>=t->time)
{
*buf=t->str;
*len=t->len;
return 1;
}
return 0;
}
void
test_strings_sent(test_strings *ts)
{
if(ts->next_send<ts->num_strings)
(ts->next_send)++;
}
int
test_strings_received(test_strings *ts, const char *buf, int len)
{
test_string *t;
if(ts->next_receive>=ts->num_strings)
return -1; //done already
t=ts->strings+ts->next_receive;
if(len!=t->len)
return 0;
while(len--)
{
if(buf[len]!=t->str[len])
return 0;
}
(ts->next_receive)++;
return 1;
}
void
generate_line(char *buf, int len)
{
while(len--)
{
do
buf[len]=(char)rand();
while(buf[len]=='\n' || buf[len]=='\0' || buf[len]=='\r'
//|| (!isalnum(buf[len])));
);
}
}
int
read_args(int argc, char *argv[])
{
char opt;
while((opt=getopt(argc, argv, "n:s:w:"))!=-1)
{
switch(opt)
{
case 'n':
num_lines=atoi(optarg);
if(num_lines<0)
{
fprintf(stderr, "Error: invalid number of lines.\n");
return 0;
}
break;
case 's':
if(!get_min_max(optarg, &min_size, &max_size))
{
fprintf(stderr, "Error: invalid size range.\n");
return 0;
}
break;
case 'w':
if(!get_min_max(optarg, &min_wait, &max_wait))
{
fprintf(stderr, "Error: invalid wait range.\n");
return 0;
}
break;
}
}
if(argc-optind<=1)
{
print_usage_str();
return 0;
}
host=argv[optind];
port=atoi(argv[optind+1]);
return 1;
}
int get_min_max(char *buf, int *min, int *max)
{
int i, len, comma_pos=-1;
char c;
len=strlen(buf);
for(i=0; i<len; i++)
{
c=buf[i];
if(!isdigit(c))
{
if(comma_pos==-1 && c==',')
comma_pos=i;
else
return 0;
}
}
buf[comma_pos]='\0';
*min=atoi(buf);
*max=atoi(buf+comma_pos+1);
if(min<0 || max<0)
return 0;
return 1;
}
inline int
rand_range(int min, int max)
{
return min+rand()%(max-min);
}
void
print_usage_str()
{
fprintf(stderr, usage_fmt, def_num_lines, def_min_size, def_max_size,
def_min_wait, def_max_wait);
}
void
print_test_strings(test_strings *ts)
{
int i;
test_string *t;
for(i=0; i<ts->num_strings; i++)
{
t=ts->strings+i;
printf("time=%d\n", t->time);
printf("string=");
print_string(t->str, t->len);
printf("\n\n");
}
}
void
print_string(const char *buf, int len)
{
int i;
for(i=0; i<len; i++)
printf("%c", buf[i]);
}