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

File: [local] / OpenXM / src / ox_math / serv1.c (download)

Revision 1.16, Fri Dec 22 04:06:37 2000 UTC (23 years, 5 months ago) by ohara
Branch: MAIN
CVS Tags: RELEASE_1_2_1
Changes since 1.15: +15 -17 lines

A signal handler is rewritten.

/* -*- mode: C; coding: euc-japan -*- */
/* $OpenXM: OpenXM/src/ox_math/serv1.c,v 1.16 2000/12/22 04:06:37 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <mathlink.h>
#include <ox_toolkit.h>
#include "sm.h"

extern OXFILE *stack_oxfp;

/* SM_control_reset_connection */
static void handler()
{
    int mask = sigsetmask(sigmask(SIGUSR1));
    fprintf(stderr, "signal received.\n");
    exchange_ox_sync_ball(stack_oxfp);
    sigsetmask(mask); /* unmasked. */
}

static int exchange_ox_sync_ball(OXFILE *oxfp)
{
    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);
        }
    }
}

int shutdown()
{
    oxf_close(stack_oxfp);
    ml_exit();
    exit(0);
}

#define VERSION 0x11121400
#define ID_STRING  "2000/11/29"

int main()
{
    OXFILE* sv;

    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 sm_receive_ox()
{
    int tag;
    int code;

    tag = receive_ox_tag(stack_oxfp);
    if (oxf_error(stack_oxfp)) {
        return 0;
    }
    switch(tag) {
    case OX_DATA:
        push(receive_cmo(stack_oxfp));
        break;
    case OX_COMMAND:
        code = receive_sm_command(stack_oxfp);
        sm_run(code);
        break;
    default:
        fprintf(stderr, "illeagal message? ox_tag = (%d)\n", tag);
        break;
    }
    return 1;
}

int sm(OXFILE *oxfp)
{
    int mask = siggetmask();
    fd_set fdmask;
    stack_oxfp = oxfp;
    stack_extend();
    signal(SIGUSR1, handler);

    FD_ZERO(&fdmask);
    FD_SET(oxf_fileno(oxfp), &fdmask);

    while(1) {
        if (select(2, &fdmask, NULL, NULL, NULL) > 0) {
            sigsetmask(sigmask(SIGUSR1));
            sm_receive_ox();
        }
        sigsetmask(mask); /* unmasked. */
    }
    fprintf(stderr, "SM: socket(%d) is closed.\n", stack_oxfp->fd);
}