[BACK]Return to ox.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_toolkit

Annotation of OpenXM/src/ox_toolkit/ox.c, Revision 1.35

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.35    ! iwane       2: /* $OpenXM: OpenXM/src/ox_toolkit/ox.c,v 1.34 2007/03/14 10:30:54 ohara Exp $ */
1.1       ohara       3:
1.8       ohara       4: /*
                      5:    This module includes functions for sending/receiveng CMO's.
                      6:    Some commnets is written in Japanese by the EUC-JP coded
                      7:    character set.
1.1       ohara       8: */
                      9:
                     10: #include <stdio.h>
                     11: #include <stdlib.h>
1.20      ohara      12: #include <stdarg.h>
1.1       ohara      13: #include <string.h>
                     14: #include <unistd.h>
                     15: #include <fcntl.h>
1.5       ohara      16: #include <sys/file.h>
1.13      ohara      17: #include <time.h>
1.1       ohara      18:
                     19: #include "mysocket.h"
1.12      ohara      20: #include "ox_toolkit.h"
1.1       ohara      21: #include "parse.h"
                     22:
1.20      ohara      23: static FILE *ox_stderr = NULL;
                     24:
1.17      ohara      25: /* sorting by the value of CMO_xxx.  (for debugging) */
1.13      ohara      26: static cmo_null*         receive_cmo_null(OXFILE *oxfp);
                     27: static cmo_int32*        receive_cmo_int32(OXFILE *oxfp);
                     28: static cmo_string*       receive_cmo_string(OXFILE *oxfp);
                     29: static cmo_mathcap*      receive_cmo_mathcap(OXFILE *oxfp);
                     30: static cmo_list*         receive_cmo_list(OXFILE *oxfp);
                     31: static cmo_monomial32*   receive_cmo_monomial32(OXFILE *oxfp);
                     32: static cmo_zero*         receive_cmo_zero(OXFILE *oxfp);
                     33: static cmo_dms_generic*  receive_cmo_dms_generic(OXFILE *oxfp);
                     34: static cmo_ring_by_name* receive_cmo_ring_by_name(OXFILE *oxfp);
                     35: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(OXFILE *oxfp);
1.26      ohara      36: static cmo_recursive_polynomial* receive_cmo_recursive_polynomial(OXFILE *oxfp);
                     37: static cmo_polynomial_in_one_variable* receive_cmo_polynomial_in_one_variable(OXFILE *oxfp);
1.32      ohara      38: static cmo_double*       receive_cmo_double(OXFILE *oxfp);
1.13      ohara      39: static cmo_error2*       receive_cmo_error2(OXFILE *oxfp);
                     40:
                     41: static int          send_cmo_null(OXFILE *oxfp, cmo_null* c);
                     42: static int          send_cmo_int32(OXFILE *oxfp, cmo_int32* m);
                     43: static int          send_cmo_string(OXFILE *oxfp, cmo_string* m);
                     44: static int          send_cmo_mathcap(OXFILE *oxfp, cmo_mathcap* c);
                     45: static int          send_cmo_list(OXFILE *oxfp, cmo_list* c);
                     46: static int          send_cmo_monomial32(OXFILE *oxfp, cmo_monomial32* c);
1.32      ohara      47: static int          send_cmo_double(OXFILE *oxfp, cmo_double* c);
1.22      ohara      48: static int          send_cmo_error2(OXFILE *oxfp, cmo_error2* c);
                     49: static int          send_cmo_distributed_polynomial(OXFILE *oxfp, cmo_distributed_polynomial* c);
1.26      ohara      50: static int send_cmo_polynomial_in_one_variable(OXFILE *oxfp, cmo_polynomial_in_one_variable* c);
                     51: static int send_cmo_recursive_polynomial(OXFILE *oxfp, cmo_recursive_polynomial* c);
1.22      ohara      52:
                     53: static cmo_zz*      receive_cmo_zz(OXFILE *oxfp);
                     54: static void         receive_mpz(OXFILE *oxfp, mpz_ptr mpz);
1.13      ohara      55: static int          send_cmo_zz(OXFILE *oxfp, cmo_zz* c);
                     56: static int          send_mpz(OXFILE *oxfp, mpz_ptr mpz);
1.3       ohara      57:
1.8       ohara      58: /* hook functions. (yet not implemented) */
1.2       ohara      59: static hook_t hook_before_send_cmo = NULL;
                     60: static hook_t hook_after_send_cmo  = NULL;
                     61:
                     62: int add_hook_before_send_cmo(hook_t func)
                     63: {
1.13      ohara      64:     hook_before_send_cmo = func;
                     65:     return 0;
1.2       ohara      66: }
                     67:
                     68: int add_hook_after_send_cmo(hook_t func)
                     69: {
1.13      ohara      70:     hook_after_send_cmo = func;
                     71:     return 0;
1.2       ohara      72: }
                     73:
