//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project
//
//File: server.cpp
//
//Version: $Revision: 1.10 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/10/06 14:45:32 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//	Vesa-Matti Puro
//      Jussi Turunen
//
//Description:
//     Instantiates the conduit tree for this demo server:
//
//
//            ___ IDL 
//      _______|________
//      \ pfTestAdapter/
//       --------------
//
//     The pfTestAdapter instance receives a message from an IDL
//     interface and forwards it to en/decoder which encodes messages
//     from pfTestAdapter to a frame and decodes messages from
//     CPCSadapter to a otMessage::SerializedMessage.
//
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 

#include <OB/CORBA.h>
#include <OB/Util.h>
#include <typeinfo>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_FSTREAM
#   include <fstream>
#else
#   include <fstream.h>
#endif
#include "pf/system.h"
#include "pf/conduit.h"
#include "pf/debug.h"

#include "../adapter/forwarder.h"

void saveIOR(CORBA_ORB_ptr orb_, const string &filename_, const string &s_)
{
    try
    {

        //
        // NOTE: use C FILEs rather than C++ file streams, because the
        // latter don't work with g++ when RTTI is in use
        //
        FILE *fp = fopen(filename_.c_str(), "w");
        if(fp == 0)
        {
            debugString("oesForwarder: can't open `", filename_);
            exit(-1);
        }

        fprintf(fp, "%s\n", s_.c_str());
        fclose(fp);
    }
#ifdef __GNUG__
    catch(CORBA_COMM_FAILURE& ex)
#else
    catch(CORBA_SystemException& ex)
#endif
    {
        OBPrintException(ex);
    }
    return;
}

int main(int argc, char* argv[], char*[])
{
    debugOutputCout();
    const string forwarder("forwarder");

    pfSystem::init(argc, argv);

    try
    {
        oesForwarder f(pfSystem::instance()->getORB());
        string s = (pfSystem::instance()->getORB())->object_to_string(&f);
        saveIOR(pfSystem::instance()->getORB(), forwarder + ".ref", s); 

        while (1)
        {
            pfSystem::instance()->next();
        } 
    }
    catch(...)
    {
        debugUser("ETS ERROR: forwardermain: received exception");
	exit(-1);
    }

    return 0;
}

