[BACK]Return to oxserver00.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kxx

Annotation of OpenXM/src/kxx/oxserver00.c, Revision 1.2

1.2     ! takayama    1: /* $OpenXM$ */
1.1       maekawa     2: /* nullserver01 */
                      3: #include <stdio.h>
                      4: #include <sys/types.h>
                      5: #include <sys/socket.h>
                      6: #include <sys/time.h>
                      7: #include <netinet/in.h>
                      8: #include <netdb.h>
                      9: #include <signal.h>
                     10: #include <setjmp.h>
                     11: /* -lnsl -lsocket /usr/ucblib/libucb.a */
                     12: #include "ox_kan.h"
                     13: #include "serversm.h"
                     14:
                     15: int OxCritical = 0;
                     16: int OxInterruptFlag = 0;
                     17: int PacketMonitor = 0;
                     18:
                     19: extern int SerialOX;  /* Serial number of the packets sent. */
                     20: extern int SerialCurrent;  /* Current Serial number of the recieved packet. */
                     21: extern int OXprintMessage; /* print oxmessages? */
                     22:
                     23: jmp_buf EnvOfChildServer;
                     24:
                     25: int JmpMessage = 0;
                     26:
                     27: static char *getSuffix(char *s);
                     28: main(int argc, char *argv[]) {
                     29:   char *s;
                     30:   char *forAsir[] = {"callsm1.sm1","callsm1b.sm1"};
                     31:   char *gnuplot[] = {"callsm1.sm1","gnuplot.sm1"};
                     32:   char *phc[] = {"callsm1.sm1","phc.sm1"};
                     33:   char *tigers[] = {"callsm1.sm1","tigers.sm1"};
                     34:   char *basicCD[] = {"basicCD.sm1"};
                     35:   /*  If you change the above, you need to change the argc of Sm1_start below.
                     36:       */
                     37:
                     38:   if (argc > 1) {
                     39:     if (strcmp(argv[1],"-monitor")==0) {
                     40:       fprintf(stderr,"Taking the packet monitor.\n");
                     41:       PacketMonitor = 1;
                     42:     }else{
                     43:       fprintf(stderr,"Unknown option. Possible options are -monitor\n");
                     44:     }
                     45:   }
                     46:   /* Load initialization files if necessary. */
                     47:   s = getSuffix(argv[0]);
                     48:   if (strcmp(s,"forAsir") == 0) {
                     49:     Sm1_start(2,forAsir,"ox_sm1_forAsir");
                     50:   }else if (strcmp(s,"gnuplot") == 0) {
                     51:     Sm1_start(2,gnuplot,"ox_sm1_gnuplot");
                     52:   }else if (strcmp(s,"phc") == 0) {
                     53:     Sm1_start(2,phc,"ox_sm1_phc");
                     54:   }else if (strcmp(s,"tigers") == 0) {
                     55:     Sm1_start(2,tigers,"ox_sm1_tigers");
                     56:   }else if (strcmp(s,"basicCD") == 0) {
                     57:     Sm1_start(1,basicCD,"basicCD");
                     58:   }else {  /* Default */
                     59:     Sm1_start(0, NULL, "ox_sm1");
                     60:   }
                     61:
                     62:   nullserver(3);
                     63: }
                     64:
                     65: static char *getSuffix(char *s) {
                     66:   /* getSuffix("ox_sm1_forAsir") returns "forAsir" */
                     67:   /* getSuffix("ox_sm1_gnuplot") returns "gnuplot" */
                     68:   int n,i,k;
                     69:   n = strlen(s);
                     70:   for (i=n-1; i>=0; i--) {
                     71:     if (s[i] == '_') {
                     72:       return( s+i+1 );
                     73:     }
                     74:   }
                     75:   return(s);
                     76: }
                     77: nullserver(int fdStream) {
                     78:   int mtag;
                     79:   int message = 1;
                     80:   ox_stream ostream;
                     81:   char sreason[1024];
                     82:   extern void controlResetHandler();
                     83:   extern jmp_buf EnvOfStackMachine;
                     84:   int engineByteOrder;
                     85:
                     86:   engineByteOrder = oxTellMyByteOrder(fdStream);
                     87:   /* Set the network byte order. */
                     88:   fprintf(stderr,"engineByteOrder=%x\n",engineByteOrder);
                     89:
                     90:   if (fdStream != -1) {
                     91:     ostream = fp2open(fdStream);
                     92:     if (ostream == NULL) {
                     93:       fprintf(stderr,"fp2open(fdStream) failed.\n");
                     94:       fdStream = -1;
                     95:     }
                     96:     if (PacketMonitor) fp2watch(ostream,stdout);
                     97:   }
                     98:   aaa : ;
                     99:   if (setjmp(EnvOfChildServer)) {
                    100:     fprintf(stderr,"childServerMain: jump here.\n");
                    101:     if (OxInterruptFlag == 0) {
                    102:       fprintf(stderr," ?! \n"); fflush(NULL);
                    103:     }
                    104:     signal(SIGUSR1,controlResetHandler); goto aaa;
                    105:   } else {
                    106:     if (JmpMessage) fprintf(stderr,"Set EnvOfChildServer.\n");
                    107:     signal(SIGUSR1,controlResetHandler);
                    108:   }
                    109:
                    110:   if (setjmp(EnvOfStackMachine)) {
                    111:     fprintf(stderr,"childServerMain: jump here by EnvOfStackMachine.\n");
                    112:     if (OxInterruptFlag == 0) {
                    113:       fprintf(stderr," ?! \n"); fflush(NULL);
                    114:     }
                    115:     /* In case of error in the stack machine, pop the error info
                    116:        and send the error packet. */
                    117:     /* oxSendOXheader(ostream,OX_DATA,SerialOX++);
                    118:     oxSendCmoError(ostream);
                    119:     oxSendOXheader(ostream,OX_DATA,SerialOX++);
                    120:     sprintf(sreason,"Jump here by sm1 error.");
                    121:     oxSendCmoError2(ostream,sreason);
                    122:     */
                    123:     Sm1_pushError2(SerialCurrent,-1,"Global jump by sm1 error");
                    124:
                    125:     signal(SIGUSR1,controlResetHandler); goto aaa ;
                    126:   } else {
                    127:     if (JmpMessage) fprintf(stderr,"Set EnvOfStackMachine.\n");
                    128:     if (signal(SIGUSR1,SIG_IGN) != SIG_IGN) {
                    129:       signal(SIGUSR1,controlResetHandler);
                    130:     }
                    131:   }
                    132:
                    133:   while (1) {
                    134:     message = OXprintMessage;
                    135:     if (OxInterruptFlag) {
                    136:       OxCritical = 1;
                    137:       if (message) {fprintf(stderr,"Clearing the read buffer.\n");fflush(NULL); }
                    138:       fp2clearReadBuf(ostream); /* clear the read buffer */
                    139:       if (message) {fprintf(stderr,"Throwing OX_SYNC_BALL\n"); fflush(NULL);}
                    140:       oxSendSyncBall(ostream);
                    141:       if (message) {fprintf(stderr,"Waiting for OX_SYNC_BALL\n");fflush(NULL);}
                    142:       oxWaitSyncBall(ostream);
                    143:       if (message) {fprintf(stderr,"Done changing OX_SYNC_BALL\n"); fflush(NULL);}
                    144:       OxInterruptFlag = 0;
                    145:       OxCritical = 0;
                    146:       goto aaa ;
                    147:     }
                    148:     OxCritical = 0;
                    149:     if (fp2select(ostream,-1)) {
                    150:       /* If there is an data in the ostream, then read data in the buffer and
                    151:          read data in the communication stream. */
                    152:       OxCritical = 1;
                    153:     }else{
                    154:       /* interrupted system call */
                    155:       /* This part is never reached. */
                    156:     }
                    157:     OxCritical = 1;
                    158:     mtag = oxGetOXheader(ostream,&SerialCurrent); /* get the message_tag */
                    159:     if (message) {
                    160:       fprintf(stderr,"\nmtag is %d (serial=%d) : ",mtag,SerialCurrent);
                    161:       switch(mtag) {
                    162:       case OX_COMMAND: fprintf(stderr," OX_COMMAND \n"); break;
                    163:       case OX_DATA:    fprintf(stderr," OX_DATA \n"); break;
                    164:       case OX_SYNC_BALL: fprintf(stderr," OX_SYNC_BALL \n"); break;
                    165:       case -1: fprintf(stderr," End of file. Exiting the server child.\n");
                    166:        exit(); break;
                    167:       default: fprintf(stderr," ?! \n"); break;
                    168:       }
                    169:     }
                    170:     /*sleep(2);  /* for dubug OX_SYNC_BALL */
                    171:     switch(mtag) {
                    172:     case OX_COMMAND:
                    173:       nullserverCommand(ostream);
                    174:       goto aaa ;  /* We always reset the jump vector. cf. memo1.txt 1998 2/13*/
                    175:       break;
                    176:     case OX_DATA:
                    177:       Sm1_pushCMO(ostream);
                    178:       break;
                    179:     case OX_SYNC_BALL:
                    180:       /* if (OxInterruptFlag)  think about it later. */
                    181:       break;
                    182:     default:
                    183:       fprintf(stderr,"Fatal error in server.\n");
                    184:       break;
                    185:     }
                    186:   }
                    187: }
                    188:
                    189: nullserverCommand(ox_stream ostream) {
                    190:   int id;
                    191:   int mtag;
                    192:   int n;
                    193:   char *name;
                    194:   char *sresult;
                    195:   struct mathCap *mathresult;
                    196:   int iresult;
                    197:   int message = 1;
                    198:   char *emsg;
                    199:   extern void controlResetHandler();
                    200:
                    201:   message = OXprintMessage;
                    202:   /* message_body */
                    203:   id = oxGetInt32(ostream);   /* get the function_id */
                    204:   if (message) {fprintf(stderr,"\nfunction_id is %d\n",id);}
                    205:   switch( id ) {
                    206:   case SM_mathcap:
                    207:     if (message) fprintf(stderr," mathcap\n");
                    208:     mathresult = (struct mathCap *)Sm1_mathcap();
                    209:     oxPushMathCap(mathresult);
                    210:     break;
                    211:   case SM_setMathCap:
                    212:     if (message) fprintf(stderr," setMathCap\n");
                    213:     Sm1_setMathCap(ostream);
                    214:     break;
                    215:   case SM_pops:
                    216:     if (message) fprintf(stderr," pops \n");
                    217:     Sm1_pops();
                    218:     break;
                    219:   case SM_getsp:
                    220:     if (message) fprintf(stderr," getsp \n");
                    221:     Sm1_getsp();
                    222:     break;
                    223:   case SM_dupErrors:
                    224:     if (message) fprintf(stderr," dupErrors \n");
                    225:     Sm1_dupErrors();
                    226:     break;
                    227:   case SM_setName:
                    228:     if (message) fprintf(stderr," setName \n");
                    229:     iresult = Sm1_setName();
                    230:     if (iresult < 0) {
                    231:       Sm1_pushError2(SerialCurrent,-1,"setName");
                    232:     }
                    233:     break;
                    234:   case SM_evalName:
                    235:     if (message) fprintf(stderr," evalName \n");
                    236:     iresult = Sm1_evalName();
                    237:     if (iresult < 0) {
                    238:       Sm1_pushError2(SerialCurrent,-1,"evalName");
                    239:     }
                    240:     break;
                    241:   case SM_executeStringByLocalParser:
                    242:     if (message) fprintf(stderr," executeStringByLocalParser\n");
                    243:     OxCritical = 0;
                    244:     iresult = Sm1_executeStringByLocalParser();
                    245:     OxCritical = 1; signal(SIGUSR1,controlResetHandler);
                    246:     if (iresult < 0) {
                    247:       emsg = Sm1_popErrorMessage("executeString: ");
                    248:       Sm1_pushError2(SerialCurrent,-1,emsg);
                    249:       return(-1);
                    250:     }
                    251:     break;
                    252:   case SM_executeFunction:
                    253:     if (message) fprintf(stderr," executeFunction\n");
                    254:     OxCritical = 0;
                    255:     iresult = Sm1_executeStringByLocalParser();
                    256:     OxCritical = 1; signal(SIGUSR1,controlResetHandler);
                    257:     if (iresult < 0) {
                    258:       emsg = Sm1_popErrorMessage("executeFunction: ");
                    259:       Sm1_pushError2(SerialCurrent,-1,emsg);
                    260:       return(-1);
                    261:     }
                    262:     break;
                    263:   case SM_popCMO:
                    264:     if (message) fprintf(stderr,"popCMO.  Start to sending data.\n",n);
                    265:     oxSendOXheader(ostream,OX_DATA,SerialOX++);
                    266:     n=Sm1_popCMO(ostream,SerialCurrent);
                    267:     if (message) fprintf(stderr,"Done.\n");
                    268:     break;
                    269:   case SM_popString:
                    270:     if (message) fprintf(stderr,"popString. send data from the stack.\n",n);
                    271:     oxSendOXheader(ostream,OX_DATA,SerialOX++);
                    272:     oxSendCmoString(ostream,Sm1_popString());
                    273:     if (message) fprintf(stderr,"Done.\n");
                    274:     break;
                    275:   case SM_shutdown:
                    276:   case SM_beginBlock:
                    277:   case SM_endBlock:
                    278:     fprintf(stderr,"This command has not yet been implemented.\n");
                    279:     return(-1);
                    280:     break;
                    281:   default:
                    282:     fprintf(stderr,"Fatal error. Unknown function_id %d\n",id);
                    283:     return(-1);
                    284:     break;
                    285:   }
                    286:   return(0);
                    287: }
                    288:
                    289:
                    290: nullserver_simplest(int fd) {
                    291:   int c;
                    292:   while(1) {
                    293:     c = readOneByte(fd);
                    294:     if (c == '@') { return; }
                    295:   }
                    296: }
                    297:
                    298:
                    299: void controlResetHandler(sig)
                    300: int sig;
                    301: {
                    302:   signal(sig,SIG_IGN);
                    303:   fprintf(stderr,"From controlResetHandler. OxCritical = %d\n",OxCritical);
                    304:   OxInterruptFlag = 1;
                    305:   if (OxCritical) {
                    306:     return;
                    307:   }else{
                    308:     longjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
                    309:   }
                    310: }
                    311:

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