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

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

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