Annotation of OpenXM/src/ox_math/serv1.c, Revision 1.19
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.19 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.18 2002/04/11 19:53:40 ohara Exp $ */
1.8 ohara 3:
4: /*
5: Copyright (C) Katsuyoshi OHARA, 2000.
6: Portions copyright 1999 Wolfram Research, Inc.
7:
8: You must see OpenXM/Copyright/Copyright.generic.
9: The MathLink Library is licensed from Wolfram Research Inc..
10: See OpenXM/Copyright/Copyright.mathlink for detail.
11: */
1.1 ohara 12:
13: #include <stdio.h>
14: #include <stdlib.h>
15: #include <unistd.h>
16: #include <signal.h>
17: #include <mathlink.h>
1.9 ohara 18: #include <ox_toolkit.h>
1.13 ohara 19: #include "sm.h"
1.1 ohara 20:
1.13 ohara 21: extern OXFILE *stack_oxfp;
1.1 ohara 22:
23: /* SM_control_reset_connection */
1.15 ohara 24: static void handler()
1.1 ohara 25: {
1.17 ohara 26: sigset_t newmask, oldmask;
27: sigemptyset(&newmask);
28: sigaddset(&newmask, SIGUSR1);
29: sigprocmask(SIG_SETMASK, &newmask, &oldmask);
1.19 ! ohara 30: fprintf(ox_stderr, "signal received.\n");
1.16 ohara 31: exchange_ox_sync_ball(stack_oxfp);
1.17 ohara 32: sigprocmask(SIG_SETMASK, &oldmask, NULL); /* unmasked. */
1.1 ohara 33: }
34:
1.15 ohara 35: static int exchange_ox_sync_ball(OXFILE *oxfp)
1.1 ohara 36: {
37: int tag;
1.15 ohara 38: send_ox_tag(oxfp, OX_SYNC_BALL);
39: while((tag = receive_ox_tag(oxfp)) != OX_SYNC_BALL) {
1.1 ohara 40: if (tag == OX_DATA) {
1.15 ohara 41: receive_cmo(oxfp);
42: }else if (tag == OX_COMMAND) {
43: receive_int32(oxfp);
1.1 ohara 44: }
45: }
46: }
47:
1.14 ohara 48: int shutdown()
49: {
50: oxf_close(stack_oxfp);
51: ml_exit();
52: exit(0);
53: }
54:
55: #define VERSION 0x11121400
56: #define ID_STRING "2000/11/29"
57:
58: int main()
59: {
60: OXFILE* sv;
61:
1.19 ! ohara 62: ox_stderr_init(NULL);
1.14 ohara 63: ml_init();
64: mathcap_init(VERSION, ID_STRING, "ox_math", NULL, NULL);
65:
66: sv = oxf_open(3);
67: oxf_determine_byteorder_server(sv);
68: sm(sv);
69: shutdown();
70: }
71:
1.7 ohara 72: /* a part of stack machine. */
1.13 ohara 73: int sm_receive_ox()
1.1 ohara 74: {
75: int tag;
76: int code;
77:
1.13 ohara 78: tag = receive_ox_tag(stack_oxfp);
79: if (oxf_error(stack_oxfp)) {
80: return 0;
81: }
1.1 ohara 82: switch(tag) {
83: case OX_DATA:
1.13 ohara 84: push(receive_cmo(stack_oxfp));
1.1 ohara 85: break;
86: case OX_COMMAND:
1.13 ohara 87: code = receive_sm_command(stack_oxfp);
88: sm_run(code);
1.1 ohara 89: break;
90: default:
1.19 ! ohara 91: fprintf(ox_stderr, "illeagal message? ox_tag = (%d)\n", tag);
1.1 ohara 92: break;
93: }
1.13 ohara 94: return 1;
1.1 ohara 95: }
96:
1.13 ohara 97: int sm(OXFILE *oxfp)
98: {
1.16 ohara 99: fd_set fdmask;
1.13 ohara 100: stack_oxfp = oxfp;
101: stack_extend();
1.16 ohara 102: signal(SIGUSR1, handler);
103:
104: FD_ZERO(&fdmask);
105: FD_SET(oxf_fileno(oxfp), &fdmask);
1.15 ohara 106:
1.16 ohara 107: while(1) {
1.18 ohara 108: if (select(5, &fdmask, NULL, NULL, NULL) > 0) {
1.17 ohara 109: sigset_t newmask, oldmask;
110: sigemptyset(&newmask);
111: sigaddset(&newmask, SIGUSR1);
112: sigprocmask(SIG_SETMASK, &newmask, &oldmask);
1.16 ohara 113: sm_receive_ox();
1.17 ohara 114: sigprocmask(SIG_SETMASK, &oldmask, NULL); /* unmasked. */
1.1 ohara 115: }
116: }
1.19 ! ohara 117: fprintf(ox_stderr, "SM: socket(%d) is closed.\n", stack_oxfp->fd);
1.1 ohara 118: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>