Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.12
1.10 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.11 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.10 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
1.12 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/tcpf.c,v 1.11 2000/08/22 05:04:18 noro Exp $
1.10 noro 48: */
1.1 noro 49: #if INET
50: #include "ca.h"
51: #include "parse.h"
52: #include "com.h"
53: #include <signal.h>
54: #if !defined(VISUAL)
55: #include <sys/types.h>
56: #include <pwd.h>
57: #endif
58: #include "ox.h"
59:
60: #if defined(VISUAL)
61: #include <winsock.h>
62: #endif
63:
64: #define OX_XTERM "ox_xterm"
65:
66: #if !defined(_PA_RISC1_1)
67: #define RSH "rsh"
68: #else
69: #define RSH "remsh"
70: #endif
71:
72: static struct m_c {
73: int m,c,af_unix;
74: } *m_c_tab;
75:
76: static int m_c_i,m_c_s;
77:
78: #if MPI
79: extern int mpi_nprocs;
80: #define valid_mctab_index(ind)\
1.8 noro 81: if((ind)<0||(ind)>=mpi_nprocs){error("invalid server id");}
1.1 noro 82: #else
83: #define valid_mctab_index(ind)\
84: if((ind)<0||(ind)>=m_c_i||\
1.8 noro 85: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){error("invalid server id");}
1.1 noro 86: #endif
87:
88: int register_server();
89: int get_mcindex(int);
90:
1.8 noro 91: void Pox_send_raw_cmo(), Pox_recv_raw_cmo();
92:
1.1 noro 93: void Pox_launch(),Pox_launch_nox(),Pox_launch_main();
94: void Pox_launch_generic();
95: void Pox_shutdown();
96:
1.5 noro 97: void Pox_rpc(),Pox_cmo_rpc(),Pox_reset(),Pox_intr(),Pox_sync(),Pox_select();
1.1 noro 98:
99: void Pox_push_local(),Pox_push_cmo(),Pox_push_vl(),Pox_push_cmd();
100:
101: void Pox_pop_local(),Pox_pop_cmo();
102: void Pox_pop0_local(),Pox_pop0_cmo();
103: void Pox_pop_string(),Pox_pop0_string();
104: void Pox_get(),Pox_pops();
105:
106: void Pox_execute_function(),Pox_execute_string();
107: void Pox_setname(), Pox_evalname();
108: void Pox_flush();
109: void Pgenerate_port(),Ptry_bind_listen(),Ptry_connect(),Ptry_accept();
110: void Pregister_server();
111: void Pox_get_serverinfo();
1.8 noro 112: void Pox_mpi_myid(), Pox_mpi_nprocs();
1.1 noro 113:
114: void ox_launch_generic();
115:
116: pointer bevalf();
117:
118: struct ftab tcp_tab[] = {
1.8 noro 119: {"ox_send_raw_cmo",Pox_send_raw_cmo,2},
120: {"ox_recv_raw_cmo",Pox_recv_raw_cmo,1},
1.1 noro 121: {"ox_get_serverinfo",Pox_get_serverinfo,-1},
122: {"generate_port",Pgenerate_port,-1},
123: {"try_bind_listen",Ptry_bind_listen,1},
124: {"try_connect",Ptry_connect,2},
125: {"try_accept",Ptry_accept,2},
126: {"register_server",Pregister_server,4},
127: {"ox_flush",Pox_flush,1},
128: {"ox_launch",Pox_launch,-3},
129: {"ox_launch_nox",Pox_launch_nox,-3},
130: {"ox_launch_generic",Pox_launch_generic,7},
131: {"ox_shutdown",Pox_shutdown,1},
132:
133: {"ox_rpc",Pox_rpc,-99999999},
134: {"ox_cmo_rpc",Pox_cmo_rpc,-99999999},
135:
136: {"ox_sync",Pox_sync,1},
1.8 noro 137: #if MPI
138: {"ox_mpi_myid",Pox_mpi_myid,0},
139: {"ox_mpi_nprocs",Pox_mpi_nprocs,0},
140: #endif
1.1 noro 141: #if !MPI
142: {"ox_reset",Pox_reset,-2},
1.5 noro 143: {"ox_intr",Pox_intr,1},
1.1 noro 144: {"ox_select",Pox_select,-2},
145: #endif
146:
147: {"ox_pop_string",Pox_pop_string,1},
148: {"ox_pop0_string",Pox_pop0_string,1},
149: {"ox_pop_local",Pox_pop_local,1},
150: {"ox_pop0_local",Pox_pop0_local,1},
151: {"ox_pop_cmo",Pox_pop_cmo,1},
152: {"ox_pop0_cmo",Pox_pop0_cmo,1},
1.3 noro 153: {"ox_get",Pox_get,-1},
1.1 noro 154: {"ox_pops",Pox_pops,-2},
155:
156: {"ox_push_vl",Pox_push_vl,1},
157: {"ox_push_local",Pox_push_local,-99999999},
158: {"ox_push_cmo",Pox_push_cmo,-99999999},
159: {"ox_push_cmd",Pox_push_cmd,2},
160:
161: {"ox_setname",Pox_setname,2},
162: {"ox_evalname",Pox_evalname,2},
163:
164: {"ox_execute_string",Pox_execute_string,2},
165: {"ox_execute_function",Pox_execute_function,3},
166:
167:
168: {0,0,0},
169: };
170:
171: extern struct IOFP iofp[];
172: extern MATHCAP my_mathcap;
1.6 noro 173: extern int ox_exchange_mathcap;
1.1 noro 174:
175: char *getenv();
176:
1.8 noro 177: #if MPI
178: extern int mpi_myid, mpi_nprocs;
179:
180: void Pox_mpi_myid(rp)
181: Q *rp;
182: {
183: STOQ(mpi_myid,*rp);
184: }
185:
186: void Pox_mpi_nprocs(rp)
187: Q *rp;
188: {
189: STOQ(mpi_nprocs,*rp);
190: }
191: #endif
192:
1.1 noro 193: void Pox_get_serverinfo(arg,rp)
194: NODE arg;
195: LIST *rp;
196: {
197: int i,c;
198: Q sid;
199: NODE t,n0,n;
200: LIST list,l;
201:
202: if ( !arg ) {
203: for ( i = 0, n0 = 0; i < m_c_i; i++ )
1.8 noro 204: if ( (m_c_tab[i].m>=0) || (m_c_tab[i].c>=0) ) {
1.1 noro 205: c = m_c_tab[i].c;
206: ox_get_serverinfo(c,&list);
207: STOQ(i,sid);
208: t = mknode(2,sid,list);
209: MKLIST(l,t);
210: NEXTNODE(n0,n);
211: BDY(n) = (pointer)l;
212: }
213: if ( n0 )
214: NEXT(n) = 0;
215: MKLIST(*rp,n0);
216: } else {
217: i = QTOS((Q)ARG0(arg));
1.8 noro 218: if ( i >= 0 && i < m_c_i && ((m_c_tab[i].m>=0) || (m_c_tab[i].c>=0)) )
1.1 noro 219: ox_get_serverinfo(m_c_tab[i].c,rp);
220: else {
221: MKLIST(*rp,0);
222: }
223: }
224: }
225:
226: /*
227: if noarg or arg[0]==0 then use IP socket and return a port number
228: else use UNIX socket and return a string which represents a path name
229: */
230:
231: void Pgenerate_port(arg,rp)
232: NODE arg;
233: Obj *rp;
234: {
235: char port_str[BUFSIZ];
236: int port;
237: char *s;
238: STRING str;
239: Q q;
240:
241: if ( !arg || !ARG0(arg) ) {
242: generate_port(0,port_str);
243: port = atoi(port_str);
244: STOQ(port,q);
245: *rp = (Obj)q;
246: } else {
247: generate_port(1,port_str);
248: s = (char *)MALLOC(strlen(port_str)+1);
249: strcpy(s,port_str);
250: MKSTR(str,s);
251: *rp = (Obj)str;
252: }
253: }
254:
255: /*
256: try_bind_listen(port)
257: */
258:
259: void Ptry_bind_listen(arg,rp)
260: NODE arg;
261: Q *rp;
262: {
263: char port_str[BUFSIZ];
264: int port,s,use_unix;
265:
266: if ( !ARG0(arg) || NUM(ARG0(arg)) ) {
267: port = QTOS((Q)ARG0(arg));
268: sprintf(port_str,"%d",port);
269: use_unix = 0;
270: } else {
271: strcpy(port_str,BDY((STRING)ARG0(arg)));
272: use_unix = 1;
273: }
274: s = try_bind_listen(use_unix,port_str);
275: STOQ(s,*rp);
276: }
277:
278: /*
279: try_connect(host,port)
280: */
281:
282: void Ptry_connect(arg,rp)
283: NODE arg;
284: Q *rp;
285: {
286: char port_str[BUFSIZ];
287: char *host;
288: int port,s,use_unix;
289:
290: if ( !ARG1(arg) || NUM(ARG1(arg)) ) {
291: port = QTOS((Q)ARG1(arg));
292: sprintf(port_str,"%d",port);
293: use_unix = 0;
294: } else {
295: strcpy(port_str,BDY((STRING)ARG1(arg)));
296: use_unix = 1;
297: }
298: host = BDY((STRING)ARG0(arg));
299: s = try_connect(use_unix,host,port_str);
300: STOQ(s,*rp);
301: }
302:
303: /*
304: try_accept(sock,port)
305: */
306:
307: void Ptry_accept(arg,rp)
308: NODE arg;
309: Q *rp;
310: {
311: int use_unix,s;
312:
313: if ( !ARG1(arg) || NUM(ARG1(arg)) )
314: use_unix = 0;
315: else
316: use_unix = 1;
317: s = try_accept(use_unix,QTOS((Q)ARG0(arg)));
318: STOQ(s,*rp);
319: }
320:
321: /*
322: register_server(cs,cport,ss,sport)
323: */
324:
325: void Pregister_server(arg,rp)
326: NODE arg;
327: Q *rp;
328: {
329: int cs,ss,cn,sn,ind,use_unix,id;
330: char cport_str[BUFSIZ],sport_str[BUFSIZ];
331: MATHCAP server_mathcap;
332:
333: cs = QTOS((Q)ARG0(arg));
334: ss = QTOS((Q)ARG2(arg));
335: if ( !ARG1(arg) || NUM(ARG1(arg)) ) {
336: sprintf(cport_str,"%d",QTOS((Q)ARG1(arg)));
337: use_unix = 0;
338: } else {
339: strcpy(cport_str,BDY((STRING)ARG1(arg)));
340: use_unix = 1;
341: }
342: if ( !ARG3(arg) || NUM(ARG3(arg)) ) {
343: if ( use_unix )
344: error("register_server : the protocol should conincide for two sockets");
345: sprintf(sport_str,"%d",QTOS((Q)ARG3(arg)));
346: } else {
347: if ( !use_unix )
348: error("register_server : the protocol should conincide for two sockets");
349: strcpy(sport_str,BDY((STRING)ARG3(arg)));
350: }
351:
352: /* client mode */
353: cn = get_iofp(cs,cport_str,0);
354: sn = get_iofp(ss,sport_str,0);
1.12 ! noro 355: /* get_iofp returns -1 if the laucher could not spawn the server */
! 356: if ( sn < 0 ) {
! 357: /* we should terminate the launcher */
! 358: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
! 359: STOQ(-1,*rp);
! 360: return;
! 361: }
1.1 noro 362:
363: /* register server to the server list */
364: ind = register_server(use_unix,cn,sn);
365:
1.6 noro 366: if ( ox_exchange_mathcap ) {
367: /* request remote mathcap */
368: ox_send_cmd(sn,SM_mathcap);
369: ox_send_cmd(sn,SM_popCMO);
370: ox_flush_stream_force(sn);
371: ox_recv(sn,&id,&server_mathcap);
372: store_remote_mathcap(sn,server_mathcap);
373:
374: /* send my mathcap */
375: create_my_mathcap("asir");
376: ox_send_data(sn,my_mathcap);
377: ox_send_cmd(sn,SM_setMathcap);
378: }
1.1 noro 379: /* return the server id */
380: STOQ(ind,*rp);
381: }
382:
383: /*
384: ox_launch_generic(host,launcher,server,use_unix,use_ssh,use_x,conn_to_serv)
385:
386: Input
387: host: hostname on which servers run
388: launcher: path name of the launcher
389: server: path name of the server
390: use_unix: use UNIX domain socket if 1
391: use_ssh: use ssh if 1
392: use_x: use X11 facilities if 1
393: conn_to_serv: connect to server if 1
394: */
395:
396: void Pox_launch_generic(arg,rp)
397: NODE arg;
398: Q *rp;
399: {
400: int use_unix,use_ssh,use_x,conn_to_serv;
401: char *host,*launcher,*server;
402:
403: host = (arg&&ARG0(arg))?BDY((STRING)ARG0(arg)):0;
404: launcher = BDY((STRING)ARG1(arg));
405: server = BDY((STRING)ARG2(arg));
406: use_unix = ARG3(arg) ? 1 : 0;
407: use_ssh = ARG4(arg) ? 1 : 0;
408: use_x = ARG5(arg) ? 1 : 0;
409: conn_to_serv = QTOS((Q)ARG6(arg));
410: if ( !host )
411: use_unix = 1;
412: ox_launch_generic(host,launcher,server,
413: use_unix,use_ssh,use_x,conn_to_serv,rp);
414: }
415:
416: void ox_launch_generic(host,launcher,server,
417: use_unix,use_ssh,use_x,conn_to_serv,rp)
418: char *host,*launcher,*server;
419: int use_unix,use_ssh,use_x,conn_to_serv;
420: Q *rp;
421: {
422: int cs,ss,cn,sn,ind,id;
423: char control_port_str[BUFSIZ];
424: char server_port_str[BUFSIZ];
425: MATHCAP server_mathcap;
426:
427: control_port_str[0] = 0;
428: server_port_str[0] = 0;
429: do {
430: generate_port(use_unix,control_port_str);
431: generate_port(use_unix,server_port_str);
432: if ( !conn_to_serv ) {
433: cs = try_bind_listen(use_unix,control_port_str);
434: ss = try_bind_listen(use_unix,server_port_str);
435: }
436: spawn_server(host,launcher,server,
437: use_unix,use_ssh,use_x,conn_to_serv,
438: control_port_str,server_port_str);
439: if ( conn_to_serv ) {
440: cs = try_connect(use_unix,host,control_port_str);
441: ss = try_connect(use_unix,host,server_port_str);
442: } else {
443: cs = try_accept(use_unix,cs);
444: ss = try_accept(use_unix,ss);
445: }
446: } while ( cs < 0 || ss < 0 );
447:
448: /* client mode */
449: cn = get_iofp(cs,control_port_str,0);
450: sn = get_iofp(ss,server_port_str,0);
1.12 ! noro 451: /* get_iofp returns -1 if the laucher could not spawn the server */
! 452: if ( sn < 0 ) {
! 453: /* we should terminate the launcher */
! 454: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
! 455: STOQ(-1,*rp);
! 456: return;
! 457: }
1.1 noro 458:
459: /* register server to the server list */
460: ind = register_server(use_unix,cn,sn);
461:
1.6 noro 462: if ( ox_exchange_mathcap ) {
463: /* request remote mathcap */
464: ox_send_cmd(sn,SM_mathcap);
465: ox_send_cmd(sn,SM_popCMO);
466: ox_flush_stream_force(sn);
467: ox_recv(sn,&id,&server_mathcap);
468: store_remote_mathcap(sn,server_mathcap);
469:
470: /* send my mathcap */
471: create_my_mathcap("asir");
472: ox_send_data(sn,my_mathcap);
473: ox_send_cmd(sn,SM_setMathcap);
474: }
1.1 noro 475: /* return the server id */
476: STOQ(ind,*rp);
477: }
478:
479: int spawn_server(host,launcher,server,
480: use_unix,use_ssh,use_x,conn_to_serv,
481: control_port_str,server_port_str)
482: char *host,*launcher,*server;
483: int use_unix,use_ssh,use_x,conn_to_serv;
484: char *control_port_str,*server_port_str;
485: {
486: char cmd[BUFSIZ];
487: char localhost[BUFSIZ];
488: char *dname,*conn_str,*rsh;
489:
490: dname = use_x ? (char *)getenv("DISPLAY") : 0;
491: conn_str = conn_to_serv ? "1" : "0";
492: rsh = use_ssh ? "ssh" : RSH;
493: gethostname(localhost,BUFSIZ);
1.4 noro 494: #if !defined(VISUAL)
1.1 noro 495: if ( use_unix ) {
496: if ( !fork() ) {
1.2 noro 497: setpgid(0,getpid());
1.1 noro 498: if ( dname )
499: execlp("xterm","xterm","-name",OX_XTERM,"-display",dname,
500: "-geometry","60x10","-e",launcher,".",conn_str,
501: control_port_str,server_port_str,server,dname,0);
502: else
503: execlp(launcher,launcher,".",conn_str,
504: control_port_str,server_port_str,server,"0",0);
505: }
506: } else if ( conn_to_serv == 2 ) {
507: /* special support for java */
508: if ( dname )
509: sprintf(cmd,
510: "%s -n %s \"(cd %s; xterm -name %s -display %s -geometry 60x10 -e java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
511: rsh,host,launcher,OX_XTERM,dname,server,localhost,control_port_str,server_port_str);
512: else
513: sprintf(cmd,
514: "%s -n %s \"(cd %s; java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
515: rsh,host,launcher,server,localhost,
516: control_port_str,server_port_str,server);
517: fprintf(stderr,"%s\n",cmd);
518: sleep(20);
519: /* system(cmd); */
1.4 noro 520: } else
521: #endif /* VISUAL */
522: {
1.1 noro 523: if ( dname )
524: sprintf(cmd,
525: "%s -n %s \"xterm -name %s -display %s -geometry 60x10 -e %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
526: rsh,host,OX_XTERM,dname,launcher,localhost,conn_str,
527: control_port_str,server_port_str,server,dname);
528: else
529: sprintf(cmd,
530: "%s -n %s \"%s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
531: rsh,host,launcher,localhost,conn_str,
532: control_port_str,server_port_str,server,"0");
533: system(cmd);
534: }
535: }
536:
537: void Pox_launch(arg,rp)
538: NODE arg;
539: Obj *rp;
540: {
541: Pox_launch_main(1,arg,rp);
542: }
543:
544: void Pox_launch_nox(arg,rp)
545: NODE arg;
546: Obj *rp;
547: {
548: Pox_launch_main(0,arg,rp);
549: }
550:
551: /*
552: ox_launch() : invoke local ox_asir
553: ox_launch(0,ox_xxx) : invoke local ox_xxx with asir_libdir/ox_launch
554: ox_launch(remote,lib,ox_xxx) : invoke remote ox_xxx with lib/ox_launch
555: */
556:
557: void Pox_launch_main(with_x,arg,rp)
558: int with_x;
559: NODE arg;
560: Obj *rp;
561: {
562: char *str;
563: char *hostname,*servername,*dname;
564: char *control;
565: int use_unix;
566: extern char *asir_libdir;
567:
568: if ( arg && ARG0(arg) && argc(arg) != 3 )
569: error("ox_launch : argument mismatch");
570: control = (char *)MALLOC(BUFSIZ);
571: if ( !arg || ( !ARG0(arg) && argc(arg) == 1 ) ) {
572: sprintf(control,"%s/ox_launch",asir_libdir);
573: use_unix = 1;
574: servername = (char *)MALLOC(BUFSIZ);
575: sprintf(servername,"%s/ox_asir",asir_libdir);
576: } else if ( !ARG0(arg) && argc(arg) == 2 ) {
577: sprintf(control,"%s/ox_launch",asir_libdir);
578: use_unix = 1;
579: str = BDY((STRING)ARG1(arg));
580: if ( str[0] == '/' )
581: servername = str;
582: else {
583: servername = (char *)MALLOC(BUFSIZ);
584: sprintf(servername,"%s/%s",asir_libdir,str);
585: }
586: } else {
587: sprintf(control,"%s/ox_launch",BDY((STRING)ARG1(arg)));
588: if ( !ARG0(arg) )
589: use_unix = 1;
590: else
591: use_unix = 0;
592: str = BDY((STRING)ARG2(arg));
593: if ( str[0] == '/' )
594: servername = str;
595: else {
596: servername = (char *)MALLOC(BUFSIZ);
597: sprintf(servername,"%s/%s",BDY((STRING)ARG1(arg)),str);
598: }
599: }
600: if ( arg && ARG0(arg) )
601: hostname = BDY((STRING)ARG0(arg));
602: else
603: hostname = 0;
604: ox_launch_generic(hostname,control,servername,use_unix,0,with_x,0,rp);
605: }
606:
607: int register_server(af_unix,m,c)
608: int af_unix,m,c;
609: {
610: int s,i,ci;
611: struct m_c *t;
1.9 noro 612: #define INIT_TAB_SIZ 64
1.1 noro 613:
614: if ( c < 0 )
615: return -1;
616: if ( !m_c_tab ) {
1.9 noro 617: s = INIT_TAB_SIZ*sizeof(struct m_c);
1.8 noro 618: m_c_tab = (struct m_c *)MALLOC_ATOMIC(s);
1.9 noro 619: for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
1.8 noro 620: m_c_tab[i].af_unix = 0;
621: m_c_tab[i].m = m_c_tab[i].c = -1;
622: }
1.9 noro 623: m_c_s = INIT_TAB_SIZ;
1.1 noro 624: }
625: #if !MPI
626: for ( i = 0; i < m_c_i; i++ )
1.8 noro 627: if ( (m_c_tab[i].m<0) && (m_c_tab[i].c<0) )
1.1 noro 628: break;
629: if ( i < m_c_i ) {
630: m_c_tab[i].m = m; m_c_tab[i].c = c;
631: m_c_tab[i].af_unix = af_unix;
632: return i;
633: }
634: #endif
635: if ( m_c_i == m_c_s ) {
1.9 noro 636: s = (m_c_s+INIT_TAB_SIZ)*sizeof(struct m_c);
1.1 noro 637: t = (struct m_c *)MALLOC_ATOMIC(s); bzero(m_c_tab,s);
638: bcopy(m_c_tab,t,m_c_s*sizeof(struct m_c));
1.9 noro 639: for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
1.8 noro 640: m_c_tab[m_c_s+i].af_unix = 0;
641: m_c_tab[m_c_s+i].m = m_c_tab[m_c_s+i].c = -1;
642: }
1.9 noro 643: m_c_s += INIT_TAB_SIZ; m_c_tab = t;
1.1 noro 644: }
645: m_c_tab[m_c_i].m = m; m_c_tab[m_c_i].c = c;
646: m_c_tab[m_c_i].af_unix = af_unix;
647: return m_c_i++;
648: }
649:
650: /* iofp index => m_c_tab index */
651:
652: int get_mcindex(i)
653: int i;
654: {
655: int j;
656:
657: for ( j = 0; j < m_c_i; j++ )
658: if ( m_c_tab[j].c == i )
659: return j;
660: return -1;
661: }
662:
663: /* arg = [ind0,ind1,...]; indk = index to m_c_tab */
664:
665: void Pox_select(arg,rp)
666: NODE arg;
667: LIST *rp;
668: {
669: int fd,n,i,index,mcind;
670: fd_set r,w,e;
671: NODE list,t,t1;
672: Q q;
673: double max;
674: struct timeval interval;
675: struct timeval *tvp;
676:
677: list = BDY((LIST)ARG0(arg)); arg = NEXT(arg);
678: if ( arg ) {
679: max = ToReal((Num)BDY(arg));
680: interval.tv_sec = (int)max;
681: interval.tv_usec = (int)((max-(int)max)*1000000);
682: tvp = &interval;
683: } else
684: tvp = 0;
685:
686: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
687: for ( t = list; t; t = NEXT(t) ) {
688: index = QTOS((Q)BDY(t));
689: valid_mctab_index(index);
690: fd = get_fd(m_c_tab[index].c); FD_SET(fd,&r);
691: }
692: n = select(FD_SETSIZE,&r,&w,&e,tvp);
693: for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
694: if ( FD_ISSET(i,&r) ) {
695: /* index : index to iofp array */
696: index = get_index(i);
697: /* mcind : index to m_c_tab array */
698: mcind = get_mcindex(index);
699: n--; STOQ(mcind,q); MKNODE(t1,q,t); t = t1;
700: }
701: MKLIST(*rp,t);
702: }
703:
704: void Pox_flush(arg,rp)
705: NODE arg;
706: Q *rp;
707: {
708: int index = QTOS((Q)ARG0(arg));
709:
710: valid_mctab_index(index);
711: ox_flush_stream_force(m_c_tab[index].c);
712: *rp = ONE;
713: }
714:
1.8 noro 715: void Pox_send_raw_cmo(arg,rp)
716: NODE arg;
717: Obj *rp;
718: {
719: int s;
720: int index = QTOS((Q)ARG0(arg));
721:
722: valid_mctab_index(index);
723: s = m_c_tab[index].c;
724: ox_write_cmo(s,(Obj)ARG1(arg));
725: /* flush always */
726: ox_flush_stream(s);
727: *rp = 0;
728: }
729:
730: void Pox_recv_raw_cmo(arg,rp)
731: NODE arg;
732: Obj *rp;
733: {
734: int s;
735: int index = QTOS((Q)ARG0(arg));
736:
737: valid_mctab_index(index);
738: s = m_c_tab[index].c;
739: ox_read_cmo(s,rp);
740: }
741:
1.1 noro 742: void Pox_push_local(arg,rp)
743: NODE arg;
744: Obj *rp;
745: {
746: int s;
747: struct oLIST dummy;
748: VL vl;
749: int index = QTOS((Q)ARG0(arg));
750:
751: valid_mctab_index(index);
752: s = m_c_tab[index].c; arg = NEXT(arg);
753:
754: dummy.id = O_LIST; dummy.body = arg;
755: get_vars_recursive((Obj)&dummy,&vl);
756:
757: ox_send_local_ring(s,vl);
758: for ( ; arg; arg = NEXT(arg) )
759: ox_send_local_data(s,BDY(arg));
760: *rp = 0;
761: }
762:
763: void Pox_push_cmo(arg,rp)
764: NODE arg;
765: Obj *rp;
766: {
767: int s;
768: int index = QTOS((Q)ARG0(arg));
769:
770: valid_mctab_index(index);
771: s = m_c_tab[index].c; arg = NEXT(arg);
772: for ( ; arg; arg = NEXT(arg) )
773: ox_send_data(s,BDY(arg));
774: *rp = 0;
775: }
776:
777: void Pox_push_vl(arg,rp)
778: NODE arg;
779: Obj *rp;
780: {
781: int s;
782: int index = QTOS((Q)ARG0(arg));
783:
784: valid_mctab_index(index);
785: ox_send_local_ring(m_c_tab[index].c,CO);
786: *rp = 0;
787: }
788:
789: void Pox_pop_local(arg,rp)
790: NODE arg;
791: Obj *rp;
792: {
793: int s;
794: int index = QTOS((Q)ARG0(arg));
795:
796: valid_mctab_index(index);
797: s = m_c_tab[index].c;
798: ox_send_cmd(s,SM_popSerializedLocalObject);
799: ox_flush_stream_force(s);
800: ox_get_result(s,rp);
801: }
802:
803: void Pox_pop_cmo(arg,rp)
804: NODE arg;
805: Obj *rp;
806: {
807: int s;
808: int index = QTOS((Q)ARG0(arg));
809:
810: valid_mctab_index(index);
811: s = m_c_tab[index].c;
812: ox_send_cmd(s,SM_popCMO);
813: ox_flush_stream_force(s);
814: ox_get_result(s,rp);
815: }
816:
817: void Pox_pop0_local(arg,rp)
818: NODE arg;
819: Obj *rp;
820: {
821: int index = QTOS((Q)ARG0(arg));
822:
823: valid_mctab_index(index);
824: ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
825: *rp = 0;
826: }
827:
828: void Pox_pop0_cmo(arg,rp)
829: NODE arg;
830: Obj *rp;
831: {
832: int index = QTOS((Q)ARG0(arg));
833:
834: valid_mctab_index(index);
835: ox_send_cmd(m_c_tab[index].c,SM_popCMO);
836: *rp = 0;
837: }
838:
839: void Pox_pop0_string(arg,rp)
840: NODE arg;
841: STRING *rp;
842: {
843: int index = QTOS((Q)ARG0(arg));
844:
845: valid_mctab_index(index);
846: ox_send_cmd(m_c_tab[index].c,SM_popString);
847: *rp = 0;
848: }
849:
850: void Pox_pop_string(arg,rp)
851: NODE arg;
852: STRING *rp;
853: {
854: int s,id;
855: int index = QTOS((Q)ARG0(arg));
856:
857: valid_mctab_index(index);
858: s = m_c_tab[index].c;
859: ox_send_cmd(s,SM_popString);
860: ox_flush_stream_force(s);
861: ox_get_result(s,rp);
862: }
863:
864: void Pox_get(arg,rp)
865: NODE arg;
866: Obj *rp;
867: {
1.3 noro 868: int index;
1.1 noro 869: int s;
870:
1.3 noro 871: if ( !arg ) {
872: /* client->server */
873: ox_get_result(0,rp);
874: } else {
875: /* server->client */
876: index = QTOS((Q)ARG0(arg));
877: valid_mctab_index(index);
878: s = m_c_tab[index].c;
879: ox_flush_stream_force(s);
880: ox_get_result(s,rp);
881: }
1.1 noro 882: }
883:
884: void Pox_pops(arg,rp)
885: NODE arg;
886: Obj *rp;
887: {
888: int s;
889: USINT n;
890: int index = QTOS((Q)ARG0(arg));
891:
892: valid_mctab_index(index);
893: s = m_c_tab[index].c;
894: if ( NEXT(arg) )
895: MKUSINT(n,QTOS((Q)ARG1(arg)));
896: else
897: MKUSINT(n,1);
898: ox_send_data(s,n);
899: ox_send_cmd(s,SM_pops);
900: *rp = 0;
901: }
902:
903: void Pox_execute_function(arg,rp)
904: NODE arg;
905: Obj *rp;
906: {
907: int s;
908: USINT ui;
909: int index = QTOS((Q)ARG0(arg));
910:
911: valid_mctab_index(index);
912: s = m_c_tab[index].c;
913: MKUSINT(ui,QTOS((Q)ARG2(arg)));
914: ox_send_data(s,ui);
915: ox_send_data(s,ARG1(arg));
916: ox_send_cmd(s,SM_executeFunction);
917: *rp = 0;
918: }
919:
920: void Pox_setname(arg,rp)
921: NODE arg;
922: Obj *rp;
923: {
924: int s;
925: int index = QTOS((Q)ARG0(arg));
926:
927: valid_mctab_index(index);
928: s = m_c_tab[index].c;
929: ox_send_data(s,ARG1(arg));
930: ox_send_cmd(s,SM_setName);
931: *rp = 0;
932: }
933:
934: void Pox_evalname(arg,rp)
935: NODE arg;
936: Obj *rp;
937: {
938: int s;
939: int index = QTOS((Q)ARG0(arg));
940:
941: valid_mctab_index(index);
942: s = m_c_tab[index].c;
943: ox_send_data(s,ARG1(arg));
944: ox_send_cmd(s,SM_evalName);
945: *rp = 0;
946: }
947:
948: void Pox_execute_string(arg,rp)
949: NODE arg;
950: Obj *rp;
951: {
952: int s;
953: int index = QTOS((Q)ARG0(arg));
954:
955: valid_mctab_index(index);
956: s = m_c_tab[index].c;
957: ox_send_data(s,ARG1(arg));
958: ox_send_cmd(s,SM_executeStringByLocalParser);
959: *rp = 0;
960: }
961:
962: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
963:
964: void Pox_rpc(arg,rp)
965: NODE arg;
966: Obj *rp;
967: {
968: int s,i,n;
969: STRING f;
970: USINT ui;
971: pointer *w;
972: NODE t;
973: int index = QTOS((Q)ARG0(arg));
974:
975: valid_mctab_index(index);
976: s = m_c_tab[index].c; arg = NEXT(arg);
977: f = (STRING)BDY(arg); arg = NEXT(arg);
978: ox_send_local_ring(s,CO);
979: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
980: w = (pointer *)ALLOCA(n*sizeof(pointer));
981: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
982: w[i] = BDY(t);
983: for ( i = n-1; i >= 0; i-- )
984: ox_send_local_data(s,w[i]);
985: MKUSINT(ui,n);
986: ox_send_data(s,ui);
987: ox_send_data(s,f);
988: ox_send_cmd(s,SM_executeFunction);
989: *rp = 0;
990: }
991:
992: void Pox_cmo_rpc(arg,rp)
993: NODE arg;
994: Obj *rp;
995: {
996: int s,i,n;
997: STRING f;
998: USINT ui;
999: NODE t;
1000: pointer *w;
1001: int index = QTOS((Q)ARG0(arg));
1002:
1003: valid_mctab_index(index);
1004: s = m_c_tab[index].c; arg = NEXT(arg);
1005: f = (STRING)BDY(arg); arg = NEXT(arg);
1006: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1007: w = (pointer *)ALLOCA(n*sizeof(pointer));
1008: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1009: w[i] = BDY(t);
1010: for ( i = n-1; i >= 0; i-- )
1011: ox_send_data(s,w[i]);
1012: MKUSINT(ui,n);
1013: ox_send_data(s,ui);
1014: ox_send_data(s,f);
1015: ox_send_cmd(s,SM_executeFunction);
1016: *rp = 0;
1017: }
1018:
1019: void Pox_reset(arg,rp)
1020: NODE arg;
1021: Q *rp;
1022: {
1023: USINT t;
1024: int s,id,c,m;
1025: Obj obj;
1026: int index = QTOS((Q)ARG0(arg));
1027:
1028: valid_mctab_index(index);
1029: m = m_c_tab[index].m;
1030: c = m_c_tab[index].c;
1031: if ( m >= 0 ) {
1032: if ( argc(arg) == 1 ) {
1033: ox_send_cmd(m,SM_control_reset_connection);
1034: ox_flush_stream_force(m);
1035: ox_recv(m,&id,&t);
1036: }
1037: *rp = ONE;
1038: #if defined(VISUAL)
1039: Sleep(100);
1040: ox_send_cmd(c,SM_nop);
1041: ox_flush_stream_force(c);
1042: #endif
1043: while ( 1 ) {
1044: ox_recv(c,&id,&obj);
1045: if ( id == OX_SYNC_BALL )
1046: break;
1047: }
1048: ox_send_sync(c);
1.5 noro 1049: } else
1050: *rp = 0;
1051: }
1052:
1053: void Pox_intr(arg,rp)
1054: NODE arg;
1055: Q *rp;
1056: {
1057: int m;
1058: Obj obj;
1059: int index = QTOS((Q)ARG0(arg));
1060:
1061: valid_mctab_index(index);
1062: m = m_c_tab[index].m;
1063: if ( m >= 0 ) {
1064: if ( argc(arg) == 1 ) {
1065: ox_send_cmd(m,SM_control_intr);
1066: ox_flush_stream_force(m);
1067: }
1068: *rp = ONE;
1.1 noro 1069: } else
1070: *rp = 0;
1071: }
1072:
1073: void Pox_sync(arg,rp)
1074: NODE arg;
1075: Q *rp;
1076: {
1077: int c;
1078: int index = QTOS((Q)ARG0(arg));
1079:
1080: valid_mctab_index(index);
1081: c = m_c_tab[index].c;
1082: ox_send_sync(c);
1083: *rp = 0;
1084: }
1085:
1086: void Pox_shutdown(arg,rp)
1087: NODE arg;
1088: Q *rp;
1089: {
1090: int s;
1091: int index = QTOS((Q)ARG0(arg));
1092: int status;
1093:
1094: valid_mctab_index(index);
1095: s = m_c_tab[index].m;
1096: ox_send_cmd(s,SM_shutdown);
1097: free_iofp(s);
1098: #if !MPI && !defined(VISUAL)
1099: if ( m_c_tab[index].af_unix )
1100: wait(&status);
1101: #endif
1.8 noro 1102: m_c_tab[index].m = -1; m_c_tab[index].c = -1;
1.1 noro 1103: m_c_tab[index].af_unix = 0;
1104: *rp = 0;
1105: }
1106:
1107: void Pox_push_cmd(arg,rp)
1108: NODE arg;
1109: Q *rp;
1110: {
1.7 noro 1111: int ui;
1.1 noro 1112: int index = QTOS((Q)ARG0(arg));
1113:
1114: valid_mctab_index(index);
1.7 noro 1115: ui = QTOS((Q)ARG1(arg));
1.1 noro 1116: ox_send_cmd(m_c_tab[index].c,ui);
1117: *rp = 0;
1118: }
1119: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>