//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / testing/mgmt
//
//File: mgmtswitchtest.cpp
//
//Version: $Revision: 1.12 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/12/16 14:15:28 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Kokkonen
//
//Description:
//      Test file for mgmtDelegationBase, mgmtCommand, mgmtSend and toveSwitch_impl.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History:
//
#include <typeinfo>
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include "pf/system.h"
#include "pf/naming.h"
#include "pf/debug.h"
#include "mgmttest.h"
#include "mgmt/mgmtcommand.h"

void printUsage(string argv0);
void runSystem(void);
mgmtCommand makeCommand(void);

main(int argc, char* argv[])
{
    debugOutputCout();

    if (argc > 2)
    {
        pfSystem::init(argc, argv);

        debugUser("creating mgmtDelegationBase ... ");
        mgmtTest *testMgmt = new mgmtTest();
        debugUser("done");

        debugUser("making mgmtTest::createOB ... ");
        testMgmt->createOB_();
        debugUser("done");

        if (argv[1][0] == 'r')
        {
            try
            {
                debugUser("Registering to NameService");
                testMgmt->registerToNameService_("test/mgmt/switch");
                debugUser("done");
                debugUser("Unregistering to NameService");
                testMgmt->unRegisterToNameService_();
                debugUser("done");
            }

            catch (...)
            {
                debugUser("Registering error");
            }
        }

        else if (argv[1][0] == 's')
        {
            debugUser("Running server");
            debugUser("Registering to NameService");
            testMgmt->registerToNameService_("test/mgmt/switch");
            debugUser("Waiting for connection ...");
            runSystem();
        }

        else if (argv[1][0] == 'c')
        {
            debugUser("Running client");
            debugUser("Get server from NameServer and register9ing to other");
            try
            {
                testMgmt->registerToOther_("test/mgmt/switch");

                debugUser("... OK");
                string name_("testName");
                debugString("Sending to server sendGet with name", name_);
                string result("");
                result = testMgmt->sendGet(name_);
                debugString("Result", result);
                debugUser("Sending to server sendSet with");
                name_ = "testName2";
                string value_("Just a test");
                debugString("name", name_);
                debugString("value", value_);
                testMgmt->sendSet(name_, value_);
                debugUser("Sending to server sendExecute");
                mgmtCommand testCommand = makeCommand();
                testMgmt->sendExecute(testCommand);
                debugUser("Execute sended");

                debugUser("Exiting from server");
                testMgmt->unRegisterToOther_();
            }

            catch (...)
            {
                debugUser("... Failed");
            }
        }

        else if (argv[1][0] == 'x')
        {
            try
            {
                debugUser("Removing test/mgmt/switch from nameserver");

                CosNaming_Name name = pfNaming::parseName("test/mgmt/switch");

                pfNaming::instance()->unbind(name);
                debugUser("Removed");
            }

            catch (pfMethodFailed &exception)
            {
                debugUser("Not found in nameserver");
                exception.printInfo();
            }

            try
            {
                debugUser("Removing test/mgmt from nameserver");

                CosNaming_Name name = pfNaming::parseName("test/mgmt");

                pfNaming::instance()->unbind(name);
                debugUser("Removed");
            }

            catch (pfMethodFailed &exception)
            {
                debugUser("Not found in nameserver");
                exception.printInfo();
            }

            try
            {
                debugUser("Removing test from nameserver");

                CosNaming_Name name = pfNaming::parseName("test");

                pfNaming::instance()->unbind(name);
                debugUser("Removed");
            }

            catch (pfMethodFailed &exception)
            {
                debugUser("Not found in nameserver");
                exception.printInfo();
            }

        }

        else
        {
            debugUser("Invalid first parameter");
            printUsage(argv[0]);
            exit(1);
        }
    }
    else
    {
        printUsage(argv[0]);
    }
}

void printUsage(string argv0_)
{
        debugUser("Usage: " + argv0_ + " [r/s/c] XXX");
        debugUser("r <-> register and unregister to nameservice");
        debugUser("s <-> Running server");
        debugUser("c <-> Running client");
        debugUser("x <-> Removes test/mgmt/switch from nameserver");
        debugUser("Where XXX = -ORBnaming `cat /projects/IORs/Naming.ior`");
}

void runSystem(void)
{
    // RUN SYSTEM !!!
    pfSystem::instance()->run();             
}

mgmtCommand makeCommand(void)
{
    string command("testCommand");
    string name("name1");
    string value("value1");
    string type("type1");
    string commandOut("");
    string nameOut("");
    string valueOut("");
    string typeOut("");

    debugString("Creating command", command);
    mgmtCommand newCommand(command);
    newCommand.addParameter(name, value, type);
    name = "name2";
    value = "value2";
    type = "type2";
    newCommand.addParameter(name, value, type);
    name = "name3";
    value = "value3";
    type = "type3";
    newCommand.addParameter(name, value);

    debugUser("Command created");

    return newCommand;
}
