Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.62
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.62 ! noro 47: * $OpenXM: OpenXM_contrib2/asir2000/io/tcpf.c,v 1.61 2011/06/16 08:17:15 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.60 noro 1073: int fd,n,i,index,mcind,s,maxfd,minfd;
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.60 noro 1091: maxfd = minfd = -1;
1.57 noro 1092: for ( t = list, t0 = 0; t; t = NEXT(t) ) {
1.1 noro 1093: index = QTOS((Q)BDY(t));
1094: valid_mctab_index(index);
1.57 noro 1095: s = m_c_tab[index].c;
1096: if ( ox_data_is_available(s) ) {
1097: MKNODE(t1,(Q)BDY(t),t0); t0 = t1;
1098: } else {
1099: fd = get_fd(s); FD_SET((unsigned int)fd,&r);
1.60 noro 1100: maxfd = maxfd<0 ? fd : MAX(fd,maxfd);
1101: minfd = minfd<0 ? fd : MIN(fd,minfd);
1.57 noro 1102: }
1103: }
1104: if ( t0 ) {
1105: MKLIST(*rp,t0); return;
1.1 noro 1106: }
1.57 noro 1107:
1.1 noro 1108: n = select(FD_SETSIZE,&r,&w,&e,tvp);
1.60 noro 1109: #if defined(VISUAL)
1110: for ( i = minfd, t = 0; n && i <= maxfd; i++ )
1111: #else
1.1 noro 1112: for ( i = 0, t = 0; n && i < FD_SETSIZE; i++ )
1.60 noro 1113: #endif
1.1 noro 1114: if ( FD_ISSET(i,&r) ) {
1115: /* index : index to iofp array */
1116: index = get_index(i);
1117: /* mcind : index to m_c_tab array */
1118: mcind = get_mcindex(index);
1119: n--; STOQ(mcind,q); MKNODE(t1,q,t); t = t1;
1120: }
1121: MKLIST(*rp,t);
1122: }
1123:
1.26 noro 1124: void Pox_flush(NODE arg,Q *rp)
1.1 noro 1125: {
1126: int index = QTOS((Q)ARG0(arg));
1127:
1128: valid_mctab_index(index);
1129: ox_flush_stream_force(m_c_tab[index].c);
1130: *rp = ONE;
1131: }
1132:
1.26 noro 1133: void Pox_send_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1134: {
1135: int s;
1136: int index = QTOS((Q)ARG0(arg));
1137:
1138: valid_mctab_index(index);
1139: s = m_c_tab[index].c;
1140: ox_write_cmo(s,(Obj)ARG1(arg));
1141: /* flush always */
1142: ox_flush_stream(s);
1143: *rp = 0;
1144: }
1145:
1.26 noro 1146: void Pox_recv_raw_cmo(NODE arg,Obj *rp)
1.8 noro 1147: {
1148: int s;
1149: int index = QTOS((Q)ARG0(arg));
1150:
1151: valid_mctab_index(index);
1152: s = m_c_tab[index].c;
1153: ox_read_cmo(s,rp);
1154: }
1155:
1.44 noro 1156: void Pox_send_102(NODE arg,Obj *rp)
1.43 noro 1157: {
1158: int rank = QTOS((Q)ARG0(arg));
1159:
1.44 noro 1160: ox_send_data_102(rank,(Obj)ARG1(arg));
1.43 noro 1161: *rp = 0;
1162: }
1163:
1.44 noro 1164: void Pox_recv_102(NODE arg,Obj *rp)
1.43 noro 1165: {
1.44 noro 1166: int id;
1.43 noro 1167: int rank = QTOS((Q)ARG0(arg));
1168:
1.44 noro 1169: ox_recv_102(rank,&id,rp);
1.46 noro 1170: }
1171:
1172: void Pox_bcast_102(NODE arg,Obj *rp)
1173: {
1174: int rank = QTOS((Q)ARG0(arg));
1175: Obj data;
1176:
1.48 noro 1177: if ( argc(arg) > 1 )
1178: asir_push_one((Obj)ARG1(arg));
1179: ox_bcast_102(rank);
1180: *rp = (Obj)asir_pop_one();
1.47 noro 1181: }
1182:
1183: void Pox_reduce_102(NODE arg,Obj *rp)
1184: {
1.48 noro 1185: int root = QTOS((Q)ARG0(arg));
1.47 noro 1186: STRING op;
1187: char *opname;
1188: void (*func)();
1189:
1190: op = (STRING)ARG1(arg);
1191: asir_assert(op,O_STR,"ox_reduce_102");
1192: opname = BDY(op);
1193: if ( !strcmp(opname,"+") )
1194: func = arf_add;
1195: else if ( !strcmp(opname,"*") )
1196: func = arf_mul;
1197: else {
1198: error("ox_reduce_102 : operation not supported");
1199: }
1.48 noro 1200: if ( argc(arg) > 2 )
1201: asir_push_one((Obj)ARG2(arg));
1202: ox_reduce_102(root,func);
1203: *rp = (Obj)asir_pop_one();
1.43 noro 1204: }
1205:
1.26 noro 1206: void Pox_push_local(NODE arg,Obj *rp)
1.1 noro 1207: {
1208: int s;
1209: struct oLIST dummy;
1210: VL vl;
1.41 noro 1211: int index;
1.1 noro 1212:
1.41 noro 1213: if ( !arg )
1214: error("ox_push_local : too few arguments.");
1215: index = QTOS((Q)ARG0(arg));
1.1 noro 1216: valid_mctab_index(index);
1217: s = m_c_tab[index].c; arg = NEXT(arg);
1218:
1219: dummy.id = O_LIST; dummy.body = arg;
1220: get_vars_recursive((Obj)&dummy,&vl);
1221:
1222: ox_send_local_ring(s,vl);
1223: for ( ; arg; arg = NEXT(arg) )
1224: ox_send_local_data(s,BDY(arg));
1225: *rp = 0;
1226: }
1227:
1.26 noro 1228: void Pox_push_cmo(NODE arg,Obj *rp)
1.1 noro 1229: {
1230: int s;
1.41 noro 1231: int index;
1.1 noro 1232:
1.41 noro 1233: if ( !arg )
1234: error("ox_push_cmo : too few arguments.");
1235: index = QTOS((Q)ARG0(arg));
1.1 noro 1236: valid_mctab_index(index);
1237: s = m_c_tab[index].c; arg = NEXT(arg);
1238: for ( ; arg; arg = NEXT(arg) )
1239: ox_send_data(s,BDY(arg));
1240: *rp = 0;
1241: }
1242:
1.26 noro 1243: void Pox_push_vl(NODE arg,Obj *rp)
1.1 noro 1244: {
1245: int index = QTOS((Q)ARG0(arg));
1246:
1247: valid_mctab_index(index);
1248: ox_send_local_ring(m_c_tab[index].c,CO);
1249: *rp = 0;
1250: }
1251:
1.26 noro 1252: void Pox_pop_local(NODE arg,Obj *rp)
1.1 noro 1253: {
1254: int s;
1255: int index = QTOS((Q)ARG0(arg));
1256:
1257: valid_mctab_index(index);
1258: s = m_c_tab[index].c;
1259: ox_send_cmd(s,SM_popSerializedLocalObject);
1260: ox_flush_stream_force(s);
1261: ox_get_result(s,rp);
1262: }
1263:
1.26 noro 1264: void Pox_pop_cmo(NODE arg,Obj *rp)
1.1 noro 1265: {
1266: int s;
1267: int index = QTOS((Q)ARG0(arg));
1268:
1269: valid_mctab_index(index);
1270: s = m_c_tab[index].c;
1271: ox_send_cmd(s,SM_popCMO);
1272: ox_flush_stream_force(s);
1273: ox_get_result(s,rp);
1274: }
1275:
1.26 noro 1276: void Pox_pop0_local(NODE arg,Obj *rp)
1.1 noro 1277: {
1278: int index = QTOS((Q)ARG0(arg));
1279:
1280: valid_mctab_index(index);
1281: ox_send_cmd(m_c_tab[index].c,SM_popSerializedLocalObject);
1282: *rp = 0;
1283: }
1284:
1.26 noro 1285: void Pox_pop0_cmo(NODE arg,Obj *rp)
1.1 noro 1286: {
1287: int index = QTOS((Q)ARG0(arg));
1288:
1289: valid_mctab_index(index);
1290: ox_send_cmd(m_c_tab[index].c,SM_popCMO);
1291: *rp = 0;
1292: }
1293:
1.26 noro 1294: void Pox_pop0_string(NODE arg,STRING *rp)
1.1 noro 1295: {
1296: int index = QTOS((Q)ARG0(arg));
1297:
1298: valid_mctab_index(index);
1299: ox_send_cmd(m_c_tab[index].c,SM_popString);
1300: *rp = 0;
1301: }
1302:
1.26 noro 1303: void Pox_pop_string(NODE arg,Obj *rp)
1.1 noro 1304: {
1.26 noro 1305: int s;
1.1 noro 1306: int index = QTOS((Q)ARG0(arg));
1307:
1308: valid_mctab_index(index);
1309: s = m_c_tab[index].c;
1310: ox_send_cmd(s,SM_popString);
1311: ox_flush_stream_force(s);
1312: ox_get_result(s,rp);
1313: }
1314:
1.26 noro 1315: void Pox_get(NODE arg,Obj *rp)
1.1 noro 1316: {
1.3 noro 1317: int index;
1.1 noro 1318: int s;
1319:
1.3 noro 1320: if ( !arg ) {
1321: /* client->server */
1322: ox_get_result(0,rp);
1323: } else {
1324: /* server->client */
1325: index = QTOS((Q)ARG0(arg));
1326: valid_mctab_index(index);
1327: s = m_c_tab[index].c;
1328: ox_flush_stream_force(s);
1329: ox_get_result(s,rp);
1330: }
1.1 noro 1331: }
1332:
1.26 noro 1333: void Pox_pops(NODE arg,Obj *rp)
1.1 noro 1334: {
1335: int s;
1336: USINT n;
1337: int index = QTOS((Q)ARG0(arg));
1338:
1339: valid_mctab_index(index);
1340: s = m_c_tab[index].c;
1341: if ( NEXT(arg) )
1342: MKUSINT(n,QTOS((Q)ARG1(arg)));
1343: else
1344: MKUSINT(n,1);
1345: ox_send_data(s,n);
1346: ox_send_cmd(s,SM_pops);
1347: *rp = 0;
1348: }
1349:
1.26 noro 1350: void Pox_execute_function(NODE arg,Obj *rp)
1.1 noro 1351: {
1352: int s;
1353: USINT ui;
1354: int index = QTOS((Q)ARG0(arg));
1355:
1356: valid_mctab_index(index);
1357: s = m_c_tab[index].c;
1358: MKUSINT(ui,QTOS((Q)ARG2(arg)));
1359: ox_send_data(s,ui);
1360: ox_send_data(s,ARG1(arg));
1361: ox_send_cmd(s,SM_executeFunction);
1362: *rp = 0;
1363: }
1364:
1.26 noro 1365: void Pox_setname(NODE arg,Obj *rp)
1.1 noro 1366: {
1367: int s;
1368: int index = QTOS((Q)ARG0(arg));
1369:
1370: valid_mctab_index(index);
1371: s = m_c_tab[index].c;
1372: ox_send_data(s,ARG1(arg));
1373: ox_send_cmd(s,SM_setName);
1374: *rp = 0;
1375: }
1376:
1.26 noro 1377: void Pox_evalname(NODE arg,Obj *rp)
1.1 noro 1378: {
1379: int s;
1380: int index = QTOS((Q)ARG0(arg));
1381:
1382: valid_mctab_index(index);
1383: s = m_c_tab[index].c;
1384: ox_send_data(s,ARG1(arg));
1385: ox_send_cmd(s,SM_evalName);
1386: *rp = 0;
1387: }
1388:
1.26 noro 1389: void Pox_execute_string(NODE arg,Obj *rp)
1.1 noro 1390: {
1391: int s;
1392: int index = QTOS((Q)ARG0(arg));
1393:
1.52 noro 1394: asir_assert(ARG1(arg),O_STR,"ox_execute_string");
1.1 noro 1395: valid_mctab_index(index);
1396: s = m_c_tab[index].c;
1.53 noro 1397: ox_send_data(s,ARG1(arg));
1.1 noro 1398: ox_send_cmd(s,SM_executeStringByLocalParser);
1399: *rp = 0;
1400: }
1401:
1402: /* arg=[sid,fname,arg0,arg1,...,arg{n-1}] */
1403:
1.26 noro 1404: void Pox_rpc(NODE arg,Obj *rp)
1.1 noro 1405: {
1406: int s,i,n;
1407: STRING f;
1408: USINT ui;
1409: pointer *w;
1410: NODE t;
1411: int index = QTOS((Q)ARG0(arg));
1412:
1413: valid_mctab_index(index);
1414: s = m_c_tab[index].c; arg = NEXT(arg);
1415: f = (STRING)BDY(arg); arg = NEXT(arg);
1416: ox_send_local_ring(s,CO);
1417: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1418: w = (pointer *)ALLOCA(n*sizeof(pointer));
1419: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1420: w[i] = BDY(t);
1421: for ( i = n-1; i >= 0; i-- )
1422: ox_send_local_data(s,w[i]);
1423: MKUSINT(ui,n);
1424: ox_send_data(s,ui);
1425: ox_send_data(s,f);
1426: ox_send_cmd(s,SM_executeFunction);
1427: *rp = 0;
1428: }
1429:
1.26 noro 1430: void Pox_cmo_rpc(NODE arg,Obj *rp)
1.1 noro 1431: {
1432: int s,i,n;
1433: STRING f;
1434: USINT ui;
1435: NODE t;
1.57 noro 1436: Obj dmy;
1.1 noro 1437: pointer *w;
1438: int index = QTOS((Q)ARG0(arg));
1.57 noro 1439: int sync,find;
1.1 noro 1440:
1.57 noro 1441: find = get_opt("sync",&sync);
1.1 noro 1442: valid_mctab_index(index);
1443: s = m_c_tab[index].c; arg = NEXT(arg);
1444: f = (STRING)BDY(arg); arg = NEXT(arg);
1445: for ( n = 0, t = arg; t; t = NEXT(t), n++ );
1446: w = (pointer *)ALLOCA(n*sizeof(pointer));
1447: for ( i = 0, t = arg; i < n; t = NEXT(t), i++ )
1448: w[i] = BDY(t);
1449: for ( i = n-1; i >= 0; i-- )
1450: ox_send_data(s,w[i]);
1451: MKUSINT(ui,n);
1452: ox_send_data(s,ui);
1453: ox_send_data(s,f);
1.57 noro 1454: if ( find && sync ) {
1455: ox_send_cmd(s,SM_executeFunctionSync);
1456: ox_get_result(s,&dmy);
1457: } else
1458: ox_send_cmd(s,SM_executeFunction);
1.1 noro 1459: *rp = 0;
1460: }
1461:
1.26 noro 1462: void Pox_reset(NODE arg,Q *rp)
1.1 noro 1463: {
1464: USINT t;
1.26 noro 1465: int id,c,m;
1.1 noro 1466: Obj obj;
1467: int index = QTOS((Q)ARG0(arg));
1468:
1469: valid_mctab_index(index);
1470: m = m_c_tab[index].m;
1471: c = m_c_tab[index].c;
1472: if ( m >= 0 ) {
1473: if ( argc(arg) == 1 ) {
1474: ox_send_cmd(m,SM_control_reset_connection);
1475: ox_flush_stream_force(m);
1.26 noro 1476: ox_recv(m,&id,&obj); t = (USINT)obj;
1.1 noro 1477: }
1478: *rp = ONE;
1479: #if defined(VISUAL)
1480: Sleep(100);
1481: ox_send_cmd(c,SM_nop);
1482: ox_flush_stream_force(c);
1483: #endif
1484: while ( 1 ) {
1485: ox_recv(c,&id,&obj);
1486: if ( id == OX_SYNC_BALL )
1487: break;
1488: }
1489: ox_send_sync(c);
1.5 noro 1490: } else
1491: *rp = 0;
1492: }
1493:
1.26 noro 1494: void Pox_intr(NODE arg,Q *rp)
1.5 noro 1495: {
1496: int m;
1497: int index = QTOS((Q)ARG0(arg));
1498:
1499: valid_mctab_index(index);
1500: m = m_c_tab[index].m;
1501: if ( m >= 0 ) {
1502: if ( argc(arg) == 1 ) {
1503: ox_send_cmd(m,SM_control_intr);
1504: ox_flush_stream_force(m);
1505: }
1506: *rp = ONE;
1.1 noro 1507: } else
1508: *rp = 0;
1509: }
1510:
1.26 noro 1511: void Pox_sync(NODE arg,Q *rp)
1.1 noro 1512: {
1513: int c;
1514: int index = QTOS((Q)ARG0(arg));
1515:
1516: valid_mctab_index(index);
1517: c = m_c_tab[index].c;
1518: ox_send_sync(c);
1519: *rp = 0;
1520: }
1521:
1.26 noro 1522: void Pox_shutdown(NODE arg,Q *rp)
1.1 noro 1523: {
1524: int s;
1525: int index = QTOS((Q)ARG0(arg));
1.26 noro 1526: #if !defined(VISUAL)
1.1 noro 1527: int status;
1.26 noro 1528: #endif
1.1 noro 1529:
1530: valid_mctab_index(index);
1531: s = m_c_tab[index].m;
1532: ox_send_cmd(s,SM_shutdown);
1533: free_iofp(s);
1.18 noro 1534: s = m_c_tab[index].c;
1535: free_iofp(s);
1.39 noro 1536: #if !defined(MPI) && !defined(VISUAL)
1.1 noro 1537: if ( m_c_tab[index].af_unix )
1538: wait(&status);
1539: #endif
1.8 noro 1540: m_c_tab[index].m = -1; m_c_tab[index].c = -1;
1.1 noro 1541: m_c_tab[index].af_unix = 0;
1542: *rp = 0;
1543: }
1544:
1.26 noro 1545: void Pox_push_cmd(NODE arg,Q *rp)
1.1 noro 1546: {
1.7 noro 1547: int ui;
1.1 noro 1548: int index = QTOS((Q)ARG0(arg));
1549:
1550: valid_mctab_index(index);
1.7 noro 1551: ui = QTOS((Q)ARG1(arg));
1.1 noro 1552: ox_send_cmd(m_c_tab[index].c,ui);
1553: *rp = 0;
1554: }
1.16 noro 1555:
1556: void shutdown_all() {
1557: int s;
1558: int i,index;
1.26 noro 1559: #if !defined(VISUAL)
1.16 noro 1560: int status;
1.26 noro 1561: #endif
1.16 noro 1562:
1563: for ( i = I_am_server?1:0; i < m_c_i; i++ ) {
1564: index = i;
1565: check_valid_mctab_index(index);
1566: if ( index < 0 )
1567: continue;
1568: s = m_c_tab[index].m;
1569: ox_send_cmd(s,SM_shutdown);
1.21 noro 1570: #if defined(VISUAL)
1571: Sleep(1000);
1572: #endif
1.16 noro 1573: free_iofp(s);
1.18 noro 1574: s = m_c_tab[index].c;
1575: free_iofp(s);
1.39 noro 1576: #if !defined(MPI) && !defined(VISUAL)
1.16 noro 1577: if ( m_c_tab[index].af_unix )
1578: wait(&status);
1579: #endif
1580: m_c_tab[index].m = 0; m_c_tab[index].c = 0;
1581: m_c_tab[index].af_unix = 0;
1582: }
1.33 noro 1583: }
1584:
1585: char *ox_get_servername(int);
1586:
1587: int is_ox_plot(int index)
1588: {
1589: char *name;
1590:
1591: check_valid_mctab_index(index);
1592: if ( index < 0 )
1593: return 0;
1594: /* m : client, c : server ??? */
1595: name = ox_get_servername(m_c_tab[index].c);
1596: return strcmp(name,"ox_plot") ? 0 : 1;
1597: }
1598:
1599: int validate_ox_plot_stream(int index)
1600: {
1601: int i;
1602: NODE arg;
1603: STRING name;
1604: Obj r;
1605:
1606: if ( is_ox_plot(index) )
1607: return index;
1608: for ( i = 0; i < m_c_i; i++ )
1609: if ( is_ox_plot(i) )
1610: return i;
1611:
1612: /* create an ox_plot server */
1613: MKSTR(name,"ox_plot");
1.61 noro 1614: arg = mknode(2,NULL,name);
1.33 noro 1615: Pox_launch_nox(arg,&r);
1.34 noro 1616: i = QTOS((Q)r);
1617: #if defined(VISUAL)
1618: Sleep(100);
1619: ox_send_cmd(m_c_tab[i].c,SM_nop);
1620: ox_flush_stream_force(m_c_tab[i].c);
1621: #endif
1622: return i;
1.42 noro 1623: }
1624:
1625: int get_ox_server_id(int index)
1626: {
1627: valid_mctab_index(index);
1628: return m_c_tab[index].c;
1.43 noro 1629: }
1630:
1631: int register_102(int s1,int rank,int is_master)
1632: {
1633: unsigned char c,rc;
1634:
1635: if ( rank >= MAXIOFP ) return -1;
1636: iofp_102[rank].s = s1;
1637: #if defined(VISUAL)
1638: iofp_102[rank].in = WSIO_open(s1,"r");
1639: iofp_102[rank].out = WSIO_open(s1,"w");
1640: #else
1641: iofp_102[rank].in = fdopen(s1,"r");
1642: iofp_102[rank].out = fdopen(s1,"w");
1643: #if !defined(__CYGWIN__)
1644: setbuffer(iofp_102[rank].in,iofp_102[rank].inbuf =
1.62 ! noro 1645: (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
1.43 noro 1646: setbuffer(iofp_102[rank].out,iofp_102[rank].outbuf =
1.62 ! noro 1647: (char *)MALLOC_ATOMIC(LBUFSIZ),LBUFSIZ);
1.43 noro 1648: #endif
1649: #endif
1650: if ( little_endian )
1651: c = 1;
1652: else
1653: c = 0xff;
1654: if ( is_master ) {
1655: /* server : write -> read */
1656: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1657: ox_flush_stream_force_102(rank);
1.43 noro 1658: read_char((FILE *)iofp_102[rank].in,&rc);
1659: } else {
1660: /* client : read -> write */
1661: read_char((FILE *)iofp_102[rank].in,&rc);
1662: /* special care for a failure of spawing a server */
1663: if ( rc !=0 && rc != 1 && rc != 0xff )
1664: return -1;
1665: write_char((FILE *)iofp_102[rank].out,&c);
1.44 noro 1666: ox_flush_stream_force_102(rank);
1.43 noro 1667: }
1668: iofp_102[rank].conv = c == rc ? 0 : 1;
1669: iofp_102[rank].socket = 0;
1670: return 0;
1671: }
1672:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>