Debian Codegraph @

libsrc/xcon/client.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28
29 /*++++++++++++++++++ osx (socket) based client interfaces +++++++++++++++++++
30 .LANGUAGE C
31 .IDENTIFICATION Module consubs.c
32 .AUTHOR K. Banse ESO - Garching
33 .KEYWORDS background Midas, communication
34 .ENVIRONMENT UNIX only
35 .COMMENTS
36 holds ClientInit, ClientClose, ClientFree, Clientinfo, ClientWait,
37 ClientWrite, ClientRead, ClientKWrite, ClientKRead
38 ida_vuelta
39 .REMARKS
40 the corresponding server interfaces are in /midas/version/monit/prepz1.c
41
42 .VERSION [1.00] 940425: creation
43 .VERSION [1.05] 941018: use serv_buf.code_id as function code
44
45 090407 last modif
46 ----------------------------------------------------------------------------*/
47
48 #include <stdlib.h>
49 #include <midas_def.h>
50 #include <idiserver.h>
51 #include <xconndef.h>
52 #include <fsydef.h>
53
54 #define CLIENT_OVF -99
55
56 static char *servname[2] = { (char *)NULL, (char *)NULL };
57
58 static int first_bytes;
59 static int osxclid[MAX_BACK] =
60 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; /* sync with xconndef.h! */
61
62
63 /*
64
65 */
66
67 int ida_vuelta(flag,idclient,size,retosx)
68 int flag; /* IN: = 1, write + read \
69 = 2, write only \
70 = 3, read only */
71 int idclient; /* IN: channel no. of client */
72 int *size; /* IN: size of return package in bytes */
73 int *retosx;
74
75
76 {
77 int osx_cod;
78 int osxchan, n_bytes;
79
80
81 osxchan = osxclid[idclient];
82
83 if (flag != 3) /* write into socket */
84 {
85 osx_cod = osxwrite(osxchan,(char *)&serv_buf,serv_buf.nobyt);
86
87 if (osx_cod <= 0)
88 {
89 *retosx = oserror;
90 return (-1);
91 }
92 }
93
94
95 if (flag != 2) /* read from socket */
96 {
97 osx_cod = osxread (osxchan,(char *)&serv_ret,first_bytes);
98 if (osx_cod == -1)
99 {
100 *retosx = oserror;
101 return (-1);
102 }
103 else if (osx_cod == 1) /* connection was closed */
104 return (1);
105
106 n_bytes = serv_ret.nobyt - RET_SIZE; /* get remaining bytes */
107 }
108 else
109 n_bytes = 0;
110
111
112 if (n_bytes > 0)
113 {
114 osx_cod = osxread(osxchan,(char *)&serv_ret.data,n_bytes);
115 if (osx_cod == -1)
116 {
117 *retosx = oserror;
118 return(-1);
119 }
120 }
121
122 *size = n_bytes; /* return actual no. of returned bytes */
123 return (0);
124 }
125 /*
126
127 */
128
129 int ClientInit(shost,sunit,idclient,retosx)
130 char *shost, *sunit;
131 int *idclient;
132 int *retosx;
133
134 {
135 int osxchan, port;
136 int jsecs, msecs, jindx;
137 int i, mode;
138
139
140 for (i=0; i<MAX_BACK; i++)
141 {
142 if (osxclid[i] == -1)
143 {
144 jindx = i;
145 goto init_start;
146 }
147 }
148 return (CLIENT_OVF);
149
150
151 init_start:
152 first_bytes = BUFHEAD;
153 jsecs = 1;
154 msecs = 0;
155
156 if (shost[0] == '\0')
157 {
158 char *pipedir, filename[128];
159
160 if (!(pipedir = getenv("MID_WORK")))
161 {
162 (void) printf
163 ("ClientInit: MID_WORK not defined - we use $HOME/midwork/ ...\n");
164 if (!(pipedir = getenv("HOME"))) return (-1);
165
166 (void) strcpy(filename,pipedir);
167 (void) strcat(filename,"/midwork");
168 }
169 else
170 (void) strcpy(filename,pipedir);
171
172 (void) strcat(filename,"/Midas_osx");
173 (void) strcat(filename,sunit); /* filename: $MID_WORK/Midas_osxXY */
174 i = (int)strlen(filename) + 1;
175 servname[0] = malloc((size_t)i);
176 (void)strcpy(servname[0],filename);
177 mode = LOCAL | IPC_WRITE;
178 }
179 else
180 {
181 i = (int)strlen(BASE_PORT) + 1;
182 servname[0] = malloc((size_t)i);
183 if ( (port = osxgetservbyname(BASE_SERVICE)) == -1)
184 (void)sprintf(servname[0],"%d",atoi(BASE_PORT) + atoi(sunit));
185 else
186 (void)sprintf(servname[0],"%d",port + atoi(sunit));
187
188 i = (int)strlen(shost) + 1;
189 servname[1] = malloc((size_t)i);
190 strcpy (servname[1],shost);
191 mode = NETW | IPC_WRITE;
192 }
193
194
195 /* Open server channel for reading. */
196
197 osxchan = osxopen(servname, mode);
198
199
200 free(servname[0]);
201 free(servname[1]);
202 if ( osxchan == -1)
203 { /* we need to look at `oserror' */
204 *retosx = oserror;
205 if (oserror == -1)
206 (void) printf("ClientInit: osxopen produced: %s\n",oserrmsg);
207 else
208 (void) printf("ClientInit: osxopen produced: %s\n",osmsg());
209 return(-1);
210 }
211
212
213 osxclid[jindx] = osxchan;
214 *idclient = jindx;
215
216 memset((char *)&serv_buf,0,sizeof(serv_buf)); /* init structures */
217 memset((char *)&serv_ret,0,sizeof(serv_ret));
218
219 serv_buf.code_id = 0;
220 return(0);
221 }
222
223 /*
224
225 */
226
227 void ClientFree()
228
229 {
230 int i;
231
232 for (i=0; i<MAX_BACK; i++)
233 osxclid[i] = -1;
234 }
235
236
237
238 int ClientInfo(idclient,fd) /* get info about socket in use */
239 int idclient, *fd;
240
241 {
242 if ((idclient < 0) || (idclient >= MAX_BACK)) return (-9);
243
244 *fd = osxclid[idclient];
245
246 return (0);
247 }
248
249
250
251 int ClientClose(idclient,retosx) /* close connection to server */
252 int idclient, *retosx;
253
254 {
255 int osxchan, kk;
256
257 if ((idclient < 0) || (idclient >= MAX_BACK)) return (-9);
258 osxchan = osxclid[idclient];
259 osxclid[idclient] = -1; /* free entry again */
260 kk = osxclose (osxchan);
261 if (kk != 0) *retosx = oserror;
262
263 return(kk);
264 }
265
266 /*
267
268 */
269
270 int ClientWait(idclient,timo)
271 int idclient, timo;
272
273 {
274 int osxchan;
275 int jsecs, msecs;
276 int np;
277
278
279 if ((idclient < 0) || (idclient >= MAX_BACK)) return (-9);
280 osxchan = osxclid[idclient];
281
282
283 if (timo < 0) /* -1 = wait until we get data */
284 {
285 jsecs = 0;
286 msecs = 100;
287
288 osx_poll:
289 np = osxinfo(osxchan,jsecs,msecs);
290 if ((np == NOCONN) || (np == -1))
291 return (-1); /* no connection ! */
292
293 if (np != DATARDY) goto osx_poll;
294 return (1); /* we got data */
295 }
296
297 else
298 {
299 jsecs = timo;
300 msecs = 0;
301 np = osxinfo(osxchan,jsecs,msecs);
302 if ((np == NOCONN) || (np == -1))
303 return (-1); /* no connection ! */
304
305 if (np == DATARDY)
306 return (1); /* we got data */
307 else
308 return (0); /* NODATA */
309 }
310 }
311
312 /*
313
314 */
315
316 int ClientWrite(idclient,comline,retosx)
317 int idclient;
318 char *comline;
319 int *retosx;
320
321 {
322 int txtlen, np, myret;
323 int retsize;
324
325 char *charbuf;
326
327
328 if ((idclient < 0) || (idclient >= MAX_BACK)) return (-9);
329
330 txtlen = (int)strlen(comline) + 1;
331 np = ((txtlen % III_SIZE) == 0) ? (txtlen/III_SIZE) : (txtlen/III_SIZE + 1);
332
333 serv_buf.nobyt = np*III_SIZE + BUFHEAD;
334 serv_buf.code_id = 10;
335
336 charbuf = (char *)(serv_buf.data.in);
337 (void)strcpy(charbuf,comline);
338
339 np = ida_vuelta(2,idclient,&retsize,&myret); /* don't wait */
340
341 if (np != 0) *retosx = myret;
342 return (np);
343
344 }
345
346 /*
347
348 */
349
350 int ClientRead(idclient,comline,bgstat,retosx)
351 int idclient;
352 char *comline;
353 int *bgstat;
354 int *retosx;
355
356 {
357 int np, retsize, myret;
358
359
360
361 if ((idclient < 0) || (idclient >= MAX_BACK)) return (-9);
362
363 np = ida_vuelta(3,idclient,&retsize,&myret); /* wait for return */
364 if (np != 0) *retosx = myret;
365
366 *bgstat = serv_ret.code; /* status of command executed by server */
367 return (np);
368
369 }
370
371 /*
372
373 */
374
375 int ClientKWrite(idclient,typ,comline,sendbuf,karr,retosx)
376 int idclient;
377 int typ;
378 char *comline;
379 char *sendbuf;
380 int *karr; /* 4 elements */
381 int *retosx;
382
383 {
384 register int nr;
385 int retsize;
386 int txtlen, np, myret, m;
387
388 char *charbuf;
389
390
391 if ((idclient < 0) || (idclient >= MAX_BACK)) return (-9);
392
393 txtlen = (int)strlen(comline) + 1;
394 np = ((txtlen % III_SIZE) == 0) ? (txtlen/III_SIZE) : (txtlen/III_SIZE + 1);
395
396 serv_buf.nobyt = np*III_SIZE + BUFHEAD;
397 serv_buf.code_id = 20;
398
399 charbuf = (char *)(serv_buf.data.in);
400 (void)strcpy(charbuf,comline);
401
402 np = ida_vuelta(1,idclient,&retsize,&myret); /* send 1. part */
403
404 if (np != 0)
405 {
406 *retosx = myret;
407 return (np);
408 }
409
410 for (nr=0; nr<4; nr++)
411 serv_buf.data.in[nr] = karr[nr];
412
413 if (typ == 1)
414 {
415 int *ipntr;
416
417 ipntr = (int *) sendbuf;
418 for (nr=4; nr<karr[0]+4; nr++)
419 serv_buf.data.in[nr] = *ipntr++;
420 m = (karr[0]+4)*III_SIZE;
421 }
422 else if (typ == 2)
423 {
424 float *fpntr;
425
426 fpntr = (float *) sendbuf;
427 for (nr=4; nr<karr[0]+4; nr++)
428 serv_buf.data.fl[nr] = *fpntr++;
429 m = (karr[0]+4)*RRR_SIZE;
430 }
431 else if (typ == 4)
432 {
433 double *dpntr;
434
435 dpntr = (double *) sendbuf;
436 for (nr=4; nr<karr[0]+4; nr++)
437 serv_buf.data.db[nr] = *dpntr++;
438 m = (karr[0]+4)*DDD_SIZE;
439 }
440 else
441 {
442 char *cpntr;
443
444 cpntr = (char *) &serv_buf.data.in[4];
445 m = karr[0];
446 (void)strncpy(cpntr,sendbuf,m);
447 m += 4*III_SIZE;
448 }
449
450 serv_buf.nobyt = m + BUFHEAD;
451 serv_buf.code_id = 21;
452
453 np = ida_vuelta(1,idclient,&retsize,&myret); /* send 2. part */
454 if (np != 0)
455 *retosx = myret;
456 else
457 {
458 for (nr=0; nr<4; nr++)
459 karr[nr] = serv_ret.data.in[nr];
460 }
461
462 return (np);
463
464 }
465 /*
466
467 */
468
469 int ClientKRead(idclient,typ,comline,retbuf,karr,retosx)
470 int idclient;
471 int typ;
472 char *comline;
473 char *retbuf;
474 int *karr; /* 4 elements */
475 int *retosx;
476
477 {
478 register int nr;
479 int retsize;
480 int txtlen, np, myret;
481
482 char *charbuf;
483
484
485
486 if ((idclient < 0) || (idclient >= MAX_BACK)) return (-9);
487
488 txtlen = (int)strlen(comline) + 1;
489
490 np = ((txtlen % III_SIZE) == 0) ? (txtlen/III_SIZE) : (txtlen/III_SIZE + 1);
491
492 serv_buf.nobyt = np*III_SIZE + BUFHEAD;
493 serv_buf.code_id = 30;
494
495 charbuf = (char *)(serv_buf.data.in);
496 (void)strcpy(charbuf,comline);
497
498 np = ida_vuelta(1,idclient,&retsize,&myret); /* write + read */
499
500 if (np != 0)
501 *retosx = myret;
502 else
503 {
504 for (nr=0; nr<4; nr++)
505 karr[nr] = serv_ret.data.in[nr];
506
507 if (typ == 1)
508 {
509 int *ipntr;
510
511 ipntr = (int *) retbuf;
512 for (nr=4; nr<karr[0]+4; nr++)
513 *ipntr++ = serv_ret.data.in[nr];
514 }
515 else if (typ == 2)
516 {
517 float *fpntr;
518
519 fpntr = (float *) retbuf;
520 for (nr=4; nr<karr[0]+4; nr++)
521 *fpntr++ = serv_ret.data.fl[nr];
522 }
523 else if (typ == 4)
524 {
525 double *dpntr;
526
527 dpntr = (double *) retbuf;
528 for (nr=4; nr<karr[0]+4; nr++)
529 *dpntr++ = serv_ret.data.db[nr];
530 }
531 else
532 {
533 char *cpntr;
534
535 cpntr = (char *) &serv_ret.data.in[4];
536 (void)strncpy(retbuf,cpntr,karr[0]);
537 }
538 }
539
540 return (np);
541 }
542