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

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

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