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

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

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