//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project
//
//File: client.cpp
//
//Version: $Revision: 1.2 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/09/07 18:43:33 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//	Vesa-Matti Puro
//
//Description:
//      A client implementation for testing debugging log interface. 
//      Sends a debugging event to an object whose reference has been
//	read from a file defined in the command line.
//
//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/debug.h"
#include "pf/frame.h"
#include "log.h"

#include "pf/system.h"
#include "sf/orbscheduler.h"
#include "common/orbnamingservice.h"

otLog_var readOneRef(char *refFile_, CORBA_ORB_var orb_)
{
    otLog_var adapter;
    try
    {
        FILE *fp = fopen(refFile_, "r");
        if (fp == 0)
        {
            debugString("can't open ", refFile_);
            exit(1);
        }
	
	char s[1000];
        fscanf(fp, "%s", s);
        fclose(fp);
	
	CORBA_Object_var obj = orb_->string_to_object(s);
	assert(!CORBA_is_nil(obj));
	
	adapter = otLog::_narrow(obj);
	assert(!CORBA_is_nil(adapter));
    }
    catch (...)
    {
        debugUser("catch");
    }
    return adapter;
}

int main(int argc, char* argv[], char*[])
{
    try
    {
        
	//CORBA_ORB_var orb = CORBA_ORB_init(argc, argv);
        //char* refFile = (char *) malloc (30*sizeof(char));
        otLog_var debugIOR;
        
//        if (argc == 2)
//        {
//            strcpy(refFile, argv[1]);
//            debugIOR = readOneRef(refFile, orb);
//        }
//        else
//        {
//            cout << "./client filecontainingtheiorofthereceiver.ref" << endl;
//            exit(-1);
//        }

        // Command line arguments
        if (argc != 3)
        {
            cout << "\nUSAGE: " << argv[0];
            cout << " -ORBnaming `cat /projects/IORs/Naming.ior`\n" << endl;
            exit(-1);
        }

        try
        {
            // Init ORB
            sfScheduler *scheduler = sfORBScheduler::instance();
            pfSystem::instance(scheduler);
            sfORBScheduler::initORB(argc, argv);
        }
        catch (...)
        {
            cerr << "ORB init failed" << endl;
            exit(-1);
        }
        // Resolve object reference for server
        CosNaming_Name name =
            orbNamingService::instance()->parseName("log/otlog");
        try
        {
            CORBA_Object_ptr obj = orbNamingService::instance()->resolve(name);
            debugIOR = otLog::_narrow(obj);
        }
        catch (...)
        {
            cerr << "Resolve object reference failed" << endl;
            exit(-1);
        }
        
        CORBA_String_var sender = CORBA_string_dup("MTC");
        CORBA_Long seq = 0;
        CORBA_String_var tim = CORBA_string_dup("12:00:00");
        CORBA_Long lin = 0;
        CORBA_String_var lab = CORBA_string_dup("");
        CORBA_String_var pco = CORBA_string_dup("LT_PCO");
        CORBA_String_var ide1 = CORBA_string_dup("BGN");
        CORBA_String_var ide2 = CORBA_string_dup("BGAK");

        char c;
        debugOutputCout();
        debugUser("X exits and U prints usage instructions");
        while (1)
        {
            cout << "send event >";
            cin >> c;
            cout << endl;

            if (c == 'U')
            {
                cout << "s = send" << endl;
                cout << "r = receive" << endl;
                cout << "----------------------------------------" << endl;
                cout << "U = usage" << endl;
                cout << "X = exit" << endl;
            }
            else if (c == 'X')
            {
                break;
            }
            else if (c == 'a')
            {
                //debugIOR->send(identifierXX, messageXX);
            }
            else if (c == 's')
            {
                otLog::AssignmentList ass;
                otLog::TimerOpList ops;
                otMessage::SerializedMessage mes;

                debugIOR->sendEvent (sender,
                           seq,
                           tim,
                           lin,
                           lab,
                           ass,
                           ops,
                           pco,
                           ide1,
                           mes);
            }
            else if (c == 'r')
            {
                otLog::AssignmentList ass;
                otLog::TimerOpList ops;
                otMessage::SerializedMessage mes;

                debugIOR->receiveEvent (sender,
                              seq,
                              tim,
                              lin,
                              lab,
                              ass,
                              ops,
                              pco,
                              ide2,
                              mes);
            }
            else
            {
                continue;
            }

            ++seq;
        }
    }
#ifdef __GNUG__
    catch(CORBA_COMM_FAILURE& ex)
#else
    catch(CORBA_SystemException& ex)
#endif
    {
	OBPrintException(ex);
	return 1;
    }
    return 0;
}

