Annotation of OpenXM/src/kxx/oxserver00.c, Revision 1.11
1.11 ! takayama 1: /* $OpenXM: OpenXM/src/kxx/oxserver00.c,v 1.10 2003/08/22 16:08:23 ohara Exp $ */
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:
1.7 takayama 23: #if defined(__CYGWIN__)
24: sigjmp_buf EnvOfChildServer;
25: #else
1.1 maekawa 26: jmp_buf EnvOfChildServer;
1.7 takayama 27: #endif
1.1 maekawa 28:
29: int JmpMessage = 0;
30:
31: static char *getSuffix(char *s);
32: main(int argc, char *argv[]) {
33: char *s;
34: char *forAsir[] = {"callsm1.sm1","callsm1b.sm1"};
35: char *gnuplot[] = {"callsm1.sm1","gnuplot.sm1"};
36: char *phc[] = {"callsm1.sm1","phc.sm1"};
37: char *tigers[] = {"callsm1.sm1","tigers.sm1"};
38: char *basicCD[] = {"basicCD.sm1"};
39: /* If you change the above, you need to change the argc of Sm1_start below.
1.5 takayama 40: */
1.1 maekawa 41:
42: if (argc > 1) {
43: if (strcmp(argv[1],"-monitor")==0) {
44: fprintf(stderr,"Taking the packet monitor.\n");
45: PacketMonitor = 1;
46: }else{
47: fprintf(stderr,"Unknown option. Possible options are -monitor\n");
48: }
49: }
50: /* Load initialization files if necessary. */
51: s = getSuffix(argv[0]);
52: if (strcmp(s,"forAsir") == 0) {
53: Sm1_start(2,forAsir,"ox_sm1_forAsir");
54: }else if (strcmp(s,"gnuplot") == 0) {
55: Sm1_start(2,gnuplot,"ox_sm1_gnuplot");
56: }else if (strcmp(s,"phc") == 0) {
57: Sm1_start(2,phc,"ox_sm1_phc");
58: }else if (strcmp(s,"tigers") == 0) {
59: Sm1_start(2,tigers,"ox_sm1_tigers");
60: }else if (strcmp(s,"basicCD") == 0) {
61: Sm1_start(1,basicCD,"basicCD");
62: }else { /* Default */
63: Sm1_start(0, NULL, "ox_sm1");
64: }
65:
1.11 ! takayama 66: nullserver(3,4);
1.1 maekawa 67: }
68:
69: static char *getSuffix(char *s) {
70: /* getSuffix("ox_sm1_forAsir") returns "forAsir" */
1.6 takayama 71: /* getSuffix("ox_sm1_forAsir.exe") returns "forAsir" */
1.1 maekawa 72: /* getSuffix("ox_sm1_gnuplot") returns "gnuplot" */
73: int n,i,k;
74: n = strlen(s);
1.6 takayama 75: if (n > 5 && (strcmp(".exe",&(s[n-4]))==0 || strcmp(".EXE",&(s[n-4]))==0)) {
76: s[n-4] = '\0';
77: }
1.1 maekawa 78: for (i=n-1; i>=0; i--) {
79: if (s[i] == '_') {
80: return( s+i+1 );
81: }
82: }
83: return(s);
84: }
1.11 ! takayama 85: nullserver(int fdStreamIn,int fdStreamOut) {
1.1 maekawa 86: int mtag;
87: int message = 1;
1.11 ! takayama 88: ox_stream ostreamIn;
! 89: ox_stream ostreamOut;
1.1 maekawa 90: char sreason[1024];
91: extern void controlResetHandler();
1.7 takayama 92: #if defined(__CYGWIN__)
93: extern sigjmp_buf EnvOfStackMachine;
94: #else
1.1 maekawa 95: extern jmp_buf EnvOfStackMachine;
1.7 takayama 96: #endif
1.1 maekawa 97: int engineByteOrder;
98:
1.11 ! takayama 99: /* for debug */
! 100: PacketMonitor = 1;
! 101:
1.4 takayama 102: fflush(NULL);
1.11 ! takayama 103: engineByteOrder = oxTellMyByteOrder(fdStreamOut,fdStreamIn);
1.1 maekawa 104: /* Set the network byte order. */
105: fprintf(stderr,"engineByteOrder=%x\n",engineByteOrder);
106:
1.11 ! takayama 107: if (fdStreamIn != -1) {
! 108: ostreamIn = fp2open(fdStreamIn);
! 109: if (ostreamIn == NULL) {
! 110: fprintf(stderr,"fp2open(fdStreamIn) failed.\n");
! 111: fdStreamIn = -1;
! 112: }
! 113: if (PacketMonitor) fp2watch(ostreamIn,stdout);
! 114: }
! 115: if (fdStreamOut != -1) {
! 116: ostreamOut = fp2open(fdStreamOut);
! 117: if (ostreamOut == NULL) {
! 118: fprintf(stderr,"fp2open(fdStreamOut) failed.\n");
! 119: fdStreamOut = -1;
1.1 maekawa 120: }
1.11 ! takayama 121: if (PacketMonitor) fp2watch(ostreamOut,stdout);
1.1 maekawa 122: }
1.11 ! takayama 123:
1.1 maekawa 124: aaa : ;
1.7 takayama 125: #if defined(__CYGWIN__)
126: if (sigsetjmp(EnvOfChildServer,1)) {
127: #else
1.1 maekawa 128: if (setjmp(EnvOfChildServer)) {
1.7 takayama 129: #endif
1.1 maekawa 130: fprintf(stderr,"childServerMain: jump here.\n");
131: if (OxInterruptFlag == 0) {
132: fprintf(stderr," ?! \n"); fflush(NULL);
133: }
1.9 takayama 134: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
135: KSexecuteString(" (Computation is interrupted.) ");
1.1 maekawa 136: signal(SIGUSR1,controlResetHandler); goto aaa;
137: } else {
138: if (JmpMessage) fprintf(stderr,"Set EnvOfChildServer.\n");
139: signal(SIGUSR1,controlResetHandler);
140: }
1.7 takayama 141: #if defined(__CYGWIN__)
142: if (sigsetjmp(EnvOfStackMachine,1)) {
143: #else
1.1 maekawa 144: if (setjmp(EnvOfStackMachine)) {
1.7 takayama 145: #endif
1.9 takayama 146: fprintf(stderr,"childServerMain: jump here by EnvOfStackMachine or timeout.\n");
1.1 maekawa 147: if (OxInterruptFlag == 0) {
148: fprintf(stderr," ?! \n"); fflush(NULL);
149: }
150: /* In case of error in the stack machine, pop the error info
151: and send the error packet. */
152: /* oxSendOXheader(ostream,OX_DATA,SerialOX++);
1.5 takayama 153: oxSendCmoError(ostream);
154: oxSendOXheader(ostream,OX_DATA,SerialOX++);
155: sprintf(sreason,"Jump here by sm1 error.");
156: oxSendCmoError2(ostream,sreason);
1.1 maekawa 157: */
158: Sm1_pushError2(SerialCurrent,-1,"Global jump by sm1 error");
159:
1.9 takayama 160: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1.1 maekawa 161: signal(SIGUSR1,controlResetHandler); goto aaa ;
162: } else {
163: if (JmpMessage) fprintf(stderr,"Set EnvOfStackMachine.\n");
164: if (signal(SIGUSR1,SIG_IGN) != SIG_IGN) {
165: signal(SIGUSR1,controlResetHandler);
166: }
167: }
168:
169: while (1) {
1.11 ! takayama 170: message = OXprintMessage;
1.1 maekawa 171: if (OxInterruptFlag) {
172: OxCritical = 1;
173: if (message) {fprintf(stderr,"Clearing the read buffer.\n");fflush(NULL); }
1.11 ! takayama 174: fp2clearReadBuf(ostreamIn); /* clear the read buffer */
1.1 maekawa 175: if (message) {fprintf(stderr,"Throwing OX_SYNC_BALL\n"); fflush(NULL);}
1.11 ! takayama 176: oxSendSyncBall(ostreamOut);
1.1 maekawa 177: if (message) {fprintf(stderr,"Waiting for OX_SYNC_BALL\n");fflush(NULL);}
1.11 ! takayama 178: oxWaitSyncBall(ostreamIn);
1.1 maekawa 179: if (message) {fprintf(stderr,"Done changing OX_SYNC_BALL\n"); fflush(NULL);}
180: OxInterruptFlag = 0;
181: OxCritical = 0;
182: goto aaa ;
183: }
184: OxCritical = 0;
1.11 ! takayama 185: if (fp2select(ostreamIn,-1)) {
1.1 maekawa 186: /* If there is an data in the ostream, then read data in the buffer and
187: read data in the communication stream. */
188: OxCritical = 1;
189: }else{
190: /* interrupted system call */
191: /* This part is never reached. */
192: }
193: OxCritical = 1;
1.11 ! takayama 194: mtag = oxGetOXheader(ostreamIn,&SerialCurrent); /* get the message_tag */
1.1 maekawa 195: if (message) {
196: fprintf(stderr,"\nmtag is %d (serial=%d) : ",mtag,SerialCurrent);
197: switch(mtag) {
198: case OX_COMMAND: fprintf(stderr," OX_COMMAND \n"); break;
199: case OX_DATA: fprintf(stderr," OX_DATA \n"); break;
200: case OX_SYNC_BALL: fprintf(stderr," OX_SYNC_BALL \n"); break;
201: case -1: fprintf(stderr," End of file. Exiting the server child.\n");
1.10 ohara 202: exit(0); break;
1.1 maekawa 203: default: fprintf(stderr," ?! \n"); break;
204: }
205: }
206: /*sleep(2); /* for dubug OX_SYNC_BALL */
207: switch(mtag) {
208: case OX_COMMAND:
1.11 ! takayama 209: nullserverCommand(ostreamIn,ostreamOut);
1.1 maekawa 210: goto aaa ; /* We always reset the jump vector. cf. memo1.txt 1998 2/13*/
211: break;
212: case OX_DATA:
1.11 ! takayama 213: Sm1_pushCMO(ostreamIn);
1.1 maekawa 214: break;
215: case OX_SYNC_BALL:
216: /* if (OxInterruptFlag) think about it later. */
217: break;
218: default:
219: fprintf(stderr,"Fatal error in server.\n");
220: break;
221: }
222: }
223: }
224:
1.11 ! takayama 225: nullserverCommand(ox_stream ostreamIn,ox_stream ostreamOut) {
1.1 maekawa 226: int id;
227: int mtag;
228: int n;
229: char *name;
230: char *sresult;
231: struct mathCap *mathresult;
232: int iresult;
233: int message = 1;
234: char *emsg;
235: extern void controlResetHandler();
236:
1.11 ! takayama 237: message = OXprintMessage;
1.1 maekawa 238: /* message_body */
1.11 ! takayama 239: id = oxGetInt32(ostreamIn); /* get the function_id */
1.1 maekawa 240: if (message) {fprintf(stderr,"\nfunction_id is %d\n",id);}
241: switch( id ) {
242: case SM_mathcap:
243: if (message) fprintf(stderr," mathcap\n");
244: mathresult = (struct mathCap *)Sm1_mathcap();
245: oxPushMathCap(mathresult);
246: break;
247: case SM_setMathCap:
248: if (message) fprintf(stderr," setMathCap\n");
1.11 ! takayama 249: Sm1_setMathCap(ostreamOut);
1.1 maekawa 250: break;
251: case SM_pops:
252: if (message) fprintf(stderr," pops \n");
253: Sm1_pops();
254: break;
255: case SM_getsp:
256: if (message) fprintf(stderr," getsp \n");
257: Sm1_getsp();
258: break;
259: case SM_dupErrors:
260: if (message) fprintf(stderr," dupErrors \n");
261: Sm1_dupErrors();
262: break;
1.3 takayama 263: case SM_pushCMOtag:
264: if (message) fprintf(stderr," pushCMOtag \n");
265: Sm1_pushCMOtag(SerialCurrent);
1.5 takayama 266: break;
1.1 maekawa 267: case SM_setName:
268: if (message) fprintf(stderr," setName \n");
269: iresult = Sm1_setName();
270: if (iresult < 0) {
271: Sm1_pushError2(SerialCurrent,-1,"setName");
272: }
273: break;
274: case SM_evalName:
275: if (message) fprintf(stderr," evalName \n");
276: iresult = Sm1_evalName();
277: if (iresult < 0) {
278: Sm1_pushError2(SerialCurrent,-1,"evalName");
279: }
280: break;
281: case SM_executeStringByLocalParser:
282: if (message) fprintf(stderr," executeStringByLocalParser\n");
283: OxCritical = 0;
284: iresult = Sm1_executeStringByLocalParser();
285: OxCritical = 1; signal(SIGUSR1,controlResetHandler);
286: if (iresult < 0) {
287: emsg = Sm1_popErrorMessage("executeString: ");
288: Sm1_pushError2(SerialCurrent,-1,emsg);
289: return(-1);
290: }
291: break;
292: case SM_executeFunction:
293: if (message) fprintf(stderr," executeFunction\n");
294: OxCritical = 0;
295: iresult = Sm1_executeStringByLocalParser();
296: OxCritical = 1; signal(SIGUSR1,controlResetHandler);
297: if (iresult < 0) {
298: emsg = Sm1_popErrorMessage("executeFunction: ");
299: Sm1_pushError2(SerialCurrent,-1,emsg);
300: return(-1);
301: }
302: break;
303: case SM_popCMO:
304: if (message) fprintf(stderr,"popCMO. Start to sending data.\n",n);
1.11 ! takayama 305: oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
! 306: n=Sm1_popCMO(ostreamOut,SerialCurrent);
1.1 maekawa 307: if (message) fprintf(stderr,"Done.\n");
308: break;
309: case SM_popString:
310: if (message) fprintf(stderr,"popString. send data from the stack.\n",n);
1.11 ! takayama 311: oxSendOXheader(ostreamOut,OX_DATA,SerialOX++);
! 312: oxSendCmoString(ostreamOut,Sm1_popString());
1.1 maekawa 313: if (message) fprintf(stderr,"Done.\n");
314: break;
315: case SM_shutdown:
1.5 takayama 316: fprintf(stderr,"Shutting down the engine.\n");
317: exit(0);
318: break;
1.1 maekawa 319: case SM_beginBlock:
320: case SM_endBlock:
321: fprintf(stderr,"This command has not yet been implemented.\n");
322: return(-1);
323: break;
324: default:
325: fprintf(stderr,"Fatal error. Unknown function_id %d\n",id);
326: return(-1);
327: break;
328: }
329: return(0);
330: }
331:
332:
333: nullserver_simplest(int fd) {
334: int c;
335: while(1) {
336: c = readOneByte(fd);
337: if (c == '@') { return; }
338: }
339: }
340:
341:
342: void controlResetHandler(sig)
1.5 takayama 343: int sig;
1.1 maekawa 344: {
345: signal(sig,SIG_IGN);
1.8 takayama 346: cancelAlarm();
1.1 maekawa 347: fprintf(stderr,"From controlResetHandler. OxCritical = %d\n",OxCritical);
348: OxInterruptFlag = 1;
349: if (OxCritical) {
350: return;
351: }else{
1.7 takayama 352: #if defined(__CYGWIN__)
353: siglongjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
354: #else
1.1 maekawa 355: longjmp(EnvOfChildServer,2); /* returns 2 for ctrl-C */
1.7 takayama 356: #endif
1.1 maekawa 357: }
358: }
359:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>