[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.9

1.1       ohara       1: /* -*- mode: C -*- */
1.9     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/testclient.c,v 1.8 2000/11/28 04:24:12 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.9     ! ohara      24:     fprintf(ox_stderr, "I have read %d byte from socket(%d).\n", len, oxfp->fd);
1.1       ohara      25:     for(i = 0; i < len; i++) {
1.9     ! ohara      26:         fprintf(ox_stderr, "%02x ", buff[i]);
1.1       ohara      27:         if (i%20 == 19) {
1.9     ! ohara      28:             fprintf(ox_stderr, "\n");
1.1       ohara      29:         }
                     30:     }
1.9     ! ohara      31:     fprintf(ox_stderr, "\n");
1.1       ohara      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.7       ohara      49:
1.1       ohara      50: int test_0()
                     51: {
                     52:     cmo* c = NULL;
                     53: #ifdef DEBUG
1.9     ! ohara      54:     fprintf(ox_stderr, "testclient:: calling ox_mathcap().\n");
1.1       ohara      55:     c = ox_mathcap(sv);
1.9     ! ohara      56:     fprintf(ox_stderr, "testclient:: cmo received.(%p)\n", c);
1.1       ohara      57: #else
1.5       ohara      58:     c = (cmo *)ox_mathcap(sv);
1.1       ohara      59: #endif
                     60:     print_cmo(c);
1.9     ! ohara      61:     fflush(ox_stderr);
1.5       ohara      62:
1.6       ohara      63:     mathcap_init(VERSION, ID_STRING, "testclient", NULL, NULL);
1.7       ohara      64:     send_ox_cmo(sv, oxf_cmo_mathcap(sv));
1.1       ohara      65:
                     66:     ox_reset(sv);
1.5       ohara      67:     send_ox_cmo(sv, (cmo *)new_cmo_string("N[ArcTan[1]]"));
                     68:     send_ox_command(sv, SM_executeStringByLocalParser);
                     69:     send_ox_command(sv, SM_popCMO);
                     70:     receive_ox_tag(sv);
                     71:     c = receive_cmo(sv);
1.9     ! ohara      72:     fprintf(ox_stderr, "testclient:: cmo received.\n");
1.1       ohara      73:     print_cmo(c);
                     74: }
                     75:
                     76: int test_1()
                     77: {
1.5       ohara      78:     cmo *c, *m;
                     79:
1.6       ohara      80:        mathcap_init(1000, "test!", "testclient", NULL, NULL);
1.7       ohara      81:        m = oxf_cmo_mathcap(sv);
1.9     ! ohara      82:     fprintf(ox_stderr, "testclient:: test cmo_mathcap.\n");
1.5       ohara      83:     send_ox_cmo(sv, m);
                     84:     send_ox_command(sv, SM_popCMO);
                     85:     receive_ox_tag(sv);
                     86:     c = receive_cmo(sv);
1.9     ! ohara      87:     fprintf(ox_stderr, "testclient:: cmo received.(%p)\n", c);
1.1       ohara      88:     print_cmo(c);
1.9     ! ohara      89:     fputc('\n', ox_stderr);
1.1       ohara      90: }
1.2       takayama   91:
                     92: /*  Example:
                     93:   testclient
                     94:   >(OX_DATA,(CMO_INT32,123))
                     95:   >(OX_COMMAND,(SM_popCMO))
1.3       ohara      96:  */
1.1       ohara      97:
                     98: int main(int argc, char* argv[])
                     99: {
                    100:     ox* m = NULL;
                    101:     cmo* c = NULL;
                    102:     int code;
                    103:     char *server = "ox_sm1";
                    104:
1.9     ! ohara     105:     ox_stderr_init(NULL);
        !           106:     setbuf(ox_stderr, NULL);
1.1       ohara     107:
                    108:     if (argc>1) {
                    109:         server = argv[1];
                    110:     }
1.9     ! ohara     111:     fprintf(ox_stderr, "testclient:: I use %s as an OX server.\n", server);
1.5       ohara     112: /*    sv = ox_start("localhost", "ox", server);  */
1.1       ohara     113:     if (sv == NULL) {
1.9     ! ohara     114:         fprintf(ox_stderr, "testclient:: I cannot connect to servers.\n");
1.1       ohara     115:         exit(1);
                    116:     }
                    117:
                    118:     if (strcmp(server, "ox_math")==0) {
                    119:         test_1();
                    120:     }
1.3       ohara     121:
                    122:     setflag_parse(PFLAG_ADDREV);
1.1       ohara     123:
                    124:     while(prompt(), (m = parse()) != NULL) {
1.5       ohara     125:         send_ox(sv, m);
1.1       ohara     126:         if (m->tag == OX_COMMAND) {
                    127:             code = ((ox_command *)m)->command;
                    128:             if (code >= 1024) {
                    129:                 break;
                    130:             }else if (code == SM_popCMO || code == SM_popString) {
1.5       ohara     131:                 receive_ox_tag(sv);
                    132:                 c = receive_cmo(sv);
1.9     ! ohara     133:                 fprintf(ox_stderr, "testclient:: cmo received.\n");
1.1       ohara     134:                 print_cmo(c);
                    135:             }
                    136:         }
                    137:     }
                    138:
                    139:     ox_reset(sv);
1.9     ! ohara     140:     fprintf(ox_stderr, "The testclient resets.\n");
1.1       ohara     141:     ox_close(sv);
1.9     ! ohara     142:     fprintf(ox_stderr, "The testclient halts.\n");
1.1       ohara     143:
                    144:     return 0;
                    145: }

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