Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.59
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.59 ! ohara 47: * $OpenXM: OpenXM_contrib2/asir2000/io/tcpf.c,v 1.58 2010/09/01 08:01:09 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>
1.27 noro 53: #include <string.h>
1.1 noro 54: #if !defined(VISUAL)
1.28 noro 55: #include <unistd.h>
1.1 noro 56: #include <sys/types.h>
57: #include <pwd.h>
58: #endif
59: #include "ox.h"
60:
61: #if defined(VISUAL)
1.15 noro 62: #include <stdlib.h>
1.19 noro 63: #include <winsock2.h>
1.15 noro 64: #include <process.h>
1.1 noro 65: #endif
66:
1.59 ! ohara 67: #define INIT_TAB_SIZ 64
1.1 noro 68: #define OX_XTERM "ox_xterm"
69:
70: #if !defined(_PA_RISC1_1)
71: #define RSH "rsh"
72: #else
73: #define RSH "remsh"
74: #endif
75:
76: static struct m_c {
77: int m,c,af_unix;
78: } *m_c_tab;
79:
80: static int m_c_i,m_c_s;
1.16 noro 81: int I_am_server;
1.1 noro 82:
1.43 noro 83: extern int little_endian;
84:
1.39 noro 85: #if defined(MPI)
1.1 noro 86: extern int mpi_nprocs;
87: #define valid_mctab_index(ind)\
1.8 noro 88: if((ind)<0||(ind)>=mpi_nprocs){error("invalid server id");}
1.16 noro 89: #define check_valid_mctab_index(ind)\
90: if((ind)<0||(ind)>=mpi_nprocs){(ind)=-1;}
1.1 noro 91: #else
92: #define valid_mctab_index(ind)\
1.54 noro 93: if((ind)<I_am_server||(ind)>=m_c_i||\
1.8 noro 94: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){error("invalid server id");}
1.16 noro 95: #define check_valid_mctab_index(ind)\
1.54 noro 96: if((ind)<I_am_server||(ind)>=m_c_i||\
1.16 noro 97: ((m_c_tab[ind].m<0)&&(m_c_tab[ind].c<0))){(ind)=-1;}
1.1 noro 98: #endif
99:
1.44 noro 100: struct IOFP iofp_102[MAXIOFP];
101: int nserver_102;
102: int myrank_102;
1.43 noro 103:
104: int register_102(int s,int rank, int is_master);
105:
1.1 noro 106: int register_server();
107: int get_mcindex(int);
108:
1.8 noro 109: void Pox_send_raw_cmo(), Pox_recv_raw_cmo();
110:
1.26 noro 111: void Pox_launch(),Pox_launch_nox();
1.1 noro 112: void Pox_launch_generic();
113: void Pox_shutdown();
114:
1.5 noro 115: void Pox_rpc(),Pox_cmo_rpc(),Pox_reset(),Pox_intr(),Pox_sync(),Pox_select();
1.1 noro 116:
117: void Pox_push_local(),Pox_push_cmo(),Pox_push_vl(),Pox_push_cmd();
118:
119: void Pox_pop_local(),Pox_pop_cmo();
120: void Pox_pop0_local(),Pox_pop0_cmo();
121: void Pox_pop_string(),Pox_pop0_string();
122: void Pox_get(),Pox_pops();
123:
124: void Pox_execute_function(),Pox_execute_string();
125: void Pox_setname(), Pox_evalname();
126: void Pox_flush();
127: void Pgenerate_port(),Ptry_bind_listen(),Ptry_connect(),Ptry_accept();
128: void Pregister_server();
129: void Pox_get_serverinfo();
1.8 noro 130: void Pox_mpi_myid(), Pox_mpi_nprocs();
1.42 noro 131: void Pnd_exec_f4_red();
1.44 noro 132: void Pox_tcp_accept_102(),Pox_tcp_connect_102();
133: void Pox_send_102(),Pox_recv_102();
134: void Pox_set_rank_102();
1.49 noro 135: void Pox_get_rank_102();
1.44 noro 136: void Pox_reset_102();
1.46 noro 137: void Pox_bcast_102();
1.47 noro 138: void Pox_reduce_102();
1.1 noro 139:
140: void ox_launch_generic();
141:
142: pointer bevalf();
143:
144: struct ftab tcp_tab[] = {
1.8 noro 145: {"ox_send_raw_cmo",Pox_send_raw_cmo,2},
146: {"ox_recv_raw_cmo",Pox_recv_raw_cmo,1},
1.1 noro 147: {"ox_get_serverinfo",Pox_get_serverinfo,-1},
148: {"generate_port",Pgenerate_port,-1},
1.44 noro 149:
1.45 noro 150: /* from master to client */
1.44 noro 151: {"ox_set_rank_102",Pox_set_rank_102,3},
1.45 noro 152: {"ox_tcp_accept_102",Pox_tcp_accept_102,3},
153: {"ox_tcp_connect_102",Pox_tcp_connect_102,4},
154: {"ox_reset_102",Pox_reset_102,1},
1.44 noro 155:
1.49 noro 156: {"ox_get_rank_102",Pox_get_rank_102,0},
1.44 noro 157: {"ox_send_102",Pox_send_102,2},
158: {"ox_recv_102",Pox_recv_102,1},
1.46 noro 159: {"ox_bcast_102",Pox_bcast_102,-2},
1.48 noro 160: {"ox_reduce_102",Pox_reduce_102,-3},
1.44 noro 161:
1.1 noro 162: {"try_bind_listen",Ptry_bind_listen,1},
163: {"try_connect",Ptry_connect,2},
164: {"try_accept",Ptry_accept,2},
165: {"register_server",Pregister_server,4},
166: {"ox_flush",Pox_flush,1},
167: {"ox_launch",Pox_launch,-3},
168: {"ox_launch_nox",Pox_launch_nox,-3},
169: {"ox_launch_generic",Pox_launch_generic,7},
170: {"ox_shutdown",Pox_shutdown,1},
171:
172: {"ox_rpc",Pox_rpc,-99999999},
173: {"ox_cmo_rpc",Pox_cmo_rpc,-99999999},
174:
175: {"ox_sync",Pox_sync,1},
1.39 noro 176: #if defined(MPI)
1.8 noro 177: {"ox_mpi_myid",Pox_mpi_myid,0},
178: {"ox_mpi_nprocs",Pox_mpi_nprocs,0},
179: #endif
1.39 noro 180: #if !defined(MPI)
1.1 noro 181: {"ox_reset",Pox_reset,-2},
1.5 noro 182: {"ox_intr",Pox_intr,1},
1.1 noro 183: {"ox_select",Pox_select,-2},
184: #endif
185:
186: {"ox_pop_string",Pox_pop_string,1},
187: {"ox_pop0_string",Pox_pop0_string,1},
188: {"ox_pop_local",Pox_pop_local,1},
189: {"ox_pop0_local",Pox_pop0_local,1},
190: {"ox_pop_cmo",Pox_pop_cmo,1},
191: {"ox_pop0_cmo",Pox_pop0_cmo,1},
1.3 noro 192: {"ox_get",Pox_get,-1},
1.1 noro 193: {"ox_pops",Pox_pops,-2},
194:
195: {"ox_push_vl",Pox_push_vl,1},
196: {"ox_push_local",Pox_push_local,-99999999},
197: {"ox_push_cmo",Pox_push_cmo,-99999999},
198: {"ox_push_cmd",Pox_push_cmd,2},
199:
200: {"ox_setname",Pox_setname,2},
201: {"ox_evalname",Pox_evalname,2},
202:
203: {"ox_execute_string",Pox_execute_string,2},
204: {"ox_execute_function",Pox_execute_function,3},
205:
1.42 noro 206: {"nd_exec_f4_red",Pnd_exec_f4_red,0},
207:
1.1 noro 208:
209: {0,0,0},
210: };
211:
212: extern struct IOFP iofp[];
213: extern MATHCAP my_mathcap;
1.6 noro 214: extern int ox_exchange_mathcap;
1.1 noro 215:
216: char *getenv();
217:
1.42 noro 218: void Pnd_exec_f4_red(Q *rp)
219: {
220: nd_exec_f4_red_dist();
221: *rp = 0;
222: }
223:
1.39 noro 224: #if defined(MPI)
1.8 noro 225: extern int mpi_myid, mpi_nprocs;
226:
1.26 noro 227: void Pox_mpi_myid(Q *rp)
1.8 noro 228: {
229: STOQ(mpi_myid,*rp);
230: }
231:
1.26 noro 232: void Pox_mpi_nprocs(Q *rp)
1.8 noro 233: {
234: STOQ(mpi_nprocs,*rp);
235: }
236: #endif
237:
1.26 noro 238: void Pox_get_serverinfo(NODE arg,LIST *rp)
1.1 noro 239: {
240: int i,c;
1.24 noro 241: Q s_id;
1.1 noro 242: NODE t,n0,n;
243: LIST list,l;
244:
245: if ( !arg ) {
1.35 noro 246: for ( i = I_am_server?1:0, n0 = 0; i < m_c_i; i++ )
1.8 noro 247: if ( (m_c_tab[i].m>=0) || (m_c_tab[i].c>=0) ) {
1.1 noro 248: c = m_c_tab[i].c;
249: ox_get_serverinfo(c,&list);
1.24 noro 250: STOQ(i,s_id);
251: t = mknode(2,s_id,list);
1.1 noro 252: MKLIST(l,t);
253: NEXTNODE(n0,n);
254: BDY(n) = (pointer)l;
255: }
256: if ( n0 )
257: NEXT(n) = 0;
258: MKLIST(*rp,n0);
259: } else {
260: i = QTOS((Q)ARG0(arg));
1.8 noro 261: if ( i >= 0 && i < m_c_i && ((m_c_tab[i].m>=0) || (m_c_tab[i].c>=0)) )
1.1 noro 262: ox_get_serverinfo(m_c_tab[i].c,rp);
263: else {
264: MKLIST(*rp,0);
265: }
266: }
267: }
268:
269: /*
270: if noarg or arg[0]==0 then use IP socket and return a port number
271: else use UNIX socket and return a string which represents a path name
272: */
273:
1.26 noro 274: void Pgenerate_port(NODE arg,Obj *rp)
1.1 noro 275: {
276: char port_str[BUFSIZ];
277: int port;
278: char *s;
279: STRING str;
280: Q q;
281:
282: if ( !arg || !ARG0(arg) ) {
283: generate_port(0,port_str);
284: port = atoi(port_str);
285: STOQ(port,q);
286: *rp = (Obj)q;
287: } else {
288: generate_port(1,port_str);
1.26 noro 289: s = (char *)MALLOC(strlen((char *)port_str)+1);
1.1 noro 290: strcpy(s,port_str);
291: MKSTR(str,s);
292: *rp = (Obj)str;
293: }
294: }
295:
1.44 noro 296: void Pox_reset_102(NODE arg,Q *rp)
297: {
298: int s;
299: int index = QTOS((Q)ARG0(arg));
300:
301: valid_mctab_index(index);
302: s = m_c_tab[index].c;
303: ox_send_cmd(s,SM_reset_102);
304: ox_flush_stream_force(s);
1.50 noro 305: *rp = 0;
1.49 noro 306: }
307:
308: void Pox_get_rank_102(LIST *rp)
309: {
310: Q n,r;
311: NODE node;
312:
313: STOQ(nserver_102,n);
314: STOQ(myrank_102,r);
315: node = mknode(2,n,r);
316: MKLIST(*rp,node);
1.44 noro 317: }
318:
319: void Pox_set_rank_102(NODE arg,Q *rp)
320: {
321: Q nserver,rank;
322: int s;
323: int index = QTOS((Q)ARG0(arg));
324:
325: valid_mctab_index(index);
326: s = m_c_tab[index].c;
327: nserver = (Q)ARG1(arg);
328: rank = (Q)ARG2(arg);
329: ox_send_data(s,nserver);
330: ox_send_data(s,rank);
331: ox_send_cmd(s,SM_set_rank_102);
332: ox_flush_stream_force(s);
333: *rp = 0;
334: }
335:
1.45 noro 336: /* ox_tcp_accept_102(server,port,rank) */
1.43 noro 337:
1.44 noro 338: void Pox_tcp_accept_102(NODE arg,Q *rp)
1.43 noro 339: {
1.45 noro 340: int s;
341: int index = QTOS((Q)ARG0(arg));
342:
343: valid_mctab_index(index);
344: s = m_c_tab[index].c;
1.43 noro 345:
1.45 noro 346: ox_send_data(s,ARG1(arg));
347: ox_send_data(s,ARG2(arg));
348: ox_send_cmd(s,SM_tcp_accept_102);
349: ox_flush_stream_force(s);
350: *rp = 0;
1.43 noro 351: }
352:
353: /*
1.45 noro 354: ox_tcp_connect_102(server,host,port,rank)
1.43 noro 355: */
356:
1.44 noro 357: void Pox_tcp_connect_102(NODE arg,Q *rp)
1.43 noro 358: {
1.45 noro 359: int s;
360: int index = QTOS((Q)ARG0(arg));
361:
362: valid_mctab_index(index);
363: s = m_c_tab[index].c;
1.43 noro 364:
1.45 noro 365: ox_send_data(s,ARG1(arg));
366: ox_send_data(s,ARG2(arg));
367: ox_send_data(s,ARG3(arg));
368: ox_send_cmd(s,SM_tcp_connect_102);
369: ox_flush_stream_force(s);
370: *rp = 0;
1.43 noro 371: }
372:
1.1 noro 373: /*
374: try_bind_listen(port)
375: */
376:
1.26 noro 377: void Ptry_bind_listen(NODE arg,Q *rp)
1.1 noro 378: {
379: char port_str[BUFSIZ];
380: int port,s,use_unix;
381:
1.28 noro 382: if ( IS_CYGWIN || !ARG0(arg) || NUM(ARG0(arg)) ) {
1.1 noro 383: port = QTOS((Q)ARG0(arg));
384: sprintf(port_str,"%d",port);
385: use_unix = 0;
386: } else {
387: strcpy(port_str,BDY((STRING)ARG0(arg)));
388: use_unix = 1;
389: }
390: s = try_bind_listen(use_unix,port_str);
391: STOQ(s,*rp);
392: }
393:
394: /*
395: try_connect(host,port)
396: */
397:
1.26 noro 398: void Ptry_connect(NODE arg,Q *rp)
1.1 noro 399: {
400: char port_str[BUFSIZ];
401: char *host;
402: int port,s,use_unix;
403:
1.28 noro 404: if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
1.1 noro 405: port = QTOS((Q)ARG1(arg));
406: sprintf(port_str,"%d",port);
407: use_unix = 0;
408: } else {
409: strcpy(port_str,BDY((STRING)ARG1(arg)));
410: use_unix = 1;
411: }
412: host = BDY((STRING)ARG0(arg));
413: s = try_connect(use_unix,host,port_str);
414: STOQ(s,*rp);
415: }
416:
417: /*
418: try_accept(sock,port)
419: */
420:
1.26 noro 421: void Ptry_accept(NODE arg,Q *rp)
1.1 noro 422: {
423: int use_unix,s;
424:
1.28 noro 425: if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) )
1.1 noro 426: use_unix = 0;
427: else
428: use_unix = 1;
429: s = try_accept(use_unix,QTOS((Q)ARG0(arg)));
430: STOQ(s,*rp);
431: }
432:
433: /*
434: register_server(cs,cport,ss,sport)
435: */
436:
1.26 noro 437: void Pregister_server(NODE arg,Q *rp)
1.1 noro 438: {
439: int cs,ss,cn,sn,ind,use_unix,id;
440: char cport_str[BUFSIZ],sport_str[BUFSIZ];
1.26 noro 441: Obj obj;
1.1 noro 442: MATHCAP server_mathcap;
443:
444: cs = QTOS((Q)ARG0(arg));
445: ss = QTOS((Q)ARG2(arg));
1.28 noro 446: if ( IS_CYGWIN || !ARG1(arg) || NUM(ARG1(arg)) ) {
1.1 noro 447: sprintf(cport_str,"%d",QTOS((Q)ARG1(arg)));
448: use_unix = 0;
449: } else {
450: strcpy(cport_str,BDY((STRING)ARG1(arg)));
451: use_unix = 1;
452: }
453: if ( !ARG3(arg) || NUM(ARG3(arg)) ) {
454: if ( use_unix )
455: error("register_server : the protocol should conincide for two sockets");
456: sprintf(sport_str,"%d",QTOS((Q)ARG3(arg)));
457: } else {
458: if ( !use_unix )
459: error("register_server : the protocol should conincide for two sockets");
460: strcpy(sport_str,BDY((STRING)ARG3(arg)));
461: }
462:
463: /* client mode */
464: cn = get_iofp(cs,cport_str,0);
465: sn = get_iofp(ss,sport_str,0);
1.12 noro 466: /* get_iofp returns -1 if the laucher could not spawn the server */
467: if ( sn < 0 ) {
468: /* we should terminate the launcher */
469: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
470: STOQ(-1,*rp);
471: return;
472: }
1.1 noro 473:
474: /* register server to the server list */
1.59 ! ohara 475: ind = register_server(use_unix,cn,sn,-1);
1.1 noro 476:
1.6 noro 477: if ( ox_exchange_mathcap ) {
478: /* request remote mathcap */
479: ox_send_cmd(sn,SM_mathcap);
480: ox_send_cmd(sn,SM_popCMO);
481: ox_flush_stream_force(sn);
1.26 noro 482: ox_recv(sn,&id,&obj); server_mathcap = (MATHCAP)obj;
1.6 noro 483: store_remote_mathcap(sn,server_mathcap);
484:
485: /* send my mathcap */
486: create_my_mathcap("asir");
487: ox_send_data(sn,my_mathcap);
488: ox_send_cmd(sn,SM_setMathcap);
489: }
1.1 noro 490: /* return the server id */
491: STOQ(ind,*rp);
492: }
493:
1.55 noro 494: #if !defined(VISUAL)
495: #include <sys/file.h>
496: #include <sys/types.h>
497: #include <sys/stat.h>
498: #include <pwd.h>
499:
500: static int find_executable(char *);
501: static int find_executable_main(char *);
502:
503: static int find_executable(char *com)
504: {
505: char *c,*s;
506: int len;
507: char dir[BUFSIZ],path[BUFSIZ];
508:
509: for ( s = (char *)getenv("PATH"); s; ) {
510: c = (char *)index(s,':');
511: if ( c ) {
512: len = c-s;
513: strncpy(dir,s,len); s = c+1; dir[len] = 0;
514: } else {
515: strcpy(dir,s); s = 0;
516: }
517: sprintf(path,"%s/%s",dir,com);
518: if ( find_executable_main(path) )
519: return 1;
520: }
521: return 0;
522: }
523:
524: static int find_executable_main(char *file)
525: {
526: struct stat buf;
527:
528: if ( stat(file,&buf) || (buf.st_mode & S_IFDIR) )
529: return 0;
530: if ( access(file,X_OK) )
531: return 0;
532: else
533: return 1;
534: }
535:
536: #endif
1.1 noro 537: /*
538: ox_launch_generic(host,launcher,server,use_unix,use_ssh,use_x,conn_to_serv)
539:
540: Input
541: host: hostname on which servers run
542: launcher: path name of the launcher
543: server: path name of the server
544: use_unix: use UNIX domain socket if 1
545: use_ssh: use ssh if 1
546: use_x: use X11 facilities if 1
547: conn_to_serv: connect to server if 1
548: */
549:
1.26 noro 550: void Pox_launch_generic(NODE arg,Q *rp)
1.1 noro 551: {
552: int use_unix,use_ssh,use_x,conn_to_serv;
553: char *host,*launcher,*server;
1.26 noro 554: Q ret;
1.1 noro 555:
556: host = (arg&&ARG0(arg))?BDY((STRING)ARG0(arg)):0;
557: launcher = BDY((STRING)ARG1(arg));
558: server = BDY((STRING)ARG2(arg));
1.28 noro 559: use_unix = !IS_CYGWIN && ARG3(arg) ? 1 : 0;
1.1 noro 560: use_ssh = ARG4(arg) ? 1 : 0;
561: use_x = ARG5(arg) ? 1 : 0;
562: conn_to_serv = QTOS((Q)ARG6(arg));
1.28 noro 563: if ( !IS_CYGWIN && !host )
1.1 noro 564: use_unix = 1;
565: ox_launch_generic(host,launcher,server,
1.26 noro 566: use_unix,use_ssh,use_x,conn_to_serv,&ret);
567: *rp = ret;
1.1 noro 568: }
1.56 noro 569:
570: #if 0
571: void ox_launcher_101_generic(char *host,char *launcher,
572: int use_unix,int use_ssh,int use_x,int conn_to_serv,Q *rp)
573: {
574: int cs,cn,ind,id;
575: char control_port_str[BUFSIZ];
576: Obj obj;
577:
578: #if !defined(VISUAL)
579: if ( use_unix && !find_executable("xterm") ) use_x = 0;
580: #endif
581: control_port_str[0] = 0;
582: do {
583: generate_port(use_unix,control_port_str);
584: if ( conn_to_serv ) {
585: spawn_server_101(host,launcher,
586: use_unix,use_ssh,use_x,conn_to_serv,
587: control_port_str);
588: cs = try_connect(use_unix,host,control_port_str);
589: } else {
590: cs = try_bind_listen(use_unix,control_port_str);
591: if ( cs < 0 ) continue;
592: spawn_laucher_101(host,launcher,
593: use_unix,use_ssh,use_x,conn_to_serv,
594: control_port_str);
595: cs = try_accept(use_unix,cs);
596: }
597: } while ( cs < 0 );
598:
599: /* client mode */
600: cn = get_iofp(cs,control_port_str,0);
601:
602: /* register server to the server list */
603: ind = register_server_101(use_unix,cn);
604:
605: STOQ(ind,*rp);
606: }
607: #endif
1.1 noro 608:
1.26 noro 609: void ox_launch_generic(char *host,char *launcher,char *server,
610: int use_unix,int use_ssh,int use_x,int conn_to_serv,Q *rp)
1.1 noro 611: {
612: int cs,ss,cn,sn,ind,id;
613: char control_port_str[BUFSIZ];
614: char server_port_str[BUFSIZ];
1.26 noro 615: Obj obj;
1.1 noro 616: MATHCAP server_mathcap;
1.59 ! ohara 617: Q value;
! 618: char *key;
! 619: int fd=-1;
! 620: NODE opt,n0;
! 621:
! 622: if ( current_option ) {
! 623: for ( opt = current_option; opt; opt = NEXT(opt) ) {
! 624: n0 = BDY((LIST)BDY(opt));
! 625: key = BDY((STRING)BDY(n0));
! 626: value = (Q)BDY(NEXT(n0));
! 627: if ( !strcmp(key,"fd") && value ) {
! 628: fd = QTOS(value);
! 629: break;
! 630: }
! 631: }
! 632: }
! 633: if (!available_mcindex(fd)) {
! 634: STOQ(-1,*rp);
! 635: return;
! 636: }
1.55 noro 637: #if !defined(VISUAL)
638: if ( use_unix && !find_executable("xterm") ) use_x = 0;
639: #endif
1.1 noro 640: control_port_str[0] = 0;
641: server_port_str[0] = 0;
642: do {
643: generate_port(use_unix,control_port_str);
644: generate_port(use_unix,server_port_str);
645: if ( !conn_to_serv ) {
646: cs = try_bind_listen(use_unix,control_port_str);
1.40 noro 647: if ( cs < 0 ) continue;
1.1 noro 648: ss = try_bind_listen(use_unix,server_port_str);
1.40 noro 649: if ( ss < 0 ) continue;
1.1 noro 650: }
651: spawn_server(host,launcher,server,
652: use_unix,use_ssh,use_x,conn_to_serv,
653: control_port_str,server_port_str);
654: if ( conn_to_serv ) {
655: cs = try_connect(use_unix,host,control_port_str);
1.40 noro 656: if ( cs < 0 ) continue;
1.1 noro 657: ss = try_connect(use_unix,host,server_port_str);
1.40 noro 658: if ( ss < 0 ) continue;
1.1 noro 659: } else {
660: cs = try_accept(use_unix,cs);
1.40 noro 661: if ( cs < 0 ) continue;
1.1 noro 662: ss = try_accept(use_unix,ss);
1.40 noro 663: if ( ss < 0 ) continue;
1.1 noro 664: }
665: } while ( cs < 0 || ss < 0 );
666:
667: /* client mode */
668: cn = get_iofp(cs,control_port_str,0);
669: sn = get_iofp(ss,server_port_str,0);
1.12 noro 670: /* get_iofp returns -1 if the laucher could not spawn the server */
671: if ( sn < 0 ) {
672: /* we should terminate the launcher */
673: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
674: STOQ(-1,*rp);
675: return;
676: }
1.1 noro 677:
678: /* register server to the server list */
1.59 ! ohara 679: ind = register_server(use_unix,cn,sn,fd);
1.1 noro 680:
1.6 noro 681: if ( ox_exchange_mathcap ) {
682: /* request remote mathcap */
683: ox_send_cmd(sn,SM_mathcap);
684: ox_send_cmd(sn,SM_popCMO);
685: ox_flush_stream_force(sn);
1.26 noro 686: ox_recv(sn,&id,&obj); server_mathcap = (MATHCAP)obj;
1.6 noro 687: store_remote_mathcap(sn,server_mathcap);
688:
689: /* send my mathcap */
690: create_my_mathcap("asir");
691: ox_send_data(sn,my_mathcap);
692: ox_send_cmd(sn,SM_setMathcap);
693: }
1.1 noro 694: /* return the server id */
695: STOQ(ind,*rp);
696: }
697:
1.28 noro 698: #if defined(__CYGWIN__)
1.29 noro 699: static void bslash2slash(char *buf)
700: {
701: char *p;
702:
703: for ( p = buf; *p; p++ )
704: if ( *p == '\\' )
705: *p = '/';
706: }
707:
1.28 noro 708: static int get_start_path(char *buf)
709: {
710: static char start_path[BUFSIZ];
711: static int start_initialized = 0;
712: char name[BUFSIZ];
713:
714: if ( start_initialized ) {
715: strcpy(buf,start_path);
716: return 1;
717: }
718:
1.32 noro 719: /* Windows98 */
720: strcpy(buf,"c:\\windows\\command\\start.exe");
721: cygwin_conv_to_full_posix_path(buf,name);
722: if ( !access(name,X_OK) ) {
723: bslash2slash(buf);
724: strcpy(start_path,buf);
725: start_initialized = 1;
726: return 1;
727: }
728:
1.28 noro 729: /* Windows2000 */
730: strcpy(buf,"c:\\winnt\\system32\\start.exe");
731: cygwin_conv_to_full_posix_path(buf,name);
732: if ( !access(name,X_OK) ) {
1.29 noro 733: bslash2slash(buf);
1.28 noro 734: strcpy(start_path,buf);
1.32 noro 735: start_initialized = 1;
736: return 1;
737: }
738:
739: strcpy(buf,"c:\\winnt\\system32\\cmd.exe");
740: cygwin_conv_to_full_posix_path(buf,name);
741: if ( !access(name,X_OK) ) {
742: bslash2slash(buf);
743: sprintf(start_path,"%s /c start",buf);
744: strcpy(buf,start_path);
745: start_initialized = 1;
1.28 noro 746: return 1;
747: }
748:
1.32 noro 749: strcpy(buf,"c:\\windows\\system32\\cmd.exe");
1.28 noro 750: cygwin_conv_to_full_posix_path(buf,name);
751: if ( !access(name,X_OK) ) {
1.29 noro 752: bslash2slash(buf);
1.32 noro 753: sprintf(start_path,"%s /c start",buf);
754: strcpy(buf,start_path);
755: start_initialized = 1;
1.28 noro 756: return 1;
757: }
758:
759: return 0;
760: }
761:
762: static void get_launcher_path(char *buf)
763: {
764: static char rootname[BUFSIZ];
765: static char launcher_path[BUFSIZ];
766: static int launcher_initialized = 0;
767: char name[BUFSIZ];
768:
769: if ( launcher_initialized ) {
770: strcpy(buf,launcher_path);
771: return;
772: }
773:
774: get_rootdir(rootname,sizeof(rootname));
775: sprintf(name,"%s/ox_launch.exe",rootname);
776: cygwin_conv_to_full_win32_path(name,launcher_path);
1.29 noro 777: bslash2slash(launcher_path);
1.28 noro 778: launcher_initialized = 1;
779: strcpy(buf,launcher_path);
780: }
781: #endif
782:
1.26 noro 783: void spawn_server(char *host,char *launcher,char *server,
784: int use_unix,int use_ssh,int use_x,int conn_to_serv,
785: char *control_port_str,char *server_port_str)
1.1 noro 786: {
787: char localhost[BUFSIZ];
1.31 noro 788: char *dname,*conn_str,*rsh,*dname0,*asirhost;
1.15 noro 789: char AsirExe[BUFSIZ];
790: STRING rootdir;
791: char prog[BUFSIZ];
792: char *av[BUFSIZ];
1.26 noro 793: #if !defined(VISUAL)
794: char cmd[BUFSIZ];
795: #endif
1.28 noro 796: #if defined(__CYGWIN__)
797: char win_start[BUFSIZ],win_launcher[BUFSIZ];
798: #endif
1.26 noro 799: void Pget_rootdir();
1.1 noro 800:
1.17 noro 801: dname0 = (char *)getenv("DISPLAY");
1.23 noro 802: if ( !dname0 )
803: dname0 = "0";
1.17 noro 804: dname = use_x ? dname0 : 0;
1.1 noro 805: conn_str = conn_to_serv ? "1" : "0";
1.13 noro 806: rsh = getenv("ASIR_RSH");
807: if ( !rsh )
808: rsh = use_ssh ? "ssh" : RSH;
1.25 noro 809: if ( !use_unix && strstr(rsh,"ssh") ) {
1.13 noro 810: /*
811: * if "ssh" is used to invoke a remote server,
812: * we should not specify "-display".
813: */
814: use_ssh = 1;
815: }
1.31 noro 816: asirhost = (char *)getenv("ASIRHOSTNAME");
817: if ( asirhost )
818: strcpy(localhost,asirhost);
819: else
820: gethostname(localhost,BUFSIZ);
1.15 noro 821: #if defined(VISUAL)
822: if ( !use_unix )
823: error("spawn_server : not implemented on Windows");
824: Pget_rootdir(&rootdir);
825: sprintf(AsirExe,"%s\\bin\\engine.exe",BDY(rootdir));
826: strcpy(prog,server);
827: server = strrchr(prog,'/')+1;
828: av[0] = "ox_launch";
829: av[1] = "127.0.0.1";
830: av[2] = conn_str;
831: av[3] = control_port_str;
832: av[4] = server_port_str;
833: av[5] = server;
834: av[6] = use_x ? "1" : "0";
835: av[7] = 0;
836:
837: _spawnv(_P_NOWAIT,AsirExe,av);
838: // _spawnv(_P_NOWAIT,"d:\\home\\noro\\engine2000\\debug\\engine.exe",av);
839: // printf("ox_launch 127.0.0.1 %s %s %s %s 0\n",conn_str,control_port_str,server_port_str,server);
840: #else
1.28 noro 841: if ( use_unix || !host ) {
1.27 noro 842: #if defined(__CYGWIN__)
1.29 noro 843: get_launcher_path(win_launcher);
1.30 noro 844: if ( dname && strchr(dname,':') ) {
845: if ( !fork() ) {
846: setpgid(0,getpid());
847: execlp("xterm","xterm","-name",OX_XTERM,"-T","ox_launch:local","-display",dname,
1.51 noro 848: "-geometry","60x10","-xrm","XTerm*locale:false","-e",launcher,use_unix?".":"127.1",conn_str,
1.58 noro 849: control_port_str,server_port_str,server,dname,(char *)0);
1.30 noro 850: }
851: } else if ( dname && get_start_path(win_start) ) {
852: sprintf(cmd,"%s %s %s %s %s %s %s 1",
1.29 noro 853: win_start,win_launcher,use_unix?".":"127.1",conn_str,
854: control_port_str,server_port_str,server);
855: system(cmd);
856: } else {
857: if ( !fork() ) {
858: setpgid(0,getpid());
859: execlp(launcher,launcher,use_unix?".":"127.1",conn_str,
1.58 noro 860: control_port_str,server_port_str,server,dname0,"-nolog",(char *)0);
1.27 noro 861: }
1.29 noro 862: }
1.27 noro 863: #else
1.29 noro 864: if ( !fork() ) {
865: setpgid(0,getpid());
1.28 noro 866: if ( dname )
1.13 noro 867: execlp("xterm","xterm","-name",OX_XTERM,"-T","ox_launch:local","-display",dname,
1.51 noro 868: "-geometry","60x10","-xrm","XTerm*locale:false","-e",launcher,use_unix?".":"127.1",conn_str,
1.58 noro 869: control_port_str,server_port_str,server,dname,(char *)0);
1.27 noro 870: else
1.28 noro 871: execlp(launcher,launcher,use_unix?".":"127.1",conn_str,
1.58 noro 872: control_port_str,server_port_str,server,dname0,"-nolog",(char *)0);
1.1 noro 873: }
1.29 noro 874: #endif
1.1 noro 875: } else if ( conn_to_serv == 2 ) {
876: /* special support for java */
877: if ( dname )
878: sprintf(cmd,
1.13 noro 879: "%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 880: rsh,host,launcher,OX_XTERM,dname,server,localhost,control_port_str,server_port_str);
881: else
882: sprintf(cmd,
883: "%s -n %s \"(cd %s; java %s -host %s -control %s -data %s)>&/dev/null&\">/dev/null",
884: rsh,host,launcher,server,localhost,
885: control_port_str,server_port_str,server);
886: fprintf(stderr,"%s\n",cmd);
887: sleep(20);
888: /* system(cmd); */
1.15 noro 889: } else {
1.1 noro 890: if ( dname )
1.13 noro 891: if ( use_ssh )
1.25 noro 892: sprintf(cmd,
1.51 noro 893: "%s -f -n %s \"xterm -name %s -title ox_launch:%s -geometry 60x10 -xrm 'XTerm*locale:false' -e %s %s %s %s %s %s %s >&/dev/null\">/dev/null",
1.13 noro 894: rsh,host,OX_XTERM,host,launcher,localhost,conn_str,
895: control_port_str,server_port_str,server,"1");
896: else
1.25 noro 897: sprintf(cmd,
1.51 noro 898: "%s -n %s \"xterm -name %s -title ox_launch:%s -display %s -geometry 60x10 -xrm 'XTerm*locale:false' -e %s %s %s %s %s %s %s >&/dev/null&\">/dev/null",
1.13 noro 899: rsh,host,OX_XTERM,host,dname,launcher,localhost,conn_str,
1.1 noro 900: control_port_str,server_port_str,server,dname);
901: else
1.25 noro 902: if ( use_ssh )
903: sprintf(cmd,
904: "%s -f -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
905: rsh,host,launcher,localhost,conn_str,
906: control_port_str,server_port_str,server,"1","-nolog");
907: else
908: sprintf(cmd,
1.17 noro 909: "%s -n %s \"%s %s %s %s %s %s %s %s>&/dev/null&\">/dev/null",
1.1 noro 910: rsh,host,launcher,localhost,conn_str,
1.17 noro 911: control_port_str,server_port_str,server,dname0,"-nolog");
1.1 noro 912: system(cmd);
913: }
1.15 noro 914: #endif /* VISUAL */
1.1 noro 915: }
916:
1.26 noro 917: void Pox_launch(NODE arg,Obj *rp)
1.1 noro 918: {
1.26 noro 919: ox_launch_main(1,arg,rp);
1.1 noro 920: }
921:
1.26 noro 922: void Pox_launch_nox(NODE arg,Obj *rp)
1.1 noro 923: {
1.26 noro 924: ox_launch_main(0,arg,rp);
1.1 noro 925: }
926:
927: /*
928: ox_launch() : invoke local ox_asir
929: ox_launch(0,ox_xxx) : invoke local ox_xxx with asir_libdir/ox_launch
930: ox_launch(remote,lib,ox_xxx) : invoke remote ox_xxx with lib/ox_launch
931: */
932:
1.26 noro 933: void ox_launch_main(int with_x,NODE arg,Obj *p)
1.1 noro 934: {
935: char *str;
1.26 noro 936: char *hostname,*servername;
1.1 noro 937: char *control;
938: int use_unix;
1.26 noro 939: Q ret;
1.1 noro 940: extern char *asir_libdir;
941:
942: if ( arg && ARG0(arg) && argc(arg) != 3 )
943: error("ox_launch : argument mismatch");
944: control = (char *)MALLOC(BUFSIZ);
945: if ( !arg || ( !ARG0(arg) && argc(arg) == 1 ) ) {
946: sprintf(control,"%s/ox_launch",asir_libdir);
1.28 noro 947: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 948: servername = (char *)MALLOC(BUFSIZ);
949: sprintf(servername,"%s/ox_asir",asir_libdir);
950: } else if ( !ARG0(arg) && argc(arg) == 2 ) {
951: sprintf(control,"%s/ox_launch",asir_libdir);
1.28 noro 952: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 953: str = BDY((STRING)ARG1(arg));
954: if ( str[0] == '/' )
955: servername = str;
956: else {
957: servername = (char *)MALLOC(BUFSIZ);
958: sprintf(servername,"%s/%s",asir_libdir,str);
959: }
960: } else {
961: sprintf(control,"%s/ox_launch",BDY((STRING)ARG1(arg)));
962: if ( !ARG0(arg) )
1.28 noro 963: use_unix = IS_CYGWIN ? 0 : 1;
1.1 noro 964: else
965: use_unix = 0;
966: str = BDY((STRING)ARG2(arg));
967: if ( str[0] == '/' )
968: servername = str;
969: else {
970: servername = (char *)MALLOC(BUFSIZ);
971: sprintf(servername,"%s/%s",BDY((STRING)ARG1(arg)),str);
972: }
973: }
974: if ( arg && ARG0(arg) )
975: hostname = BDY((STRING)ARG0(arg));
976: else
977: hostname = 0;
1.26 noro 978: ox_launch_generic(hostname,control,servername,use_unix,0,with_x,0,&ret);
979: *p = (Obj)ret;
1.1 noro 980: }
981:
1.59 ! ohara 982: void extend_mctab(int bound)
! 983: {
! 984: int s,i,n;
! 985: struct m_c *t;
! 986: if ( !m_c_tab ) {
! 987: n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
! 988: t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
! 989: for ( i = m_c_s; i < n; i++ ) {
! 990: t[i].af_unix = 0;
! 991: t[i].m = t[i].c = -1;
! 992: }
! 993: m_c_s = n; m_c_tab = t;
! 994: }else if (bound >= m_c_s) {
! 995: n = (bound/INIT_TAB_SIZ + 1)*INIT_TAB_SIZ;
! 996: t = (struct m_c *)MALLOC_ATOMIC(n*sizeof(struct m_c));
! 997: bzero((void *)t,s);
! 998: bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
! 999: for ( i = m_c_s; i < n; i++ ) {
! 1000: t[i].af_unix = 0;
! 1001: t[i].m = t[i].c = -1;
! 1002: }
! 1003: m_c_s = n; m_c_tab = t;
! 1004: }else {
! 1005: return;
! 1006: }
! 1007: }
! 1008:
! 1009: int available_mcindex(int ind)
! 1010: {
! 1011: if (ind < 0) return 1;
! 1012: extend_mctab(ind);
! 1013: return m_c_tab[ind].m<0 && m_c_tab[ind].c<0;
! 1014: }
! 1015:
! 1016: int register_server(int af_unix,int m,int c,int ind)
1.1 noro 1017: {
1.26 noro 1018: int s,i;
1.1 noro 1019: struct m_c *t;
1020: if ( c < 0 )
1021: return -1;
1.59 ! ohara 1022: extend_mctab( (ind<0)? 0: ind );
! 1023: if(ind >= 0) {
! 1024: if (m_c_tab[ind].m<0 && m_c_tab[ind].c<0) {
! 1025: m_c_tab[ind].m = m; m_c_tab[ind].c = c;
! 1026: m_c_tab[ind].af_unix = af_unix;
! 1027: if (ind>=m_c_i) m_c_i = ind+1;
! 1028: return ind;
1.8 noro 1029: }
1.59 ! ohara 1030: return -1;
1.1 noro 1031: }
1.39 noro 1032: #if !defined(MPI)
1.1 noro 1033: for ( i = 0; i < m_c_i; i++ )
1.8 noro 1034: if ( (m_c_tab[i].m<0) && (m_c_tab[i].c<0) )
1.1 noro 1035: break;
1036: if ( i < m_c_i ) {
1037: m_c_tab[i].m = m; m_c_tab[i].c = c;
1038: m_c_tab[i].af_unix = af_unix;
1039: return i;
1040: }
1041: #endif
1042: if ( m_c_i == m_c_s ) {
1.9 noro 1043: s = (m_c_s+INIT_TAB_SIZ)*sizeof(struct m_c);
1.27 noro 1044: t = (struct m_c *)MALLOC_ATOMIC(s); bzero((void *)m_c_tab,s);
1045: bcopy((void *)m_c_tab,(void *)t,m_c_s*sizeof(struct m_c));
1.9 noro 1046: for ( i = 0; i < INIT_TAB_SIZ; i++ ) {
1.8 noro 1047: m_c_tab[m_c_s+i].af_unix = 0;
1048: m_c_tab[m_c_s+i].m = m_c_tab[m_c_s+i].c = -1;
1049: }
1.9 noro 1050: m_c_s += INIT_TAB_SIZ; m_c_tab = t;
1.1 noro 1051: }
1052: m_c_tab[m_c_i].m = m; m_c_tab[m_c_i].c = c;
1053: m_c_tab[m_c_i].af_unix = af_unix;
1054: return m_c_i++;
1055: }
1056:
1057: /* iofp index => m_c_tab index */
1058:
1.26 noro 1059: int get_mcindex(int i)
1.1 noro 1060: {
1061: int j;
1062:
1063: for ( j = 0; j < m_c_i; j++ )
1064: if ( m_c_tab[j].c == i )
1065: return j;
1066: return -1;
1067: }
1068:
1069: /* arg = [ind0,ind1,...]; indk = index to m_c_tab */
1070:
1.26 noro 1071: void Pox_select(NODE arg,LIST *rp)
1.1 noro 1072: {
1.57 noro 1073: int fd,n,i,index,mcind,s;
1.1 noro 1074: fd_set r,w,e;
1.57 noro 1075: NODE list,t,t1,t0;
1.1 noro 1076: Q q;
1077: double max;
1078: struct timeval interval;
1079: struct timeval *tvp;
1080:
1081: list = BDY((LIST)ARG0(arg)); arg = NEXT(arg);
1082: if ( arg ) {
1083: max = ToReal((Num)BDY(arg));
1084: interval.tv_sec = (int)max;
1085: interval.tv_usec = (int)((max-(int)max)*1000000);
1086: tvp = &interval;
1087: } else
1088: tvp = 0;
1089:
1090: FD_ZERO(&r); FD_ZERO(&w); FD_ZERO(&e);
1.57 noro 1091: for ( t = list, t0 = 0; t; t = NEXT(t) ) {
1.1 noro 1092: index = QTOS((Q)BDY(t));
1093: valid_mctab_index(index);
1.57 noro 1094: s = m_c_tab[index].c;
1095: if ( ox_data_is_available(s) ) {
1096: MKNODE(t1,(Q)BDY(t),t0); t0 = t1;
1097: } else {
1098: fd = get_fd(s); FD_SET((unsigned int)fd,&r);
1099: }
1100: }
1101: if ( t0 ) {
1102: MKLIST(*rp,t0); return;
1.1 noro 1103: }
1.57 noro 1104:
1.1 noro 1105: n = select(FD_SETSIZE,&r,&w,&e,tvp);
1106: for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
1107: if ( FD_ISSET(i,&r) ) {
1108: /* index : index to iofp array */
1109: index = get_index(i);
1110: /* mcind : index to m_c_tab array */
1111: mcind = get_mcindex(index);
1112: n--; STOQ(mcind,q); MKNODE(t1,q,t); t = t1;
1113: }
1114: MKLIST(*rp,t);
1115: }
1116:
1.26 noro 1117: void Pox_flush(NODE arg,Q *rp)
1.1 noro 1118: {
1119: int index = QTOS((Q)ARG0(arg));
1120:
1121: valid_mctab_index(index);
1122: ox_flush_stream_force(m_c_tab[index].c);
1123: *rp = ONE;
1124: }
1125:
1.26 noro 1126: void Pox_send_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1127: {
1128: int s;
1129: int index = QTOS((Q)ARG0(arg));
1130:
1131: valid_mctab_index(index);
1132: s = m_c_tab[index].c;
1133: ox_write_cmo(s,(Obj)ARG1(arg));
1134: /* flush always */
1135: ox_flush_stream(s);
1136: *rp = 0;
1137: }
1138:
1.26 noro 1139: void Pox_recv_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1140: {
1141: int s;
1142: int index = QTOS((Q)ARG0(arg));
1143:
1144: valid_mctab_index(index);
1145: s = m_c_tab[index].c;
1146: ox_read_cmo(s,rp);
1147: }
1148:
1.44 noro 1149: void Pox_send_102(NODE arg,Obj *rp)
1.43 noro 1150: {
1151: int rank = QTOS((Q)ARG0(arg));
1152:
1.44 noro 1153: ox_send_data_102(rank,(Obj)ARG1(arg));
1.43 noro 1154: *rp = 0;
1155: }
1156:
1.44 noro 1157: void Pox_recv_102(NODE arg,Obj *rp)
1.43 noro 1158: {
1.44 noro 1159: int id;
1.43 noro 1160: int rank = QTOS((Q)ARG0(arg));
1161:
1.44 noro 1162: ox_recv_102(rank,&id,rp);
1.46 noro 1163: }
1164:
1165: void Pox_bcast_102(NODE arg,Obj *rp)
1166: {
1167: int rank = QTOS((Q)ARG0(arg));
1168: Obj data;
1169:
1.48 noro 1170: if ( argc(arg) > 1 )
1171: asir_push_one((Obj)ARG1(arg));
1172: ox_bcast_102(rank);
1173: *rp = (Obj)asir_pop_one();
1.47 noro 1174: }
1175:
1176: void Pox_reduce_102(NODE arg,Obj *rp)
1177: {
1.48 noro 1178: int root = QTOS((Q)ARG0(arg));
1.47 noro 1179: STRING op;
1180: char *opname;
1181: void (*func)();
1182:
1183: op = (STRING)ARG1(arg);
1184: asir_assert(op,O_STR,"ox_reduce_102");
1185: opname = BDY(op);
1186: if ( !strcmp(opname,"+") )
1187: func = arf_add;
1188: else if ( !strcmp(opname,"*") )
1189: func = arf_mul;
1190: else {
1191: error("ox_reduce_102 : operation not supported");
1192: }
1.48 noro 1193: if ( argc(arg) > 2 )
1194: asir_push_one((Obj)ARG2(arg));
1195: ox_reduce_102(root,func);
1196: *rp = (Obj)asir_pop_one();
1.43 noro 1197: }
1198:
1.26 noro 1199: void Pox_push_local(NODE arg,Obj *rp)
1.1 noro 1200: {
1201: int s;
1202: struct oLIST dummy;
1203: VL vl;
1.41 noro 1204: int index;
1.1 noro 1205:
1.41 noro 1206: if ( !arg )
1207: error("ox_push_local : too few arguments.");
1208: index = QTOS((Q)ARG0(arg));
1.1 noro 1209: valid_mctab_index(index);
1210: s = m_c_tab[index].c; arg = NEXT(arg);
1211:
1212: dummy.id = O_LIST; dummy.body = arg;
1213: get_vars_recursive((Obj)&dummy,&vl);
1214:
1215: ox_send_local_ring(s,vl);
1216: for ( ; arg; arg = NEXT(arg) )
1217: ox_send_local_data(s,BDY(arg));
1218: *rp = 0;
1219: }
1220:
1.26 noro 1221: void Pox_push_cmo(NODE arg,Obj *rp)
1.1 noro 1222: {
1223: int s;
1.41 noro 1224: int index;
1.1 noro 1225:
1.41 noro 1226: if ( !arg )
1227: error("ox_push_cmo : too few arguments.");
1228: index = QTOS((Q)ARG0(arg));
1.1 noro 1229: valid_mctab_index(index);
1230: s = m_c_tab[index].c; arg = NEXT(arg);
1231: for ( ; arg; arg = NEXT(arg) )
1232: ox_send_data(s,BDY(arg));
1233: *rp = 0;
1234: }
1235:
1.26 noro 1236: void Pox_push_vl(NODE arg,Obj *rp)
1.1 noro 1237: {
1238: int index = QTOS((Q)ARG0(arg));
1239:
1240: valid_mctab_index(index);
1241: ox_send_local_ring(m_c_tab[index].c,CO);
1242: *rp = 0;
1243: }
1244:
1.26 noro 1245: void Pox_pop_local(NODE arg,Obj *rp)
1.1 noro 1246: {
1247: int s;
1248: int index = QTOS((Q)ARG0(arg));
1249:
1250: valid_mctab_index(index);
1251: s = m_c_tab[index].c;
1252: ox_send_cmd(s,SM_popSerializedLocalObject);
1253: ox_flush_stream_force(s);
1254: ox_get_result(s,rp);
1255: }
1256:
1.26 noro 1257: void Pox_pop_cmo(NODE arg,Obj *rp)
1.1 noro 1258: {
1259: int s;
1260: int index = QTOS((Q)ARG0(arg));
1261:
1262: valid_mctab_index(index);
1263: s = m_c_tab[index].c;
1264: ox_send_cmd(s,SM_popCMO);
1265: ox_flush_stream_force(s);
1266: ox_get_result(s,rp);
1267: }
1268:
1.26 noro 1269: void Pox_pop0_local(NODE arg,Obj *rp)
1.1 noro 1270: {
1271: int index = QTOS((Q)ARG0(arg));
1272:
1273: valid_mctab_index(index);
1274: ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
1275: *rp = 0;
1276: }
1277:
1.26 noro 1278: void Pox_pop0_cmo(NODE arg,Obj *rp)
1.1 noro 1279: {
1280: int index = QTOS((Q)ARG0(arg));
1281:
1282: valid_mctab_index(index);
1283: ox_send_cmd(m_c_tab[index].c,SM_popCMO);
1284: *rp = 0;
1285: }
1286:
1.26 noro 1287: void Pox_pop0_string(NODE arg,STRING *rp)
1.1 noro 1288: {
1289: int index = QTOS((Q)ARG0(arg));
1290:
1291: valid_mctab_index(index);
1292: ox_send_cmd(m_c_tab[index].c,SM_popString);
1293: *rp = 0;
1294: }
1295:
1.26 noro 1296: void Pox_pop_string(NODE arg,Obj *rp)
1.1 noro 1297: {
1.26 noro 1298: int s;
1.1 noro 1299: int index = QTOS((Q)ARG0(arg));
1300:
1301: valid_mctab_index(index);
1302: s = m_c_tab[index].c;
1303: ox_send_cmd(s,SM_popString);
1304: ox_flush_stream_force(s);
1305: ox_get_result(s,rp);
1306: }
1307:
1.26 noro 1308: void Pox_get(NODE arg,Obj *rp)
1.1 noro 1309: {
1.3 noro 1310: int index;
1.1 noro 1311: int s;
1312:
1.3 noro 1313: if ( !arg ) {
1314: /* client->server */
1315: ox_get_result(0,rp);
1316: } else {
1317: /* server->client */
1318: index = QTOS((Q)ARG0(arg));
1319: valid_mctab_index(index);
1320: s = m_c_tab[index].c;
1321: ox_flush_stream_force(s);
1322: ox_get_result(s,rp);
1323: }
1.1 noro 1324: }
1325:
1.26 noro 1326: void Pox_pops(NODE arg,Obj *rp)
1.1 noro 1327: {
1328: int s;
1329: USINT n;
1330: int index = QTOS((Q)ARG0(arg));
1331:
1332: valid_mctab_index(index);
1333: s = m_c_tab[index].c;
1334: if ( NEXT(arg) )
1335: MKUSINT(n,QTOS((Q)ARG1(arg)));
1336: else
1337: MKUSINT(n,1);
1338: ox_send_data(s,n);
1339: ox_send_cmd(s,SM_pops);
1340: *rp = 0;
1341: }
1342:
1.26 noro 1343: void Pox_execute_function(NODE arg,Obj *rp)
1.1 noro 1344: {
1345: int s;
1346: USINT ui;
1347: int index = QTOS((Q)ARG0(arg));
1348:
1349: valid_mctab_index(index);
1350: s = m_c_tab[index].c;
1351: MKUSINT(ui,QTOS((Q)ARG2(arg)));
1352: ox_send_data(s,ui);
1353: ox_send_data(s,ARG1(arg));
1354: ox_send_cmd(s,SM_executeFunction);
1355: *rp = 0;
1356: }
1357:
1.26 noro 1358: void Pox_setname(NODE arg,Obj *rp)
1.1 noro 1359: {
1360: int s;
1361: int index = QTOS((Q)ARG0(arg));
1362:
1363: valid_mctab_index(index);
1364: s = m_c_tab[index].c;
1365: ox_send_data(s,ARG1(arg));
1366: ox_send_cmd(s,SM_setName);
1367: *rp = 0;
1368: }
1369:
1.26 noro 1370: void Pox_evalname(NODE arg,Obj *rp)
1.1 noro 1371: {
1372: int s;
1373: int index = QTOS((Q)ARG0(arg));
1374:
1375: valid_mctab_index(index);
1376: s = m_c_tab[index].c;
1377: ox_send_data(s,ARG1(arg));
1378: ox_send_cmd(s,SM_evalName);
1379: *rp = 0;
1380: }
1381:
1.26 noro 1382: void Pox_execute_string(NODE arg,Obj *rp)
1.1 noro 1383: {
1384: int s;
1385: int index = QTOS((Q)ARG0(arg));
1386:
1.52 noro 1387: asir_assert(ARG1(arg),O_STR,"ox_execute_string");
1.1 noro 1388: valid_mctab_index(index);
1389: s = m_c_tab[index].c;
1.53 noro 1390: ox_send_data(s,ARG1(arg));
1.1 noro 1391: ox_send_cmd(s,SM_executeStringByLocalParser);
1392: *rp = 0;
1393: }
1394:
1395: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
1396:
1.26 noro 1397: void Pox_rpc(NODE arg,Obj *rp)
1.1 noro 1398: {
1399: int s,i,n;
1400: STRING f;
1401: USINT ui;
1402: pointer *w;
1403: NODE t;
1404: int index = QTOS((Q)ARG0(arg));
1405:
1406: valid_mctab_index(index);
1407: s = m_c_tab[index].c; arg = NEXT(arg);
1408: f = (STRING)BDY(arg); arg = NEXT(arg);
1409: ox_send_local_ring(s,CO);
1410: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1411: w = (pointer *)ALLOCA(n*sizeof(pointer));
1412: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1413: w[i] = BDY(t);
1414: for ( i = n-1; i >= 0; i-- )
1415: ox_send_local_data(s,w[i]);
1416: MKUSINT(ui,n);
1417: ox_send_data(s,ui);
1418: ox_send_data(s,f);
1419: ox_send_cmd(s,SM_executeFunction);
1420: *rp = 0;
1421: }
1422:
1.26 noro 1423: void Pox_cmo_rpc(NODE arg,Obj *rp)
1.1 noro 1424: {
1425: int s,i,n;
1426: STRING f;
1427: USINT ui;
1428: NODE t;
1.57 noro 1429: Obj dmy;
1.1 noro 1430: pointer *w;
1431: int index = QTOS((Q)ARG0(arg));
1.57 noro 1432: int sync,find;
1.1 noro 1433:
1.57 noro 1434: find = get_opt("sync",&sync);
1.1 noro 1435: valid_mctab_index(index);
1436: s = m_c_tab[index].c; arg = NEXT(arg);
1437: f = (STRING)BDY(arg); arg = NEXT(arg);
1438: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1439: w = (pointer *)ALLOCA(n*sizeof(pointer));
1440: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1441: w[i] = BDY(t);
1442: for ( i = n-1; i >= 0; i-- )
1443: ox_send_data(s,w[i]);
1444: MKUSINT(ui,n);
1445: ox_send_data(s,ui);
1446: ox_send_data(s,f);
1.57 noro 1447: if ( find && sync ) {
1448: ox_send_cmd(s,SM_executeFunctionSync);
1449: ox_get_result(s,&dmy);
1450: } else
1451: ox_send_cmd(s,SM_executeFunction);
1.1 noro 1452: *rp = 0;
1453: }
1454:
1.26 noro 1455: void Pox_reset(NODE arg,Q *rp)
1.1 noro 1456: {
1457: USINT t;
1.26 noro 1458: int id,c,m;
1.1 noro 1459: Obj obj;
1460: int index = QTOS((Q)ARG0(arg));
1461:
1462: valid_mctab_index(index);
1463: m = m_c_tab[index].m;
1464: c = m_c_tab[index].c;
1465: if ( m >= 0 ) {
1466: if ( argc(arg) == 1 ) {
1467: ox_send_cmd(m,SM_control_reset_connection);
1468: ox_flush_stream_force(m);
1.26 noro 1469: ox_recv(m,&id,&obj); t = (USINT)obj;
1.1 noro 1470: }
1471: *rp = ONE;
1472: #if defined(VISUAL)
1473: Sleep(100);
1474: ox_send_cmd(c,SM_nop);
1475: ox_flush_stream_force(c);
1476: #endif
1477: while ( 1 ) {
1478: ox_recv(c,&id,&obj);
1479: if ( id == OX_SYNC_BALL )
1480: break;
1481: }
1482: ox_send_sync(c);
1.5 noro 1483: } else
1484: *rp = 0;
1485: }
1486:
1.26 noro 1487: void Pox_intr(NODE arg,Q *rp)
1.5 noro 1488: {
1489: int m;
1490: int index = QTOS((Q)ARG0(arg));
1491:
1492: valid_mctab_index(index);
1493: m = m_c_tab[index].m;
1494: if ( m >= 0 ) {
1495: if ( argc(arg) == 1 ) {
1496: ox_send_cmd(m,SM_control_intr);
1497: ox_flush_stream_force(m);
1498: }
1499: *rp = ONE;
1.1 noro 1500: } else
1501: *rp = 0;
1502: }
1503:
1.26 noro 1504: void Pox_sync(NODE arg,Q *rp)
1.1 noro 1505: {
1506: int c;
1507: int index = QTOS((Q)ARG0(arg));
1508:
1509: valid_mctab_index(index);
1510: c = m_c_tab[index].c;
1511: ox_send_sync(c);
1512: *rp = 0;
1513: }
1514:
1.26 noro 1515: void Pox_shutdown(NODE arg,Q *rp)
1.1 noro 1516: {
1517: int s;
1518: int index = QTOS((Q)ARG0(arg));
1.26 noro 1519: #if !defined(VISUAL)
1.1 noro 1520: int status;
1.26 noro 1521: #endif
1.1 noro 1522:
1523: valid_mctab_index(index);
1524: s = m_c_tab[index].m;
1525: ox_send_cmd(s,SM_shutdown);
1526: free_iofp(s);
1.18 noro 1527: s = m_c_tab[index].c;
1528: free_iofp(s);
1.39 noro 1529: #if !defined(MPI) && !defined(VISUAL)
1.1 noro 1530: if ( m_c_tab[index].af_unix )
1531: wait(&status);
1532: #endif
1.8 noro 1533: m_c_tab[index].m = -1; m_c_tab[index].c = -1;
1.1 noro 1534: m_c_tab[index].af_unix = 0;
1535: *rp = 0;
1536: }
1537:
1.26 noro 1538: void Pox_push_cmd(NODE arg,Q *rp)
1.1 noro 1539: {
1.7 noro 1540: int ui;
1.1 noro 1541: int index = QTOS((Q)ARG0(arg));
1542:
1543: valid_mctab_index(index);
1.7 noro 1544: ui = QTOS((Q)ARG1(arg));
1.1 noro 1545: ox_send_cmd(m_c_tab[index].c,ui);
1546: *rp = 0;
1547: }
1.16 noro 1548:
1549: void shutdown_all() {
1550: int s;
1551: int i,index;
1.26 noro 1552: #if !defined(VISUAL)
1.16 noro 1553: int status;
1.26 noro 1554: #endif
1.16 noro 1555:
1556: for ( i = I_am_server?1:0; i < m_c_i; i++ ) {
1557: index = i;
1558: check_valid_mctab_index(index);
1559: if ( index < 0 )
1560: continue;
1561: s = m_c_tab[index].m;
1562: ox_send_cmd(s,SM_shutdown);
1.21 noro 1563: #if defined(VISUAL)
1564: Sleep(1000);
1565: #endif
1.16 noro 1566: free_iofp(s);
1.18 noro 1567: s = m_c_tab[index].c;
1568: free_iofp(s);
1.39 noro 1569: #if !defined(MPI) && !defined(VISUAL)
1.16 noro 1570: if ( m_c_tab[index].af_unix )
1571: wait(&status);
1572: #endif
1573: m_c_tab[index].m = 0; m_c_tab[index].c = 0;
1574: m_c_tab[index].af_unix = 0;
1575: }
1.33 noro 1576: }
1577:
1578: char *ox_get_servername(int);
1579:
1580: int is_ox_plot(int index)
1581: {
1582: char *name;
1583:
1584: check_valid_mctab_index(index);
1585: if ( index < 0 )
1586: return 0;
1587: /* m : client, c : server ??? */
1588: name = ox_get_servername(m_c_tab[index].c);
1589: return strcmp(name,"ox_plot") ? 0 : 1;
1590: }
1591:
1592: int validate_ox_plot_stream(int index)
1593: {
1594: int i;
1595: NODE arg;
1596: STRING name;
1597: Obj r;
1598:
1599: if ( is_ox_plot(index) )
1600: return index;
1601: for ( i = 0; i < m_c_i; i++ )
1602: if ( is_ox_plot(i) )
1603: return i;
1604:
1605: /* create an ox_plot server */
1606: MKSTR(name,"ox_plot");
1607: arg = mknode(2,0,name);
1608: Pox_launch_nox(arg,&r);
1.34 noro 1609: i = QTOS((Q)r);
1610: #if defined(VISUAL)
1611: Sleep(100);
1612: ox_send_cmd(m_c_tab[i].c,SM_nop);
1613: ox_flush_stream_force(m_c_tab[i].c);
1614: #endif
1615: return i;
1.42 noro 1616: }
1617:
1618: int get_ox_server_id(int index)
1619: {
1620: valid_mctab_index(index);
1621: return m_c_tab[index].c;
1.43 noro 1622: }
1623:
1624: int register_102(int s1,int rank,int is_master)
1625: {
1626: unsigned char c,rc;
1627:
1628: if ( rank >= MAXIOFP ) return -1;
1629: iofp_102[rank].s = s1;
1630: #if defined(VISUAL)
1631: iofp_102[rank].in = WSIO_open(s1,"r");
1632: iofp_102[rank].out = WSIO_open(s1,"w");
1633: #else
1634: iofp_102[rank].in = fdopen(s1,"r");
1635: iofp_102[rank].out = fdopen(s1,"w");
1636: #if !defined(__CYGWIN__)
1637: setbuffer(iofp_102[rank].in,iofp_102[rank].inbuf =
1638: (char *)GC_malloc_atomic(LBUFSIZ),LBUFSIZ);
1639: setbuffer(iofp_102[rank].out,iofp_102[rank].outbuf =
1640: (char *)GC_malloc_atomic(LBUFSIZ),LBUFSIZ);
1641: #endif
1642: #endif
1643: if ( little_endian )
1644: c = 1;
1645: else
1646: c = 0xff;
1647: if ( is_master ) {
1648: /* server : write -> read */
1649: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1650: ox_flush_stream_force_102(rank);
1.43 noro 1651: read_char((FILE *)iofp_102[rank].in,&rc);
1652: } else {
1653: /* client : read -> write */
1654: read_char((FILE *)iofp_102[rank].in,&rc);
1655: /* special care for a failure of spawing a server */
1656: if ( rc !=0 && rc != 1 && rc != 0xff )
1657: return -1;
1658: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1659: ox_flush_stream_force_102(rank);
1.43 noro 1660: }
1661: iofp_102[rank].conv = c == rc ? 0 : 1;
1662: iofp_102[rank].socket = 0;
1663: return 0;
1664: }
1665:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>