=================================================================== RCS file: /home/cvs/OpenXM/src/ox_math/serv1.c,v retrieving revision 1.1.1.1 retrieving revision 1.22 diff -u -p -r1.1.1.1 -r1.22 --- OpenXM/src/ox_math/serv1.c 1999/10/29 08:06:41 1.1.1.1 +++ OpenXM/src/ox_math/serv1.c 2003/02/13 01:29:38 1.22 @@ -1,131 +1,114 @@ /* -*- mode: C; coding: euc-japan -*- */ -/* $OpenXM$ */ -/* $Id: serv1.c,v 1.1.1.1 1999/10/29 08:06:41 ohara Exp $ */ +/* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.21 2003/02/12 08:28:41 ohara Exp $ */ +/* + Copyright (C) Katsuyoshi OHARA, 2000. + Portions copyright 1999 Wolfram Research, Inc. + + You must see OpenXM/Copyright/Copyright.generic. + The MathLink Library is licensed from Wolfram Research Inc.. + See OpenXM/Copyright/Copyright.mathlink for detail. +*/ + #include #include #include #include -#include #include -#include "ox.h" -#include "serv2.h" +#include +#include "sm.h" -extern MLINK lp; -static int sv_read = 3; -static int sv_write = 4; +extern OXFILE *stack_oxfp; -static int flag_sigusr1 = 0; -static int flag_sigusr2 = 0; - -/* 1 のとき割り込み禁止 */ -static int in_critical = 0; - -static int set_critical() +static int exchange_ox_sync_ball(OXFILE *oxfp) { - in_critical = 1; -} - -static int unset_critical() -{ - 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(sv_write); - already_send_ox_sync_ball = 1; + int tag; + ox_printf("exchanging OX_SYNC_BALL\n"); + 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() { - close(3); - close(4); - exit(1); + oxf_close(stack_oxfp); + ml_exit(); + exit(0); } -static int send_ox_sync_ball(int fd) -{ - fprintf(stderr, "sending a sync_ball.\n"); - send_ox_tag(fd, OX_SYNC_BALL); -} +/* (Heisei)15/02/01 */ +#define VERSION 0x15020100 +#define ID_STRING "2003/02/01" -static int exchange_ox_syncball(int fd) +int main() { - int tag; + OXFILE* sv; - while((tag = receive_ox_tag(fd)) != OX_SYNC_BALL) { - /* skipping a message. */ - if (tag == OX_DATA) { - receive_cmo(fd); - }else { - receive_int32(fd); - } - } - fprintf(stderr, "received a sync_ball.\n"); + ox_stderr_init(stderr); + ml_init(); + mathcap_init(VERSION, ID_STRING, "ox_math", NULL, NULL); + + sv = oxf_open(3); + oxf_determine_byteorder_server(sv); + sm(sv); + shutdown(); } -/* スタックマシン部分 */ -int receive_ox(int fd_read, int fd_write) +/* a part of stack machine. */ +int sm_receive_ox() { int tag; int code; - tag = receive_ox_tag(fd_read); + tag = receive_ox_tag(stack_oxfp); + if (oxf_error(stack_oxfp)) { + return 0; + } switch(tag) { case OX_DATA: - push(receive_cmo(fd_read)); + push(receive_cmo(stack_oxfp)); break; case OX_COMMAND: - code = receive_sm_command(fd_read); - set_critical(); - execute_sm_command(fd_write, 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 OX message(%d)\n", tag); break; } - return 0; + return 1; } -int main() +int sm(OXFILE *oxfp) { - MATH_init(); - initialize_stack(); + int i=0; + fd_set fdmask; + stack_oxfp = oxfp; + stack_extend(); + sm_siginit(); - signal(SIGUSR1, handler_reset1); - signal(SIGKILL, handler_kill); + FD_ZERO(&fdmask); + FD_SET(oxf_fileno(oxfp), &fdmask); - /* バイトオーダの決定 */ - decideByteOrder2(sv_read, sv_write, 0); - - while(1) { - receive_ox(sv_read, sv_write); - if(flag_sigusr1) { - if (!already_send_ox_sync_ball) { - send_ox_sync_ball(sv_write); - already_send_ox_sync_ball = 1; - } - exchange_ox_syncball(sv_read); - flag_sigusr1 = 0; - already_send_ox_sync_ball = 0; + for (i=0; ; i++) { + sm_sigunmask(); + ox_printf("phase%d: select\n",i); + if (select(5, &fdmask, NULL, NULL, NULL) > 0) { + sm_sigmask(); + ox_printf("phase%d: receiving\n",i); + sm_receive_ox(); } + sm_sigmask(); + ox_printf("phase%d: clearing(%d)\n",i,sm_state_interrupting()); + if (sm_state_interrupting()) { + exchange_ox_sync_ball(stack_oxfp); + sm_state_clear_interrupting(); + } } - MATH_exit(); + ox_printf("ox_math::socket(%d) is closed.\n", stack_oxfp->fd); }