1.13      ohara      74: static cmo *call_hook_before_send_cmo(OXFILE *oxfp, cmo *c)
1.2       ohara      75: {
1.13      ohara      76:     if (hook_before_send_cmo != NULL) {
                     77:         return hook_before_send_cmo(oxfp, c);
                     78:     }
                     79:     return c;
1.2       ohara      80: }
                     81:
1.13      ohara      82: static cmo *call_hook_after_send_cmo(OXFILE *oxfp, cmo *c)
1.2       ohara      83: {
1.13      ohara      84:     if (hook_after_send_cmo != NULL) {
                     85:         return hook_after_send_cmo(oxfp, c);
                     86:     }
                     87:     return c;
1.2       ohara      88: }
1.1       ohara      89:
1.7       ohara      90: /* Handling an error. */
1.1       ohara      91: static int current_received_serial = 0;
                     92:
1.7       ohara      93: /* If an error object be needed, then a server call the following function. */
1.1       ohara      94: cmo_error2* make_error_object(int err_code, cmo *ob)
                     95: {
                     96:     cmo_list* li = new_cmo_list();
1.13      ohara      97:     list_append(li, (cmo *)new_cmo_int32(current_received_serial));
                     98:     list_append(li, (cmo *)new_cmo_int32(err_code));
                     99:     list_append(li, ob);
1.1       ohara     100:     return new_cmo_error2((cmo *)li);
                    101: }
                    102:
1.7       ohara     103: /* getting a next serial number. */
1.13      ohara     104: int next_serial(OXFILE *oxfp)
1.1       ohara     105: {
1.13      ohara     106:     return oxfp->serial_number++;
1.1       ohara     107: }
                    108:
1.7       ohara     109: /* sending an object of int32 type. (not equal to cmo_int32 type)  */
1.13      ohara     110: int send_int32(OXFILE *oxfp, int int32)
1.1       ohara     111: {
1.13      ohara     112:     return oxfp->send_int32(oxfp, int32);
1.1       ohara     113: }
                    114:
1.13      ohara     115: /* receiving an object of int32 type. (not equal to cmo_int32 type)  */
                    116: int receive_int32(OXFILE *oxfp)
1.1       ohara     117: {
1.13      ohara     118:     return oxfp->receive_int32(oxfp);
1.1       ohara     119: }
                    120:
1.32      ohara     121: /* sending an object of int32 type. (not equal to cmo_int32 type)  */
                    122: int send_double(OXFILE *oxfp, double d)
                    123: {
                    124:     return oxfp->send_double(oxfp, d);
                    125: }
                    126:
                    127: /* receiving an object of int32 type. (not equal to cmo_int32 type)  */
                    128: double receive_double(OXFILE *oxfp)
                    129: {
                    130:     return oxfp->receive_double(oxfp);
                    131: }
                    132:
1.13      ohara     133: /* receiving an (OX_tag, serial number)  */
                    134: int receive_ox_tag(OXFILE *oxfp)
1.1       ohara     135: {
1.13      ohara     136:     int tag = receive_int32(oxfp);
1.16      ohara     137:     oxfp->received_serial_number = receive_int32(oxfp);
1.13      ohara     138:     return tag;
1.1       ohara     139: }
                    140:
1.13      ohara     141: /* sending an (OX_tag, serial number)  */
                    142: int send_ox_tag(OXFILE *oxfp, int tag)
1.3       ohara     143: {
1.13      ohara     144:     send_int32(oxfp, tag);
                    145:     return send_int32(oxfp, next_serial(oxfp));
1.1       ohara     146: }
                    147:
1.7       ohara     148: /* functions named receive_cmo_*. */
1.13      ohara     149: static cmo_null* receive_cmo_null(OXFILE *oxfp)
1.1       ohara     150: {
                    151:     return new_cmo_null();
                    152: }
                    153:
1.13      ohara     154: static cmo_int32* receive_cmo_int32(OXFILE *oxfp)
1.1       ohara     155: {
1.13      ohara     156:     int i = receive_int32(oxfp);
1.1       ohara     157:     return new_cmo_int32(i);
                    158: }
                    159:
1.13      ohara     160: static cmo_string* receive_cmo_string(OXFILE *oxfp)
1.1       ohara     161: {
1.13      ohara     162:     int len = receive_int32(oxfp);
1.25      ohara     163:     char* s = MALLOC(len+1);
1.1       ohara     164:     memset(s, '\0', len+1);
                    165:     if (len > 0) {
1.13      ohara     166:         oxf_read(s, 1, len, oxfp);
1.1       ohara     167:     }
                    168:     return new_cmo_string(s);
                    169: }
                    170:
