=================================================================== RCS file: /home/cvs/OpenXM/src/ox_math/serv1.c,v retrieving revision 1.11 retrieving revision 1.20 diff -u -p -r1.11 -r1.20 --- OpenXM/src/ox_math/serv1.c 2000/11/28 20:16:03 1.11 +++ OpenXM/src/ox_math/serv1.c 2003/01/13 12:04:53 1.20 @@ -1,5 +1,5 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.10 2000/10/10 19:58:30 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.19 2003/01/11 12:38:57 ohara Exp $ */ /* Copyright (C) Katsuyoshi OHARA, 2000. @@ -16,134 +16,103 @@ #include #include #include -#include "serv2.h" +#include "sm.h" -static int send_ox_sync_ball(); +extern OXFILE *stack_oxfp; -static OXFILE *sv; - -static int flag_sigusr1 = 0; -static int flag_sigusr2 = 0; - -/* if in_critical equals to 1 then we do not permit an interrupt. */ -static int in_critical = 0; - -static int set_critical() +/* SM_control_reset_connection */ +static void handler() { - in_critical = 1; + sigset_t newmask, oldmask; + sigemptyset(&newmask); + sigaddset(&newmask, SIGUSR1); + sigprocmask(SIG_SETMASK, &newmask, &oldmask); + ox_printf("signal received.\n"); + exchange_ox_sync_ball(stack_oxfp); + sigprocmask(SIG_SETMASK, &oldmask, NULL); /* unmasked. */ } -static int unset_critical() +static int exchange_ox_sync_ball(OXFILE *oxfp) { - in_critical = 0; -} - -static int critical_p() { - return in_critical; -} - -static int already_send_ox_sync_ball = 0; - -/* SM_control_reset_connection */ -static int handler_reset1() -{ - fprintf(stderr, "signal received.\n"); - signal(SIGUSR1, handler_reset1); - if (!flag_sigusr1) { - flag_sigusr1 = 1; - if(critical_p()) { - send_ox_sync_ball(); - already_send_ox_sync_ball = 1; + int tag; + send_ox_tag(oxfp, OX_SYNC_BALL); + while((tag = receive_ox_tag(oxfp)) != OX_SYNC_BALL) { + if (tag == OX_DATA) { + receive_cmo(oxfp); + }else if (tag == OX_COMMAND) { + receive_int32(oxfp); } } } -static int handler_kill() +int shutdown() { - oxf_close(sv); - exit(1); + oxf_close(stack_oxfp); + ml_exit(); + exit(0); } -static int send_ox_sync_ball() -{ - fprintf(stderr, "sending a sync_ball.\n"); - send_ox_tag(sv, OX_SYNC_BALL); -} +#define VERSION 0x11121400 +#define ID_STRING "2000/11/29" -static int exchange_ox_syncball() +int main() { - int tag; + OXFILE* sv; - while((tag = receive_ox_tag(sv)) != OX_SYNC_BALL) { - /* skipping a message. */ - if (tag == OX_DATA) { - receive_cmo(sv); - }else { - receive_int32(sv); - } - } - fprintf(stderr, "received a sync_ball.\n"); + ox_stderr_init(NULL); + ml_init(); + mathcap_init(VERSION, ID_STRING, "ox_math", NULL, NULL); + + sv = oxf_open(3); + oxf_determine_byteorder_server(sv); + sm(sv); + shutdown(); } /* a part of stack machine. */ -int receive_ox(OXFILE *oxfp) +int sm_receive_ox() { int tag; int code; - tag = receive_ox_tag(oxfp); + tag = receive_ox_tag(stack_oxfp); + if (oxf_error(stack_oxfp)) { + return 0; + } switch(tag) { case OX_DATA: - push(receive_cmo(oxfp)); + push(receive_cmo(stack_oxfp)); break; case OX_COMMAND: - code = receive_sm_command(oxfp); - set_critical(); - execute_sm_command(oxfp, code); - unset_critical(); + code = receive_sm_command(stack_oxfp); + sm_run(code); break; default: - fprintf(stderr, "illeagal message? ox_tag = (%d)\n", tag); - return -1; + ox_printf("illeagal message? ox_tag = (%d)\n", tag); break; } - return 0; + return 1; } -int shutdown() +int sm(OXFILE *oxfp) { - oxf_close(sv); - ml_exit(); - exit(0); -} + fd_set fdmask; + stack_oxfp = oxfp; + stack_extend(); + signal(SIGUSR1, handler); -#define VERSION 0x11121400 -#define ID_STRING "2000/11/29" + FD_ZERO(&fdmask); + FD_SET(oxf_fileno(oxfp), &fdmask); -int main() -{ - sv = oxf_open(3); - - ml_init(); - initialize_stack(); - mathcap_init(VERSION, ID_STRING, "ox_math", NULL, NULL); - - signal(SIGUSR1, handler_reset1); - signal(SIGKILL, handler_kill); - - oxf_determine_byteorder_server(sv); - while(1) { - receive_ox(sv); - if(flag_sigusr1) { - if (!already_send_ox_sync_ball) { - send_ox_sync_ball(); - already_send_ox_sync_ball = 1; - } - exchange_ox_syncball(); - flag_sigusr1 = 0; - already_send_ox_sync_ball = 0; + if (select(5, &fdmask, NULL, NULL, NULL) > 0) { + sigset_t newmask, oldmask; + sigemptyset(&newmask); + sigaddset(&newmask, SIGUSR1); + sigprocmask(SIG_SETMASK, &newmask, &oldmask); + sm_receive_ox(); + sigprocmask(SIG_SETMASK, &oldmask, NULL); /* unmasked. */ } } - shutdown(); + ox_printf("SM: socket(%d) is closed.\n", stack_oxfp->fd); }