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