1.13      ohara     171: static cmo_mathcap* receive_cmo_mathcap(OXFILE *oxfp)
1.1       ohara     172: {
1.13      ohara     173:     cmo* ob = receive_cmo(oxfp);
1.1       ohara     174:     return new_cmo_mathcap(ob);
                    175: }
                    176:
1.13      ohara     177: static cmo_list* receive_cmo_list(OXFILE *oxfp)
1.1       ohara     178: {
                    179:     cmo* ob;
                    180:     cmo_list* c = new_cmo_list();
1.13      ohara     181:     int len = receive_int32(oxfp);
1.1       ohara     182:
                    183:     while (len>0) {
1.13      ohara     184:         ob = receive_cmo(oxfp);
                    185:         list_append(c, ob);
1.1       ohara     186:         len--;
                    187:     }
                    188:     return c;
                    189: }
                    190:
1.13      ohara     191: static cmo_monomial32* receive_cmo_monomial32(OXFILE *oxfp)
1.1       ohara     192: {
                    193:     int i;
1.13      ohara     194:     int len = receive_int32(oxfp);
1.1       ohara     195:     cmo_monomial32* c = new_cmo_monomial32(len);
                    196:
                    197:     for(i=0; i<len; i++) {
1.13      ohara     198:         c->exps[i] = receive_int32(oxfp);
1.1       ohara     199:     }
1.13      ohara     200:     c->coef = receive_cmo(oxfp);
1.1       ohara     201:     return c;
                    202: }
                    203:
1.13      ohara     204: static cmo_zz* receive_cmo_zz(OXFILE *oxfp)
1.1       ohara     205: {
                    206:     cmo_zz* c = new_cmo_zz();
1.13      ohara     207:     receive_mpz(oxfp, c->mpz);
1.1       ohara     208:     return c;
                    209: }
                    210:
1.33      ohara     211: static cmo_qq* receive_cmo_qq(OXFILE *oxfp)
                    212: {
1.34      ohara     213:     mpz_t num, den;
                    214:     mpz_init(num);
                    215:     mpz_init(den);
                    216:     receive_mpz(oxfp, num);
                    217:     receive_mpz(oxfp, den);
                    218:     return new_cmo_qq_set_mpz(num, den);
1.33      ohara     219: }
                    220:
1.13      ohara     221: static cmo_zero* receive_cmo_zero(OXFILE *oxfp)
1.1       ohara     222: {
                    223:     return new_cmo_zero();
                    224: }
                    225:
1.13      ohara     226: static cmo_dms_generic* receive_cmo_dms_generic(OXFILE *oxfp)
1.1       ohara     227: {
                    228:     return new_cmo_dms_generic();
                    229: }
                    230:
1.13      ohara     231: static cmo_ring_by_name* receive_cmo_ring_by_name(OXFILE *oxfp)
1.1       ohara     232: {
1.13      ohara     233:     cmo* ob = receive_cmo(oxfp);
                    234:     /* We need to check semantics but yet ... */
1.1       ohara     235:     return new_cmo_ring_by_name(ob);
                    236: }
                    237:
1.26      ohara     238: static cmo_recursive_polynomial* receive_cmo_recursive_polynomial(OXFILE *oxfp)
                    239: {
1.27      ohara     240:        cmo_list* ringdef = (cmo_list *)receive_cmo(oxfp);
                    241:        cmo* coef         = receive_cmo(oxfp);
1.26      ohara     242:     return new_cmo_recursive_polynomial(ringdef, coef);
                    243: }
                    244:
1.13      ohara     245: static cmo_distributed_polynomial* receive_cmo_distributed_polynomial(OXFILE *oxfp)
1.1       ohara     246: {
                    247:     cmo* ob;
                    248:     cmo_distributed_polynomial* c = new_cmo_distributed_polynomial();
1.13      ohara     249:     int len = receive_int32(oxfp);
                    250:     c->ringdef = receive_cmo(oxfp);
1.1       ohara     251:
                    252:     while (len>0) {
1.13      ohara     253:         ob = receive_cmo(oxfp);
                    254:         list_append((cmo_list *)c, ob);
1.1       ohara     255:         len--;
                    256:     }
                    257:     return c;
                    258: }
                    259:
1.26      ohara     260: static cmo_polynomial_in_one_variable* receive_cmo_polynomial_in_one_variable(OXFILE *oxfp)
                    261: {
                    262:     cmo* coef;
                    263:     cmo_polynomial_in_one_variable* c;
                    264:     int len = receive_int32(oxfp);
                    265:     int var = receive_int32(oxfp);
                    266:     int exp;
                    267:     c = new_cmo_polynomial_in_one_variable(var);
                    268:     while (len>0) {
                    269:         exp  = receive_int32(oxfp);
                    270:         coef = receive_cmo(oxfp);
1.27      ohara     271:         list_append_monomial((cmo_list *)c, coef, exp);
1.26      ohara     272:         len--;
                    273:     }
                    274:     return c;
                    275: }
                    276:
