Annotation of OpenXM/src/kan96xx/plugin/oxmisc2.c, Revision 1.20
1.20 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/plugin/oxmisc2.c,v 1.19 2003/11/17 08:49:16 takayama Exp $ */
1.1 maekawa 2: #include <stdio.h>
3: #include "ox_kan.h"
4: #include "oxmisc2.h" /* This file requires sm1 object description. */
5: #include "cmo.h"
1.8 takayama 6:
1.1 maekawa 7: extern FILE *MyErrorOut;
1.6 takayama 8: #define SET_MYERROROUT { if (MyErrorOut == NULL) MyErrorOut=stdout; }
9: /* It is also defined in oxmisc.c */
10:
1.1 maekawa 11: extern int SerialOX; /* defined in SerialOX */
12:
13: extern int OxVersion;
14:
15: int DebugMathCap = 1;
16:
17:
18:
19: int oxGet(oxclientp client, struct object *op,int *isObj)
1.12 takayama 20: /* This method should be synchronized. */
21: /* oxGet is a function for client. */
1.1 maekawa 22: {
23: int ans;
24: ox_stream os;
25: int m;
26: struct object rob;
27: int sss; /* Serial number of the recieved packet. */
28: *isObj = 0;
29: op->tag = Snull;
30: os = client->datafp2;
31: switch(client->dstate) {
32: case DSTATE_ANY:
33: m = oxGetOXheader(os,&sss);
34: switch(m) {
35: case OX_DATA:
36: client->dstate = DSTATE_WAIT_OX_DATA;
37: return(oxGet(client,op,isObj));
38: case OX_SYNC_BALL:
39: client->dstate = DSTATE_ANY;
40: return(OX_SYNC_BALL);
41: default:
42: errorOxmisc2("oxGet: cannot handle this tag.\n");
43: client->dstate = DSTATE_ERROR;
44: return(-1);
45: }
46: break;
47: case DSTATE_FIRST_SYNC: /* waiting the first sync ball */
48: /* I need to clear the buffer?? */
49: oxWaitSyncBall(os);
50: client->dstate = DSTATE_ANY;
51: oxSendSyncBall(os);
52: return(OX_SYNC_BALL);
53: break;
54: case DSTATE_WAIT_OX_DATA: /* waiting a cmo data. */
55: *op = cmoObjectFromStream2(client->datafp2);
56: client->dstate = DSTATE_ANY;
57: *isObj = 1;
58: return(0);
59: break;
60: case DSTATE_ERROR:
61: client->dstate = DSTATE_ERROR;
62: errorOxmisc2("oxGet: dstate == DSTATE_ERROR (error state)\n");
63: return(-1);
64: default:
65: client->dstate = DSTATE_ERROR;
66: errorOxmisc2("oxGet: Unknown state number.");
67: }
68:
69: return(-1);
70: }
71:
72: int oxGetFromControl(oxclientp client)
73: {
74: int ans = -1;
1.7 takayama 75: AbortIfRFC_101(client);
1.1 maekawa 76: if (client->cstate != -1) {
77: ans = oxGetResultOfControlInt32(client->controlfd);
78: if (ans != -1) { client->cstate = 0; }
79: else {client->cstate = -1; }
80: }
81: return(ans);
82: }
83:
84: int oxReq(oxclientp client,int func,struct object ob)
85: {
86: struct object *ob1p;
1.6 takayama 87: SET_MYERROROUT;
1.1 maekawa 88: /* request to the control channel */
89: if (func == SM_control_reset_connection ||
90: func == SM_control_kill) {
1.12 takayama 91: AbortIfRFC_101(client);
1.1 maekawa 92: switch(func) {
93: case SM_control_reset_connection:
94: oxReqControlResetConnection(client->controlfd);
1.9 takayama 95: client->cstate = 0;
1.1 maekawa 96: client->dstate = DSTATE_FIRST_SYNC;
97: break;
98: case SM_control_kill:
99: oxReqControlKill(client->controlfd);
100: client->cstate = 0;
101: client->dstate = DSTATE_ANY;
102: break;
103: }
104: fflush(NULL);
105: return(0);
106: }
107:
108: /* request to the data channel */
109: if (client->dstate != DSTATE_ANY) {
110: errorOxmisc2("oxReq: client->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
111: return(-1);
112: }
113: switch(func) {
114: case SM_DUMMY_sendcmo:
115: if (!cmoCheckMathCap(ob,(struct object *)client->mathcapObjp)) {
116: errorOxmisc2("oxReq: your peer does not understand this cmo.\n");
117: return(-1);
118: }
119: oxSendOXheader(client->datafp2,OX_DATA,SerialOX++);
120: cmoObjectToStream2(ob,client->datafp2);
121: client->dstate = DSTATE_ANY;
122: break;
123: case SM_sync_ball:
124: oxSendSyncBall(client->datafp2);
125: client->dstate = DSTATE_ANY; /* We do not expect the sync ball.*/
126: client->cstate = 0; /* clear the cstate */
127: break;
128: case SM_popCMO:
129: oxReqPopCMO(client->datafp2);
130: client->dstate = DSTATE_ANY;
131: break;
132: case SM_mathcap:
133: oxReqMathCap(client->datafp2);
134: client->dstate = DSTATE_ANY;
135: break;
136: case SM_setMathCap:
137: /* ob = [(mathcap-obj) [[version num, system name] [sm tags]
1.12 takayama 138: ob1 smtags
139: oxtags [[ox numbers, [cmo numbers]]]
140: ob3 ob2 */
141: /* oxtags [[OX_DATA, [cmo numbers]],[OX_DATA_LOCAL,[opt]],...]*/
1.1 maekawa 142: {
143: struct object ob1;
144: struct object ob2;
145: struct object ob3;
1.4 takayama 146: struct object obm;
1.1 maekawa 147: struct object smtags;
1.4 takayama 148: struct object oxtags;
149: struct object ox;
1.12 takayama 150: int n,i;
151: struct mathCap mathcap;
1.1 maekawa 152:
1.12 takayama 153: if (strcmp(KopString(getoa(ob,0)),"mathcap-object") != 0) {
154: errorOxmisc2("data format error in oxReqSetMathCap");
155: client->dstate = DSTATE_ANY;
156: break;
157: }
1.4 takayama 158: obm = getoa(ob,1);
159: ob1 = getoa(obm,0);
160: smtags = getoa(obm,1);
161: oxtags = getoa(obm,2);
162: if (smtags.tag != Sarray || oxtags.tag != Sarray) {
1.12 takayama 163: errorOxmisc2("data format error in oxReqSetMathCap");
1.4 takayama 164: }
1.1 maekawa 165: ob1p = (struct object *) sGC_malloc(sizeof(struct object));
166: *ob1p = ob1;
167: mathcap.infop = ob1p;
1.4 takayama 168:
169: n = getoaSize(oxtags);
1.1 maekawa 170: if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
171: mathcap.oxSize = n;
172: for (i=0; i<n; i++) {
1.12 takayama 173: ox = getoa(oxtags,i);
174: if (ox.tag != Sarray) {
175: errorOxmisc2("Data format error of the third argument of mathcap.");
176: }
177: mathcap.ox[i] = KopInteger(getoa(ox,0));
178: if (mathcap.ox[i] == OX_DATA) {
179: if (getoaSize(ox) < 2) {
180: errorOxmisc2("Data format error in an entry of the third argument of mathcap.");
181: }
182: ob2 = getoa(ox,1);
183: if (ob2.tag != Sarray) {
184: errorOxmisc2("Data format error in an entry of the third argument of mathcap.");
185: }
186: mathcap.n = getoaSize(ob2);
187: if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
188: for (i=0; i<mathcap.n; i++) {
189: mathcap.cmo[i] = KopInteger(getoa(ob2,i));
190: }
191: }
1.1 maekawa 192: }
193:
194: n = getoaSize(smtags);
195: if (n >= MATHCAP_SIZE) errorOxmisc2("Too big mathcap of your peer.");
196: mathcap.smSize = n;
197: for (i=0; i<n; i++) {
1.12 takayama 198: mathcap.sm[i] = KopInteger(getoa(smtags,i));
1.1 maekawa 199: }
200:
201: oxReqSetMathCap(client->datafp2,&mathcap);
202: client->dstate = DSTATE_ANY;
203: }
204: break;
205: case SM_pops:
206: if (ob.tag != Sinteger) {
207: errorOxmisc2("SM_pops : the argument must be an integer.");
208: return(-1);
209: }
210: oxReqPops(client->datafp2, KopInteger(ob));
211: client->dstate = DSTATE_ANY;
212: break;
213: case SM_executeStringByLocalParser:
214: if (ob.tag != Sdollar) {
215: errorOxmisc2("SM_executeStringByLocalParser : the argument must be a string.");
216: return(-1);
217: }
218: oxReqExecuteStringByLocalParser(client->datafp2,KopString(ob));
219: client->dstate = DSTATE_ANY;
220: break;
221: case SM_executeFunction:
222: if (ob.tag != Sdollar) {
223: errorOxmisc2("SM_executeFunction : the argument must be a string.");
224: return(-1);
225: }
226: oxReqExecuteFunction(client->datafp2,KopString(ob));
227: client->dstate = DSTATE_ANY;
228: break;
229: case SM_popString:
230: oxReqPopString(client->datafp2);
231: client->dstate = DSTATE_ANY;
232: break;
233: case SM_evalName:
234: if (ob.tag != Sdollar) {
235: errorOxmisc2("SM_evalName : the argument must be a string.");
236: return(-1);
237: }
238: oxReqEvalName(client->datafp2,KopString(ob));
239: client->dstate = DSTATE_ANY;
240: break;
241: case SM_setName:
242: if (ob.tag != Sdollar) {
243: errorOxmisc2("SM_setName : the argument must be a string.");
244: return(-1);
245: }
246: oxReqSetName(client->datafp2,KopString(ob));
247: client->dstate = DSTATE_ANY;
248: break;
249: case SM_getsp:
250: oxReqSingleOperand(client->datafp2,SM_getsp);
251: client->dstate = DSTATE_ANY;
252: break;
253: case SM_dupErrors:
254: oxReqSingleOperand(client->datafp2,SM_dupErrors);
255: client->dstate = DSTATE_ANY;
256: break;
257: default:
258: fprintf(MyErrorOut,"func=%d ",func);
259: errorOxmisc2("This function is not implemented.");
260: break;
261: }
262: fp2fflush(client->datafp2);
263: return(0);
264: }
265:
266: struct object KoxCreateClient(struct object ip,
1.12 takayama 267: struct object portStream,
268: struct object portControl)
1.1 maekawa 269: {
270: struct object rob;
271: oxclientp client;
272: rob.tag = Snull;
273: if (ip.tag != Sdollar) {
274: errorOxmisc2("KoxCreateClient(): The first argument must be a hostname given by a string.");
275: return(rob);
276: }
277: if (portStream.tag == Sdollar) {
278: client = oxCreateClientFile(KopString(ip),KopString(portStream),
1.12 takayama 279: "/dev/null","w");
1.1 maekawa 280: if (client == NULL) {
281: errorOxmisc2("KoxCreateClient(): Open error.");
282: return(rob);
283: }
284: rob = newObjectArray(N_OF_CLIENT_FIELDS);
285: oxClientToObject(client,rob);
286: return(rob);
287: }
288:
289: if (portStream.tag != Sinteger) {
290: errorOxmisc2("KoxCreateClient(): The second argument must be a port number given in an integer.");
291: return(rob);
292: }
293: if (portControl.tag != Sinteger) {
294: errorOxmisc2("KoxCreateClient(): The third argument must be a port number given in an integer.");
295: return(rob);
296: }
297: client = oxCreateClient(KopString(ip),KopInteger(portStream),KopInteger(portControl));
298: if (client == NULL) {
299: errorOxmisc2("KoxCreateClient(): Open error.");
300: return(rob);
301: }
302: rob = newObjectArray(N_OF_CLIENT_FIELDS);
303: oxClientToObject(client,rob);
1.14 takayama 304: oxClientListUpdate(rob);
1.1 maekawa 305: return(rob);
306: }
307:
308: static int isItClientObject(struct object ob)
309: {
310: int size,i;
311: struct object ee[N_OF_CLIENT_FIELDS];
312: if (ob.tag != Sarray) {
313: return(0);
314: }
315: size = getoaSize(ob);
316: if (size != N_OF_CLIENT_FIELDS) return(0);
317: for (i=0; i<N_OF_CLIENT_FIELDS; i++) {
318: ee[i] = getoa(ob,i);
319: }
320:
321: if (ee[0].tag != Sdollar) return(0);
322: if (strcmp(KopString(ee[0]),"client")!=0) return(0);
323:
324: if (ee[1].tag != Sfile) return(0);
325: if (strcmp((ee[1]).lc.str,MAGIC2) != 0) return(0);
326:
327: for (i=2; i<=9; i++) {
328: if (ee[i].tag != Sinteger) return(0);
329: }
330: return(1);
331: }
332:
333:
334: struct object KoxIsThereErrorClient(struct object ob)
335: {
336: struct object rob;
337: int ans;
338: int size;
339: oxclient cc;
340: rob.tag = Snull;
341: if (!isItClientObject(ob)) {
342: errorOxmisc2("KoxIsThereErrorClient(): the argument must be an array for client object.");
343: return(rob);
344: }
345: if (oxObjectToClient(ob,&cc) == -1) return(KpoInteger(-1));
346: ans = oxIsThereErrorClient(&cc);
347: return(KpoInteger(ans));
348: }
349:
350: int oxClientToObject(oxclientp client,struct object rob)
351: {
352: struct object ob;
353: if (client == NULL) return;
354: /* rob = newObjectArray(N_OF_CLIENT_FIELDS); */
355: if (rob.tag != Sarray) {
356: errorOxmisc2("oxClientToObject(): the second argument must be an array.");
357: return(-1);
358: }
359: if (getoaSize(rob) != N_OF_CLIENT_FIELDS) {
360: errorOxmisc2("oxClientToObject(): the second argument must be an array of size N_OF_CLIENT_FIELDS.");
361: return(-1);
362: }
363:
364: ob = KpoString("client");
365: putoa(rob,0,ob);
366:
367: ob.tag = Sfile; ob.lc.str = MAGIC2; ob.rc.voidp = (void *)(client->datafp2);
368: putoa(rob,1,ob);
369:
370: putoa(rob,2,KpoInteger(client->dataport));
371: putoa(rob,3,KpoInteger(client->controlfd));
372: putoa(rob,4,KpoInteger(client->controlport));
373: putoa(rob,5,KpoInteger(client->dstate));
374: putoa(rob,6,KpoInteger(client->cstate));
375: putoa(rob,7,KpoInteger(client->humanio));
376: putoa(rob,8,KpoInteger(client->id));
377: putoa(rob,9,KpoInteger(client->type));
378: if (client->mathcapObjp == NULL) {
379: putoa(rob,10,NullObject);
380: }else{
381: putoa(rob,10,*((struct object *)(client->mathcapObjp)));
382: }
383: putoa(rob,11,KpoInteger(client->engineByteOrder));
384: putoa(rob,12,KpoInteger(client->controlByteOrder));
1.16 takayama 385: putoa(rob,13,KpoInteger(client->engineID));
1.1 maekawa 386: return(0);
387: }
388:
389: int oxObjectToClient(struct object ob,oxclientp cp)
390: {
391: struct object ob1;
392: struct object *obp;
393: if (cp == NULL) {
394: errorOxmisc2("oxObjectToClient(): the second argument is NULL");
395: return(-1);
396: }
397: if (!isItClientObject(ob)) {
398: errorOxmisc2("oxObjectToClient(): the first argument is not client object.");
399: oxInitClient(cp);
400: return(-1);
401: }
402:
403: ob1 = getoa(ob,1);
404: cp->datafp2 = (FILE2 *) (ob1.rc.voidp);
405:
406: ob1 = getoa(ob,2);
407: cp->dataport = KopInteger(ob1);
408:
409: ob1 = getoa(ob,3);
410: cp->controlfd = KopInteger(ob1);
411:
412: ob1 = getoa(ob,4);
413: cp->controlport = KopInteger(ob1);
414:
415: ob1 = getoa(ob,5);
416: cp->dstate = KopInteger(ob1);
417:
418: ob1 = getoa(ob,6);
419: cp->cstate = KopInteger(ob1);
420:
421: ob1 = getoa(ob,7);
422: cp->humanio = KopInteger(ob1);
423:
424: ob1 = getoa(ob,8);
425: cp->id = KopInteger(ob1);
426:
427: ob1 = getoa(ob,9);
428: cp->type = KopInteger(ob1);
429:
430: ob1 = getoa(ob,10);
431: if (ob1.tag == Snull) {
432: cp->mathcapObjp = NULL;
433: }else{
434: obp = (struct object *) sGC_malloc(sizeof(struct object));
435: *obp = ob1;
436: cp->mathcapObjp = (void *)obp;
437: }
438: ob1 = getoa(ob,11);
439: cp->engineByteOrder = KopInteger(ob1);
440: ob1 = getoa(ob,12);
441: cp->controlByteOrder = KopInteger(ob1);
1.16 takayama 442:
443: ob1 = getoa(ob,13);
444: cp->engineID = KopInteger(ob1);
1.1 maekawa 445:
446:
447: return(0);
448: }
449:
450: struct object KoxReq(struct object client,
1.12 takayama 451: struct object func,
452: struct object ob1)
1.1 maekawa 453: {
454: int ans;
455: static oxclientp cc1 = NULL;
456: struct object rob;
457: rob.tag = Snull;
458: if (cc1 == NULL) {
459: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
460: if (cc1 == NULL) {
461: errorOxmisc2("KoxReq(): no more memory.");
462: return(rob);
463: }
464: oxInitClient(cc1);
465: }
466:
467: if (oxObjectToClient(client,cc1) == -1) return(rob);
468: if (cc1 == NULL) {
469: errorOxmisc2("KoxReq(): the first argument must be a client object.");
470: return(rob);
471: }
472: if (func.tag != Sinteger) {
473: errorOxmisc2("KoxReq(): the second argument must be an integer.");
474: return(rob);
475: }
476: ans = oxReq(cc1,KopInteger(func),ob1);
477: /* synchronize cc1 and client. */
478: oxClientToObject(cc1,client);
479:
480: return(KpoInteger(ans));
481: }
482:
483: struct object KoxGet(struct object client)
484: {
485: int ans,k;
486: static oxclientp cc1 = NULL;
487: struct object rob;
488: rob.tag = Snull;
489: if (cc1 == NULL) {
490: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
491: if (cc1 == NULL) {
492: errorOxmisc2("KoxGet(): no more memory.");
493: return(rob);
494: }
495: oxInitClient(cc1);
496: }
497:
498: if (oxObjectToClient(client,cc1) == -1) return(rob);
499: if (cc1 == NULL) {
500: errorOxmisc2("KoxGet(): the first argument must be a client object.");
501: return(rob);
502: }
503:
504: ans = oxGet(cc1,&rob,&k);
505: /* synchronize cc1 and client. */
506: oxClientToObject(cc1,client);
507:
508: if (k) return(rob);
509: else {
510: return(KpoInteger(ans));
511: }
512: }
513:
514: struct object KoxGetFromControl(struct object client)
515: {
516: int ans;
517: static oxclientp cc1 = NULL;
518: struct object rob;
519: rob.tag = Snull;
520: if (cc1 == NULL) {
521: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
522: if (cc1 == NULL) {
523: errorOxmisc2("KoxGetFromControl(): no more memory.");
524: return(rob);
525: }
526: oxInitClient(cc1);
527: }
528:
529: if (oxObjectToClient(client,cc1) == -1) return(rob);
530: if (cc1 == NULL) {
531: errorOxmisc2("KoxGetFromControl(): the first argument must be a client object.");
532: return(rob);
533: }
534:
535: ans = oxGetFromControl(cc1);
536: /* synchronize cc1 and client. */
537: oxClientToObject(cc1,client);
538:
539: return(KpoInteger(ans));
540: }
541:
542: struct object KoxMultiSelect(struct object oclients,struct object t)
543: {
544: static int first = 1;
545: static int csize = 0;
546: static oxclientp *clients = NULL;
547: oxclientp cc1;
548: struct object rob;
549: int i;
550: int tt;
551: struct object ob1;
552: struct object ob2;
553: struct object ob0;
554: int size;
555: int ans;
556: int dataready[1024];
557: int controlready[1024];
558:
559: rob.tag = Snull;
560: if (oclients.tag != Sarray) {
561: errorOxmisc2("KoxMultiSelect(): the first argument must be an array.");
562: return(rob);
563: }
564: size = getoaSize(oclients);
565: if (first) {
566: first = 0; csize = size;
567: clients = (oxclientp *)mymalloc(sizeof(oxclientp)*(size+1));
1.12 takayama 568: if (clients == NULL) {
1.1 maekawa 569: errorOxmisc2("KoxMultiSelect(): no more memory.");
570: return(rob);
571: }
572: for (i=0; i<size; i++) {
573: clients[i] = (oxclientp) mymalloc(sizeof(oxclient));
574: if (clients[i] == NULL) {
1.12 takayama 575: errorOxmisc2("KoxMultiSelect(): no more memory.");
576: return(rob);
1.1 maekawa 577: }
578: oxInitClient(clients[i]);
579: }
580: }
581: if (csize < size) {
582: first = 1;
583: return(KoxMultiSelect(oclients,t));
584: }
585: for (i=0; i<size; i++) {
586: ob0 = getoa(oclients,i);
587: if (oxObjectToClient(ob0,clients[i]) == -1) return(rob);
588: }
589: if (t.tag != Sinteger) {
590: errorOxmisc2("KoxMultiSelect(): the second argument must be an integer.");
591: }
592: tt = KopInteger(t);
593: ans = oxclientMultiSelect(clients,dataready,controlready,size,tt);
594: /* synchronize oclients and clients. */
595: for (i=0; i<size; i++) {
596: ob0 = getoa(oclients,i);
597: oxClientToObject(clients[i],ob0);
598: putoa(oclients,i,ob0);
599: }
600: rob = newObjectArray(3);
601: putoa(rob,0,KpoInteger(ans));
602: ob1 = newObjectArray(size);
603: ob2 = newObjectArray(size);
604: for (i=0; i<size; i++) {
605: putoa(ob1,i,KpoInteger(dataready[i]));
606: putoa(ob2,i,KpoInteger(controlready[i]));
607: }
608: putoa(rob,1,ob1);
609: putoa(rob,2,ob2);
610: return(rob);
611: }
612:
613: struct object KoxWatch(struct object client,struct object f)
1.12 takayama 614: /* f is not used for now. It should be log file. */
1.1 maekawa 615: {
616: int ans,k;
617: static oxclientp cc1 = NULL;
618: struct object rob;
1.3 takayama 619: extern int WatchStream;
1.1 maekawa 620: rob.tag = Snull;
1.3 takayama 621: if (client.tag == Sinteger) {
1.12 takayama 622: if (KopInteger(client)) {
623: WatchStream = 1;
624: }else{
625: WatchStream = 0;
626: }
627: return;
1.3 takayama 628: }
1.1 maekawa 629: if (cc1 == NULL) {
630: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
631: if (cc1 == NULL) {
632: errorOxmisc2("KoxWatch(): no more memory.");
633: return(rob);
634: }
635: oxInitClient(cc1);
636: }
637:
638: if (oxObjectToClient(client,cc1) == -1) return(rob);
639: if (cc1 == NULL) {
640: errorOxmisc2("KoxWatch(): the first argument must be a client object.");
641: return(rob);
642: }
643:
644: k = fp2watch(cc1->datafp2,stdout);
645: /* synchronize cc1 and client. */
646: oxClientToObject(cc1,client);
647:
648: return(KpoInteger(ans));
649: }
650:
1.18 takayama 651: struct object KoxLog(struct object client,struct object in,struct object out)
652: {
653: int ans,k;
654: static oxclientp cc1 = NULL;
655: struct object rob;
656: rob.tag = Snull;
657: if (cc1 == NULL) {
658: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
659: if (cc1 == NULL) {
660: errorOxmisc2("KoxLog(): no more memory.");
661: return(rob);
662: }
663: oxInitClient(cc1);
664: }
665:
666: if (oxObjectToClient(client,cc1) == -1) return(rob);
667: if (cc1 == NULL) {
668: errorOxmisc2("KoxLog(): the first argument must be a client object.");
669: return(rob);
670: }
671:
672: if (in.tag != Sfile) {
673: errorOxmisc2("KoxLog(): the second argument is not a file object.");
674: return rob;
675: }
676: if (out.tag != Sfile) {
677: errorOxmisc2("KoxLog(): the third argument is not a file object.");
678: return rob;
679: }
680: k = fp2log(cc1->datafp2,in.rc.file,out.rc.file);
1.19 takayama 681: fputc(cc1->engineByteOrder,out.rc.file); /* Output engineByteOrder. */
1.18 takayama 682: /* synchronize cc1 and client. */
683: oxClientToObject(cc1,client);
684:
685: return(KpoInteger(ans));
686: }
687:
688: struct object KoxLogStop(struct object client) {
689: static oxclientp cc1 = NULL;
690: struct object rob;
691: rob.tag = Snull;
692: if (cc1 == NULL) {
693: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
694: if (cc1 == NULL) {
695: errorOxmisc2("KoxLog(): no more memory.");
696: return(rob);
697: }
698: oxInitClient(cc1);
699: }
700:
701: if (oxObjectToClient(client,cc1) == -1) return(rob);
702: if (cc1 == NULL) {
703: errorOxmisc2("KoxLog(): the first argument must be a client object.");
704: return(rob);
705: }
706: return(KpoInteger(fp2stopLog(cc1->datafp2)));
707: }
1.1 maekawa 708:
709: struct object KoxCloseClient(struct object client) {
710: oxclientp cc1 = NULL;
711: oxclient cc;
712: struct object rob;
713: rob.tag = Snull;
714: cc1 = &cc;
715: if (oxObjectToClient(client,cc1) == -1) return(rob);
716: if (cc1 == NULL) {
717: errorOxmisc2("KoxCloseClient(): the first argument must be a client object.");
718: return(rob);
719: }
720:
721: fp2fflush(cc1->datafp2);
722: if (cc1->humanio) {
723: /* Do not close the file. */
724: return(KpoInteger(0));
725: }
726: switch (cc1->type) {
727: case CLIENT_SOCKET:
728: fp2fclose(cc1->datafp2);
729: close(cc1->controlfd);
730: break;
731: case CLIENT_FILE:
732: fp2fclose(cc1->datafp2);
733: close(cc1->controlfd);
734: break;
735: default:
736: errorOxmisc2("Unknown client->type\n");
737: break;
738: }
1.14 takayama 739: oxClientListRemove(client);
1.1 maekawa 740: return(KpoInteger(0));
741:
742: }
743:
744: static int cmoCheck00(struct object obj,int cmo[], int n) {
745: int i,j,m;
746: int ttt;
747: #define CHECK00_N 4098 /* look up stackm.h and kclass.h */
748: static int typeTrans[CHECK00_N];
749: static int init = 0;
1.5 takayama 750: /* if n == 0, report the cmo tag of the object obj.
1.12 takayama 751: If it cannot be translated to cmo, then return -1. */
1.5 takayama 752:
1.1 maekawa 753: if (!init) {
754: for (i=0; i<CHECK00_N; i++) {
755: typeTrans[i] = 0; /* unknown cmo number */
756: }
757: typeTrans[Snull] = CMO_NULL;
758: typeTrans[Sinteger] = CMO_INT32;
759: typeTrans[Sdollar] = CMO_STRING;
760: if (OxVersion >= 199907170) {
761: typeTrans[SuniversalNumber] = CMO_ZZ;
762: }else{
763: typeTrans[SuniversalNumber] = CMO_ZZ_OLD;
764: }
765: typeTrans[Sarray] = CMO_LIST;
766: /* typeTrans[Spoly] = CMO_DMS; */
767: typeTrans[Spoly] = CMO_DISTRIBUTED_POLYNOMIAL;
768: typeTrans[Sdouble] = CMO_64BIT_MACHINE_DOUBLE;
1.20 ! takayama 769: typeTrans[SrationalFunction] = CMO_RATIONAL;
1.1 maekawa 770: typeTrans[CLASSNAME_ERROR_PACKET] = CMO_ERROR2;
771: typeTrans[CLASSNAME_mathcap] = CMO_MATHCAP;
772: typeTrans[CLASSNAME_indeterminate] = CMO_INDETERMINATE;
773: typeTrans[CLASSNAME_tree] = CMO_TREE;
774: typeTrans[CLASSNAME_recursivePolynomial] = CMO_RECURSIVE_POLYNOMIAL;
775: typeTrans[CLASSNAME_polynomialInOneVariable] = CMO_POLYNOMIAL_IN_ONE_VARIABLE;
776: init = 1;
777: }
778: ttt = typeTrans[obj.tag];
779: if (obj.tag == Sclass) {
780: ttt = typeTrans[ectag(obj)];
781: }
1.5 takayama 782: /* Only report the cmo tag. */
783: if (n == 0) {
1.12 takayama 784: if (ttt == 0) return(-1);
785: else return(ttt);
1.5 takayama 786: }
1.1 maekawa 787:
788: for (i=0; i<n; i++) {
789: if (ttt == cmo[i]) {
790: if (ttt != CMO_LIST) return(1);
791: else {
1.12 takayama 792: m = getoaSize(obj);
793: for (j=0; j<m; j++) {
794: if (!cmoCheck00(getoa(obj,j),cmo,n)) return(0);
795: }
796: return(1);
1.1 maekawa 797: }
798: }
799: }
800: if (DebugMathCap) {
801: if (DebugMathCap && 1) {
802: fprintf(stderr,"Type translation table (internal object tag --> CMO tag)\n");
803: for (i=0; i<20; i++) {
1.12 takayama 804: printf("%d ", typeTrans[i]);
1.1 maekawa 805: }
806: printf("\n");
807: }
808: fprintf(stderr,"The type of the argument object in sm1 is %d.\n",obj.tag);
809: fprintf(stderr,"The type of the argument object in CMO is %d.\n",ttt);
810: fprintf(stderr,"Available CMO tags in mathcap= %d elements : [ ",n);
811: for (i=0; i<n; i++) {
812: fprintf(stderr," %d ",cmo[i]);
813: }
814: fprintf(stderr," ] \n");
815: }
816: return(0);
817: }
818:
819: int cmoCheckMathCap(struct object obj, struct object *obp)
820: {
821: struct object mathcap;
822: struct object cmolist;
1.2 takayama 823: struct object mathcapMain;
1.4 takayama 824: struct object mathcapThird;
825: struct object ox;
826: struct object oxtag;
1.2 takayama 827: struct object ob0;
1.4 takayama 828: int oxsize;
1.1 maekawa 829: int n;
830: int i;
831: #define CMO_CHECK_MATH_CAP_LIST_SIZE 1024
832: int cmo[CMO_CHECK_MATH_CAP_LIST_SIZE];
833: if (obp == NULL) return(1);
1.2 takayama 834: /* printObject(*obp,0,stderr); for debug*/
1.1 maekawa 835: if (obp->tag != Sarray) {
836: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
837: printObject(*obp,0,stderr);
838: fprintf(stderr,"\n");
839: errorOxmisc2("cmoCheckMathCap: format error in the client->mathcapObjp field.\n");
840: }
841: mathcap = *obp;
1.2 takayama 842: /* Example of mathcap
1.12 takayama 843: [ $mathcap-object$ ,
844: [ [ 199909080 , $Ox_system=ox_sm1.plain$ , $Version=2.991106$ ,
845: $HOSTTYPE=i386$ ] ,
846: [ 262 , 263 , 264 , 265 , 266 , 268 , 269 , 272 , 273 , 275 ,
847: 276 ] ,
848: [ [ 514 , [ 2130706434 , 1 , 2 , 4 , 5 , 17 , 19 , 20 , 22 , 23 , 24 , 25 , 26 , 30 , 31 , 60 , 61 , 27 , 33 , 40 , 16 , 34 ] ] ] ] ]
1.2 takayama 849: */
850:
1.1 maekawa 851: n = getoaSize(mathcap);
852: if (n < 2) {
853: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
854: printObject(*obp,0,stderr);
855: fprintf(stderr,"\n");
856: errorOxmisc2("cmoCheckMathCap: length of mathcap is wrong in the client->mathcapObjp field.\n");
857: }
1.2 takayama 858: ob0 = getoa(mathcap,0);
859: if (ob0.tag != Sdollar) {
860: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
861: printObject(*obp,0,stderr);
862: fprintf(stderr,"\n");
863: errorOxmisc2("cmoCheckMathCap: The first field must be the string mathcap-object.\n");
864: }
865: if (strcmp(KopString(ob0),"mathcap-object") != 0) {
866: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
867: printObject(*obp,0,stderr);
868: fprintf(stderr,"\n");
869: errorOxmisc2("cmoCheckMathCap: The mathcap must be of the form [(mathcap-object) [...]]\n");
870: }
871:
1.1 maekawa 872: /* I should check
1.12 takayama 873: getoa(getoa(mathcap,1),2)
1.1 maekawa 874: contains OX_DATA.
875: It has not yet implemented.
876: */
1.2 takayama 877: mathcapMain = getoa(mathcap,1);
878: if (mathcapMain.tag != Sarray) {
879: fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
880: printObject(mathcapMain,0,stderr);
881: fprintf(stderr,"\n");
882: errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array.\n");
883: }
884: if (getoaSize(mathcapMain) < 3) {
885: fprintf(stderr,"cmoCheckMathCap: mathcap[1] is \n");
886: printObject(mathcapMain,0,stderr);
887: fprintf(stderr,"\n");
888: errorOxmisc2("cmoCheckMathCap: format error in the (client->mathcapObjp)[1] field. It should be an array of which length is more than 2.\n");
889: }
1.4 takayama 890: mathcapThird = getoa(mathcapMain,2);
891: oxsize = getoaSize(mathcapThird);
892: for (i=0; i<oxsize; i++) {
893: ox = getoa(mathcapThird,i);
894: if (ox.tag != Sarray) {
895: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
896: printObject(*obp,0,stderr);
897: fprintf(stderr,"\n");
898: errorOxmisc2("cmoCheckMathCap: the third element of mathcap is a list of lists.");
899: }
900: if (getoaSize(ox) != 0) {
901: oxtag = getoa(ox,0);
902: if (oxtag.tag != Sinteger) {
1.12 takayama 903: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
904: printObject(*obp,0,stderr);
905: fprintf(stderr,"\n");
906: errorOxmisc2("cmoCheckMathCap: the third element of mathcap must be [OX_DATA_xxx, [ ]].");
1.4 takayama 907: }
908: if (KopInteger(oxtag) == OX_DATA) {
1.12 takayama 909: if (getoaSize(ox) > 1) {
910: cmolist = getoa(ox,1);
911: if (cmolist.tag != Sarray) {
912: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
913: printObject(*obp,0,stderr);
914: fprintf(stderr,"\n");
915: errorOxmisc2("cmoCheckMathCap: mathcap[1] must be an array of integers.\n");
916: }
917: n = getoaSize(cmolist);
918: if (n > CMO_CHECK_MATH_CAP_LIST_SIZE) {
919: errorOxmisc2("cmoCheckMathCap: Too big cmo list.\n");
920: }
921: for (i=0; i<n; i++) {
922: cmo[i] = KopInteger(getoa(cmolist,i));
923: }
924: }else{
925: fprintf(stderr,"cmoCheckMathCap: the mathcap obj is \n");
926: printObject(*obp,0,stderr);
927: fprintf(stderr,"\nox=");
928: printObject(ox,0,stderr);
929: errorOxmisc2("cmoCheckMathCap: [OX_DATA, cmolist]");
930: }
1.4 takayama 931: }
932: }
1.1 maekawa 933: }
934: return(cmoCheck00(obj,cmo,n));
935: }
936:
1.12 takayama 937:
1.1 maekawa 938: struct object KoxGenPortFile(void) {
939: struct object ob;
940: ob = KpoString(oxGenPortFile());
941: return(ob);
942: }
943: void KoxRemovePortFile(void) {
944: oxRemovePortFile();
945: }
946:
947: void oxPushMathCap(struct mathCap *mathcap)
948: {
949: struct object rob;
950: rob = newMathCap(mathcap);
951: Kpush(rob);
952: }
953:
954: struct object KoxGenPass(void) {
955: struct object rob;
956: rob = KpoString(oxGenPass());
957: return(rob);
958: }
959:
960: struct object KoxGetPort(struct object host)
961: {
962: struct object rob;
963: int fdStream, fdControl;
964: int portStream, portControl;
965: extern int OpenedSocket;
966: char *sname;
967: rob = NullObject;
968: if (host.tag != Sdollar) {
969: errorOxmisc2("KoxGetPort: argument is not a string.");
970: return(rob);
971: }
972: sname = KopString(host);
973: fdControl = socketOpen(sname,0);
974: portControl = OpenedSocket;
975: fdStream = socketOpen(sname,0);
976: portStream = OpenedSocket;
977: rob = newObjectArray(4);
978: putoa(rob,0,KpoInteger(fdStream));
979: putoa(rob,1,KpoInteger(portStream));
980: putoa(rob,2,KpoInteger(fdControl));
981: putoa(rob,3,KpoInteger(portControl));
1.10 takayama 982: return(rob);
983: }
984: struct object KoxGetPort1(struct object host)
985: {
986: struct object rob;
987: int fdStream;
988: int portStream;
989: extern int OpenedSocket;
990: char *sname;
991: rob = NullObject;
992: if (host.tag != Sdollar) {
993: errorOxmisc2("KoxGetPort1: argument is not a string.");
994: return(rob);
995: }
996: sname = KopString(host);
997: fdStream = socketOpen(sname,0);
998: portStream = OpenedSocket;
999: rob = newObjectArray(2);
1000: putoa(rob,0,KpoInteger(fdStream));
1001: putoa(rob,1,KpoInteger(portStream));
1.1 maekawa 1002: return(rob);
1003: }
1004:
1005: struct object KoxCreateClient2(struct object peer,
1.12 takayama 1006: struct object ipmask,
1007: struct object pass)
1.1 maekawa 1008: {
1009: struct object rob;
1010: oxclientp client;
1011: int fdStream, portStream, fdControl, portControl;
1012: int i;
1013: struct object ob1;
1014: rob.tag = Snull;
1015: if (peer.tag != Sarray) {
1016: errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl]");
1017: return(rob);
1018: }
1019: if (getoaSize(peer) != 4) {
1020: errorOxmisc2("KoxCreateClient2(): The first argument must be an array [fdStream, portStream, fdControl, portControl] of size 4.");
1021: return(rob);
1022: }
1023: for (i=0; i<4; i++) {
1024: ob1 = getoa(peer,i);
1025: if (ob1.tag != Sinteger) {
1026: errorOxmisc2("KoxCreateClient2(): The element of the first argument must be an integer.");
1027: }
1028: }
1029: fdStream = KopInteger(getoa(peer,0));
1030: portStream = KopInteger(getoa(peer,1));
1031: fdControl = KopInteger(getoa(peer,2));
1032: portControl = KopInteger(getoa(peer,3));
1033:
1034: if (ipmask.tag != Sinteger) {
1035: errorOxmisc2("KoxCreateClient2(): ipmask must be an integer.");
1036: }
1037: if (pass.tag != Sdollar) {
1038: errorOxmisc2("KoxCreateClient2(): pass must be a string.");
1039: }
1040:
1041: client = oxCreateClient2(fdStream, portStream, fdControl, portControl,
1.12 takayama 1042: KopInteger(ipmask), KopString(pass));
1.1 maekawa 1043: if (client == NULL) {
1044: errorOxmisc2("KoxCreateClient2(): Open error.");
1045: return(rob);
1046: }
1047: rob = newObjectArray(N_OF_CLIENT_FIELDS);
1048: oxClientToObject(client,rob);
1.14 takayama 1049: oxClientListUpdate(rob);
1.1 maekawa 1050: return(rob);
1.5 takayama 1051: }
1052:
1053: int KgetCmoTagOfObject(struct object obj) {
1054: int k;
1055: k=cmoCheck00(obj,(int *)NULL,0);
1056: return(k);
1.1 maekawa 1057: }
1058:
1059: errorOxmisc2(char *s) {
1.6 takayama 1060: SET_MYERROROUT;
1.1 maekawa 1061: fprintf(MyErrorOut,"error in oxmisc2.c: %s\n",s);
1062: errorKan1("%s\n"," ");
1.7 takayama 1063: }
1064:
1065: struct object KoxPushCMD(struct object client,struct object cmd) {
1066: int ans;
1067: static oxclientp cc1 = NULL;
1068: struct object rob;
1069: rob.tag = Snull;
1070: if (cc1 == NULL) {
1071: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
1072: if (cc1 == NULL) {
1073: errorOxmisc2("KoxReq(): no more memory.");
1074: return(rob);
1075: }
1076: oxInitClient(cc1); /* BUG: is it fine? */
1077: }
1078:
1079: if (oxObjectToClient(client,cc1) == -1) return(rob);
1080: if (cc1 == NULL) {
1081: errorOxmisc2("KoxReq(): the first argument must be a client object.");
1082: return(rob);
1083: }
1084: if (cmd.tag != Sinteger) {
1085: errorOxmisc2("KoxReq(): the second argument must be an integer.");
1086: return(rob);
1087: }
1088: /* BUG: check the mathcap */
1089: oxSendOXheader(cc1->datafp2,OX_COMMAND,SerialOX++);
1090: oxSendInt32(cc1->datafp2,KopInteger(cmd));
1091: /* synchronize cc1 and client. */
1092: oxClientToObject(cc1,client);
1093: return(cmd);
1094: }
1095:
1096: struct object KoxPushCMO(struct object client,struct object ob) {
1097: int ans;
1098: static oxclientp cc1 = NULL;
1099: struct object rob;
1100: rob.tag = Snull;
1101: if (cc1 == NULL) {
1102: cc1 = (oxclientp) mymalloc(sizeof(oxclient));
1103: if (cc1 == NULL) {
1104: errorOxmisc2("KoxReq(): no more memory.");
1105: return(rob);
1106: }
1107: oxInitClient(cc1); /* BUG: is it fine? */
1108: }
1109:
1110: if (oxObjectToClient(client,cc1) == -1) return(rob);
1111: if (cc1 == NULL) {
1112: errorOxmisc2("KoxReq(): the first argument must be a client object.");
1113: return(rob);
1114: }
1115:
1116: /* request to the data channel */
1117: if (cc1->dstate != DSTATE_ANY) {
1118: errorOxmisc2("oxPushCMO: cc1->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
1119: return(rob);
1120: }
1121:
1122: if (!cmoCheckMathCap(ob,(struct object *)cc1->mathcapObjp)) {
1.12 takayama 1123: errorOxmisc2("oxPushCMO: your peer does not understand this cmo.\n");
1124: return(rob);
1.7 takayama 1125: }
1126: oxSendOXheader(cc1->datafp2,OX_DATA,SerialOX++);
1127: cmoObjectToStream2(ob,cc1->datafp2);
1128: /* synchronize cc1 and client. */
1129: oxClientToObject(cc1,client);
1130: return(ob);
1.8 takayama 1131: }
1132:
1133: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12 takayama 1134: int ipmask,char *pass);
1.8 takayama 1135: struct object KoxCreateControl_RFC_101(struct object peer,struct object ipmask,struct object pass)
1136: {
1137: struct object rob;
1138: oxclientp client;
1139: int fdStream, portStream;
1140: int i;
1141: struct object ob1;
1142: rob.tag = Snull;
1143: if (peer.tag != Sarray) {
1144: errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream]");
1145: return(rob);
1146: }
1147: if (getoaSize(peer) != 2 ) {
1148: errorOxmisc2("KoxCreateControl_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
1149: return(rob);
1150: }
1151: for (i=0; i<getoaSize(peer); i++) {
1152: ob1 = getoa(peer,i);
1153: if (ob1.tag != Sinteger) {
1154: errorOxmisc2("KoxCreateControl_RFC_101(): The element of the first argument must be an integer.");
1155: }
1156: }
1157: fdStream = KopInteger(getoa(peer,0));
1158: portStream = KopInteger(getoa(peer,1));
1159:
1160: if (ipmask.tag != Sinteger) {
1161: errorOxmisc2("KoxCreateControl_RFC_101(): ipmask must be an integer.");
1162: }
1163: if (pass.tag != Sdollar) {
1164: errorOxmisc2("KoxCreateControl_RFC_101(): pass must be a string.");
1165: }
1166:
1167: client = oxCreateControl_RFC_101(fdStream, portStream,
1.12 takayama 1168: KopInteger(ipmask), KopString(pass));
1.8 takayama 1169: if (client == NULL) {
1170: errorOxmisc2("KoxCreateControl_RFC_101(): Open error.");
1171: return(rob);
1172: }
1173: rob = newObjectArray(N_OF_CLIENT_FIELDS);
1174: oxClientToObject(client,rob);
1175: return(rob);
1176: }
1177:
1178: oxclientp oxCreateControl_RFC_101(int fdstream,int portStream,
1.12 takayama 1179: int ipmask,char *pass)
1.8 takayama 1180: {
1181: int v = 0;
1182: int fdControl = -1;
1183: int fdStream = -1;
1184: int m;
1185:
1186: char *s;
1187: oxclientp client;
1.13 takayama 1188: #if defined(__CYGWIN__)
1189: extern sigjmp_buf MyEnv_oxmisc;
1190: #else
1191: extern jmp_buf MyEnv_oxmisc;
1192: #endif
1.8 takayama 1193: int engineByteOrder;
1194: extern int Quiet;
1195:
1196: v = !Quiet;
1197:
1198: switch(ipmask) {
1199: case 0:/* only local */
1200: fdStream = socketAcceptLocal(fdstream);
1201: break;
1202: default:/* any */
1203: fdStream = socketAccept(fdstream);
1204: break;
1205: }
1206: if (v) fprintf(stderr,"\nControl port %d : Connected.\n",portStream);
1207:
1208: if (fdStream == -1 ) {
1209: fprintf(stderr,"\nOpen error in oxCreateControl_RFC_101.\n");
1210: return(NULL);
1211: }
1212:
1213: /* Authentication by password. */
1214: m = strlen(pass);
1215: s = (char *)mymalloc(sizeof(char)*(m+1));
1216: read(fdStream,s,m+1); s[m] = '\0';
1217: if (strcmp(s,pass) != 0) {
1218: fprintf(stderr,"oxCreateControl_RFC_101(): password authentication failed for control channel.\n");
1219: close(fdStream);
1220: return(NULL);
1221: }
1222:
1223:
1224: engineByteOrder = oxSetByteOrder(fdStream);
1225: if (v) fprintf(stderr,"Byte order for control stackmacine is %s.\n",
1.12 takayama 1226: (engineByteOrder == 0? "network byte order":
1227: (engineByteOrder == 1? "little indican":
1228: "big indian")));
1.8 takayama 1229:
1230:
1231: client = (oxclientp) mymalloc(sizeof(oxclient));
1232: oxInitClient(client);
1233: client->datafp2 = fp2open(fdStream);
1234: if (client->datafp2 == NULL) {
1235: fprintf(stderr,"oxCreateControl_RFC_101(): fp2open(fd) failed.\n");
1236: return(NULL);
1237: }
1238: client->dataport = portStream;
1239: client->controlport = -1;
1240: client->controlfd = -1;
1.14 takayama 1241: client->id = oxGetClientID();
1.11 takayama 1242: client->type = CLIENT_SOCKET; /* socket */
1243: client->engineByteOrder = engineByteOrder;
1244: client->controlByteOrder = -1;
1245: return(client);
1246: }
1247:
1248: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12 takayama 1249: int ipmask,char *pass, int engineID);
1.11 takayama 1250: struct object KoxCreateEngine_RFC_101(struct object peer,struct object ipmask,struct object pass, struct object engineID)
1251: {
1252: struct object rob;
1253: oxclientp client;
1254: int fdStream, portStream;
1255: int i;
1256: struct object ob1;
1257: rob.tag = Snull;
1258: if (peer.tag != Sarray) {
1259: errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream]");
1260: return(rob);
1261: }
1262: if (getoaSize(peer) != 2 ) {
1263: errorOxmisc2("KoxCreateEngine_RFC_101(): The first argument must be an array [fdStream, portStream] of size 2.");
1264: return(rob);
1265: }
1266: for (i=0; i<getoaSize(peer); i++) {
1267: ob1 = getoa(peer,i);
1268: if (ob1.tag != Sinteger) {
1269: errorOxmisc2("KoxCreateEngine_RFC_101(): The element of the first argument must be an integer.");
1270: }
1271: }
1272: fdStream = KopInteger(getoa(peer,0));
1273: portStream = KopInteger(getoa(peer,1));
1274:
1275: if (ipmask.tag != Sinteger) {
1276: errorOxmisc2("KoxCreateEngine_RFC_101(): ipmask must be an integer.");
1277: }
1278: if (pass.tag != Sdollar) {
1279: errorOxmisc2("KoxCreateEngine_RFC_101(): pass must be a string.");
1280: }
1281: if (engineID.tag != Sinteger) {
1282: errorOxmisc2("KoxCreateEngine_RFC_101(): engineID must be an integer.");
1283: }
1284:
1285: client = oxCreateEngine_RFC_101(fdStream, portStream,
1.12 takayama 1286: KopInteger(ipmask), KopString(pass),KopInteger(engineID));
1.11 takayama 1287: if (client == NULL) {
1288: errorOxmisc2("KoxCreateEngine_RFC_101(): Open error.");
1289: return(rob);
1290: }
1291: rob = newObjectArray(N_OF_CLIENT_FIELDS);
1292: oxClientToObject(client,rob);
1.14 takayama 1293: oxClientListUpdate(rob);
1.11 takayama 1294: return(rob);
1295: }
1296:
1297: oxclientp oxCreateEngine_RFC_101(int fdstream,int portStream,
1.12 takayama 1298: int ipmask,char *pass,int engineID)
1.11 takayama 1299: {
1300: int v = 0;
1301: int fdControl = -1;
1302: int fdStream = -1;
1303: int m;
1304:
1305: char *s;
1306: oxclientp client;
1.13 takayama 1307: #if defined(__CYGWIN__)
1308: extern sigjmp_buf MyEnv_oxmisc;
1309: #else
1310: extern jmp_buf MyEnv_oxmisc;
1311: #endif
1.11 takayama 1312: int engineByteOrder;
1313: extern int Quiet;
1314:
1315: v = !Quiet;
1316:
1317: switch(ipmask) {
1318: case 0:/* only local */
1319: fdStream = socketAcceptLocal(fdstream);
1320: break;
1321: default:/* any */
1322: fdStream = socketAccept(fdstream);
1323: break;
1324: }
1325: if (v) fprintf(stderr,"\nEngine port %d : Connected.\n",portStream);
1326:
1327: if (fdStream == -1 ) {
1328: fprintf(stderr,"\nOpen error in oxCreateEngine_RFC_101.\n");
1329: return(NULL);
1330: }
1331:
1332: /* Authentication by password. */
1333: /* skip password check for now. BUG.
1.12 takayama 1334: m = strlen(pass);
1335: s = (char *)mymalloc(sizeof(char)*(m+1));
1336: read(fdStream,s,m+1); s[m] = '\0';
1337: if (strcmp(s,pass) != 0) {
1338: fprintf(stderr,"oxCreateEngine_RFC_101(): password authentication failed for control channel.\n");
1339: close(fdStream);
1340: return(NULL);
1341: }
1.11 takayama 1342: */
1343:
1344: engineByteOrder = oxSetByteOrder(fdStream);
1345: if (v) fprintf(stderr,"Byte order for engine stackmacine is %s.\n",
1.12 takayama 1346: (engineByteOrder == 0? "network byte order":
1347: (engineByteOrder == 1? "little indican":
1348: "big indian")));
1.11 takayama 1349:
1350:
1351: client = (oxclientp) mymalloc(sizeof(oxclient));
1352: oxInitClient(client);
1353: client->datafp2 = fp2open(fdStream);
1354: if (client->datafp2 == NULL) {
1355: fprintf(stderr,"oxCreateEngine_RFC_101(): fp2open(fd) failed.\n");
1356: return(NULL);
1357: }
1358: client->dataport = portStream;
1359: client->controlport = -1;
1360: client->controlfd = -1;
1.16 takayama 1361: client->id = oxGetClientID();
1362: client->engineID = engineID;
1.8 takayama 1363: client->type = CLIENT_SOCKET; /* socket */
1364: client->engineByteOrder = engineByteOrder;
1365: client->controlByteOrder = -1;
1366: return(client);
1.1 maekawa 1367: }
1.14 takayama 1368:
1369: void oxClientListUpdate(struct object ob) {
1370: int id;
1371: extern struct object OxClientList[];
1372: id = KopInteger(getoa(ob,8));
1373: /* printf("id=%d\n",id); */
1374: if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
1375: OxClientList[id] = ob;
1376: }else{
1377: errorOxmisc2("oxClientListUpdate(): the client table is full.\n");
1378: }
1379: }
1380: void oxClientListRemove(struct object ob) {
1381: int id;
1382: extern struct object OxClientList[];
1383: id = KopInteger(getoa(ob,8));
1384: if ((id <MAX_N_OF_CLIENT) && (id >= 0)) {
1385: (OxClientList[id]).tag = Snull;
1386: }else{
1387: /* errorOxmisc2("oxClientListRemove(): the client table is full.\n");*/
1388: }
1389: }
1.17 takayama 1390: static void KoxCleanClientList() {
1391: extern int OxClientListn;
1392: extern struct object OxClientList[];
1393: int i,j,n;
1394: struct object ob;
1395: n = 0;
1396: for (i=0; i<OxClientListn; i++) {
1397: if ((OxClientList[i]).tag != Snull) {
1398: if (!isItClientObject(OxClientList[i])) {
1399: (OxClientList[i]).tag = Snull;
1400: }
1401: }
1402: }
1403: }
1.14 takayama 1404: struct object KoxGetClientList() {
1405: extern int OxClientListn;
1406: extern struct object OxClientList[];
1407: int i,j,n;
1408: struct object rob;
1.17 takayama 1409: KoxCleanClientList();
1.14 takayama 1410: n = 0;
1411: for (i=0; i<OxClientListn; i++) {
1412: if ((OxClientList[i]).tag != Snull) n++;
1413: }
1414: rob = newObjectArray(n);
1415: for (i=0, j=0; i<OxClientListn; i++) {
1416: if ((OxClientList[i]).tag != Snull) {
1417: if (j >= n) {
1418: j=0;
1419: errorOxmisc2("oxGetClientList(): the client table is broken.\n");
1420: }
1421: putoa(rob,j,OxClientList[i]);
1422: j++;
1423: }
1424: }
1425: return rob;
1426: }
1427:
1428:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>