[BACK]Return to tcpf.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / io

Annotation of OpenXM_contrib2/asir2000/io/tcpf.c, Revision 1.43

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>