1.32      ohara     277: static cmo_double* receive_cmo_double(OXFILE *oxfp)
                    278: {
                    279:        double d = receive_double(oxfp);
                    280:        return new_cmo_double(d);
                    281: }
                    282:
1.29      ohara     283: static cmo_indeterminate* receive_cmo_indeterminate(OXFILE *oxfp)
                    284: {
                    285:     cmo* ob = receive_cmo(oxfp);
                    286:     return new_cmo_indeterminate(ob);
                    287: }
                    288:
1.28      ohara     289: static cmo_tree* receive_cmo_tree(OXFILE *oxfp)
                    290: {
                    291:     cmo_string* name = (cmo_string *)receive_cmo(oxfp);
                    292:     cmo_list* attrib = (cmo_list *)receive_cmo(oxfp);
                    293:     cmo_list* leaves = (cmo_list *)receive_cmo(oxfp);
                    294:     return new_cmo_tree(name, attrib, leaves);
                    295: }
                    296:
                    297: static cmo_lambda* receive_cmo_lambda(OXFILE *oxfp)
                    298: {
                    299:     cmo_list* args = (cmo_list *)receive_cmo(oxfp);
                    300:     cmo_tree* body = (cmo_tree *)receive_cmo(oxfp);
                    301:     return new_cmo_lambda(args, body);
                    302: }
                    303:
1.13      ohara     304: static cmo_error2* receive_cmo_error2(OXFILE *oxfp)
1.1       ohara     305: {
1.13      ohara     306:     cmo* ob = receive_cmo(oxfp);
1.1       ohara     307:     return new_cmo_error2(ob);
                    308: }
                    309:
1.17      ohara     310: /* receive_cmo() is called after receive_ox_tag(). */
1.13      ohara     311: cmo* receive_cmo(OXFILE *oxfp)
1.1       ohara     312: {
1.24      ohara     313:     int tag = receive_int32(oxfp);
                    314:     return receive_cmo_tag(oxfp, tag);
                    315: }
                    316:
                    317: cmo *receive_cmo_tag(OXFILE *oxfp, int tag)
                    318: {
1.1       ohara     319:     cmo* m;
                    320:     switch(tag) {
                    321:     case CMO_NULL:
1.13      ohara     322:         m = receive_cmo_null(oxfp);
1.1       ohara     323:         break;
                    324:     case CMO_INT32:
1.13      ohara     325:         m = (cmo *)receive_cmo_int32(oxfp);
1.1       ohara     326:         break;
                    327:     case CMO_STRING:
1.13      ohara     328:         m = (cmo *)receive_cmo_string(oxfp);
1.1       ohara     329:         break;
                    330:     case CMO_MATHCAP:
1.13      ohara     331:         m = (cmo *)receive_cmo_mathcap(oxfp);
1.1       ohara     332:         break;
                    333:     case CMO_LIST:
1.13      ohara     334:         m = (cmo *)receive_cmo_list(oxfp);
1.1       ohara     335:         break;
                    336:     case CMO_MONOMIAL32:
1.13      ohara     337:         m = (cmo *)receive_cmo_monomial32(oxfp);
1.1       ohara     338:         break;
                    339:     case CMO_ZZ:
1.13      ohara     340:         m = (cmo *)receive_cmo_zz(oxfp);
1.1       ohara     341:         break;
1.33      ohara     342:     case CMO_QQ:
                    343:         m = (cmo *)receive_cmo_qq(oxfp);
                    344:         break;
1.1       ohara     345:     case CMO_ZERO:
1.13      ohara     346:         m = (cmo *)receive_cmo_zero(oxfp);
1.1       ohara     347:         break;
                    348:     case CMO_DMS_GENERIC:
1.13      ohara     349:         m = (cmo *)receive_cmo_dms_generic(oxfp);
1.1       ohara     350:         break;
                    351:     case CMO_RING_BY_NAME:
1.13      ohara     352:         m = (cmo *)receive_cmo_ring_by_name(oxfp);
1.1       ohara     353:         break;
                    354:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.13      ohara     355:         m = (cmo *)receive_cmo_distributed_polynomial(oxfp);
1.1       ohara     356:         break;
1.26      ohara     357:     case CMO_RECURSIVE_POLYNOMIAL:
                    358:         m = (cmo *)receive_cmo_recursive_polynomial(oxfp);
                    359:         break;
                    360:     case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
                    361:         m = (cmo *)receive_cmo_polynomial_in_one_variable(oxfp);
1.29      ohara     362:         break;
1.32      ohara     363:        case CMO_64BIT_MACHINE_DOUBLE:
                    364:        case CMO_IEEE_DOUBLE_FLOAT:
                    365:         m = (cmo *)receive_cmo_double(oxfp);
                    366:         break;
1.29      ohara     367:     case CMO_INDETERMINATE:
                    368:         m = (cmo *)receive_cmo_indeterminate(oxfp);
1.28      ohara     369:         break;
                    370:     case CMO_TREE:
                    371:         m = (cmo *)receive_cmo_tree(oxfp);
                    372:         break;
                    373:     case CMO_LAMBDA:
                    374:         m = (cmo *)receive_cmo_lambda(oxfp);
                    375:         break;
1.1       ohara     376:     case CMO_ERROR2:
1.13      ohara     377:         m = (cmo *)receive_cmo_error2(oxfp);
1.1       ohara     378:         break;
                    379:     case CMO_DATUM:
                    380:     default:
1.13      ohara     381:         m = NULL;
1.20      ohara     382:         ox_printf("the CMO (%d) is not implemented.\n", tag);
1.1       ohara     383:     }
                    384:     return m;
                    385: }
                    386:
