Annotation of OpenXM/src/ox_toolkit/zclient.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 <sys/param.h>
! 13:
! 14: #include "mysocket.h"
! 15: #include "ox_toolkit.h"
! 16:
! 17: OXFILE *sv;
! 18:
! 19: int dumpx(OXFILE *oxfp, int n)
! 20: {
! 21: unsigned char buff[2048];
! 22: int i;
! 23: int len = oxf_read(buff, 1, n, oxfp);
! 24:
! 25: fprintf(stderr, "I have read %d byte from socket.\n", len);
! 26: for(i = 0; i < len; i++) {
! 27: fprintf(stderr, "%02x ", buff[i]);
! 28: if (i%20 == 19) {
! 29: fprintf(stderr, "\n");
! 30: }
! 31: }
! 32: fprintf(stderr, "\n");
! 33: return len;
! 34: }
! 35:
! 36: #define SIZE_CMDLINE 8192
! 37:
! 38: static int size = SIZE_CMDLINE;
! 39: static char cmdline[SIZE_CMDLINE];
! 40:
! 41: static int prompt()
! 42: {
! 43: fprintf(stdout, "> ");
! 44: fgets(cmdline, size, stdin);
! 45: init_parser(cmdline);
! 46: }
! 47:
! 48: #define VERSION 0
! 49: #define ID_STRING "v0.0"
! 50:
! 51: int test_0()
! 52: {
! 53: cmo* c = NULL;
! 54: #ifdef DEBUG
! 55: fprintf(stderr, "zclient:: calling ox_mathcap().\n");
! 56: c = (cmo *)ox_mathcap(sv);
! 57: fprintf(stderr, "zclient:: cmo received.(%p)\n", c);
! 58: #else
! 59: c = (cmo *)ox_mathcap(sv);
! 60: #endif
! 61: print_cmo(c);
! 62: fflush(stderr);
! 63:
! 64: mathcap_sysinfo_set(VERSION, ID_STRING, "zclient");
! 65: send_ox_cmo(sv, mathcap_get());
! 66:
! 67: ox_reset(sv);
! 68: send_ox_cmo(sv, (cmo *)new_cmo_string("N[ArcTan[1]]"));
! 69: send_ox_command(sv, SM_executeStringByLocalParser);
! 70: send_ox_command(sv, SM_popCMO);
! 71: receive_ox_tag(sv);
! 72: c = receive_cmo(sv);
! 73: fprintf(stderr, "zclient:: cmo received.\n");
! 74: print_cmo(c);
! 75: }
! 76:
! 77: int test_1()
! 78: {
! 79: cmo *c, *m;
! 80:
! 81: mathcap_sysinfo_set(1000, "test!", "zclient");
! 82: m = mathcap_get();
! 83: fprintf(stderr, "zclient:: test cmo_mathcap.\n");
! 84: send_ox_cmo(sv, m);
! 85: send_ox_command(sv, SM_popCMO);
! 86: receive_ox_tag(sv);
! 87: c = receive_cmo(sv);
! 88: fprintf(stderr, "zclient:: cmo received.(%p)\n", c);
! 89: print_cmo(c);
! 90: fputc('\n', stderr);
! 91: }
! 92:
! 93: static OXFILE *mysocketAccept2(int listened, char *passwd)
! 94: {
! 95: OXFILE *oxfp = oxf_connect_passive(listened);
! 96: if(oxf_confirm_client(oxfp, passwd)) {
! 97: oxf_determine_byteorder_client(oxfp);
! 98: return oxfp;
! 99: }
! 100: oxf_close(oxfp);
! 101: return NULL;
! 102: }
! 103:
! 104: OXFILE *open_server(char *remote_host)
! 105: {
! 106: short port;
! 107: int listen;
! 108: char localhost[MAXHOSTNAMELEN];
! 109: char *passwd = generate_otp();
! 110:
! 111: if (gethostname(localhost, MAXHOSTNAMELEN)==0) {
! 112: fprintf(stderr, "zclient:: localhost = %s.\n", localhost);
! 113: listen = mysocketListen(localhost, &port);
! 114: if (oxc_start(remote_host, port, passwd) != 0) {
! 115: fprintf(stderr, "zclient:: remotehost = %s.\n", remote_host);
! 116: return mysocketAccept2(listen, passwd);
! 117: }
! 118: }
! 119: return NULL;
! 120: }
! 121:
! 122: /* Example:
! 123: zclient
! 124: >(OX_DATA,(CMO_INT32,123))
! 125: >(OX_COMMAND,(SM_popCMO))
! 126: */
! 127:
! 128: int main(int argc, char* argv[])
! 129: {
! 130: OXFILE *ctlp;
! 131: char *server = "ox_sm1";
! 132:
! 133: setbuf(stderr, NULL);
! 134:
! 135: if (argc>1) {
! 136: server = argv[1];
! 137: }
! 138:
! 139: fprintf(stderr, "zclient:: I try to open a server.\n");
! 140: if ((ctlp = open_server("izumi")) == NULL) {
! 141: fprintf(stderr, "zclient:: I cannot open a server.\n");
! 142: exit(1);
! 143: }
! 144: fprintf(stderr, "zclient:: I succeed to open a server.\n");
! 145: return 0;
! 146: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>