//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / testing/switch
//
//File: switchtestimpl.cpp
//
//Version: $Revision: 1.6 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/07/09 13:14:02 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//      See corresponding header file.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 

#include "switchtestimpl.h"
#include "mgmt/mgmtcommand.h"
#include "pf/debug.h"


switchTestImpl :: switchTestImpl(const string &pointCode_, const string &port_)
    : mgmtDelegationBase(),
      _counter(1)
{
    createOB();
    try
    {
        // Construct name for naming component
        string name = pointCode_;
        name += "/ports/";
        name += port_;
        name += "/ilmi";
        
        registerToNameService(name);
    }
    catch (...)
    {
        debugUser("registerToNameService failed");
        exit(1);
    }
    return;
}

switchTestImpl :: ~switchTestImpl(void)
{
    // ++TODO++
    // unRegisterFromNameService();
    return;
}
        
string switchTestImpl :: receiveGet(const string &name_)
{
    debugString("receiveGet", name_);
    string result = "result";
    return result;
}

void switchTestImpl :: receiveSet(const string &name_, const string &value_)
{
    debugUser("receiveSet :");
    debugString(name_, value_);
    return;
}

void switchTestImpl :: receiveExecute(mgmtCommand &command_)
{
    debugUser("receiveExecute");
    string commandName = command_.getCommandName();

    if (commandName.compare("start") == 0)
    {
        debugUser("start");
        try
        {
            mgmtCommand command("start");
            sendExecute(command);
        }
        catch (...)
        {
            debugUser("start failed");
        }
    }
    else if (commandName.compare("stop") == 0)
    {
        debugUser("stop");
        try
        {
            mgmtCommand command("stop");
            sendExecute(command);
        }
        catch (...)
        {
            debugUser("stop failed");
        }
    }
    else if (commandName.compare("listPrefixes") == 0)
    {
        debugUser("listPrefixes");
        
        string port("0");
        mgmtCommand command("listPrefixes");
        
        for (int i=0; i<3; i++)
        {
            string exampleNetPrefixValue("71.00.05.1.1.1.1.0.0.1.1.1.");
            exampleNetPrefixValue += (char)(48 + _counter);
            _counter++;
            command.addParameter(port, exampleNetPrefixValue);
        }
        try
        {
            sendExecute(command);
        }
        catch (...)
        {
            debugUser("listprefixes failed");
        }
    }
    //
    // --------- incoming commands -------------------
    //
    else if (commandName.compare("listAddresses") == 0)
    {
        debugUser("listAddresses");
        for (pfUlong i=0; i<command_.getCommandLength(); i++)
        {
            string port;
            string address;
            string type;
            command_.getNextParameter(port, address, type);
            debugString("Address registered", address);
        }
    }
    else if (commandName.compare("return") == 0)
    {
        debugUser("return");
        // ++TODO++
    }
    else if (commandName.compare("error") == 0)
    {
        debugUser("error");
        for (pfUlong i=0; i < command_.getCommandLength(); i++)
        {
            string name;
            string value;
            string type;
            command_.getNextParameter(name, value, type);
            debugString("name", name);
            debugString("value", value);
            if (i == 0)
            {
                int code = atoi(value.c_str());
                switch (code)
                {
                    case 1 :
                        debugUser("tooBig");
                        break;
                    case 2 :
                        debugUser("noSuchName");
                        break;
                    case 3 :
                        debugUser("badValue");
                        break;
                    case 4 :
                        debugUser("readOnly");
                        break;
                    case 5 :
                        debugUser("genErr");
                        break;
                    default :
                        debugUser("unknown error code");
                        break;
                }
            }
            debugString("type", type);
        }
    }
    else
    {
        debugUser("Invalid command");
    }
    
    return;
}
