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

1.1       ohara       1: /* -*- mode: C -*- */
1.4     ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/testclient.c,v 1.3 1999/12/22 11:26:37 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:
                     15: ox_file_t sv;
                     16:
                     17: int dumpx(int fd, int n)
                     18: {
                     19:     unsigned char buff[2048];
                     20:     int i;
                     21:     int len = read(fd, buff, n);
                     22:
                     23:     fprintf(stderr, "I have read %d byte from socket.\n", len);
                     24:     for(i = 0; i < len; i++) {
                     25:         fprintf(stderr, "%02x ", buff[i]);
                     26:         if (i%20 == 19) {
                     27:             fprintf(stderr, "\n");
                     28:         }
                     29:     }
                     30:     fprintf(stderr, "\n");
                     31:     return len;
                     32: }
                     33:
1.3       ohara      34: #define SIZE_CMDLINE  8192
                     35:
                     36: static int  size = SIZE_CMDLINE;
                     37: static char cmdline[SIZE_CMDLINE];
1.1       ohara      38:
1.3       ohara      39: static int prompt()
1.1       ohara      40: {
1.3       ohara      41:     fprintf(stdout, "> ");
                     42:     fgets(cmdline, size, stdin);
                     43:     init_parser(cmdline);
1.1       ohara      44: }
                     45:
1.3       ohara      46: #define VERSION 0x11121500
                     47: #define ID_STRING  "testclient version 0.11121500"
                     48:
1.1       ohara      49: int test_0()
                     50: {
                     51:     cmo* c = NULL;
                     52: #ifdef DEBUG
                     53:     fprintf(stderr, "testclient:: calling ox_mathcap().\n");
                     54:     c = ox_mathcap(sv);
                     55:     fprintf(stderr, "testclient:: cmo received.(%p)\n", c);
                     56: #else
                     57:     c = ox_mathcap(sv);
                     58: #endif
                     59:     print_cmo(c);
                     60:     fflush(stderr);
                     61:     send_ox_cmo(sv->stream, make_mathcap_object(VERSION, ID_STRING));
                     62:
                     63:     ox_reset(sv);
                     64:     send_ox_cmo(sv->stream, new_cmo_string("N[ArcTan[1]]"));
                     65:     send_ox_command(sv->stream, SM_executeStringByLocalParser);
                     66:     send_ox_command(sv->stream, SM_popCMO);
                     67:     receive_ox_tag(sv->stream);
                     68:     c = receive_cmo(sv->stream);
                     69:     fprintf(stderr, "testclient:: cmo received.\n");
                     70:     print_cmo(c);
                     71: }
                     72:
                     73: int test_1()
                     74: {
                     75:     cmo *c = NULL;
                     76:     cmo *m = make_mathcap_object(1000, "test!");
                     77:     fprintf(stderr, "testclient:: test cmo_mathcap.\n");
                     78:     send_ox_cmo(sv->stream, m);
                     79:     send_ox_command(sv->stream, SM_popCMO);
                     80:     receive_ox_tag(sv->stream);
                     81:     c = receive_cmo(sv->stream);
                     82:     fprintf(stderr, "testclient:: cmo received.(%p)\n", c);
                     83:     print_cmo(c);
                     84:     fputc('\n', stderr);
                     85: }
1.2       takayama   86:
                     87: /*  Example:
                     88:   testclient
                     89:   >(OX_DATA,(CMO_INT32,123))
                     90:   >(OX_COMMAND,(SM_popCMO))
1.3       ohara      91:  */
1.1       ohara      92:
                     93: int main(int argc, char* argv[])
                     94: {
                     95:     ox* m = NULL;
                     96:     cmo* c = NULL;
                     97:     int code;
                     98:     char *server = "ox_sm1";
                     99:
                    100:     setbuf(stderr, NULL);
                    101:
                    102:     if (argc>1) {
                    103:         server = argv[1];
                    104:     }
                    105:     fprintf(stderr, "testclient:: I use %s as an OX server.\n", server);
                    106:     sv = ox_start("localhost", "ox", server);
                    107:     if (sv == NULL) {
                    108:         fprintf(stderr, "testclient:: I cannot connect to servers.\n");
                    109:         exit(1);
                    110:     }
                    111:
                    112:     if (strcmp(server, "ox_math")==0) {
                    113:         test_1();
                    114:     }
1.3       ohara     115:
                    116:     setflag_parse(PFLAG_ADDREV);
1.1       ohara     117:
                    118:     while(prompt(), (m = parse()) != NULL) {
                    119:         send_ox(sv->stream, m);
                    120:         if (m->tag == OX_COMMAND) {
                    121:             code = ((ox_command *)m)->command;
                    122:             if (code >= 1024) {
                    123:                 break;
                    124:             }else if (code == SM_popCMO || code == SM_popString) {
                    125:                 receive_ox_tag(sv->stream);
                    126:                 c = receive_cmo(sv->stream);
                    127:                 fprintf(stderr, "testclient:: cmo received.\n");
                    128:                 print_cmo(c);
                    129:             }
                    130:         }
                    131:     }
                    132:
                    133:     ox_reset(sv);
                    134:     fprintf(stderr, "The testclient resets.\n");
                    135:     ox_close(sv);
                    136:     fprintf(stderr, "The testclient halts.\n");
                    137:
                    138:     return 0;
                    139: }

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