summaryrefslogtreecommitdiffstats
path: root/xpa/rtest.c
blob: 94387a79937550d80065ed91d73b83f34da11858 (plain)
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
/*
 *	Copyright (c) 2004 Smithsonian Astrophysical Observatory
 */

/*
 *
 * rtest -- server test for xpa
 *
 */
#include <xpap.h>

#define MAX_XPAS 10

extern char *optarg;
extern int optind;

#define BUFSIZE 1000000

char xbuf[BUFSIZE+1];
size_t xlen;
int quiet=0;

#ifdef ANSI_FUNC
int send_cb (void *client_data, void *call_data, char *paramlist, 
	     char **buf, size_t *len)
#else
int send_cb(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char **buf;
     size_t *len;
#endif
{
  XPA xpa = (XPA)call_data;

  if( !xpa ) return(-1);
  *len = xlen;
  *buf = (char *)xmalloc(*len);
  memcpy(*buf, xbuf, *len);
  if( !quiet ) fprintf(stderr, "sent (%s): %lu\n", 
		       paramlist, (unsigned long)xlen);
  return(0);
}

#ifdef ANSI_FUNC
int receive_cb (void *client_data, void *call_data, char *paramlist,
		char *buf, size_t len)
#else
int receive_cb(client_data, call_data, paramlist, buf, len)
     void *client_data;
     void *call_data;
     char *paramlist;
     char *buf;
     size_t len;
#endif
{
  XPA xpa = (XPA)call_data;

  if( !xpa ) return(-1);
  if( !quiet ) fprintf(stderr, "recd (%s): %s\n", paramlist, buf);
  return(0);
}

#ifdef ANSI_FUNC
int 
main (int argc, char **argv)
#else
main(argc, argv)
     int argc;
     char **argv;
#endif
{
  int c;
  int i, j;
  int got;
  int delay=-1;
  int total=0;
  int dobig=0;
  int dofd=0;
  int doexit=0;
  int poll=1;
  int msec=100;
  size_t lens[MAX_XPAS];
  char *bufs[MAX_XPAS];
  char *names[MAX_XPAS];
  char *errs[MAX_XPAS];
  char *xmode="";
  char xname[SZ_LINE];
  char xclass[SZ_LINE];
  char paramlist[SZ_LINE];
  
  int xfds[1];
  XPA xpa;

  /* we use the xpa timestamp routine */
  putenv("XPA_TIMESTAMP_ERRORS=true");

  /* process switch arguments */
  while ((c = getopt(argc, argv, "bd:fm:pqv")) != -1){
    switch(c){
    case 'b':
      dobig = 1;
      break;
    case 'd':
      delay = atoi(optarg);
      break;
    case 'f':
      dofd = 1;
      break;
    case 'm':
      msec = atoi(optarg);
      break;
    case 'p':
      poll = 0;
      break;
    case 'q':
      quiet = 1;
      break;
    case 'v':
      break;
    default:
      break;
    }
  }
  /* make sure we have the xpa argument */
  if( optind >= argc ){
    fprintf(stderr, "usage: %s [xpa] [target1 target2 ...]\n", argv[0]);
    exit(1);
  }
  strcpy(xname, argv[optind++]);
  strcpy(xclass, xname);
  cluc(xclass);

  if( (xpa = XPANew(xclass, xname, "help is on the way", 
		    send_cb, (void *)xname, xmode, 
		    receive_cb, (void *)xname, xmode)) ){
    fprintf(stdout, "%s using method: %s\n", xpa_name(xpa), xpa_method(xpa));
  }
  else{
    fprintf(stderr, "ERROR: could not init xpa\n");
    exit(1);
  }

  if( dobig ){
    for(i=0; i<BUFSIZE; i++){
      xbuf[i] = 'a' + (i%26);
    }
  }
  else{
    snprintf(xbuf, SZ_LINE,
	     "%s:%s %s (%s);",
	     xpa_class(xpa), xpa_name(xpa), xpa_method(xpa), xpa_sendian(xpa));
  }
  xlen = strlen(xbuf);
  xfds[0] = fileno(stderr);

  fprintf(stdout, "\nEntering select loop ...\n");
  while( 1 ){
    if( poll ){
      fprintf(stderr, "*");
      XPAPoll(msec, 1);
    }
    else{
      fprintf(stderr, ".");
    }
    for(doexit=0, j=optind; j<argc; j++){
      snprintf(paramlist, SZ_LINE, "#%d", total++);
      if( total && ((total % 1000) == 0) ) fprintf(stderr, "%d", total);
      if( dofd ){
	got = XPAGetFd(NULL, argv[j], paramlist, NULL, xfds, names, errs,
		       -MAX_XPAS);
	for(i=0; i<got; i++){
	  if( errs[i] ){
	    fprintf(stderr, "ERROR in %s:\n%s", names[i], errs[i]);
	    fprintf(stderr, "Exiting after error\n");
	    doexit = 1;
	  }
	}
      }
      else{
	got = XPAGet(NULL, argv[j], paramlist, NULL, bufs, lens, names, errs,
		     MAX_XPAS);
	for(i=0; i<got; i++){
	  if( errs[i] ){
	    fprintf(stderr, "ERROR in %s:\n%s", names[i], errs[i]);
	    fprintf(stderr, "Exiting after error\n");
	    doexit = 1;
	  }
	  else{
	    if( !quiet ) fprintf(stderr, "%s", bufs[i]);
	    if( bufs[i] ) xfree(bufs[i]);
	  }
	}
      }
      for(i=0; i<got; i++){
	if( names[i] ) xfree(names[i]);
	if( errs[i] ) xfree(errs[i]);
      }
      if( doexit ) exit(1);
      XPASet(NULL, argv[j], paramlist, NULL, xbuf, xlen, names, errs,
	     MAX_XPAS);
      for(i=0; i<got; i++){
	if( errs[i] ){
	  fprintf(stderr, "ERROR in %s:\n%s", names[i], errs[i]);
	  fprintf(stderr, "Exiting after error\n");
	  doexit = 1;
	}
      }
      for(i=0; i<got; i++){
	if( names[i] ) xfree(names[i]);
	if( errs[i] ) xfree(errs[i]);
      }
      if( doexit ) goto done;
      if( delay > 0 ) XPASleep(delay);
    }
  }

done:
  XPAFree(xpa);
  exit(0);
}