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

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

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