//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / OVOPS++
//
//File: ss7test.cpp
//
//Version: $Revision: 1.9 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/10/08 14:19:28 $
//
//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 "ss7test.h"
#include "pf/debug.h"
#include <iostream.h>
#include "iface/mtpif/mtpdownprimitives.h"
//#include "iface/naalif/naalupprimitives.h"
#include "iface/sccpif/sccpdownprimitives.h"
#include "mgmt/mgmtcommand.h"

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


tcapAdapter :: tcapAdapter(const string &pointCode_, 
                           pfBoolean acknowledge_)
    : pfProtocol(),
      mgmtDelegationBase(),
      _acknowledge(acknowledge_),
      _counter(0)
{
    createOB();
    try
    {
        // Construct name for naming component
        string name = pointCode_;
        name += "/ss7test";
        
        registerToNameService(name);
    }
    catch (...)
    {
        debugUser("registerToNameService failed");
        exit(1);
    }

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

// Mgmt iface
string tcapAdapter :: receiveGet(const string &name_)
{
    return;
}

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

void tcapAdapter :: receiveExecute(mgmtCommand &command_)
{
    pfStorage calledAddress;
    calledAddress.defineInteger("routingIndicator");
    calledAddress.defineInteger("pointCode");
    calledAddress.defineInteger("subSystemNumber");
    calledAddress.defineString("globalTitle");

    pfStorage callingAddress;
    callingAddress.defineInteger("routingIndicator");
    callingAddress.defineInteger("pointCode");
    callingAddress.defineInteger("subSystemNumber");
    callingAddress.defineString("globalTitle");

    // Parameters
    command_.setCurrentTheFirst();
    string calledGT;
    string callingGT;
    command_.getNextParameter(calledGT);
    command_.getNextParameter(callingGT);

    calledAddress.setString("globalTitle", calledGT);
    callingAddress.setString("globalTitle", callingGT);
    
    pfFrame frame;
    frame.putFirst16bit(0xFFFF);
 
    debugUser("Send sccpN_UNITDATAreq");
    sendSccpN_UNITDATAreq(calledAddress, callingAddress, frame);
    return;
}

void tcapAdapter :: sendSccpN_UNITDATAreq(pfStorage &calledAddress_,
                                          pfStorage &callingAddress_,
                                          pfFrame &frame_)
{
    sccpN_UNITDATAreq *messenger = new sccpN_UNITDATAreq;
    messenger->setCalledAddress(calledAddress_);
    messenger->setCallingAddress(callingAddress_);
    messenger->setUserData(frame_);
    toA(messenger);
    return;
}

void tcapAdapter :: sendAcknowledge(sccpN_UNITDATAind *messenger_)
{
    _counter++;
    if ((_counter % 100) == 0)
    {
        cout << "COUNTER" <<  _counter << endl;
    }

    pfStorage calledAddress = messenger_->getStorage("calledAddress");
    pfStorage callingAddress = messenger_->getStorage("callingAddress");

    cout << "---- CalledAddress ----" << endl;
    printAddressValues(calledAddress);
    cout << "---- CallingAddress ----" << endl;      
    printAddressValues(callingAddress);

    if (_acknowledge != 0)
    {
	pfFrame frame = messenger_->getFrame("userData");
        sendSccpN_UNITDATAreq(calledAddress, callingAddress, frame);
    }
    return;
}

void tcapAdapter :: printAddressValues(pfStorage &address_)
{
    if (address_.isValuePresent("pointCode") != 0)
    {
        cout << "PC  = " << address_.getInteger("pointCode") << endl;
    }
    if (address_.isValuePresent("globalTitle") != 0)
    {
        cout << "GT  = " << address_.getString("globalTitle") << endl;
    }
    if (address_.isValuePresent("subSystemNumber") != 0)
    {
        cout << "SSN = " << address_.getInteger("subSystemNumber") << endl;
    }
    return;
}

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

tcapAdapterState *tcapAdapterState :: _only = 0;

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

tcapAdapterState :: tcapAdapterState(void)
{
    return;
}

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

void tcapAdapterState :: sccpN_UNITDATAindAct(sccpN_UNITDATAind *messenger_,
					      pfProtocol *protocol_)
{

    ((tcapAdapter*)protocol_)->sendAcknowledge(messenger_);
    return;
}

void tcapAdapterState :: sccpN_NOTICEindAct(sccpN_NOTICEind *,
					    pfProtocol *)
{
    return;
}
