Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.16.2.1
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.16.2.1! maekawa 47: * $OpenXM: OpenXM_contrib2/asir2000/io/tcpf.c,v 1.16 2000/11/07 06:35:39 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)
1.15 noro 61: #include <stdlib.h>
1.1 noro 62: #include <winsock.h>
1.15 noro 63: #include <process.h>
1.1 noro 64: #endif
65:
66: #define OX_XTERM "ox_xterm"
67:
68: #if !defined(_PA_RISC1_1)
69: #define RSH "rsh"
70: #else
71: #define RSH "remsh"
72: #endif
73:
74: static struct m_c {
75: int m,c,af_unix;
76: } *m_c_tab;
77:
78: static int m_c_i,m_c_s;
1.16 noro 79: int I_am_server;
1.1 noro 80:
81: #if MPI
82: extern int mpi_nprocs;
83: #define valid_mctab_index(ind)\
1.8 noro 84: if((ind)<0||(ind)>=mpi_nprocs){error("invalid server id");}
1.16 noro 85: #define check_valid_mctab_index(ind)\
86: if((ind)<0||(ind)>=mpi_nprocs){(ind)=-1;}
1.1 noro 87: #else
88: #define valid_mctab_index(ind)\
89: if((ind)<0||(ind)>=m_c_i||\
1.8 noro 90: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){error("invalid server id");}
1.16 noro 91: #define check_valid_mctab_index(ind)\
92: if((ind)<0||(ind)>=m_c_i||\
93: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){(ind)=-1;}
1.1 noro 94: #endif
95:
96: int register_server();
97: int get_mcindex(int);
98:
1.8 noro 99: void Pox_send_raw_cmo(), Pox_recv_raw_cmo();
100:
1.1 noro 101: void Pox_launch(),Pox_launch_nox(),Pox_launch_main();
102: void Pox_launch_generic();
103: void Pox_shutdown();
104:
1.5 noro 105: void Pox_rpc(),Pox_cmo_rpc(),Pox_reset(),Pox_intr(),Pox_sync(),Pox_select();
1.1 noro 106:
107: void Pox_push_local(),Pox_push_cmo(),Pox_push_vl(),Pox_push_cmd();
108:
109: void Pox_pop_local(),Pox_pop_cmo();
110: void Pox_pop0_local(),Pox_pop0_cmo();
111: void Pox_pop_string(),Pox_pop0_string();
112: void Pox_get(),Pox_pops();
113:
114: void Pox_execute_function(),Pox_execute_string();
115: void Pox_setname(), Pox_evalname();
116: void Pox_flush();
117: void Pgenerate_port(),Ptry_bind_listen(),Ptry_connect(),Ptry_accept();
118: void Pregister_server();
119: void Pox_get_serverinfo();
1.8 noro 120: void Pox_mpi_myid(), Pox_mpi_nprocs();
1.1 noro 121:
122: void ox_launch_generic();
123:
124: pointer bevalf();
125:
126: struct ftab tcp_tab[] = {
1.8 noro 127: {"ox_send_raw_cmo",Pox_send_raw_cmo,2},
128: {"ox_recv_raw_cmo",Pox_recv_raw_cmo,1},
1.1 noro 129: {"ox_get_serverinfo",Pox_get_serverinfo,-1},
130: {"generate_port",Pgenerate_port,-1},
131: {"try_bind_listen",Ptry_bind_listen,1},
132: {"try_connect",Ptry_connect,2},
133: {"try_accept",Ptry_accept,2},
134: {"register_server",Pregister_server,4},
135: {"ox_flush",Pox_flush,1},
136: {"ox_launch",Pox_launch,-3},
137: {"ox_launch_nox",Pox_launch_nox,-3},
138: {"ox_launch_generic",Pox_launch_generic,7},
139: {"ox_shutdown",Pox_shutdown,1},
140:
141: {"ox_rpc",Pox_rpc,-99999999},
142: {"ox_cmo_rpc",Pox_cmo_rpc,-99999999},
143:
144: {"ox_sync",Pox_sync,1},
1.8 noro 145: #if MPI
146: {"ox_mpi_myid",Pox_mpi_myid,0},
147: {"ox_mpi_nprocs",Pox_mpi_nprocs,0},
148: #endif
1.1 noro 149: #if !MPI
150: {"ox_reset",Pox_reset,-2},
1.5 noro 151: {"ox_intr",Pox_intr,1},
1.1 noro 152: {"ox_select",Pox_select,-2},
153: #endif
154:
155: {"ox_pop_string",Pox_pop_string,1},
156: {"ox_pop0_string",Pox_pop0_string,1},
157: {"ox_pop_local",Pox_pop_local,1},
158: {"ox_pop0_local",Pox_pop0_local,1},
159: {"ox_pop_cmo",Pox_pop_cmo,1},
160: {"ox_pop0_cmo",Pox_pop0_cmo,1},
1.3 noro 161: {"ox_get",Pox_get,-1},
1.1 noro 162: {"ox_pops",Pox_pops,-2},
163:
164: {"ox_push_vl",Pox_push_vl,1},
165: {"ox_push_local",Pox_push_local,-99999999},
166: {"ox_push_cmo",Pox_push_cmo,-99999999},
167: {"ox_push_cmd",Pox_push_cmd,2},
168:
169: {"ox_setname",Pox_setname,2},
170: {"ox_evalname",Pox_evalname,2},
171:
172: {"ox_execute_string",Pox_execute_string,2},
173: {"ox_execute_function",Pox_execute_function,3},
174:
175:
176: {0,0,0},
177: };
178:
179: extern struct IOFP iofp[];
180: extern MATHCAP my_mathcap;
1.6 noro 181: extern int ox_exchange_mathcap;
1.1 noro 182:
183: char *getenv();
184:
1.8 noro 185: #if MPI
186: extern int mpi_myid, mpi_nprocs;
187:
188: void Pox_mpi_myid(rp)
189: Q *rp;
190: {
191: STOQ(mpi_myid,*rp);
192: }
193:
194: void Pox_mpi_nprocs(rp)
195: Q *rp;
196: {
197: STOQ(mpi_nprocs,*rp);
198: }
199: #endif
200:
1.1 noro 201: void Pox_get_serverinfo(arg,rp)
202: NODE arg;
203: LIST *rp;
204: {
205: int i,c;
206: Q sid;
207: NODE t,n0,n;
208: LIST list,l;
209:
210: if ( !arg ) {
211: for ( i = 0, n0 = 0; i < m_c_i; i++ )
1.8 noro 212: if ( (m_c_tab[i].m>=0) || (m_c_tab[i].c>=0) ) {
1.1 noro 213: c = m_c_tab[i].c;
214: ox_get_serverinfo(c,&list);
215: STOQ(i,sid);
216: t = mknode(2,sid,list);
217: MKLIST(l,t);
218: NEXTNODE(n0,n);
219: BDY(n) = (pointer)l;
220: }
221: if ( n0 )
222: NEXT(n) = 0;
223: MKLIST(*rp,n0);
224: } else {
225: i = QTOS((Q)ARG0(arg));
1.8 noro 226: if ( i >= 0 && i < m_c_i && ((m_c_tab[i].m>=0) || (m_c_tab[i].c>=0)) )
1.1 noro 227: ox_get_serverinfo(m_c_tab[i].c,rp);
228: else {
229: MKLIST(*rp,0);
230: }
231: }
232: }
233:
234: /*
235: if noarg or arg[0]==0 then use IP socket and return a port number
236: else use UNIX socket and return a string which represents a path name
237: */
238:
239: void Pgenerate_port(arg,rp)
240: NODE arg;
241: Obj *rp;
242: {
243: char port_str[BUFSIZ];
244: int port;
245: char *s;
246: STRING str;
247: Q q;
248:
249: if ( !arg || !ARG0(arg) ) {
250: generate_port(0,port_str);
251: port = atoi(port_str);
252: STOQ(port,q);
253: *rp = (Obj)q;
254: } else {
255: generate_port(1,port_str);
256: s = (char *)MALLOC(strlen(port_str)+1);
257: strcpy(s,port_str);
258: MKSTR(str,s);
259: *rp = (Obj)str;
260: }
261: }
262:
263: /*
264: try_bind_listen(port)
265: */
266:
267: void Ptry_bind_listen(arg,rp)
268: NODE arg;
269: Q *rp;
270: {
271: char port_str[BUFSIZ];
272: int port,s,use_unix;
273:
274: if ( !ARG0(arg) || NUM(ARG0(arg)) ) {
275: port = QTOS((Q)ARG0(arg));
276: sprintf(port_str,"%d",port);
277: use_unix = 0;
278: } else {
279: strcpy(port_str,BDY((STRING)ARG0(arg)));
280: use_unix = 1;
281: }
282: s = try_bind_listen(use_unix,port_str);
283: STOQ(s,*rp);
284: }
285:
286: /*
287: try_connect(host,port)
288: */
289:
290: void Ptry_connect(arg,rp)
291: NODE arg;
292: Q *rp;
293: {
294: char port_str[BUFSIZ];
295: char *host;
296: int port,s,use_unix;
297:
298: if ( !ARG1(arg) || NUM(ARG1(arg)) ) {
299: port = QTOS((Q)ARG1(arg));
300: sprintf(port_str,"%d",port);
301: use_unix = 0;
302: } else {
303: strcpy(port_str,BDY((STRING)ARG1(arg)));
304: use_unix = 1;
305: }
306: host = BDY((STRING)ARG0(arg));
307: s = try_connect(use_unix,host,port_str);
308: STOQ(s,*rp);
309: }
310:
311: /*
1.16.2.1! maekawa 312: try_accept(sock)
1.1 noro 313: */
314:
315: void Ptry_accept(arg,rp)
316: NODE arg;
317: Q *rp;
318: {
319: int use_unix,s;
320:
321: if ( !ARG1(arg) || NUM(ARG1(arg)) )
322: use_unix = 0;
323: else
324: use_unix = 1;
1.16.2.1! maekawa 325: s = try_accept(QTOS((Q)ARG0(arg)));
1.1 noro 326: STOQ(s,*rp);
327: }
328:
329: /*
330: register_server(cs,cport,ss,sport)
331: */
332:
333: void Pregister_server(arg,rp)
334: NODE arg;
335: Q *rp;
336: {
337: int cs,ss,cn,sn,ind,use_unix,id;
338: char cport_str[BUFSIZ],sport_str[BUFSIZ];
339: MATHCAP server_mathcap;
340:
341: cs = QTOS((Q)ARG0(arg));
342: ss = QTOS((Q)ARG2(arg));
343: if ( !ARG1(arg) || NUM(ARG1(arg)) ) {
344: sprintf(cport_str,"%d",QTOS((Q)ARG1(arg)));
345: use_unix = 0;
346: } else {
347: strcpy(cport_str,BDY((STRING)ARG1(arg)));
348: use_unix = 1;
349: }
350: if ( !ARG3(arg) || NUM(ARG3(arg)) ) {
351: if ( use_unix )
352: error("register_server : the protocol should conincide for two sockets");
353: sprintf(sport_str,"%d",QTOS((Q)ARG3(arg)));
354: } else {
355: if ( !use_unix )
356: error("register_server : the protocol should conincide for two sockets");
357: strcpy(sport_str,BDY((STRING)ARG3(arg)));
358: }
359:
360: /* client mode */
361: cn = get_iofp(cs,cport_str,0);
362: sn = get_iofp(ss,sport_str,0);
1.12 noro 363: /* get_iofp returns -1 if the laucher could not spawn the server */
364: if ( sn < 0 ) {
365: /* we should terminate the launcher */
366: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
367: STOQ(-1,*rp);
368: return;
369: }
1.1 noro 370:
371: /* register server to the server list */
372: ind = register_server(use_unix,cn,sn);
373:
1.6 noro 374: if ( ox_exchange_mathcap ) {
375: /* request remote mathcap */
376: ox_send_cmd(sn,SM_mathcap);
377: ox_send_cmd(sn,SM_popCMO);
378: ox_flush_stream_force(sn);
379: ox_recv(sn,&id,&server_mathcap);
380: store_remote_mathcap(sn,server_mathcap);
381:
382: /* send my mathcap */
383: create_my_mathcap("asir");
384: ox_send_data(sn,my_mathcap);
385: ox_send_cmd(sn,SM_setMathcap);
386: }
1.1 noro 387: /* return the server id */
388: STOQ(ind,*rp);
389: }
390:
391: /*
392: ox_launch_generic(host,launcher,server,use_unix,use_ssh,use_x,conn_to_serv)
393:
394: Input
395: host: hostname on which servers run
396: launcher: path name of the launcher
397: server: path name of the server
398: use_unix: use UNIX domain socket if 1
399: use_ssh: use ssh if 1
400: use_x: use X11 facilities if 1
401: conn_to_serv: connect to server if 1
402: */
403:
404: void Pox_launch_generic(arg,rp)
405: NODE arg;
406: Q *rp;
407: {
408: int use_unix,use_ssh,use_x,conn_to_serv;
409: char *host,*launcher,*server;
410:
411: host = (arg&&ARG0(arg))?BDY((STRING)ARG0(arg)):0;
412: launcher = BDY((STRING)ARG1(arg));
413: server = BDY((STRING)ARG2(arg));
414: use_unix = ARG3(arg) ? 1 : 0;
415: use_ssh = ARG4(arg) ? 1 : 0;
416: use_x = ARG5(arg) ? 1 : 0;
417: conn_to_serv = QTOS((Q)ARG6(arg));
418: if ( !host )
419: use_unix = 1;
420: ox_launch_generic(host,launcher,server,
421: use_unix,use_ssh,use_x,conn_to_serv,rp);
422: }
423:
424: void ox_launch_generic(host,launcher,server,
425: use_unix,use_ssh,use_x,conn_to_serv,rp)
426: char *host,*launcher,*server;
427: int use_unix,use_ssh,use_x,conn_to_serv;
428: Q *rp;
429: {
430: int cs,ss,cn,sn,ind,id;
431: char control_port_str[BUFSIZ];
432: char server_port_str[BUFSIZ];
433: MATHCAP server_mathcap;
434:
435: control_port_str[0] = 0;
436: server_port_str[0] = 0;
437: do {
438: generate_port(use_unix,control_port_str);
439: generate_port(use_unix,server_port_str);
440: if ( !conn_to_serv ) {
441: cs = try_bind_listen(use_unix,control_port_str);
442: ss = try_bind_listen(use_unix,server_port_str);
443: }
444: spawn_server(host,launcher,server,
445: use_unix,use_ssh,use_x,conn_to_serv,
446: control_port_str,server_port_str);
447: if ( conn_to_serv ) {
448: cs = try_connect(use_unix,host,control_port_str);
449: ss = try_connect(use_unix,host,server_port_str);
450: } else {
1.16.2.1! maekawa 451: cs = try_accept(cs);
! 452: ss = try_accept(ss);
1.1 noro 453: }
454: } while ( cs < 0 || ss < 0 );
455:
456: /* client mode */
457: cn = get_iofp(cs,control_port_str,0);
458: sn = get_iofp(ss,server_port_str,0);
1.12 noro 459: /* get_iofp returns -1 if the laucher could not spawn the server */
460: if ( sn < 0 ) {
461: /* we should terminate the launcher */
462: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
463: STOQ(-1,*rp);
464: return;
465: }
1.1 noro 466:
467: /* register server to the server list */
468: ind = register_server(use_unix,cn,sn);
469:
1.6 noro 470: if ( ox_exchange_mathcap ) {
471: /* request remote mathcap */
472: ox_send_cmd(sn,SM_mathcap);
473: ox_send_cmd(sn,SM_popCMO);
474: ox_flush_stream_force(sn);
475: ox_recv(sn,&id,&server_mathcap);
476: store_remote_mathcap(sn,server_mathcap);
477:
478: /* send my mathcap */
479: create_my_mathcap("asir");
480: ox_send_data(sn,my_mathcap);
481: ox_send_cmd(sn,SM_setMathcap);
482: }
1.1 noro 483: /* return the server id */
484: STOQ(ind,*rp);
485: }
486:
487: int spawn_server(host,launcher,server,
488: use_unix,use_ssh,use_x,conn_to_serv,
489: control_port_str,server_port_str)
490: char *host,*launcher,*server;
491: int use_unix,use_ssh,use_x,conn_to_serv;
492: char *control_port_str,*server_port_str;
493: {
494: char cmd[BUFSIZ];
495: char localhost[BUFSIZ];
496: char *dname,*conn_str,*rsh;
1.13 noro 497: char dname_str[BUFSIZ];
1.15 noro 498: char AsirExe[BUFSIZ];
499: STRING rootdir;
500: char prog[BUFSIZ];
501: char *av[BUFSIZ];
1.1 noro 502:
503: dname = use_x ? (char *)getenv("DISPLAY") : 0;
504: conn_str = conn_to_serv ? "1" : "0";
1.13 noro 505: rsh = getenv("ASIR_RSH");
506: if ( !rsh )
507: rsh = use_ssh ? "ssh" : RSH;
508: if ( !use_unix && dname && strstr(rsh,"ssh") ) {
509: /*
510: * if "ssh" is used to invoke a remote server,
511: * we should not specify "-display".
512: */
513: use_ssh = 1;
514: }
1.1 noro 515: gethostname(localhost,BUFSIZ);
1.15 noro 516: #if defined(VISUAL)
517: if ( !use_unix )
518: error("spawn_server : not implemented on Windows");
519: Pget_rootdir(&rootdir);
520: sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
521: strcpy(prog,server);
522: server = strrchr(prog,'/')+1;
523: av[0] = "ox_launch";
524: av[1] = "127.0.0.1";
525: av[2] = conn_str;
526: av[3] = control_port_str;
527: av[4] = server_port_str;
528: av[5] = server;
529: av[6] = use_x ? "1" : "0";
530: av[7] = 0;
531:
532: _spawnv(_P_NOWAIT,AsirExe,av);
533: // _spawnv(_P_NOWAIT,"d:\\home\\noro\\engine2000\\debug\\engine.exe",av);
534: // printf("ox_launch 127.0.0.1 %s %s %s %s 0\n",conn_str,control_port_str,server_port_str,server);
535: #else
1.1 noro 536: if ( use_unix ) {
537: if ( !fork() ) {
1.2 noro 538: setpgid(0,getpid());
1.1 noro 539: if ( dname )
1.13 noro 540: execlp("xterm","xterm","-name",OX_XTERM,"-T","ox_launch:local","-display",dname,
1.1 noro 541: "-geometry","60x10","-e",launcher,".",conn_str,
542: control_port_str,server_port_str,server,dname,0);
543: else
544: execlp(launcher,launcher,".",conn_str,
545: control_port_str,server_port_str,server,"0",0);
546: }
547: } else if ( conn_to_serv == 2 ) {
548: /* special support for java */
549: if ( dname )
550: sprintf(cmd,
1.13 noro 551: "%s -n %s \"(cd %s; xterm -name %s %s -geometry 60x10 -e java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
1.1 noro 552: rsh,host,launcher,OX_XTERM,dname,server,localhost,control_port_str,server_port_str);
553: else
554: sprintf(cmd,
555: "%s -n %s \"(cd %s; java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
556: rsh,host,launcher,server,localhost,
557: control_port_str,server_port_str,server);
558: fprintf(stderr,"%s\n",cmd);
559: sleep(20);
560: /* system(cmd); */
1.15 noro 561: } else {
1.1 noro 562: if ( dname )
1.13 noro 563: if ( use_ssh )
564: sprintf(cmd,
1.14 noro 565: "%s -f -n %s \"xterm -name %s -title ox_launch:%s -geometry 60x10 -e %s %s %s %s %s %s %s >&/dev/null\">/dev/null",
1.13 noro 566: rsh,host,OX_XTERM,host,launcher,localhost,conn_str,
567: control_port_str,server_port_str,server,"1");
568: else
1.1 noro 569: sprintf(cmd,
1.14 noro 570: "%s -n %s \"xterm -name %s -title ox_launch:%s -display %s -geometry 60x10 -e %s %s %s %s %s %s %s >&/dev/null&\">/dev/null",
1.13 noro 571: rsh,host,OX_XTERM,host,dname,launcher,localhost,conn_str,
1.1 noro 572: control_port_str,server_port_str,server,dname);
573: else
574: sprintf(cmd,
1.14 noro 575: "%s -n %s \"%s %s %s %s %s %s %s >&/dev/null&\">/dev/null",
1.1 noro 576: rsh,host,launcher,localhost,conn_str,
577: control_port_str,server_port_str,server,"0");
578: system(cmd);
579: }
1.15 noro 580: #endif /* VISUAL */
1.1 noro 581: }
582:
583: void Pox_launch(arg,rp)
584: NODE arg;
585: Obj *rp;
586: {
587: Pox_launch_main(1,arg,rp);
588: }
589:
590: void Pox_launch_nox(arg,rp)
591: NODE arg;
592: Obj *rp;
593: {
594: Pox_launch_main(0,arg,rp);
595: }
596:
597: /*
598: ox_launch() : invoke local ox_asir
599: ox_launch(0,ox_xxx) : invoke local ox_xxx with asir_libdir/ox_launch
600: ox_launch(remote,lib,ox_xxx) : invoke remote ox_xxx with lib/ox_launch
601: */
602:
603: void Pox_launch_main(with_x,arg,rp)
604: int with_x;
605: NODE arg;
606: Obj *rp;
607: {
608: char *str;
609: char *hostname,*servername,*dname;
610: char *control;
611: int use_unix;
612: extern char *asir_libdir;
613:
614: if ( arg && ARG0(arg) && argc(arg) != 3 )
615: error("ox_launch : argument mismatch");
616: control = (char *)MALLOC(BUFSIZ);
617: if ( !arg || ( !ARG0(arg) && argc(arg) == 1 ) ) {
618: sprintf(control,"%s/ox_launch",asir_libdir);
619: use_unix = 1;
620: servername = (char *)MALLOC(BUFSIZ);
621: sprintf(servername,"%s/ox_asir",asir_libdir);
622: } else if ( !ARG0(arg) && argc(arg) == 2 ) {
623: sprintf(control,"%s/ox_launch",asir_libdir);
624: use_unix = 1;
625: str = BDY((STRING)ARG1(arg));
626: if ( str[0] == '/' )
627: servername = str;
628: else {
629: servername = (char *)MALLOC(BUFSIZ);
630: sprintf(servername,"%s/%s",asir_libdir,str);
631: }
632: } else {
633: sprintf(control,"%s/ox_launch",BDY((STRING)ARG1(arg)));
634: if ( !ARG0(arg) )
635: use_unix = 1;
636: else
637: use_unix = 0;
638: str = BDY((STRING)ARG2(arg));
639: if ( str[0] == '/' )
640: servername = str;
641: else {
642: servername = (char *)MALLOC(BUFSIZ);
643: sprintf(servername,"%s/%s",BDY((STRING)ARG1(arg)),str);
644: }
645: }
646: if ( arg && ARG0(arg) )
647: hostname = BDY((STRING)ARG0(arg));
648: else
649: hostname = 0;
650: ox_launch_generic(hostname,control,servername,use_unix,0,with_x,0,rp);
651: }
652:
653: int register_server(af_unix,m,c)
654: int af_unix,m,c;
655: {
656: int s,i,ci;
657: struct m_c *t;
1.9 noro 658: #define INIT_TAB_SIZ 64
1.1 noro 659:
660: if ( c < 0 )
661: return -1;
662: if ( !m_c_tab ) {
1.9 noro 663: s = INIT_TAB_SIZ*sizeof(struct m_c);
1.8 noro 664: m_c_tab = (struct m_c *)MALLOC_ATOMIC(s);
1.9 noro 665: for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
1.8 noro 666: m_c_tab[i].af_unix = 0;
667: m_c_tab[i].m = m_c_tab[i].c = -1;
668: }
1.9 noro 669: m_c_s = INIT_TAB_SIZ;
1.1 noro 670: }
671: #if !MPI
672: for ( i = 0; i < m_c_i; i++ )
1.8 noro 673: if ( (m_c_tab[i].m<0) && (m_c_tab[i].c<0) )
1.1 noro 674: break;
675: if ( i < m_c_i ) {
676: m_c_tab[i].m = m; m_c_tab[i].c = c;
677: m_c_tab[i].af_unix = af_unix;
678: return i;
679: }
680: #endif
681: if ( m_c_i == m_c_s ) {
1.9 noro 682: s = (m_c_s+INIT_TAB_SIZ)*sizeof(struct m_c);
1.1 noro 683: t = (struct m_c *)MALLOC_ATOMIC(s); bzero(m_c_tab,s);
684: bcopy(m_c_tab,t,m_c_s*sizeof(struct m_c));
1.9 noro 685: for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
1.8 noro 686: m_c_tab[m_c_s+i].af_unix = 0;
687: m_c_tab[m_c_s+i].m = m_c_tab[m_c_s+i].c = -1;
688: }
1.9 noro 689: m_c_s += INIT_TAB_SIZ; m_c_tab = t;
1.1 noro 690: }
691: m_c_tab[m_c_i].m = m; m_c_tab[m_c_i].c = c;
692: m_c_tab[m_c_i].af_unix = af_unix;
693: return m_c_i++;
694: }
695:
696: /* iofp index => m_c_tab index */
697:
698: int get_mcindex(i)
699: int i;
700: {
701: int j;
702:
703: for ( j = 0; j < m_c_i; j++ )
704: if ( m_c_tab[j].c == i )
705: return j;
706: return -1;
707: }
708:
709: /* arg = [ind0,ind1,...]; indk = index to m_c_tab */
710:
711: void Pox_select(arg,rp)
712: NODE arg;
713: LIST *rp;
714: {
715: int fd,n,i,index,mcind;
716: fd_set r,w,e;
717: NODE list,t,t1;
718: Q q;
719: double max;
720: struct timeval interval;
721: struct timeval *tvp;
722:
723: list = BDY((LIST)ARG0(arg)); arg = NEXT(arg);
724: if ( arg ) {
725: max = ToReal((Num)BDY(arg));
726: interval.tv_sec = (int)max;
727: interval.tv_usec = (int)((max-(int)max)*1000000);
728: tvp = &interval;
729: } else
730: tvp = 0;
731:
732: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
733: for ( t = list; t; t = NEXT(t) ) {
734: index = QTOS((Q)BDY(t));
735: valid_mctab_index(index);
736: fd = get_fd(m_c_tab[index].c); FD_SET(fd,&r);
737: }
738: n = select(FD_SETSIZE,&r,&w,&e,tvp);
739: for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
740: if ( FD_ISSET(i,&r) ) {
741: /* index : index to iofp array */
742: index = get_index(i);
743: /* mcind : index to m_c_tab array */
744: mcind = get_mcindex(index);
745: n--; STOQ(mcind,q); MKNODE(t1,q,t); t = t1;
746: }
747: MKLIST(*rp,t);
748: }
749:
750: void Pox_flush(arg,rp)
751: NODE arg;
752: Q *rp;
753: {
754: int index = QTOS((Q)ARG0(arg));
755:
756: valid_mctab_index(index);
757: ox_flush_stream_force(m_c_tab[index].c);
758: *rp = ONE;
759: }
760:
1.8 noro 761: void Pox_send_raw_cmo(arg,rp)
762: NODE arg;
763: Obj *rp;
764: {
765: int s;
766: int index = QTOS((Q)ARG0(arg));
767:
768: valid_mctab_index(index);
769: s = m_c_tab[index].c;
770: ox_write_cmo(s,(Obj)ARG1(arg));
771: /* flush always */
772: ox_flush_stream(s);
773: *rp = 0;
774: }
775:
776: void Pox_recv_raw_cmo(arg,rp)
777: NODE arg;
778: Obj *rp;
779: {
780: int s;
781: int index = QTOS((Q)ARG0(arg));
782:
783: valid_mctab_index(index);
784: s = m_c_tab[index].c;
785: ox_read_cmo(s,rp);
786: }
787:
1.1 noro 788: void Pox_push_local(arg,rp)
789: NODE arg;
790: Obj *rp;
791: {
792: int s;
793: struct oLIST dummy;
794: VL vl;
795: int index = QTOS((Q)ARG0(arg));
796:
797: valid_mctab_index(index);
798: s = m_c_tab[index].c; arg = NEXT(arg);
799:
800: dummy.id = O_LIST; dummy.body = arg;
801: get_vars_recursive((Obj)&dummy,&vl);
802:
803: ox_send_local_ring(s,vl);
804: for ( ; arg; arg = NEXT(arg) )
805: ox_send_local_data(s,BDY(arg));
806: *rp = 0;
807: }
808:
809: void Pox_push_cmo(arg,rp)
810: NODE arg;
811: Obj *rp;
812: {
813: int s;
814: int index = QTOS((Q)ARG0(arg));
815:
816: valid_mctab_index(index);
817: s = m_c_tab[index].c; arg = NEXT(arg);
818: for ( ; arg; arg = NEXT(arg) )
819: ox_send_data(s,BDY(arg));
820: *rp = 0;
821: }
822:
823: void Pox_push_vl(arg,rp)
824: NODE arg;
825: Obj *rp;
826: {
827: int s;
828: int index = QTOS((Q)ARG0(arg));
829:
830: valid_mctab_index(index);
831: ox_send_local_ring(m_c_tab[index].c,CO);
832: *rp = 0;
833: }
834:
835: void Pox_pop_local(arg,rp)
836: NODE arg;
837: Obj *rp;
838: {
839: int s;
840: int index = QTOS((Q)ARG0(arg));
841:
842: valid_mctab_index(index);
843: s = m_c_tab[index].c;
844: ox_send_cmd(s,SM_popSerializedLocalObject);
845: ox_flush_stream_force(s);
846: ox_get_result(s,rp);
847: }
848:
849: void Pox_pop_cmo(arg,rp)
850: NODE arg;
851: Obj *rp;
852: {
853: int s;
854: int index = QTOS((Q)ARG0(arg));
855:
856: valid_mctab_index(index);
857: s = m_c_tab[index].c;
858: ox_send_cmd(s,SM_popCMO);
859: ox_flush_stream_force(s);
860: ox_get_result(s,rp);
861: }
862:
863: void Pox_pop0_local(arg,rp)
864: NODE arg;
865: Obj *rp;
866: {
867: int index = QTOS((Q)ARG0(arg));
868:
869: valid_mctab_index(index);
870: ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
871: *rp = 0;
872: }
873:
874: void Pox_pop0_cmo(arg,rp)
875: NODE arg;
876: Obj *rp;
877: {
878: int index = QTOS((Q)ARG0(arg));
879:
880: valid_mctab_index(index);
881: ox_send_cmd(m_c_tab[index].c,SM_popCMO);
882: *rp = 0;
883: }
884:
885: void Pox_pop0_string(arg,rp)
886: NODE arg;
887: STRING *rp;
888: {
889: int index = QTOS((Q)ARG0(arg));
890:
891: valid_mctab_index(index);
892: ox_send_cmd(m_c_tab[index].c,SM_popString);
893: *rp = 0;
894: }
895:
896: void Pox_pop_string(arg,rp)
897: NODE arg;
898: STRING *rp;
899: {
900: int s,id;
901: int index = QTOS((Q)ARG0(arg));
902:
903: valid_mctab_index(index);
904: s = m_c_tab[index].c;
905: ox_send_cmd(s,SM_popString);
906: ox_flush_stream_force(s);
907: ox_get_result(s,rp);
908: }
909:
910: void Pox_get(arg,rp)
911: NODE arg;
912: Obj *rp;
913: {
1.3 noro 914: int index;
1.1 noro 915: int s;
916:
1.3 noro 917: if ( !arg ) {
918: /* client->server */
919: ox_get_result(0,rp);
920: } else {
921: /* server->client */
922: index = QTOS((Q)ARG0(arg));
923: valid_mctab_index(index);
924: s = m_c_tab[index].c;
925: ox_flush_stream_force(s);
926: ox_get_result(s,rp);
927: }
1.1 noro 928: }
929:
930: void Pox_pops(arg,rp)
931: NODE arg;
932: Obj *rp;
933: {
934: int s;
935: USINT n;
936: int index = QTOS((Q)ARG0(arg));
937:
938: valid_mctab_index(index);
939: s = m_c_tab[index].c;
940: if ( NEXT(arg) )
941: MKUSINT(n,QTOS((Q)ARG1(arg)));
942: else
943: MKUSINT(n,1);
944: ox_send_data(s,n);
945: ox_send_cmd(s,SM_pops);
946: *rp = 0;
947: }
948:
949: void Pox_execute_function(arg,rp)
950: NODE arg;
951: Obj *rp;
952: {
953: int s;
954: USINT ui;
955: int index = QTOS((Q)ARG0(arg));
956:
957: valid_mctab_index(index);
958: s = m_c_tab[index].c;
959: MKUSINT(ui,QTOS((Q)ARG2(arg)));
960: ox_send_data(s,ui);
961: ox_send_data(s,ARG1(arg));
962: ox_send_cmd(s,SM_executeFunction);
963: *rp = 0;
964: }
965:
966: void Pox_setname(arg,rp)
967: NODE arg;
968: Obj *rp;
969: {
970: int s;
971: int index = QTOS((Q)ARG0(arg));
972:
973: valid_mctab_index(index);
974: s = m_c_tab[index].c;
975: ox_send_data(s,ARG1(arg));
976: ox_send_cmd(s,SM_setName);
977: *rp = 0;
978: }
979:
980: void Pox_evalname(arg,rp)
981: NODE arg;
982: Obj *rp;
983: {
984: int s;
985: int index = QTOS((Q)ARG0(arg));
986:
987: valid_mctab_index(index);
988: s = m_c_tab[index].c;
989: ox_send_data(s,ARG1(arg));
990: ox_send_cmd(s,SM_evalName);
991: *rp = 0;
992: }
993:
994: void Pox_execute_string(arg,rp)
995: NODE arg;
996: Obj *rp;
997: {
998: int s;
999: int index = QTOS((Q)ARG0(arg));
1000:
1001: valid_mctab_index(index);
1002: s = m_c_tab[index].c;
1003: ox_send_data(s,ARG1(arg));
1004: ox_send_cmd(s,SM_executeStringByLocalParser);
1005: *rp = 0;
1006: }
1007:
1008: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
1009:
1010: void Pox_rpc(arg,rp)
1011: NODE arg;
1012: Obj *rp;
1013: {
1014: int s,i,n;
1015: STRING f;
1016: USINT ui;
1017: pointer *w;
1018: NODE t;
1019: int index = QTOS((Q)ARG0(arg));
1020:
1021: valid_mctab_index(index);
1022: s = m_c_tab[index].c; arg = NEXT(arg);
1023: f = (STRING)BDY(arg); arg = NEXT(arg);
1024: ox_send_local_ring(s,CO);
1025: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1026: w = (pointer *)ALLOCA(n*sizeof(pointer));
1027: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1028: w[i] = BDY(t);
1029: for ( i = n-1; i >= 0; i-- )
1030: ox_send_local_data(s,w[i]);
1031: MKUSINT(ui,n);
1032: ox_send_data(s,ui);
1033: ox_send_data(s,f);
1034: ox_send_cmd(s,SM_executeFunction);
1035: *rp = 0;
1036: }
1037:
1038: void Pox_cmo_rpc(arg,rp)
1039: NODE arg;
1040: Obj *rp;
1041: {
1042: int s,i,n;
1043: STRING f;
1044: USINT ui;
1045: NODE t;
1046: pointer *w;
1047: int index = QTOS((Q)ARG0(arg));
1048:
1049: valid_mctab_index(index);
1050: s = m_c_tab[index].c; arg = NEXT(arg);
1051: f = (STRING)BDY(arg); arg = NEXT(arg);
1052: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1053: w = (pointer *)ALLOCA(n*sizeof(pointer));
1054: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1055: w[i] = BDY(t);
1056: for ( i = n-1; i >= 0; i-- )
1057: ox_send_data(s,w[i]);
1058: MKUSINT(ui,n);
1059: ox_send_data(s,ui);
1060: ox_send_data(s,f);
1061: ox_send_cmd(s,SM_executeFunction);
1062: *rp = 0;
1063: }
1064:
1065: void Pox_reset(arg,rp)
1066: NODE arg;
1067: Q *rp;
1068: {
1069: USINT t;
1070: int s,id,c,m;
1071: Obj obj;
1072: int index = QTOS((Q)ARG0(arg));
1073:
1074: valid_mctab_index(index);
1075: m = m_c_tab[index].m;
1076: c = m_c_tab[index].c;
1077: if ( m >= 0 ) {
1078: if ( argc(arg) == 1 ) {
1079: ox_send_cmd(m,SM_control_reset_connection);
1080: ox_flush_stream_force(m);
1081: ox_recv(m,&id,&t);
1082: }
1083: *rp = ONE;
1084: #if defined(VISUAL)
1085: Sleep(100);
1086: ox_send_cmd(c,SM_nop);
1087: ox_flush_stream_force(c);
1088: #endif
1089: while ( 1 ) {
1090: ox_recv(c,&id,&obj);
1091: if ( id == OX_SYNC_BALL )
1092: break;
1093: }
1094: ox_send_sync(c);
1.5 noro 1095: } else
1096: *rp = 0;
1097: }
1098:
1099: void Pox_intr(arg,rp)
1100: NODE arg;
1101: Q *rp;
1102: {
1103: int m;
1104: Obj obj;
1105: int index = QTOS((Q)ARG0(arg));
1106:
1107: valid_mctab_index(index);
1108: m = m_c_tab[index].m;
1109: if ( m >= 0 ) {
1110: if ( argc(arg) == 1 ) {
1111: ox_send_cmd(m,SM_control_intr);
1112: ox_flush_stream_force(m);
1113: }
1114: *rp = ONE;
1.1 noro 1115: } else
1116: *rp = 0;
1117: }
1118:
1119: void Pox_sync(arg,rp)
1120: NODE arg;
1121: Q *rp;
1122: {
1123: int c;
1124: int index = QTOS((Q)ARG0(arg));
1125:
1126: valid_mctab_index(index);
1127: c = m_c_tab[index].c;
1128: ox_send_sync(c);
1129: *rp = 0;
1130: }
1131:
1132: void Pox_shutdown(arg,rp)
1133: NODE arg;
1134: Q *rp;
1135: {
1136: int s;
1137: int index = QTOS((Q)ARG0(arg));
1138: int status;
1139:
1140: valid_mctab_index(index);
1141: s = m_c_tab[index].m;
1142: ox_send_cmd(s,SM_shutdown);
1143: free_iofp(s);
1144: #if !MPI && !defined(VISUAL)
1145: if ( m_c_tab[index].af_unix )
1146: wait(&status);
1147: #endif
1.8 noro 1148: m_c_tab[index].m = -1; m_c_tab[index].c = -1;
1.1 noro 1149: m_c_tab[index].af_unix = 0;
1150: *rp = 0;
1151: }
1152:
1153: void Pox_push_cmd(arg,rp)
1154: NODE arg;
1155: Q *rp;
1156: {
1.7 noro 1157: int ui;
1.1 noro 1158: int index = QTOS((Q)ARG0(arg));
1159:
1160: valid_mctab_index(index);
1.7 noro 1161: ui = QTOS((Q)ARG1(arg));
1.1 noro 1162: ox_send_cmd(m_c_tab[index].c,ui);
1163: *rp = 0;
1164: }
1165: #endif
1.16 noro 1166:
1167: void shutdown_all() {
1168: int s;
1169: int i,index;
1170: int status;
1171:
1172: for ( i = I_am_server?1:0; i < m_c_i; i++ ) {
1173: index = i;
1174: check_valid_mctab_index(index);
1175: if ( index < 0 )
1176: continue;
1177: s = m_c_tab[index].m;
1178: ox_send_cmd(s,SM_shutdown);
1179: free_iofp(s);
1180: #if !MPI && !defined(VISUAL)
1181: if ( m_c_tab[index].af_unix )
1182: wait(&status);
1183: #endif
1184: m_c_tab[index].m = 0; m_c_tab[index].c = 0;
1185: m_c_tab[index].af_unix = 0;
1186: }
1187: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>