1.13      ohara     387: static void receive_mpz(OXFILE *oxfp, mpz_ptr mpz)
1.1       ohara     388: {
                    389:     int i;
1.35    ! iwane     390:        int n = sizeof(mpz->_mp_d[0]) / sizeof(int);
1.13      ohara     391:     int size  = receive_int32(oxfp);
1.1       ohara     392:     int len   = abs(size);
1.35    ! iwane     393:        int *ptr;
        !           394:        if (n == 1) {
        !           395:            resize_mpz(mpz, size);
        !           396:        } else {
        !           397:            resize_mpz(mpz, (size+1) / n);
        !           398:        }
        !           399:
        !           400:        ptr = (int *)mpz->_mp_d;
        !           401:        for(i= len-1; i>=0; i--) {
        !           402:                ptr[i] = receive_int32(oxfp);
        !           403:        }
1.1       ohara     404: }
                    405:
1.13      ohara     406: void send_ox_command(OXFILE *oxfp, int sm_command)
1.1       ohara     407: {
1.13      ohara     408:     send_ox_tag(oxfp, OX_COMMAND);
                    409:     send_int32(oxfp, sm_command);
                    410:     oxf_flush(oxfp);
1.1       ohara     411: }
                    412:
1.13      ohara     413: void ox_close(OXFILE *sv)
1.1       ohara     414: {
1.13      ohara     415:     send_ox_command(oxf_control(sv), SM_control_kill);
1.7       ohara     416:     sleep(2);
1.13      ohara     417:     /* We wait thar an OpenXM server terminates. */
1.20      ohara     418:     ox_printf("I have closed the connection to an Open XM server.\n");
1.1       ohara     419: }
                    420:
1.13      ohara     421: void ox_shutdown(OXFILE *sv)
1.3       ohara     422: {
1.13      ohara     423:     /* We need to use SM_shutdown but yet ... */
                    424:     ox_close(sv);
1.3       ohara     425: }
                    426:
1.13      ohara     427: void ox_cmo_rpc(OXFILE *sv, char *function, int argc, cmo *argv[])
1.3       ohara     428: {
1.13      ohara     429:     int i = argc;
                    430:     while(i-- > 0) {
                    431:         send_ox_cmo(sv, argv[i]);
                    432:     }
                    433:     send_ox_cmo(sv, (cmo *)new_cmo_int32(argc));
                    434:     send_ox_cmo(sv, (cmo *)new_cmo_string(function));
                    435:     send_ox_command(sv, SM_executeFunction);
1.3       ohara     436: }
                    437:
1.13      ohara     438: void ox_execute_string(OXFILE *sv, char* s)
1.3       ohara     439: {
1.13      ohara     440:     send_ox_cmo(sv, (cmo *)new_cmo_string(s));
                    441:     send_ox_command(sv, SM_executeStringByLocalParser);
1.3       ohara     442: }
                    443:
1.13      ohara     444: void ox_push_cmd(OXFILE *sv, int sm_code)
1.1       ohara     445: {
1.13      ohara     446:     send_ox_command(sv, sm_code);
1.1       ohara     447: }
                    448:
1.13      ohara     449: cmo_mathcap* ox_mathcap(OXFILE *sv)
1.1       ohara     450: {
1.13      ohara     451:     send_ox_command(sv, SM_mathcap);
                    452:     send_ox_command(sv, SM_popCMO);
                    453:     receive_ox_tag(sv);          /* OX_DATA */
                    454:     return (cmo_mathcap *)receive_cmo(sv);
1.1       ohara     455: }
                    456:
1.13      ohara     457: char* ox_popString(OXFILE *sv)
1.1       ohara     458: {
                    459:     cmo_string* m = NULL;
                    460:
1.13      ohara     461:     send_ox_command(sv, SM_popString);
                    462:     receive_ox_tag(sv); /* OX_DATA */
                    463:     m = (cmo_string *)receive_cmo(sv);
1.1       ohara     464:     return m->s;
                    465: }
                    466:
