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