//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / TCAP
//
//File: tcapstate.cpp
//
//Version: $Revision: 1.2 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/11/04 18:10:22 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author: 
//      Timo Pärnänen
//
//Description:
//      See file description.
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include <string>
#include "pf/frame.h"
#include "pf/debug.h"
#include "tcapstate.h"
#include "tcapadapter.h"

//
// Variable: _only
// Function: instance()
//
// Description:
//     Implements the singleton pattern used for actual states
//

tcapState *tcapState::_only = 0;

tcapState * tcapState :: instance(void)
{
    if (_only == 0)
    {
        _only = new tcapState;
    }
    return _only;
}

tcapState :: tcapState(void)
    : pfState()
{
    return;
}

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

//
//Functions: Primitive inputs from SCCP
//           
//Description:
//    Methods describe primitive inputs form SCCP protocol.
//    Primitives are forwarded to TCAP using CORBA.
//

void tcapState :: sccpN_UNITDATAindAct(sccpN_UNITDATAind *messenger_,
                                       pfProtocol *protocol_)
{
    const pfStorage calledAddress = messenger_->getCalledAddress();
    const pfStorage callingAddress = messenger_->getCallingAddress();
    pfFrame userData = messenger_->getUserData();
    
    tcap_AddressType tcapCalledAddress = createAddress(calledAddress);
    tcap_AddressType tcapCallingAddress = createAddress(callingAddress);
    
    int length = userData.length();
    
    tcap_UserDataType tcapUserData;
    tcapUserData.length(length);
    for (int i=0; i<length; i++)
    {
        tcapUserData[i] = userData.getFirst();
    }

    tcapAdapter *adapter = (tcapAdapter*)protocol_;

    try
    {
        adapter->sendToTCAP(tcapCalledAddress,
                            tcapCallingAddress,
                            tcapUserData);
       debugUser("N_UNITDATAind sent to TCAP");
    }
    catch(...)
    {
       debugUser("Send to TCAP error");
       //++TODO++
    }
    return;
}

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

//
//Function: createAddress
//
//Description:
//    Method transforms pfStorage type address to user defined
//    CORBA IDL addresss type.
//

tcap_AddressType tcapState :: createAddress(const pfStorage &address_)
{
    tcap_AddressType idlAddress;
    int type = 0;
    if (address_.isValuePresent("pointCode") != 0)
    {
        type = 1;
        idlAddress.pointCode = address_.getInteger("pointCode");
    
    }
    if (address_.isValuePresent("subSystemNumber") != 0)
    {
        type += 2;
        idlAddress.subSystemNumber = address_.getInteger("subSystemNumber");
    }
    if (address_.isValuePresent("globalTitle") != 0)
    {
        type += 4;
        string globalTitle = address_.getString("globalTitle");
        idlAddress.globalTitle = globalTitle.c_str();
    }
    idlAddress.routingIndicator = type;
    return idlAddress;
}
