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

Annotation of OpenXM/src/ox_math/testclient.c, Revision 1.2

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
                      2: /* $OpenXM$ */
                      3: /* $Id: client.c,v 1.2 1999/10/14 06:34:05 ohara Exp ohara $ */
                      4:
                      5: /*
                      6: ソケットで通信する場合のサーバの立ち上げ方。
                      7: dc3$ cd ~/openasir/bin
                      8: dc3$ ./ox -ox ox_sm1 -host dc3
                      9: */
                     10:
                     11: #include <stdio.h>
                     12: #include <stdlib.h>
                     13: #include <unistd.h>
                     14: #include <string.h>
                     15: #include <errno.h>
                     16: #include <fcntl.h>
                     17: #include "ox.h"
                     18:
                     19: ox_file_t sv;
                     20:
                     21: int dumpx(int fd, int n)
                     22: {
                     23:     unsigned char buff[2048];
                     24:     int i;
                     25:     int len = read(fd, buff, n);
                     26:
                     27:     fprintf(stderr, "I have read %d byte from socket.\n", len);
                     28:     for(i = 0; i < len; i++) {
                     29:         fprintf(stderr, "%02x ", buff[i]);
                     30:         if (i%20 == 19) {
                     31:             fprintf(stderr, "\n");
                     32:         }
                     33:     }
                     34:     fprintf(stderr, "\n");
                     35:     return len;
                     36: }
                     37:
                     38: int mathematica_main()
                     39: {
                     40:     int fd = sv->stream;
                     41:     char* s = "Plus[31415926535, -1234567]";
                     42:     char* s2 = "Plus[1, 2]";
                     43:     char* s_result;
                     44:     cmo* m = NULL;
                     45:
                     46:     fprintf(stderr, "%s\n", s2);
                     47:     ox_executeStringByLocalParser(sv, s2);
                     48:
                     49:     m = ox_pop_cmo(sv, sv->stream);
                     50:     fprintf(stderr, "the tag of result is %d.\n", m->tag);
                     51:
                     52:     fprintf(stderr, "executeFunction(\"Plus\", 2, zz(5), zz(3))\n");
                     53:     send_ox_cmo(sv->stream, new_cmo_zz_set_si(3));
                     54:     send_ox_cmo(sv->stream, new_cmo_zz_set_si(5));
                     55:     send_ox_cmo(sv->stream, new_cmo_int32(2));  /* number of arguments */
                     56:     send_ox_cmo(sv->stream, new_cmo_string("Plus"));
                     57:     send_ox_command(sv->stream, SM_executeFunction);
                     58:
                     59:     s_result = ox_popString(sv, sv->stream);
                     60:     fprintf(stderr, "result = %s.\n", s_result);
                     61:
                     62:     return;
                     63: }
                     64:
                     65: void asir_main()
                     66: {
                     67:     cmo* m = NULL;
                     68:     int fd = sv->stream;
                     69:
                     70:     send_ox_cmo(sv->stream, new_cmo_int32(12345));
                     71:     ox_executeStringByLocalParser(sv, "print(\"Hello world!!\");");
                     72:     fprintf(stderr, "result = %s.\n", ox_popString(sv, sv->stream));
                     73:     return;
                     74:
                     75:     send_ox_command(fd, SM_popString);
                     76:     receive_ox_tag(fd); /* OX_DATA */
                     77:     m = receive_cmo(fd);
                     78:
                     79:     m = ox_pop_cmo(sv, sv->stream);
                     80:     fprintf(stderr, "tag = %d.\n", m->tag);
                     81: }
                     82:
                     83: void asir_main0()
                     84: {
                     85:
                     86:     char* sf1 = "diff((x+2*y)^2,x);";
                     87:
                     88:     /* この辺は ox_asir に実行させる場合に使う。 */
                     89:     ox_executeStringByLocalParser(sv, "print(\"Hello world!!\");");
                     90:
                     91:     ox_executeStringByLocalParser(sv, "diff((x+2*y)^2,x);");
                     92:     fprintf(stderr,
                     93:             "I let Asir evaluate \"%s\"\n and get a result \"%s\"\n", sf1, ox_popString(sv, sv->stream));
                     94: }
                     95:
                     96: void asir_main1()
                     97: {
                     98:     send_ox_cmo(sv->stream, new_cmo_zz_set_si(10));
                     99:     send_ox_command(sv->stream, SM_popCMO);
                    100:     fprintf(stderr, "I send SM_popCMO.\n");
                    101:
                    102:     dumpx(sv->stream, 17);
                    103: }
                    104:
                    105: void asir_main2()
                    106: {
                    107:     cmo* m = NULL;
                    108:
                    109:     /* add 関数の定義 */
                    110:     /* char* adddef = "def add(X,Y){return X+Y;};"; */
                    111:
                    112:     /* この辺は ox_asir に実行させる場合に使う。 */
                    113:     /*  ox_executeStringByLocalParser(sv, adddef); */
                    114:     send_ox_cmo(sv->stream, new_cmo_int32(3));
                    115:     send_ox_cmo(sv->stream, new_cmo_int32(5));
                    116:     send_ox_cmo(sv->stream, new_cmo_int32(2));  /* number of arguments */
                    117:     send_ox_cmo(sv->stream, new_cmo_string("igcd"));
                    118:     send_ox_command(sv->stream, SM_executeFunction);
                    119:     /* この段階でCMO_ERROR2 が積まれるはず */
                    120:
                    121:     send_ox_command(sv->stream, SM_popCMO);
                    122:
                    123: #if 0
                    124:     fprintf(stderr, "dump:\n");
                    125:     dumpx(sv->stream, 36);
                    126:     return;
                    127: #endif
                    128:
                    129:     receive_ox_tag(sv->stream);    /* OX_DATA */
                    130:     m = receive_cmo(sv->stream);
                    131:     fprintf(stderr, "tag is (%d).\n", m->tag);
                    132: }
                    133:
                    134: void asir_main3()
                    135: {
                    136:     int i1, i2;
                    137:     cmo_zz* z1;
                    138:     cmo_zz* z2;
                    139:
                    140:     i1 = 2*7;
                    141:     i2 = 2*11;
                    142:
                    143:     z1 = new_cmo_zz_set_si(i1);
                    144:     z2 = new_cmo_zz_set_si(i2);
                    145:     fprintf(stderr, "i1 = %d, i2 = %d.\n", i1, i2);
                    146:
                    147:     send_ox_cmo(sv->stream, z1);
                    148:     send_ox_cmo(sv->stream, z2);
                    149:     send_ox_cmo(sv->stream, new_cmo_int32(2));  /* number of arguments */
                    150:     send_ox_cmo(sv->stream, new_cmo_string("igcd"));
                    151:     send_ox_command(sv->stream, SM_executeFunction);
                    152:
                    153:     fprintf(stderr, "result1 = (%s).\n", ox_popString(sv, sv->stream));
                    154: }
                    155:
                    156: void sm_main()
                    157: {
                    158:     ox_mathcap(sv);
                    159: }
                    160:
                    161: /* 平成11年7月10日 */
                    162: #define VERSION 0x11071000
                    163: #define ID_STRING  "client version 0.11071000"
                    164:
                    165: int prompt()
                    166: {
                    167:     printf(":- ");
                    168: }
                    169:
                    170:
                    171: int main(int argc, char* argv[])
                    172: {
                    173:     ox* m = NULL;
                    174:     cmo* c = NULL;
                    175:     int code;
                    176:
                    177:     setbuf(stderr, NULL);
                    178:
                    179:     if (argc>1) {
                    180:         fprintf(stderr, "I use ox_math.\n");
                    181:         sv = ox_start("localhost", "ox", "ox_math");
                    182:         ox_mathcap(sv);
                    183:         send_ox_cmo(sv->stream, make_mathcap_object(VERSION, ID_STRING));
                    184:     }else {
                    185:         fprintf(stderr, "I use ox_sm1.\n");
                    186:         sv = ox_start("localhost", "ox", "ox_sm1");
                    187:     }
                    188:
                    189:     ox_reset(sv);
                    190:     send_ox_cmo(sv->stream, new_cmo_string("N[ArcTan[1]]"));
                    191:     send_ox_command(sv->stream, SM_executeStringByLocalParser);
                    192:     send_ox_command(sv->stream, SM_popCMO);
                    193:     fprintf(stderr, "for debug.\n");
                    194:
                    195:     receive_ox_tag(sv->stream);
                    196:     fprintf(stderr, "for debug.\n");
                    197:     c = receive_cmo(sv->stream);
                    198:     fprintf(stderr, "for debug.\n");
                    199:     fprintf(stderr, "local:: cmo received.\n");
                    200:     print_cmo(c);
                    201:
                    202:     while(prompt(), (m = parse()) != NULL) {
                    203:         send_ox(sv, m);
                    204:         if (m->tag == OX_COMMAND) {
                    205:             code = ((ox_command *)m)->command;
                    206:             if (code >= 1024) {
                    207:                 break;
                    208:             }else if (code == SM_popCMO || code == SM_popString) {
                    209:                 receive_ox_tag(sv->stream);
                    210:                 c = receive_cmo(sv->stream);
                    211:                 fprintf(stderr, "local:: cmo received.\n");
                    212:                 print_cmo(c);
                    213:             }
                    214:         }
                    215:     }
                    216:
                    217: #if 0
                    218:     sm_main();
                    219:     asir_main();
                    220:     mathematica_main();
                    221: #endif
                    222:
                    223:     ox_reset(sv);
                    224:     fprintf(stderr, "The client resets.\n");
                    225:     ox_close(sv);
                    226:     fprintf(stderr, "The client halts.\n");
                    227:
                    228:     return 0;
                    229: }

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