1.13      ohara     467: void ox_pops(OXFILE *sv, int num)
1.3       ohara     468: {
1.13      ohara     469:     send_ox_cmo(sv, (cmo *)new_cmo_int32(num));
                    470:     send_ox_command(sv, SM_pops);
1.3       ohara     471: }
                    472:
1.13      ohara     473: cmo* ox_pop_cmo(OXFILE *sv)
1.3       ohara     474: {
1.13      ohara     475:     send_ox_command(sv, SM_popCMO);
                    476:     receive_ox_tag(sv); /* OX_DATA */
                    477:     return receive_cmo(sv);
1.3       ohara     478: }
                    479:
1.13      ohara     480: void ox_push_cmo(OXFILE *sv, cmo *c)
1.3       ohara     481: {
1.13      ohara     482:     send_ox_cmo(sv, c);
1.3       ohara     483: }
                    484:
1.7       ohara     485: /* a dummy function for flushing a connection. */
1.13      ohara     486: int ox_flush(OXFILE *sv)
1.5       ohara     487: {
1.13      ohara     488:     return 1;
1.5       ohara     489: }
                    490:
1.13      ohara     491: void ox_reset(OXFILE *sv)
1.1       ohara     492: {
1.13      ohara     493:     send_ox_command(oxf_control(sv), SM_control_reset_connection);
1.31      iwane     494:     while(receive_ox_tag(sv) != OX_SYNC_BALL) {
                    495:         receive_cmo(sv); /* skipping a message. */
1.1       ohara     496:     }
                    497:
1.13      ohara     498:     send_ox_tag(sv, OX_SYNC_BALL);
1.20      ohara     499:     ox_printf("I have reset an Open XM server.\n");
1.1       ohara     500: }
                    501:
1.13      ohara     502: void send_ox(OXFILE *oxfp, ox *m)
1.1       ohara     503: {
                    504:     switch(m->tag) {
1.13      ohara     505:     case OX_DATA:
                    506:         send_ox_cmo(oxfp, ((ox_data *)m)->cmo);
1.1       ohara     507:         break;
1.13      ohara     508:     case OX_COMMAND:
                    509:         send_ox_command(oxfp, ((ox_command *)m)->command);
1.1       ohara     510:         break;
                    511:     default:
                    512:         /* CMO?? */
1.13      ohara     513:         send_ox_cmo(oxfp, (cmo *)m);
1.1       ohara     514:     }
                    515: }
                    516:
1.13      ohara     517: void send_ox_cmo(OXFILE *oxfp, cmo* m)
1.1       ohara     518: {
1.13      ohara     519:     send_ox_tag(oxfp, OX_DATA);
                    520:     send_cmo(oxfp, m);
                    521:     oxf_flush(oxfp);
1.1       ohara     522: }
                    523:
1.8       ohara     524: /* send_cmo_* functions */
1.13      ohara     525: static int send_cmo_null(OXFILE *oxfp, cmo_null* c)
1.1       ohara     526: {
                    527:     return 0;
                    528: }
                    529:
1.13      ohara     530: static int send_cmo_int32(OXFILE *oxfp, cmo_int32* m)
1.1       ohara     531: {
1.13      ohara     532:     return send_int32(oxfp, m->i);
1.1       ohara     533: }
                    534:
1.13      ohara     535: static int send_cmo_string(OXFILE *oxfp, cmo_string* m)
1.1       ohara     536: {
                    537:     int len = (m->s != NULL)? strlen(m->s): 0;
1.13      ohara     538:     send_int32(oxfp, len);
1.1       ohara     539:     if (len > 0) {
1.13      ohara     540:         oxf_write(m->s, 1, len, oxfp);
1.1       ohara     541:     }
                    542:     return 0;
                    543: }
                    544:
1.13      ohara     545: static int send_cmo_mathcap(OXFILE *oxfp, cmo_mathcap* c)
1.1       ohara     546: {
1.13      ohara     547:     send_cmo(oxfp, c->ob);
1.1       ohara     548:     return 0;
                    549: }
                    550:
1.13      ohara     551: static int send_cmo_list(OXFILE *oxfp, cmo_list* c)
1.1       ohara     552: {
1.13      ohara     553:     cell* el = list_first(c);
                    554:     int len = list_length(c);
                    555:     send_int32(oxfp, len);
                    556:
                    557:     while(!list_endof(c, el)) {
                    558:         send_cmo(oxfp, el->cmo);
                    559:         el = list_next(el);
1.1       ohara     560:     }
                    561:     return 0;
                    562: }
                    563:
