Debian Codegraph @

xpa/ctest.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 /*
6 *
7 * ctest -- client test for xpa
8 *
9 */
10 #include <xpap.h>
11
12 extern char *optarg;
13 extern int optind;
14
15 int quiet=0;
16 int dowait=0;
17 int n=0;
18 int don=0;
19 int save_bytes=-1;
20 int exitonerror=0;
21 char *save_buf=NULL;
22 char *mode="";
23 char name[SZ_LINE];
24
25 #define MAX_FPS 4
26
27 #ifdef ANSI_FUNC
28 int
29 send_cb (void *client_data, void *call_data, char *paramlist,
30 char **buf, size_t *len)
31 #else
32 int send_cb(client_data, call_data, paramlist, buf, len)
33 void *client_data;
34 void *call_data;
35 char *paramlist;
36 char **buf;
37 size_t *len;
38 #endif
39 {
40 char *s = (char *)client_data;
41 XPA xpa = (XPA)call_data;
42 char tbuf[SZ_LINE];
43 int sendbuf=0;
44
45 /* introduce ourselves */
46 if( !quiet ){
47 fprintf(stdout, "SEND_CB #%d: %s:%s (%s)\n",
48 n++, xpa_class(xpa), xpa_name(xpa), s);
49 }
50
51 /* process special paramlist tokens */
52 if( paramlist && *paramlist ){
53 if( !quiet )
54 fprintf(stdout, "\tparamlist: %s\n", paramlist);
55 if( !strncmp(paramlist, "buf", 3) ){
56 sendbuf=1;
57 }
58 else if( !strncmp(paramlist, "error", 5) ){
59 if( !quiet )
60 fprintf(stdout, "\treturning error: %s\n", &paramlist[6]);
61 *len = 0;
62 *buf = NULL;
63 XPAError(xpa, &paramlist[6]);
64 return(-1);
65 }
66 else if( !strcmp(paramlist, "exit") ){
67 if( !quiet )
68 fprintf(stdout, "Exiting\n");
69 exit(0);
70 }
71 else if( !strcmp(paramlist, "wait") ){
72 fprintf(stdout, "Press <CR> to continue ...");
73 fgets(tbuf, SZ_LINE, stdin);
74 }
75 }
76 else if( dowait ){
77 fprintf(stdout, "Press <CR> to continue ...");
78 fgets(tbuf, SZ_LINE, stdin);
79 }
80
81 /* return information about this xpa */
82 if( !sendbuf ){
83 snprintf(tbuf, SZ_LINE,
84 "nclass: %s\nname: %s\nmethod: %s\nsendian: %s\ncendian: %s\n",
85 xpa_class(xpa), xpa_name(xpa), xpa_method(xpa),
86 xpa_sendian(xpa), xpa_cendian(xpa));
87
88 if( (xpa->send_mode & XPA_MODE_FILLBUF) ){
89 send(xpa_datafd(xpa), tbuf, strlen(tbuf), 0);
90 *len = 0;
91 *buf = NULL;
92 if( !quiet)
93 fprintf(stdout, "\tcallback writes %d bytes to client\n",
94 (int)strlen(tbuf));
95 }
96 /* return the buffer and let xpa transmit it */
97 else{
98 *len = strlen(tbuf);
99 *buf = (char *)xmalloc(*len);
100 memcpy(*buf, tbuf, *len);
101 if( !quiet)
102 fprintf(stdout, "\tcallback returns %d bytes to xpa handler\n",
103 (int)strlen(tbuf));
104 }
105 }
106 /* return the last buffer we were sent */
107 else{
108 if( (xpa->send_mode & XPA_MODE_FILLBUF) ){
109 send(xpa_datafd(xpa), save_buf, save_bytes, 0);
110 *len = 0;
111 *buf = NULL;
112 if( !quiet)
113 fprintf(stdout, "\tcallback writes %d bytes to client\n", save_bytes);
114 }
115 /* return the buffer and let xpa transmit it */
116 else{
117 *len = save_bytes;
118 *buf = (char *)xmalloc(*len);
119 memcpy(*buf, save_buf, *len);
120 if( !quiet)
121 fprintf(stdout, "\tcallback returns %d bytes to xpa handler\n",
122 save_bytes);
123 }
124 }
125 fflush(stdout);
126 fflush(stderr);
127 return(0);
128 }
129
130 #ifdef ANSI_FUNC
131 int
132 receive_cb (void *client_data, void *call_data, char *paramlist,
133 char *buf, size_t len)
134 #else
135 int receive_cb(client_data, call_data, paramlist, buf, len)
136 void *client_data;
137 void *call_data;
138 char *paramlist;
139 char *buf;
140 size_t len;
141 #endif
142 {
143 XPA xpa = (XPA)call_data;
144 char *s = (char *)client_data;
145 char tbuf[SZ_LINE];
146 char *errs[1];
147 int i;
148 int ip;
149 int got;
150 int xwait;
151
152 if( !quiet ){
153 fprintf(stdout, "RECEIVE_CB #%d: %s:%s (%s)\n",
154 n++, xpa_class(xpa), xpa_name(xpa), s);
155 }
156 if( !quiet ){
157 fprintf(stdout, "\tbuf: %lu bytes\n", (unsigned long)len);
158 }
159
160 /* process param list */
161 if( paramlist && *paramlist ){
162 if( !quiet )
163 fprintf(stdout, "\tparamlist: %s\n", paramlist);
164 if( !strcmp(paramlist, "free") ){
165 if( !quiet )
166 fprintf(stdout, "Freeing xpa struct\n");
167 XPAFree(xpa);
168 return(0);
169 }
170 else if( !strncmp(paramlist, "error", 5) ){
171 if( !quiet )
172 fprintf(stdout, "Processing error: %s\n", &paramlist[6]);
173 XPAError(xpa, &paramlist[6]);
174 return(-1);
175 }
176 else if( !strcmp(paramlist, "exit") ){
177 if( !quiet )
178 fprintf(stdout, "Exiting\n");
179 exit(0);
180 }
181 else if( !strcmp(paramlist, "wait") ){
182 fprintf(stdout, "Press <CR> to continue ...");
183 fgets(tbuf, SZ_LINE, stdin);
184 }
185 else if( !strncmp(paramlist, "xpaset", 6) ){
186 ip = 0;
187 word(paramlist, tbuf, &ip);
188 if( word(paramlist, tbuf, &ip) ){
189 if( !quiet )
190 fprintf(stdout, "calling XPASet(%s, \"%s\")\n",
191 tbuf, &(paramlist[ip]));
192 got = XPASet(NULL, tbuf, &(paramlist[ip]), mode,
193 paramlist, strlen(paramlist), NULL, errs, 1);
194 if( got == 0 ){
195 if( !quiet )
196 fprintf(stdout, "no XPA access points matched template %s\n",
197 tbuf);
198 }
199 else if( errs[0] != NULL ){
200 if( !quiet )
201 fprintf(stdout, "Error on xpaset to %s: %s\n", tbuf, errs[0]);
202 xfree(errs[0]);
203 if( exitonerror )
204 exit(1);
205 }
206 else{
207 if( !quiet )
208 fprintf(stdout, "XPASet to %s successful\n", tbuf);
209 }
210 }
211 return(0);
212 }
213 }
214 else if( dowait ){
215 fprintf(stdout, "Press <CR> to continue ...");
216 fgets(tbuf, SZ_LINE, stdin);
217 }
218
219 /* reset save buffer */
220 if( save_buf != NULL ){
221 xfree(save_buf);
222 save_buf = NULL;
223 }
224 save_bytes = 0;
225
226 xwait = dowait;
227 if( !(xpa->receive_mode & XPA_MODE_FILLBUF) ){
228 while( (got=recv(xpa_datafd(xpa), tbuf, SZ_LINE, 0)) >0 ){
229 if( xwait >0 ){
230 fprintf(stdout, "got %d bytes ... press <CR> to continue ...", got);
231 fgets(tbuf, SZ_LINE, stdin);
232 xwait--;
233 }
234 i = save_bytes;
235 save_bytes += got;
236 if( save_buf == NULL )
237 save_buf = (char *)xmalloc(save_bytes);
238 else
239 save_buf = (char *)xrealloc(save_buf, save_bytes);
240 memcpy(&save_buf[i], tbuf, got);
241 }
242 if( !quiet )
243 fprintf(stdout, "callback read %d bytes\n", save_bytes);
244 }
245 else{
246 save_bytes = len;
247 save_buf = (char *)xmalloc(len);
248 memcpy(save_buf, buf, len);
249 }
250 fflush(stdout);
251 fflush(stderr);
252 return(0);
253 }
254
255 #ifdef ANSI_FUNC
256 int
257 info_cb (void *client_data, void *call_data, char *paramlist)
258 #else
259 int info_cb(client_data, call_data, paramlist)
260 void *client_data;
261 void *call_data;
262 char *paramlist;
263 #endif
264 {
265 XPA xpa = (XPA)call_data;
266 char *s = (char *)client_data;
267 char xtemplate[SZ_LINE];
268 char *names[MAX_FPS];
269 char *bufs[MAX_FPS];
270 char *errs[MAX_FPS];
271 size_t lens[MAX_FPS];
272 int i;
273 int ip;
274 int got;
275
276 if( !quiet ){
277 fprintf(stdout, "INFO_CB #%d: %s:%s (%s)\n",
278 n++, xpa_class(xpa), xpa_name(xpa), s);
279 }
280
281 if( paramlist && *paramlist ){
282 if( !quiet )
283 fprintf(stdout, "\tparamlist: %s\n", paramlist);
284 ip = 0;
285 word(paramlist, xtemplate, &ip);
286 if( !strcmp(xtemplate, "xpaget") ){
287 word(paramlist, xtemplate, &ip);
288 got = XPAGet(xpa, xtemplate, &(paramlist[ip]), NULL,
289 bufs, lens, names, errs, MAX_FPS);
290 if( !quiet )
291 fprintf(stdout, "XPAGet (%s) returned %d buffer(s)\n", xtemplate, got);
292 for(i=0; i<got; i++){
293 if( !quiet )
294 fprintf(stdout, "\t%d: %s\n", i, names[i]);
295 if( errs[i] == NULL ){
296 if( lens[i] > 0 ){
297 fprintf(stdout, "contents (%lu bytes):\n", (unsigned long)lens[i]);
298 write(fileno(stdout), bufs[i], lens[i]);
299 }
300 if( !quiet )
301 fprintf(stdout, "\n");
302 }
303 else{
304 write(fileno(stdout), errs[i], strlen(errs[i]));
305 if( exitonerror )
306 exit(1);
307 }
308 if( bufs[i] )
309 xfree(bufs[i]);
310 if( names[i] )
311 xfree(names[i]);
312 if( errs[i] )
313 xfree(errs[i]);
314 }
315 }
316 }
317 fflush(stdout);
318 fflush(stderr);
319 return(0);
320 }
321
322 #ifdef ANSI_FUNC
323 int
324 main (int argc, char **argv)
325 #else
326 int
327 main(argc, argv)
328 int argc;
329 char **argv;
330 #endif
331 {
332 char *plist=NULL;
333 char *paramlist=NULL;
334 char *xtemplate="*:*";
335 char *smode=NULL;
336 char tbuf[SZ_LINE];
337 char name[SZ_LINE];
338 char cmd[SZ_LINE];
339 char fxpa[SZ_LINE];
340 char *names[MAX_FPS];
341 char *bufs[MAX_FPS];
342 char *dbufs[MAX_FPS];
343 char *errs[MAX_FPS];
344 char *buf=NULL;
345 int c;
346 size_t lens[MAX_FPS];
347 size_t dlens[MAX_FPS];
348 int i;
349 int got;
350 int maxbufs;
351 int j=0;
352 int loop=1;
353 int delay=0;
354 int get=1;
355 int set=0;
356 int info=0;
357 int access=0;
358 int pipe=0;
359 int tiny=0;
360 int doxpa = 0;
361 int xpaopen=0;
362 int wait=0;
363 XPA xpa=NULL;
364 XPA xpa1=NULL;
365 int fds[1];
366
367 #if HAVE_MINGW32==0
368 setvbuf(stdout, NULL, _IONBF, 0);
369 setvbuf(stderr, NULL, _IONBF, 0);
370 #endif
371
372 *cmd = '\0';
373 /* process switch arguments */
374 while ((c = getopt(argc, argv, "abd:ef:gil:m:nopqstwx:")) != -1){
375 switch(c){
376 case 'a':
377 get = 0;
378 set = 0;
379 info = 0;
380 access = 1;
381 break;
382 case 'b':
383 get = 1;
384 set = 1;
385 info = 0;
386 access = 0;
387 break;
388 case 'd':
389 delay = atoi(optarg);
390 break;
391 case 'e':
392 exitonerror++;
393 break;
394 case 'f':
395 snprintf(cmd, SZ_LINE, "stest %s &\n", optarg);
396 snprintf(fxpa, SZ_LINE, "%s1", optarg);
397 break;
398 case 'g':
399 get = 1;
400 set = 0;
401 info = 0;
402 access = 0;
403 break;
404 case 'i':
405 get = 0;
406 set = 0;
407 info = 1;
408 access = 0;
409 break;
410 case 'l':
411 loop = atoi(optarg);
412 break;
413 case 'm':
414 smode = xstrdup(optarg);
415 break;
416 case 'n':
417 don = 1;
418 break;
419 case 'o':
420 xpaopen = 1;
421 break;
422 case 'p':
423 pipe = 1;
424 fds[0] = fileno(stdout);
425 break;
426 case 'q':
427 quiet = 1;
428 break;
429 case 's':
430 get = 0;
431 set = 1;
432 info = 0;
433 access = 0;
434 break;
435 case 't':
436 tiny = 1;
437 break;
438 case 'w':
439 wait = 1;
440 break;
441 case 'x':
442 doxpa = 1;
443 strcpy(name, optarg);
444 break;
445 }
446 }
447 if( optind < argc ){
448 xtemplate = argv[optind];
449 optind++;
450 }
451
452 /* make up the paramlist */
453 plist = XPAArgvParamlist(argc, argv, optind);
454 if( !don )
455 paramlist = plist;
456 else
457 paramlist = (char *)xcalloc(strlen(plist)+SZ_LINE, sizeof(char));
458
459 /* for setting, make up a number of dbufs that we will send */
460 if( set ){
461 /* must be less than MAX_FPS */
462 maxbufs = 2;
463
464 dbufs[0] = (char *)xmalloc(SZ_LINE);
465 strcpy(dbufs[0], "this is a short string");
466 dlens[0] = strlen(dbufs[0]);
467
468 if( tiny )
469 dlens[1] = 256+1;
470 else
471 dlens[1] = 256*1000+1;
472 dbufs[1] = (char *)xmalloc(dlens[1]);
473 for(j=0, buf=dbufs[1]; j<dlens[1]; j++){
474 buf[j] = j%256;
475 }
476 }
477
478 /* background process initialization */
479 if( *cmd != '\0' ){
480 fprintf(stdout, "starting bkgd process: %s", cmd);
481 system(cmd);
482 for(i=0, n=0; i<5; i++){
483 if( (got=XPAAccess(NULL, fxpa, "g", NULL, names, errs, MAX_FPS)) ){
484 for(j=0; j<got; j++){
485 if( names[j] ) xfree(names[j]);
486 if( !errs[j] )
487 n++;
488 else
489 xfree(errs[j]);
490 }
491 if( n ){
492 fprintf(stdout, "process is started\n");
493 break;
494 }
495 }
496 else{
497 XPASleep(1000);
498 }
499 }
500 }
501
502 /* start up an xpa server, if necessary */
503 if( doxpa ) {
504 snprintf(tbuf, SZ_LINE, "%s from ctest", name);
505 if( (xpa1 = XPANew("CLIENT", name, name,
506 send_cb, tbuf, mode, receive_cb, tbuf, mode)) == NULL ){
507 fprintf(stderr, "ERROR: could not init client xpa: %s\n", name);
508 }
509 else{
510 fprintf(stdout, "%s using method: %s\n",
511 xpa_name(xpa1), xpa_method(xpa1));
512 }
513 }
514
515 /* XPAOpen */
516 if( xpaopen ){
517 if( (xpa = XPAOpen(NULL)) == NULL ){
518 fprintf(stderr, "ERROR: could not open any xpa access points: %s\n",
519 xtemplate);
520 exit(1);
521 }
522 }
523
524 /* iterate */
525 for(j=0; j<loop; j++){
526 if( loop > 1 ){
527 if( !quiet ){
528 fprintf(stdout, "CLIENT #%d: %s\n", j, xtemplate);
529 if( paramlist && *paramlist )
530 fprintf(stdout, "\tparamlist: %s\n", paramlist);
531 }
532 }
533 /* make up paramlist, if necessary */
534 if( don )
535 snprintf(paramlist, SZ_LINE, "%s %d", plist, j);
536 /* XPAGet, XPAGetFd */
537 if( get ){
538 if( pipe ){
539 got = XPAGetFd(xpa, xtemplate, paramlist, smode,
540 fds, names, errs, -MAX_FPS);
541 }
542 else{
543 got = XPAGet(xpa, xtemplate, paramlist, smode,
544 bufs, lens, names, errs, MAX_FPS);
545 for(i=0; i<got; i++){
546 if( !quiet ){
547 if( strcmp(paramlist, "buf") )
548 fprintf(stdout, "\treceived from %s\n", names[i]);
549 if( errs[i] == NULL ){
550 if( lens[i] > 0 ){
551 write(fileno(stdout), bufs[i], lens[i]);
552 }
553 if( strcmp(paramlist, "buf") )
554 fprintf(stdout, "\n");
555 }
556 else{
557 write(fileno(stdout), errs[i], strlen(errs[i]));
558 if( exitonerror )
559 exit(1);
560 }
561 }
562 if( bufs[i] )
563 xfree(bufs[i]);
564 if( names[i] )
565 xfree(names[i]);
566 if( errs[i] )
567 xfree(errs[i]);
568 }
569 }
570 }
571
572 /* XPASet, XPASetFd */
573 if( set ){
574 if( pipe ){
575 if( !quiet )
576 fprintf(stdout, "\tsending data from stdin\n");
577 got =XPASetFd(xpa, xtemplate, paramlist, smode,
578 fileno(stdin), names, errs, MAX_FPS);
579 }
580 else{
581 got = XPASet(xpa, xtemplate, paramlist, smode,
582 dbufs[j%maxbufs], dlens[j%maxbufs],
583 names, errs, MAX_FPS);
584 }
585 for(i=0; i<got; i++){
586 if( !quiet )
587 fprintf(stdout, "\tsent %lu bytes to %s\n",
588 (unsigned long)dlens[j%maxbufs], names[i]);
589 if( errs[i] != NULL ){
590 fprintf(stdout, "Error from server %s\n", names[i]);
591 write(fileno(stdout), errs[i], strlen(errs[i]));
592 if( exitonerror )
593 exit(1);
594 }
595 if( names[i] )
596 xfree(names[i]);
597 if( errs[i] )
598 xfree(errs[i]);
599 }
600 }
601
602 /* XPAInfo */
603 if( info ){
604 got = XPAInfo(xpa, xtemplate, paramlist, smode, names, errs, MAX_FPS);
605 for(i=0; i<got; i++){
606 if( !quiet ){
607 fprintf(stdout, "\tinfo from %s\n", names[i]);
608 if( errs[i] != NULL ){
609 write(fileno(stdout), errs[i], strlen(errs[i]));
610 if( exitonerror )
611 exit(1);
612 }
613 }
614 if( names[i] )
615 xfree(names[i]);
616 if( errs[i] )
617 xfree(errs[i]);
618 }
619 }
620
621 /* XPAAccess */
622 if( access ){
623 got = XPAAccess(xpa, xtemplate, paramlist, smode, names, errs, MAX_FPS);
624 for(i=0; i<got; i++){
625 if( !quiet ){
626 fprintf(stdout, "\taccess from %s\n", names[i]);
627 if( errs[i] != NULL ){
628 write(fileno(stdout), errs[i], strlen(errs[i]));
629 if( exitonerror )
630 exit(1);
631 }
632 }
633 if( names[i] )
634 xfree(names[i]);
635 if( errs[i] )
636 xfree(errs[i]);
637 }
638 }
639
640 /* if we processed no access points, let`s hear about it */
641 if( got == 0 ){
642 fprintf(stderr, "XPA$ERROR: no xpa access points processed\n");
643 }
644 else{
645 if( !quiet ){
646 if( strcmp(paramlist, "buf") )
647 fprintf(stdout, "CLIENT #%d complete\n", j);
648 }
649 else{
650 fprintf(stdout, ".");
651 fflush(stdout);
652 }
653 }
654
655 /* wait for next iteration */
656 if( wait ){
657 fprintf(stdout, "Press <CR> to continue ...");
658 fgets(tbuf, SZ_LINE, stdin);
659 }
660
661 /* delay */
662 if( delay )
663 XPASleep(delay);
664 }
665
666 /* XPAOpen/XPAClose */
667 if( xpaopen ){
668 XPAClose(xpa);
669 }
670
671 /* free up space */
672 if( set ){
673 for(j=0; j<maxbufs; j++){
674 if( dbufs[j] )
675 xfree(dbufs[j]);
676 }
677 }
678 if( plist ) xfree(plist);
679 if( don ) xfree(paramlist);
680 if( smode ) xfree(smode);
681 /* make valgrind happy */
682 XPACleanup();
683 exit(0);
684 }