[BACK]Return to serv1.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_math

Annotation of OpenXM/src/ox_math/serv1.c, Revision 1.13

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.13    ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.12 2000/12/03 15:19:23 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.10      ohara      21: static int send_ox_sync_ball();
1.2       ohara      22:
1.13    ! ohara      23: extern OXFILE *stack_oxfp;
1.1       ohara      24:
                     25: static int flag_sigusr1 = 0;
                     26: static int flag_sigusr2 = 0;
                     27:
1.7       ohara      28: /* if in_critical equals to 1 then we do not permit an interrupt. */
1.1       ohara      29: static int in_critical = 0;
                     30:
                     31: static int set_critical()
                     32: {
                     33:     in_critical = 1;
                     34: }
                     35:
                     36: static int unset_critical()
                     37: {
                     38:     in_critical = 0;
                     39: }
                     40:
                     41: static int critical_p() {
                     42:     return in_critical;
                     43: }
                     44:
                     45: static int already_send_ox_sync_ball = 0;
                     46:
                     47: /* SM_control_reset_connection */
                     48: static int handler_reset1()
                     49: {
                     50:     fprintf(stderr, "signal received.\n");
                     51:     signal(SIGUSR1, handler_reset1);
                     52:     if (!flag_sigusr1) {
                     53:         flag_sigusr1 = 1;
                     54:         if(critical_p()) {
1.10      ohara      55:             send_ox_sync_ball();
1.1       ohara      56:             already_send_ox_sync_ball = 1;
                     57:         }
                     58:     }
                     59: }
                     60:
                     61: static int handler_kill()
                     62: {
1.13    ! ohara      63:     oxf_close(stack_oxfp);
1.1       ohara      64:     exit(1);
                     65: }
                     66:
1.10      ohara      67: static int send_ox_sync_ball()
1.1       ohara      68: {
                     69:     fprintf(stderr, "sending a sync_ball.\n");
1.13    ! ohara      70:     send_ox_tag(stack_oxfp, OX_SYNC_BALL);
1.1       ohara      71: }
                     72:
1.10      ohara      73: static int exchange_ox_syncball()
1.1       ohara      74: {
                     75:     int tag;
                     76:
1.13    ! ohara      77:     while((tag = receive_ox_tag(stack_oxfp)) != OX_SYNC_BALL) {
1.1       ohara      78:         /* skipping a message. */
                     79:         if (tag == OX_DATA) {
1.13    ! ohara      80:             receive_cmo(stack_oxfp);
1.1       ohara      81:         }else {
1.13    ! ohara      82:             receive_int32(stack_oxfp);
1.1       ohara      83:         }
                     84:     }
                     85:     fprintf(stderr, "received a sync_ball.\n");
                     86: }
                     87:
1.7       ohara      88: /* a part of stack machine. */
1.13    ! ohara      89: int sm_receive_ox()
1.1       ohara      90: {
                     91:     int tag;
                     92:     int code;
                     93:
1.13    ! ohara      94:     tag = receive_ox_tag(stack_oxfp);
        !            95:     if (oxf_error(stack_oxfp)) {
        !            96:         return 0;
        !            97:     }
1.1       ohara      98:     switch(tag) {
                     99:     case OX_DATA:
1.13    ! ohara     100:         push(receive_cmo(stack_oxfp));
1.1       ohara     101:         break;
                    102:     case OX_COMMAND:
1.13    ! ohara     103:         code = receive_sm_command(stack_oxfp);
1.1       ohara     104:         set_critical();
1.13    ! ohara     105:         sm_run(code);
1.1       ohara     106:         unset_critical();
                    107:         break;
                    108:     default:
                    109:         fprintf(stderr, "illeagal message? ox_tag = (%d)\n", tag);
1.13    ! ohara     110:         return 0;
1.1       ohara     111:         break;
                    112:     }
1.13    ! ohara     113:     return 1;
1.1       ohara     114: }
                    115:
1.4       ohara     116: int shutdown()
                    117: {
1.13    ! ohara     118:     oxf_close(stack_oxfp);
1.6       ohara     119:     ml_exit();
                    120:     exit(0);
1.4       ohara     121: }
                    122:
1.11      ohara     123: #define VERSION 0x11121400
                    124: #define ID_STRING  "2000/11/29"
                    125:
1.13    ! ohara     126: int oxf_error(OXFILE *oxfp)
        !           127: {
        !           128:     int e = oxfp->error;
        !           129:     if (e != 0) {
        !           130:         oxfp->error = 0;
        !           131:     }
        !           132:     return e;
        !           133: }
        !           134:
1.1       ohara     135: int main()
                    136: {
1.13    ! ohara     137:     OXFILE* sv;
1.10      ohara     138:
1.6       ohara     139:     ml_init();
1.11      ohara     140:     mathcap_init(VERSION, ID_STRING, "ox_math", NULL, NULL);
1.1       ohara     141:
                    142:     signal(SIGUSR1, handler_reset1);
                    143:     signal(SIGKILL, handler_kill);
                    144:
1.13    ! ohara     145:     sv = oxf_open(3);
        !           146:     oxf_determine_byteorder_server(sv);
        !           147:     sm(sv);
        !           148:     shutdown();
        !           149: }
1.1       ohara     150:
1.13    ! ohara     151: int sm(OXFILE *oxfp)
        !           152: {
        !           153:     stack_oxfp = oxfp;
        !           154:     stack_extend();
        !           155:     while(sm_receive_ox()) {
1.1       ohara     156:         if(flag_sigusr1) {
                    157:             if (!already_send_ox_sync_ball) {
1.10      ohara     158:               send_ox_sync_ball();
1.1       ohara     159:                 already_send_ox_sync_ball = 1;
                    160:             }
1.10      ohara     161:             exchange_ox_syncball();
1.1       ohara     162:             flag_sigusr1 = 0;
                    163:             already_send_ox_sync_ball = 0;
                    164:         }
                    165:     }
1.13    ! ohara     166:     fprintf(stderr, "SM: socket(%d) is closed.\n", stack_oxfp->fd);
1.1       ohara     167: }

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