//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project
//
//File: viciface_impl.cpp
//
//Version: $Revision: 1.3 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/05/29 12:18:39 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Jussi Turunen
//
//Description:
//      Implementation for vic's IDL-interface implementation class.
//      Fork - exec is done to start the vic client. Address comes from
//      the CORBA client. Kill() is used to stop the vic client. Vic's
//      own signal handler takes care of the shutdown receinving SIGINT.
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include <OB/CORBA.h>
#include "viciface_impl.h"

#include <signal.h>
#include <unistd.h>


vic_control_impl :: vic_control_impl(void)
    : vic_control_skel(),
      _cpid(0)
{
    return;
}

vic_control_impl :: ~vic_control_impl(void)
{
    return;
}

void vic_control_impl :: start(const char* address_)
{
    cout << "got start " << address_ << endl;

    if ((_cpid = fork()) < 0)
    {
        throw vic_control::error("fork failed");
    }
    else if ( _cpid == 0 )
    {
        cout << "In child: execing ..." << endl;
        if ( (execlp("./viccl", "./viccl", "-n", "atm",
                     address_, (char *)NULL )) < 0 )
        {
            throw vic_control::error("exec failed");
        }
    }
    else
    {
        cout << "In main: forked ok, now waiting for stop" << endl;
    }
    return;
}

void vic_control_impl :: stop(void)
{
    cout << "got stop" << endl;
    if (_cpid != 0)
    {
        if ( (kill(_cpid, SIGINT)) < 0 )
        {
            cout << "nyt lähti throw vic_control::error" << endl;
            throw vic_control::error("kill failed");
        }
    }
    return;
}


