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