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

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

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

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