//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / OVOPS++
//
//File: mtptest.cpp
//
//Version: $Revision: 1.7 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/11/16 13:37:20 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//      See corresponding header file
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include "mtptest.h"
#include <iostream.h>
#include "pf/debug.h"
#include "mgmt/mgmtcommand.h"
#include "iface/mtpif/mtpdownprimitives.h"

//----------------------------------------------------------------------


mtpTestUpAdapter :: mtpTestUpAdapter(string &pointCode_,
                                     pfBoolean acknowledge_)
    : pfProtocol(),
      mgmtDelegationBase(),
      _acknowledge(acknowledge_),
      _counter(0)

{
    // Create OmniBroker CORBA implementation of switchInterface.
    createOB();

    // Construct name for naming component
    string name = pointCode_;
    name += "/mtptest";

    // Register agent to a name serive
    try
    {
        registerToNameService(name);
    }
    catch (...)
    {
        debugUser("registerToNameService failed");
        exit(1);
    }

    changeState(mtpTestUpAdapterState::instance());
    return;
}

string mtpTestUpAdapter :: receiveGet(const string &name_)
{
    string result("");
    return result;
}

void mtpTestUpAdapter :: receiveSet(const string &name_, const string &value_)
{
    return;
}

void mtpTestUpAdapter :: receiveExecute(mgmtCommand &command_)
{
    string commandName = command_.getCommandName();
    
    if (commandName.compare("mtpTRANSFERreq") == 0)
    { 
        string DPCstr;
        string OPCstr;
        command_.getNextParameter(DPCstr);
        command_.getNextParameter(OPCstr);

        pfUlong DPC = atoi(DPCstr.c_str());
        pfUlong OPC = atoi(OPCstr.c_str());

        mtpTRANSFERreq *messenger = new mtpTRANSFERreq;
        messenger->setDestinationPointCode(DPC);
        messenger->setOriginatingPointCode(OPC);
        messenger->setServiceInformationOctet(3);
        messenger->setSignallingLinkSelection(30);
        pfFrame frame;
        frame.putFirst16bit(0xFFFF);
        messenger->setSignallingInformation(frame);
        
        cout << "Send mtpTRANSFERreq..." << endl;
        toA(messenger);
    }
    return;
}

void mtpTestUpAdapter :: sendAcknowledge(pfUlong DPC_, pfUlong OPC_)
{
    if (_acknowledge != 0)
    {
        mtpTRANSFERreq *messenger = new mtpTRANSFERreq;
        messenger->setDestinationPointCode(DPC_);
        messenger->setOriginatingPointCode(OPC_);
        messenger->setServiceInformationOctet(3);
        messenger->setSignallingLinkSelection(30);
        pfFrame frame;
        frame.putFirst16bit(0xFFFF);
        messenger->setSignallingInformation(frame);
        toA(messenger);
    }
    return;
}

pfUlong mtpTestUpAdapter :: incCounter(void)
{
    return ++_counter;
}

//----------------------------------------------------------------------

mtpTestUpAdapterState *mtpTestUpAdapterState :: _only = 0;

mtpTestUpAdapterState *mtpTestUpAdapterState :: instance(void)
{    
    if (_only == 0)
    {
        _only = new mtpTestUpAdapterState;
        assert(_only != 0);
    }
    return _only;
}

mtpTestUpAdapterState :: mtpTestUpAdapterState(void)
{
    return;
}

mtpTestUpAdapterState :: ~mtpTestUpAdapterState(void)
{
    _only = 0;
    return;
}

void mtpTestUpAdapterState :: mtpTRANSFERindAct(mtpTRANSFERind *messenger_,
                                                pfProtocol *protocol_)
{
    mtpTestUpAdapter *protocol = (mtpTestUpAdapter *)protocol_;
    cout << "mtpTRANSFERind [" << protocol->incCounter() << "]" << endl;

    pfUlong DPC = messenger_->getDestinationPointCode();
    pfUlong OPC = messenger_->getOriginatingPointCode();

    // DPC = incoming OPC and vice versa
    protocol->sendAcknowledge(OPC, DPC);
    return;
}

//----------------------------------------------------------------------

// END OF FILE
