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