[BACK]Return to oxmisc2.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / plugin / Old

Annotation of OpenXM/src/kan96xx/plugin/Old/oxmisc2.c, Revision 1.1.1.1

1.1       maekawa     1: #include <stdio.h>
                      2: #include "ox_kan.h"
                      3: #include "oxmisc2.h"   /* This file requires sm1 object description. */
                      4: extern FILE *MyErrorOut;
                      5:
                      6: int oxGet(oxclientp client, struct object *op,int *isObj)
                      7: /* This method should be synchronized. */
                      8: /* oxGet is a function for client. */
                      9: {
                     10:   int ans;
                     11:   ox_stream os;
                     12:   int m;
                     13:   struct object rob;
                     14:   *isObj = 0;
                     15:   op->tag = Snull;
                     16:   os = client->datafp2;
                     17:   switch(client->dstate) {
                     18:   case DSTATE_ANY:
                     19:     m = oxGetInt32(os);
                     20:     switch(m) {
                     21:     case OX_DATA:
                     22:       client->dstate = DSTATE_WAIT_OX_DATA;
                     23:       return(oxGet(client,op,isObj));
                     24:     case OX_SYNC_BALL:
                     25:       client->dstate = DSTATE_ANY;
                     26:       return(OX_SYNC_BALL);
                     27:     default:
                     28:       errorOxmisc2("oxGet: cannot handle this tag.\n");
                     29:       client->dstate = DSTATE_ERROR;
                     30:       return(-1);
                     31:     }
                     32:     break;
                     33:   case DSTATE_FIRST_SYNC:  /* waiting the first sync ball */
                     34:     /* I need to clear the buffer?? */
                     35:     oxWaitSyncBall(os);
                     36:     client->dstate = DSTATE_ANY;
                     37:     oxSendSyncBall(os);
                     38:     return(OX_SYNC_BALL);
                     39:     break;
                     40:   case DSTATE_WAIT_OX_DATA:  /* waiting a cmo data. */
                     41:     *op = cmoObjectFromStream2(client->datafp2);
                     42:     client->dstate = DSTATE_ANY;
                     43:     *isObj = 1;
                     44:     return(0);
                     45:     break;
                     46:   case DSTATE_ERROR:
                     47:     client->dstate = DSTATE_ERROR;
                     48:     errorOxmisc2("oxGet: dstate == DSTATE_ERROR (error state)\n");
                     49:     return(-1);
                     50:   default:
                     51:     client->dstate = DSTATE_ERROR;
                     52:     errorOxmisc2("oxGet: Unknown state number.");
                     53:   }
                     54:
                     55:   return(-1);
                     56: }
                     57:
                     58: int oxGetFromControl(oxclientp client)
                     59: {
                     60:   int ans = -1;
                     61:   if (client->cstate != -1) {
                     62:     ans = oxGetResultOfControlInt32(client->controlfd);
                     63:     if (ans != -1) {    client->cstate = 0; }
                     64:     else {client->cstate = -1; }
                     65:   }
                     66:   return(ans);
                     67: }
                     68:
                     69: int oxReq(oxclientp client,int func,struct object ob)
                     70: {
                     71:   /* request to the control channel */
                     72:   if (func == SM_control_reset_connection ||
                     73:       func == SM_control_kill) {
                     74:     switch(func) {
                     75:     case SM_control_reset_connection:
                     76:       oxReqControlResetConnection(client->controlfd);
                     77:       client->cstate = 1;
                     78:       client->dstate = DSTATE_FIRST_SYNC;
                     79:       break;
                     80:     case SM_control_kill:
                     81:       oxReqControlKill(client->controlfd);
                     82:       client->cstate = 0;
                     83:       client->dstate = DSTATE_ANY;
                     84:       break;
                     85:     }
                     86:     fflush(NULL);
                     87:     return(0);
                     88:   }
                     89:
                     90:   /* request to the data channel */
                     91:   if (client->dstate != DSTATE_ANY) {
                     92:     errorOxmisc2("oxReq: client->dstate != DSTATE_ANY, data channel is not ready to send data.\n");
                     93:     return(-1);
                     94:   }
                     95:   switch(func) {
                     96:   case SM_DUMMY_sendcmo:
                     97:     oxSendInt32(client->datafp2,OX_DATA);
                     98:     cmoObjectToStream2(ob,client->datafp2);
                     99:     client->dstate = DSTATE_ANY;
                    100:     break;
                    101:   case SM_sync_ball:
                    102:     oxSendSyncBall(client->datafp2);
                    103:     client->dstate = DSTATE_ANY; /*  We do not expect the sync ball.*/
                    104:     client->cstate = 0;          /* clear the cstate */
                    105:     break;
                    106:   case SM_popCMO:
                    107:     oxReqPopCMO(client->datafp2);
                    108:     client->dstate = DSTATE_ANY;
                    109:     break;
                    110:   case SM_mathcap:
                    111:     oxReqMathCap(client->datafp2);
                    112:     client->dstate = DSTATE_ANY;
                    113:     break;
                    114:   case SM_pops:
                    115:     if (ob.tag != Sinteger) {
                    116:       errorOxmisc2("SM_pops : the argument must be an integer.");
                    117:       return(-1);
                    118:     }
                    119:     oxReqPops(client->datafp2, KopInteger(ob));
                    120:     client->dstate = DSTATE_ANY;
                    121:     break;
                    122:   case SM_executeStringByLocalParser:
                    123:     if (ob.tag != Sdollar) {
                    124:       errorOxmisc2("SM_executeStringByLocalParser : the argument must be a string.");
                    125:       return(-1);
                    126:     }
                    127:     oxReqExecuteStringByLocalParser(client->datafp2,KopString(ob));
                    128:     client->dstate = DSTATE_ANY;
                    129:     break;
                    130:   case SM_executeFunction:
                    131:     if (ob.tag != Sdollar) {
                    132:       errorOxmisc2("SM_executeFunction : the argument must be a string.");
                    133:       return(-1);
                    134:     }
                    135:     oxReqExecuteFunction(client->datafp2,KopString(ob));
                    136:     client->dstate = DSTATE_ANY;
                    137:     break;
                    138:   case SM_popString:
                    139:     oxReqPopString(client->datafp2);
                    140:     client->dstate = DSTATE_ANY;
                    141:     break;
                    142:   case SM_evalName:
                    143:     if (ob.tag != Sdollar) {
                    144:       errorOxmisc2("SM_evalName : the argument must be a string.");
                    145:       return(-1);
                    146:     }
                    147:     oxReqEvalName(client->datafp2,KopString(ob));
                    148:     client->dstate = DSTATE_ANY;
                    149:     break;
                    150:   case SM_setName:
                    151:     if (ob.tag != Sdollar) {
                    152:       errorOxmisc2("SM_setName : the argument must be a string.");
                    153:       return(-1);
                    154:     }
                    155:     oxReqSetName(client->datafp2,KopString(ob));
                    156:     client->dstate = DSTATE_ANY;
                    157:     break;
                    158:   default:
                    159:     fprintf(MyErrorOut,"func=%d ",func);
                    160:     errorOxmisc2("This function is not implemented.");
                    161:     break;
                    162:   }
                    163:   fp2fflush(client->datafp2);
                    164:   return(0);
                    165: }
                    166:
                    167: struct object KoxCreateClient(struct object ip,
                    168:                              struct object portStream,
                    169:                              struct object portControl)
                    170: {
                    171:   struct object rob;
                    172:   oxclientp client;
                    173:   rob.tag = Snull;
                    174:   if (ip.tag != Sdollar) {
                    175:     errorOxmisc2("KoxCreateClient(): The first argument must be a hostname given by a string.");
                    176:     return(rob);
                    177:   }
                    178:   if (portStream.tag == Sdollar) {
                    179:     client = oxCreateClientFile(KopString(ip),KopString(portStream),
                    180:                                "/dev/null","w");
                    181:     if (client == NULL) {
                    182:       errorOxmisc2("KoxCreateClient(): Open error.");
                    183:       return(rob);
                    184:     }
                    185:     rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    186:     oxClientToObject(client,rob);
                    187:     return(rob);
                    188:   }
                    189:
                    190:   if (portStream.tag != Sinteger) {
                    191:     errorOxmisc2("KoxCreateClient(): The second argument must be a port number given in an integer.");
                    192:     return(rob);
                    193:   }
                    194:   if (portControl.tag != Sinteger) {
                    195:     errorOxmisc2("KoxCreateClient(): The third argument must be a port number given in an integer.");
                    196:     return(rob);
                    197:   }
                    198:   client = oxCreateClient(KopString(ip),KopInteger(portStream),KopInteger(portControl));
                    199:   if (client == NULL) {
                    200:     errorOxmisc2("KoxCreateClient(): Open error.");
                    201:     return(rob);
                    202:   }
                    203:   rob = newObjectArray(N_OF_CLIENT_FIELDS);
                    204:   oxClientToObject(client,rob);
                    205:   return(rob);
                    206: }
                    207:
                    208: static int isItClientObject(struct object ob)
                    209: {
                    210:   int size,i;
                    211:   struct object ee[N_OF_CLIENT_FIELDS];
                    212:   if (ob.tag != Sarray) {
                    213:     return(0);
                    214:   }
                    215:   size = getoaSize(ob);
                    216:   if (size != N_OF_CLIENT_FIELDS) return(0);
                    217:   for (i=0; i<N_OF_CLIENT_FIELDS; i++) {
                    218:     ee[i] = getoa(ob,i);
                    219:   }
                    220:
                    221:   if (ee[0].tag != Sdollar) return(0);
                    222:   if (strcmp(KopString(ee[0]),"client")!=0) return(0);
                    223:
                    224:   if (ee[1].tag != Sfile) return(0);
                    225:   if (strcmp((ee[1]).lc.str,MAGIC2) != 0) return(0);
                    226:
                    227:   for (i=2; i<N_OF_CLIENT_FIELDS; i++) {
                    228:     if (ee[i].tag != Sinteger) return(0);
                    229:   }
                    230:   return(1);
                    231: }
                    232:
                    233:
                    234: struct object KoxIsThereErrorClient(struct object ob)
                    235: {
                    236:   struct object rob;
                    237:   int ans;
                    238:   int size;
                    239:   oxclient cc;
                    240:   rob.tag = Snull;
                    241:   if (!isItClientObject(ob)) {
                    242:     errorOxmisc2("KoxIsThereErrorClient(): the argument must be an array for client object.");
                    243:     return(rob);
                    244:   }
                    245:   if (oxObjectToClient(ob,&cc) == -1) return(KpoInteger(-1));
                    246:   ans = oxIsThereErrorClient(&cc);
                    247:   return(KpoInteger(ans));
                    248: }
                    249:
                    250: int oxClientToObject(oxclientp client,struct object rob)
                    251: {
                    252:   struct object ob;
                    253:   if (client == NULL) return;
                    254:   /* rob = newObjectArray(N_OF_CLIENT_FIELDS); */
                    255:   if (rob.tag != Sarray) {
                    256:     errorOxmisc2("oxClientToObject(): the second argument must be an array.");
                    257:     return(-1);
                    258:   }
                    259:   if (getoaSize(rob) != N_OF_CLIENT_FIELDS) {
                    260:     errorOxmisc2("oxClientToObject(): the second argument must be an array of size N_OF_CLIENT_FIELDS.");
                    261:     return(-1);
                    262:   }
                    263:
                    264:   ob = KpoString("client");
                    265:   putoa(rob,0,ob);
                    266:
                    267:   ob.tag = Sfile; ob.lc.str = MAGIC2; ob.rc.voidp = (void *)(client->datafp2);
                    268:   putoa(rob,1,ob);
                    269:
                    270:   putoa(rob,2,KpoInteger(client->dataport));
                    271:   putoa(rob,3,KpoInteger(client->controlfd));
                    272:   putoa(rob,4,KpoInteger(client->controlport));
                    273:   putoa(rob,5,KpoInteger(client->dstate));
                    274:   putoa(rob,6,KpoInteger(client->cstate));
                    275:   putoa(rob,7,KpoInteger(client->humanio));
                    276:   putoa(rob,8,KpoInteger(client->id));
                    277:   putoa(rob,9,KpoInteger(client->type));
                    278:   return(0);
                    279: }
                    280:
                    281: int oxObjectToClient(struct object ob,oxclientp cp)
                    282: {
                    283:   struct object ob1;
                    284:   if (cp == NULL) {
                    285:     errorOxmisc2("oxObjectToClient(): the second argument is NULL");
                    286:     return(-1);
                    287:   }
                    288:   if (!isItClientObject(ob)) {
                    289:     errorOxmisc2("oxObjectToClient(): the first argument is not client object.");
                    290:     oxInitClient(cp);
                    291:     return(-1);
                    292:   }
                    293:
                    294:   ob1 = getoa(ob,1);
                    295:   cp->datafp2 = (FILE2 *) (ob1.rc.voidp);
                    296:
                    297:   ob1 = getoa(ob,2);
                    298:   cp->dataport = KopInteger(ob1);
                    299:
                    300:   ob1 = getoa(ob,3);
                    301:   cp->controlfd = KopInteger(ob1);
                    302:
                    303:   ob1 = getoa(ob,4);
                    304:   cp->controlport = KopInteger(ob1);
                    305:
                    306:   ob1 = getoa(ob,5);
                    307:   cp->dstate = KopInteger(ob1);
                    308:
                    309:   ob1 = getoa(ob,6);
                    310:   cp->cstate = KopInteger(ob1);
                    311:
                    312:   ob1 = getoa(ob,7);
                    313:   cp->humanio = KopInteger(ob1);
                    314:
                    315:   ob1 = getoa(ob,8);
                    316:   cp->id = KopInteger(ob1);
                    317:
                    318:   ob1 = getoa(ob,9);
                    319:   cp->type = KopInteger(ob1);
                    320:
                    321:   return(0);
                    322: }
                    323:
                    324: struct object KoxReq(struct object client,
                    325:                     struct object func,
                    326:                     struct object ob1)
                    327: {
                    328:   int ans;
                    329:   static oxclientp cc1 = NULL;
                    330:   struct object rob;
                    331:   rob.tag = Snull;
                    332:   if (cc1 == NULL) {
                    333:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    334:     if (cc1 == NULL) {
                    335:       errorOxmisc2("KoxReq(): no more memory.");
                    336:       return(rob);
                    337:     }
                    338:     oxInitClient(cc1);
                    339:   }
                    340:
                    341:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    342:   if (cc1 == NULL) {
                    343:     errorOxmisc2("KoxReq(): the first argument must be a client object.");
                    344:     return(rob);
                    345:   }
                    346:   if (func.tag != Sinteger) {
                    347:     errorOxmisc2("KoxReq(): the second argument must be an integer.");
                    348:     return(rob);
                    349:   }
                    350:   ans = oxReq(cc1,KopInteger(func),ob1);
                    351:   /* synchronize cc1 and client. */
                    352:   oxClientToObject(cc1,client);
                    353:
                    354:   return(KpoInteger(ans));
                    355: }
                    356:
                    357: struct object KoxGet(struct object client)
                    358: {
                    359:   int ans,k;
                    360:   static oxclientp cc1 = NULL;
                    361:   struct object rob;
                    362:   rob.tag = Snull;
                    363:   if (cc1 == NULL) {
                    364:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    365:     if (cc1 == NULL) {
                    366:       errorOxmisc2("KoxGet(): no more memory.");
                    367:       return(rob);
                    368:     }
                    369:     oxInitClient(cc1);
                    370:   }
                    371:
                    372:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    373:   if (cc1 == NULL) {
                    374:     errorOxmisc2("KoxGet(): the first argument must be a client object.");
                    375:     return(rob);
                    376:   }
                    377:
                    378:   ans = oxGet(cc1,&rob,&k);
                    379:   /* synchronize cc1 and client. */
                    380:   oxClientToObject(cc1,client);
                    381:
                    382:   if (k) return(rob);
                    383:   else {
                    384:     return(KpoInteger(ans));
                    385:   }
                    386: }
                    387:
                    388: struct object KoxGetFromControl(struct object client)
                    389: {
                    390:   int ans;
                    391:   static oxclientp cc1 = NULL;
                    392:   struct object rob;
                    393:   rob.tag = Snull;
                    394:   if (cc1 == NULL) {
                    395:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    396:     if (cc1 == NULL) {
                    397:       errorOxmisc2("KoxGetFromControl(): no more memory.");
                    398:       return(rob);
                    399:     }
                    400:     oxInitClient(cc1);
                    401:   }
                    402:
                    403:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    404:   if (cc1 == NULL) {
                    405:     errorOxmisc2("KoxGetFromControl(): the first argument must be a client object.");
                    406:     return(rob);
                    407:   }
                    408:
                    409:   ans = oxGetFromControl(cc1);
                    410:   /* synchronize cc1 and client. */
                    411:   oxClientToObject(cc1,client);
                    412:
                    413:   return(KpoInteger(ans));
                    414: }
                    415:
                    416: struct object KoxMultiSelect(struct object oclients,struct object t)
                    417: {
                    418:   static int first = 1;
                    419:   static int csize = 0;
                    420:   static oxclientp *clients = NULL;
                    421:   oxclientp cc1;
                    422:   struct object rob;
                    423:   int i;
                    424:   int tt;
                    425:   struct object ob1;
                    426:   struct object ob2;
                    427:   struct object ob0;
                    428:   int size;
                    429:   int ans;
                    430:   int dataready[1024];
                    431:   int controlready[1024];
                    432:
                    433:   rob.tag = Snull;
                    434:   if (oclients.tag != Sarray) {
                    435:     errorOxmisc2("KoxMultiSelect(): the first argument must be an array.");
                    436:     return(rob);
                    437:   }
                    438:   size = getoaSize(oclients);
                    439:   if (first) {
                    440:     first = 0; csize = size;
                    441:     clients = (oxclientp *)mymalloc(sizeof(oxclientp)*(size+1));
                    442:      if (clients == NULL) {
                    443:       errorOxmisc2("KoxMultiSelect(): no more memory.");
                    444:       return(rob);
                    445:     }
                    446:     for (i=0; i<size; i++) {
                    447:       clients[i] =  (oxclientp) mymalloc(sizeof(oxclient));
                    448:       if (clients[i] == NULL) {
                    449:        errorOxmisc2("KoxMultiSelect(): no more memory.");
                    450:        return(rob);
                    451:       }
                    452:       oxInitClient(clients[i]);
                    453:     }
                    454:   }
                    455:   if (csize < size)  {
                    456:     first = 1;
                    457:     return(KoxMultiSelect(oclients,t));
                    458:   }
                    459:   for (i=0; i<size; i++) {
                    460:     ob0 = getoa(oclients,i);
                    461:     if (oxObjectToClient(ob0,clients[i]) == -1) return(rob);
                    462:   }
                    463:   if (t.tag != Sinteger) {
                    464:     errorOxmisc2("KoxMultiSelect(): the second argument must be an integer.");
                    465:   }
                    466:   tt = KopInteger(t);
                    467:   ans = oxclientMultiSelect(clients,dataready,controlready,size,tt);
                    468:   /* synchronize oclients and clients. */
                    469:   for (i=0; i<size; i++) {
                    470:     ob0 = getoa(oclients,i);
                    471:     oxClientToObject(clients[i],ob0);
                    472:     putoa(oclients,i,ob0);
                    473:   }
                    474:   rob = newObjectArray(3);
                    475:   putoa(rob,0,KpoInteger(ans));
                    476:   ob1 = newObjectArray(size);
                    477:   ob2 = newObjectArray(size);
                    478:   for (i=0; i<size; i++) {
                    479:     putoa(ob1,i,KpoInteger(dataready[i]));
                    480:     putoa(ob2,i,KpoInteger(controlready[i]));
                    481:   }
                    482:   putoa(rob,1,ob1);
                    483:   putoa(rob,2,ob2);
                    484:   return(rob);
                    485: }
                    486:
                    487: struct object KoxWatch(struct object client,struct object f)
                    488: /* f is not used for now. It should be log file. */
                    489: {
                    490:   int ans,k;
                    491:   static oxclientp cc1 = NULL;
                    492:   struct object rob;
                    493:   rob.tag = Snull;
                    494:   if (cc1 == NULL) {
                    495:     cc1 = (oxclientp) mymalloc(sizeof(oxclient));
                    496:     if (cc1 == NULL) {
                    497:       errorOxmisc2("KoxWatch(): no more memory.");
                    498:       return(rob);
                    499:     }
                    500:     oxInitClient(cc1);
                    501:   }
                    502:
                    503:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    504:   if (cc1 == NULL) {
                    505:     errorOxmisc2("KoxWatch(): the first argument must be a client object.");
                    506:     return(rob);
                    507:   }
                    508:
                    509:   k = fp2watch(cc1->datafp2,stdout);
                    510:   /* synchronize cc1 and client. */
                    511:   oxClientToObject(cc1,client);
                    512:
                    513:   return(KpoInteger(ans));
                    514: }
                    515:
                    516:
                    517: struct object KoxCloseClient(struct object client) {
                    518:   oxclientp cc1 = NULL;
                    519:   oxclient cc;
                    520:   struct object rob;
                    521:   rob.tag = Snull;
                    522:   cc1  = &cc;
                    523:   if (oxObjectToClient(client,cc1) == -1) return(rob);
                    524:   if (cc1 == NULL) {
                    525:     errorOxmisc2("KoxCloseClient(): the first argument must be a client object.");
                    526:     return(rob);
                    527:   }
                    528:
                    529:   fp2fflush(cc1->datafp2);
                    530:   if (cc1->humanio) {
                    531:     /* Do not close the file. */
                    532:     return(KpoInteger(0));
                    533:   }
                    534:   switch (cc1->type) {
                    535:   case CLIENT_SOCKET:
                    536:     fp2fclose(cc1->datafp2);
                    537:     close(cc1->controlfd);
                    538:     break;
                    539:   case CLIENT_FILE:
                    540:     fp2fclose(cc1->datafp2);
                    541:     close(cc1->controlfd);
                    542:     break;
                    543:   default:
                    544:     errorOxmisc2("Unknown client->type\n");
                    545:     break;
                    546:   }
                    547:   return(KpoInteger(0));
                    548:
                    549: }
                    550:
                    551:
                    552: errorOxmisc2(char *s) {
                    553:   fprintf(MyErrorOut,"error in oxmisc2.c: %s\n",s);
                    554:   return;
                    555: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>