[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.45

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

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