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