1.13      ohara     564: static int send_cmo_distributed_polynomial(OXFILE *oxfp, cmo_distributed_polynomial* c)
1.1       ohara     565: {
1.13      ohara     566:     cell* el = list_first((cmo_list *)c);
                    567:     int len = list_length((cmo_list *)c);
                    568:     send_int32(oxfp, len);
                    569:     send_cmo(oxfp, c->ringdef);
                    570:     while(!list_endof((cmo_list *)c, el)) {
                    571:         send_cmo(oxfp, el->cmo);
                    572:         el = list_next(el);
1.1       ohara     573:     }
                    574:     return 0;
                    575: }
                    576:
1.26      ohara     577: static int send_cmo_polynomial_in_one_variable(OXFILE *oxfp, cmo_polynomial_in_one_variable* c)
                    578: {
1.27      ohara     579:     cell* el = list_first((cmo_list *)c);
                    580:     int len = list_length((cmo_list *)c);
1.26      ohara     581:     send_int32(oxfp, len);
                    582:        send_int32(oxfp, c->var);
                    583:
1.27      ohara     584:     while(!list_endof((cmo_list *)c, el)) {
                    585:         send_int32(oxfp, el->exp);
1.26      ohara     586:         send_cmo(oxfp, el->cmo);
                    587:         el = list_next(el);
                    588:     }
                    589:     return 0;
                    590: }
                    591:
1.32      ohara     592: static int send_cmo_double(OXFILE *oxfp, cmo_double* c)
                    593: {
                    594:     return send_double(oxfp, c->d);
                    595: }
                    596:
1.13      ohara     597: static int send_cmo_monomial32(OXFILE *oxfp, cmo_monomial32* c)
1.1       ohara     598: {
                    599:     int i;
                    600:     int len = c->length;
1.13      ohara     601:     send_int32(oxfp, len);
1.1       ohara     602:     for(i=0; i<len; i++) {
1.13      ohara     603:         send_int32(oxfp, c->exps[i]);
1.1       ohara     604:     }
1.13      ohara     605:     send_cmo(oxfp, c->coef);
1.1       ohara     606:     return 0;
                    607: }
                    608:
1.13      ohara     609: static int send_cmo_zz(OXFILE *oxfp, cmo_zz* c)
1.1       ohara     610: {
1.13      ohara     611:     send_mpz(oxfp, c->mpz);
1.1       ohara     612:     return 0;
                    613: }
                    614:
1.33      ohara     615: static int send_cmo_qq(OXFILE *oxfp, cmo_qq* c)
                    616: {
1.34      ohara     617:     send_mpz(oxfp, mpq_numref(c->mpq));
                    618:     send_mpz(oxfp, mpq_denref(c->mpq));
1.33      ohara     619:     return 0;
                    620: }
                    621:
1.26      ohara     622: static int send_cmo_recursive_polynomial(OXFILE *oxfp, cmo_recursive_polynomial* c)
                    623: {
1.27      ohara     624:        send_cmo(oxfp, (cmo *)c->ringdef);
1.26      ohara     625:     send_cmo(oxfp, c->coef);
                    626:     return 0;
                    627: }
                    628:
1.28      ohara     629: static int send_cmo_tree(OXFILE *oxfp, cmo_tree *c)
                    630: {
                    631:     send_cmo(oxfp, (cmo *)c->name);
                    632:     send_cmo(oxfp, (cmo *)c->attributes);
                    633:     send_cmo(oxfp, (cmo *)c->leaves);
                    634:     return 0;
                    635: }
                    636:
                    637: static int send_cmo_lambda(OXFILE *oxfp, cmo_lambda *c)
                    638: {
                    639:     send_cmo(oxfp, (cmo *)c->args);
                    640:     send_cmo(oxfp, (cmo *)c->body);
                    641:     return 0;
                    642: }
                    643:
1.13      ohara     644: static int send_cmo_error2(OXFILE *oxfp, cmo_error2* c)
1.1       ohara     645: {
1.13      ohara     646:     send_cmo(oxfp, c->ob);
1.1       ohara     647:     return 0;
                    648: }
                    649:
