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

1.1       ohara       1: /* -*- mode: C -*- */
1.14    ! ohara       2: /* $OpenXM: OpenXM/src/ox_toolkit/testclient.c,v 1.13 2003/09/15 09:31:42 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.10      ohara      24:     ox_printf("I have read %d byte from socket(%d).\n", len, oxfp->fd);
1.1       ohara      25:     for(i = 0; i < len; i++) {
1.10      ohara      26:         ox_printf("%02x ", buff[i]);
1.1       ohara      27:         if (i%20 == 19) {
1.10      ohara      28:             ox_printf("\n");
1.1       ohara      29:         }
                     30:     }
1.10      ohara      31:     ox_printf("\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.13      ohara      40: static void prompt()
1.1       ohara      41: {
1.3       ohara      42:     fprintf(stdout, "> ");
                     43:     fgets(cmdline, size, stdin);
1.1       ohara      44: }
                     45:
1.3       ohara      46: #define VERSION 0x11121500
1.5       ohara      47: #define ID_STRING  "v0.11121500"
1.7       ohara      48:
1.13      ohara      49: void test_0()
1.1       ohara      50: {
                     51:     cmo* c = NULL;
1.10      ohara      52:     ox_printf("testclient:: calling ox_mathcap().\n");
1.13      ohara      53:     c = (cmo *)ox_mathcap(sv);
1.10      ohara      54:     ox_printf("testclient:: cmo received.(%p)\n", c);
1.1       ohara      55:     print_cmo(c);
1.5       ohara      56:
1.6       ohara      57:     mathcap_init(VERSION, ID_STRING, "testclient", NULL, NULL);
1.13      ohara      58:     send_ox_cmo(sv, (cmo *)oxf_cmo_mathcap(sv));
1.1       ohara      59:
                     60:     ox_reset(sv);
1.5       ohara      61:     send_ox_cmo(sv, (cmo *)new_cmo_string("N[ArcTan[1]]"));
                     62:     send_ox_command(sv, SM_executeStringByLocalParser);
                     63:     send_ox_command(sv, SM_popCMO);
                     64:     receive_ox_tag(sv);
                     65:     c = receive_cmo(sv);
1.10      ohara      66:     ox_printf("testclient:: cmo received.\n");
1.1       ohara      67:     print_cmo(c);
                     68: }
                     69:
1.13      ohara      70: void test_1()
1.1       ohara      71: {
1.5       ohara      72:     cmo *c, *m;
                     73:
1.6       ohara      74:        mathcap_init(1000, "test!", "testclient", NULL, NULL);
1.13      ohara      75:        m = (cmo *)oxf_cmo_mathcap(sv);
1.10      ohara      76:     ox_printf("testclient:: test cmo_mathcap.\n");
1.5       ohara      77:     send_ox_cmo(sv, m);
                     78:     send_ox_command(sv, SM_popCMO);
                     79:     receive_ox_tag(sv);
                     80:     c = receive_cmo(sv);
1.10      ohara      81:     ox_printf("testclient:: cmo received.(%p)\n", c);
1.1       ohara      82:     print_cmo(c);
1.10      ohara      83:        ox_printf("\n");
1.1       ohara      84: }
1.2       takayama   85:
                     86: /*  Example:
                     87:   testclient
                     88:   >(OX_DATA,(CMO_INT32,123))
                     89:   >(OX_COMMAND,(SM_popCMO))
1.3       ohara      90:  */
1.1       ohara      91:
                     92: int main(int argc, char* argv[])
                     93: {
                     94:     ox* m = NULL;
                     95:     cmo* c = NULL;
                     96:     int code;
                     97:     char *server = "ox_sm1";
                     98:
1.11      ohara      99:     ox_stderr_init(stderr);
1.1       ohara     100:
                    101:     if (argc>1) {
                    102:         server = argv[1];
                    103:     }
1.10      ohara     104:     ox_printf("testclient:: I use %s as an OX server.\n", server);
1.5       ohara     105: /*    sv = ox_start("localhost", "ox", server);  */
1.1       ohara     106:     if (sv == NULL) {
1.10      ohara     107:         ox_printf("testclient:: I cannot connect to servers.\n");
1.1       ohara     108:         exit(1);
                    109:     }
                    110:
                    111:     if (strcmp(server, "ox_math")==0) {
                    112:         test_1();
                    113:     }
1.3       ohara     114:
1.14    ! ohara     115:     while(prompt(), (m = ox_parse_lisp(cmdline)) != NULL) {
1.5       ohara     116:         send_ox(sv, m);
1.1       ohara     117:         if (m->tag == OX_COMMAND) {
                    118:             code = ((ox_command *)m)->command;
                    119:             if (code >= 1024) {
                    120:                 break;
                    121:             }else if (code == SM_popCMO || code == SM_popString) {
1.5       ohara     122:                 receive_ox_tag(sv);
                    123:                 c = receive_cmo(sv);
1.10      ohara     124:                 ox_printf("testclient:: cmo received.\n");
1.1       ohara     125:                 print_cmo(c);
                    126:             }
                    127:         }
                    128:     }
                    129:
                    130:     ox_reset(sv);
1.10      ohara     131:     ox_printf("The testclient resets.\n");
1.1       ohara     132:     ox_close(sv);
1.10      ohara     133:     ox_printf("The testclient halts.\n");
1.1       ohara     134:
                    135:     return 0;
                    136: }

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