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

Annotation of OpenXM/src/ox_toolkit/testclient.c, Revision 1.7

1.1       ohara       1: /* -*- mode: C -*- */
1.7     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/testclient.c,v 1.6 2000/11/21 07:59:08 ohara Exp $ */
1.1       ohara       3:
                      4: /* A sample implementation of an OpenXM client with OpenXM C library */
                      5:
                      6: #include <stdio.h>
                      7: #include <stdlib.h>
                      8: #include <unistd.h>
                      9: #include <string.h>
                     10: #include <errno.h>
                     11: #include <fcntl.h>
1.4       ohara      12:
                     13: #include "ox_toolkit.h"
1.1       ohara      14:
1.5       ohara      15: extern OXFILE *ox_start(char* host, char* prog1, char* prog2);
                     16: OXFILE *sv;
1.1       ohara      17:
1.5       ohara      18: int dumpx(OXFILE *oxfp, int n)
1.1       ohara      19: {
                     20:     unsigned char buff[2048];
                     21:     int i;
1.5       ohara      22:     int len = oxf_read(buff, 1, n, oxfp);
1.1       ohara      23:
1.5       ohara      24:     fprintf(stderr, "I have read %d byte from socket(%d).\n", len, oxfp->fd);
1.1       ohara      25:     for(i = 0; i < len; i++) {
                     26:         fprintf(stderr, "%02x ", buff[i]);
                     27:         if (i%20 == 19) {
                     28:             fprintf(stderr, "\n");
                     29:         }
                     30:     }
                     31:     fprintf(stderr, "\n");
                     32:     return len;
                     33: }
                     34:
1.3       ohara      35: #define SIZE_CMDLINE  8192
                     36:
                     37: static int  size = SIZE_CMDLINE;
                     38: static char cmdline[SIZE_CMDLINE];
1.1       ohara      39:
1.3       ohara      40: static int prompt()
1.1       ohara      41: {
1.3       ohara      42:     fprintf(stdout, "> ");
                     43:     fgets(cmdline, size, stdin);
                     44:     init_parser(cmdline);
1.1       ohara      45: }
                     46:
1.3       ohara      47: #define VERSION 0x11121500
1.5       ohara      48: #define ID_STRING  "v0.11121500"
1.3       ohara      49:
1.7     ! ohara      50: mathcap *oxf_mathcap(OXFILE *oxfp)
        !            51: {
        !            52:     if (oxfp->mathcap == NULL) {
        !            53:         oxfp->mathcap = new_mathcap();
        !            54:     }
        !            55:        return oxfp->mathcap;
        !            56: }
        !            57:
        !            58: cmo_mathcap *oxf_cmo_mathcap(OXFILE *oxfp)
        !            59: {
        !            60:        return mathcap_get(oxf_mathcap(oxfp));
        !            61: }
        !            62:
1.1       ohara      63: int test_0()
                     64: {
                     65:     cmo* c = NULL;
                     66: #ifdef DEBUG
                     67:     fprintf(stderr, "testclient:: calling ox_mathcap().\n");
                     68:     c = ox_mathcap(sv);
                     69:     fprintf(stderr, "testclient:: cmo received.(%p)\n", c);
                     70: #else
1.5       ohara      71:     c = (cmo *)ox_mathcap(sv);
1.1       ohara      72: #endif
                     73:     print_cmo(c);
                     74:     fflush(stderr);
1.5       ohara      75:
1.6       ohara      76:     mathcap_init(VERSION, ID_STRING, "testclient", NULL, NULL);
1.7     ! ohara      77:     send_ox_cmo(sv, oxf_cmo_mathcap(sv));
1.1       ohara      78:
                     79:     ox_reset(sv);
1.5       ohara      80:     send_ox_cmo(sv, (cmo *)new_cmo_string("N[ArcTan[1]]"));
                     81:     send_ox_command(sv, SM_executeStringByLocalParser);
                     82:     send_ox_command(sv, SM_popCMO);
                     83:     receive_ox_tag(sv);
                     84:     c = receive_cmo(sv);
1.1       ohara      85:     fprintf(stderr, "testclient:: cmo received.\n");
                     86:     print_cmo(c);
                     87: }
                     88:
                     89: int test_1()
                     90: {
1.5       ohara      91:     cmo *c, *m;
                     92:
1.6       ohara      93:        mathcap_init(1000, "test!", "testclient", NULL, NULL);
1.7     ! ohara      94:        m = oxf_cmo_mathcap(sv);
1.1       ohara      95:     fprintf(stderr, "testclient:: test cmo_mathcap.\n");
1.5       ohara      96:     send_ox_cmo(sv, m);
                     97:     send_ox_command(sv, SM_popCMO);
                     98:     receive_ox_tag(sv);
                     99:     c = receive_cmo(sv);
1.1       ohara     100:     fprintf(stderr, "testclient:: cmo received.(%p)\n", c);
                    101:     print_cmo(c);
                    102:     fputc('\n', stderr);
                    103: }
1.2       takayama  104:
                    105: /*  Example:
                    106:   testclient
                    107:   >(OX_DATA,(CMO_INT32,123))
                    108:   >(OX_COMMAND,(SM_popCMO))
1.3       ohara     109:  */
1.1       ohara     110:
                    111: int main(int argc, char* argv[])
                    112: {
                    113:     ox* m = NULL;
                    114:     cmo* c = NULL;
                    115:     int code;
                    116:     char *server = "ox_sm1";
                    117:
                    118:     setbuf(stderr, NULL);
                    119:
                    120:     if (argc>1) {
                    121:         server = argv[1];
                    122:     }
                    123:     fprintf(stderr, "testclient:: I use %s as an OX server.\n", server);
1.5       ohara     124: /*    sv = ox_start("localhost", "ox", server);  */
1.1       ohara     125:     if (sv == NULL) {
                    126:         fprintf(stderr, "testclient:: I cannot connect to servers.\n");
                    127:         exit(1);
                    128:     }
                    129:
                    130:     if (strcmp(server, "ox_math")==0) {
                    131:         test_1();
                    132:     }
1.3       ohara     133:
                    134:     setflag_parse(PFLAG_ADDREV);
1.1       ohara     135:
                    136:     while(prompt(), (m = parse()) != NULL) {
1.5       ohara     137:         send_ox(sv, m);
1.1       ohara     138:         if (m->tag == OX_COMMAND) {
                    139:             code = ((ox_command *)m)->command;
                    140:             if (code >= 1024) {
                    141:                 break;
                    142:             }else if (code == SM_popCMO || code == SM_popString) {
1.5       ohara     143:                 receive_ox_tag(sv);
                    144:                 c = receive_cmo(sv);
1.1       ohara     145:                 fprintf(stderr, "testclient:: cmo received.\n");
                    146:                 print_cmo(c);
                    147:             }
                    148:         }
                    149:     }
                    150:
                    151:     ox_reset(sv);
                    152:     fprintf(stderr, "The testclient resets.\n");
                    153:     ox_close(sv);
                    154:     fprintf(stderr, "The testclient halts.\n");
                    155:
                    156:     return 0;
                    157: }

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