1.8       ohara     650: /* sending a CMO.  (Remarks: OX_tag is already sent.) */
1.13      ohara     651: void send_cmo(OXFILE *oxfp, cmo* c)
1.1       ohara     652: {
                    653:     int tag = c->tag;
                    654:
1.13      ohara     655:     c = call_hook_before_send_cmo(oxfp, c);
1.2       ohara     656:
1.13      ohara     657:     send_int32(oxfp, tag);
1.1       ohara     658:     switch(tag) {
                    659:     case CMO_NULL:
                    660:     case CMO_ZERO:
                    661:     case CMO_DMS_GENERIC:
1.13      ohara     662:         send_cmo_null(oxfp, c);  /* empty function. */
1.1       ohara     663:         break;
                    664:     case CMO_INT32:
1.13      ohara     665:         send_cmo_int32(oxfp, (cmo_int32 *)c);
1.1       ohara     666:         break;
                    667:     case CMO_STRING:
1.13      ohara     668:         send_cmo_string(oxfp, (cmo_string *)c);
1.1       ohara     669:         break;
                    670:     case CMO_MATHCAP:
                    671:     case CMO_ERROR2:
                    672:     case CMO_RING_BY_NAME:
                    673:     case CMO_INDETERMINATE:
1.13      ohara     674:         send_cmo_mathcap(oxfp, (cmo_mathcap *)c);
1.1       ohara     675:         break;
                    676:     case CMO_LIST:
1.13      ohara     677:         send_cmo_list(oxfp, (cmo_list *)c);
1.1       ohara     678:         break;
                    679:     case CMO_MONOMIAL32:
1.13      ohara     680:         send_cmo_monomial32(oxfp, (cmo_monomial32 *)c);
1.1       ohara     681:         break;
                    682:     case CMO_ZZ:
1.13      ohara     683:         send_cmo_zz(oxfp, (cmo_zz *)c);
1.33      ohara     684:         break;
                    685:     case CMO_QQ:
                    686:         send_cmo_qq(oxfp, (cmo_qq *)c);
1.1       ohara     687:         break;
                    688:     case CMO_DISTRIBUTED_POLYNOMIAL:
1.13      ohara     689:         send_cmo_distributed_polynomial(oxfp, (cmo_distributed_polynomial *)c);
1.26      ohara     690:         break;
                    691:     case CMO_RECURSIVE_POLYNOMIAL:
                    692:         send_cmo_recursive_polynomial(oxfp, (cmo_recursive_polynomial *)c);
                    693:         break;
                    694:     case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
                    695:         send_cmo_polynomial_in_one_variable(oxfp, (cmo_polynomial_in_one_variable *)c);
1.28      ohara     696:         break;
1.32      ohara     697:        case CMO_64BIT_MACHINE_DOUBLE:
                    698:        case CMO_IEEE_DOUBLE_FLOAT:
                    699:                send_cmo_double(oxfp, (cmo_double *)c);
                    700:                break;
1.28      ohara     701:     case CMO_TREE:
                    702:         send_cmo_tree(oxfp, (cmo_tree *)c);
                    703:         break;
                    704:     case CMO_LAMBDA:
                    705:         send_cmo_lambda(oxfp, (cmo_lambda *)c);
1.1       ohara     706:         break;
                    707:     default:
1.13      ohara     708:         call_hook_after_send_cmo(oxfp, c);
1.1       ohara     709:     }
                    710: }
                    711:
1.13      ohara     712: static int send_mpz(OXFILE *oxfp, mpz_ptr mpz)
1.1       ohara     713: {
                    714:     int i;
1.35    ! iwane     715:        int n = sizeof(mpz->_mp_d[0]) / sizeof(int);
        !           716:     int len = abs(mpz->_mp_size) * n;
        !           717:        int *ptr = (int *)mpz->_mp_d;
        !           718:     send_int32(oxfp, mpz->_mp_size * n);
1.1       ohara     719:     for(i=0; i<len; i++) {
1.35    ! iwane     720:         send_int32(oxfp, ptr[i]);
1.1       ohara     721:     }
                    722:     return 0;
                    723: }
                    724:
                    725: ox_data* new_ox_data(cmo* c)
                    726: {
1.25      ohara     727:     ox_data* m = MALLOC(sizeof(ox_data));
1.1       ohara     728:     m->tag = OX_DATA;
                    729:     m->cmo = c;
                    730:     return m;
                    731: }
                    732:
                    733: ox_command* new_ox_command(int sm_code)
                    734: {
1.25      ohara     735:     ox_command* m = MALLOC(sizeof(ox_command));
1.1       ohara     736:     m->tag = OX_COMMAND;
                    737:     m->command = sm_code;
                    738:     return m;
                    739: }
                    740:
                    741: ox_sync_ball* new_ox_sync_ball()
                    742: {
1.25      ohara     743:     ox_sync_ball *m = MALLOC(sizeof(ox_sync_ball));
1.1       ohara     744:     m->tag = OX_SYNC_BALL;
                    745:     return m;
1.19      ohara     746: }
                    747:
                    748: int ox_stderr_init(FILE *fp)
                    749: {
1.21      ohara     750:     ox_stderr = fp;
                    751:     if (ox_stderr != NULL) {
                    752:         setbuf(ox_stderr, NULL);
                    753:     }
1.27      ohara     754:     return 0;
1.20      ohara     755: }
                    756:
                    757: int ox_printf(char *format, ...)
                    758: {
1.21      ohara     759:     if (ox_stderr != NULL) {
                    760:         va_list ap;
                    761:         va_start(ap, format);
                    762:         vfprintf(ox_stderr, format, ap);
                    763:     }
1.27      ohara     764:     return 0;
1.1       ohara     765: }

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