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