//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / TRS
//
//File: routingclient.cpp
//
//Version: $Revision: 1.1 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/10/06 12:41:21 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Harri Sunila
//
//Description:
//      See corresponding header file.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 

#include "routingclient.h"
#include "common/orbnamingservice.h"
#include "pf/exception.h"
#include "sf/orbscheduler.h"
#include "trs.h"
#include "trs_impl.h"
#include "sw/swswitch.h"
#include "pf/ie.h"
#include "ie/transitnetworkselection.h"


//
// Function: trsRoutingClient
//
// Description:
//

trsRoutingClient :: trsRoutingClient(ccProtocol *host_)
    : _prefix()
{
    _callback = new trs_RouteServerCallback_impl(host_);
    ((sfORBScheduler::instance())->getORB())->connect(_callback);
    return;
}

//
// Function: trsRoutingClient
//
// Description:
//     Constructor for testing. User provides the network prefix and _callback
//     is initialized with a null pointer host_.
//

trsRoutingClient :: trsRoutingClient(const string &prefix_)
    : _prefix(prefix_)
{
    _callback = new trs_RouteServerCallback_impl(0);
    ((sfORBScheduler::instance())->getORB())->connect(_callback);
    return;
}

//
// Function: ~trsRoutingClient
//
// Description:  
//

trsRoutingClient :: ~trsRoutingClient(void)
{
    CORBA_release(_callback);
    return;
}

//
// Function: selectRoute
//
// Description:
//     This method is used to query the route to the destination
//

void trsRoutingClient :: selectRoute(const string &calledParty_,
                                     ieTransitNetworkSelection *,
                                     pfIE *, // qos
                                     pfUlong numberOfRoutes_)
{
    // Get the caller address (prefix of the calling switch)
    string caller; //= swSwitch::instance()->getPrefix(); // Later!!!

    // Use the _prefix, if caller is an empty string.
    if(caller.empty != 0)
    {
        caller = _prefix;
    }
    
    // Transform caller and calledParty_ to char *
    const char *callingNode = caller.c_str();
    const char *calledHost = calledParty_.c_str();

    // Transform information elements
    // ++TODO++

    trs_DTLstack dtlStack;            // from Transit Network Selection -ie
    trs_ParameterList qosParameters;  // from QoS ie's

    // Create the server name and naming context
    string serverNameString = "Routing/" + caller;

    CosNaming_Name serverName = orbNamingService::parseName(serverNameString);

    try
    {
        CORBA_Object_var serverObject =
            orbNamingService::instance()->resolve(serverName);

        trs_RouteServer_var server = trs_RouteServer::_narrow(serverObject);

        if(CORBA_is_nil(server) != 0)
        {
            throw pfMethodFailed("TOVE Route Server is a nil object reference",
                                 PF_EX_INFO); 
        }

        server->selectRoute(callingNode,
                            calledHost,
                            dtlStack,
                            qosParameters,
                            _callback,
                            numberOfRoutes_);
    }
    
    catch(pfMethodFailed &)
    {
        throw pfMethodFailed("pfMethodFailed Exception", PF_EX_INFO);
    }
    catch(...)
    {
        // CORBA exceptions
        throw pfMethodFailed("CORBA Exception", PF_EX_INFO);
    }
    return;
}
