//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / TESTING
//
//File: ilmitestmain.cpp
//
//Version: $Revision: 1.8 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/12/16 14:14:34 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include <typeinfo>
#include <iostream.h>
#include <string.h>
#include <OB/CORBA.h>
#include "pf/system.h"
#include "pf/debug.h"
#include "pf/naming.h"
#include "mgmt/toveswitch_impl.h"
#include "mgmt/mgmtcommand.h"

void printUsage(char *name_)
{
    cerr << endl;
    cerr << "Usage: " << name_ << " [p PORT] [c POINT CODE] ";
    cerr << " -ORBnaming `cat /projects/IORs/Naming.ior`" << endl;
    cerr << endl;
    cerr << "- [p PORT]  Port number" << endl;
    cerr << "- [c POINT CODE]  Point code value of the switch" << endl;
    cerr << "- [n COMMAND NAME] Name of command for excute" << endl;
    cerr << endl;
    cerr << "Default values: PORT = 0, POINT CODE = 10" << endl;
    exit(0);
}

//
// main function
//

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

    string port = "0";
    string pointCode = "10";
    int count = 1;
    string commandName("start");
    
    while (count < argc)
    {
        switch (*argv[count++])
        {
            case 'p':
                port = argv[count++];
                break;
            case 'c':
                pointCode = argv[count++];
                break;
            case 'n' :
                commandName = argv[count++];
                break;
            case '-':
                if (strcmp(argv[--count], "-ORBnaming") != 0)
                {
                    printUsage(argv[0]);
                }
                else
                {
                    count += 2;
                }
                break;
            case 'h':
            default :
                printUsage(argv[0]);
                break;
        }
    }

    pfSystem::init(argc, argv);

    // Construct name for naming component
    string tmpName = pointCode;
    tmpName += "/ports/";
    tmpName += port;
    tmpName += "/ilmi";

    CosNaming_Name name = pfNaming::parseName(tmpName);
    
    CORBA_Object_ptr tmp = 0;
    toveSwitch_switchInterface_ptr server;
    
    try
    {
        tmp = pfNaming::instance()->resolve(name);
        server = toveSwitch_switchInterface::_narrow(tmp);
    }
    catch (...)
    {
        debugUser("resolve in NS failed");
        exit(1);
    }
    
    mgmtCommand command(commandName);
    try
    {
	toveSwitch_ParameterList parameterList = command.getList();
        server->execute(commandName.c_str(), parameterList);
    }
    catch (...)
    {
        cerr << "Command >" << commandName << " < failed" << endl;
        exit(1);
    }
    
//    pfSystem::instance()->run();

    return 0;
}


