//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / unitest
//
//File: unitestccprotocol.cpp
//
//Version: $Revision: 1.8 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/12/04 12:02:52 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Jari Katajavuori
//
//Description:
//
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include "unitestccprotocol.h"

#include "pf/mux.h"

#include "protocol/uni/unitransporter.h"

static pfUlong connectPar = 1;
static pfUlong vciPar = 105;

pfConduit uniTestCCProtocol :: _topMux(0);

uniTestCCProtocol :: uniTestCCProtocol(void)
    : pfProtocol()
{
    changeState(uniTestCCState::instance());
    return;
}

uniTestCCProtocol :: ~uniTestCCProtocol(void)
{
    return;
}

void uniTestCCProtocol :: connectCCs()
{
    string keyName("REFERENCE");

    cout << "Connecting CCs....." << endl;
    uniCrossConnecter crosser = uniCrossConnecter::createUNICrossConnecter();
    crosser.setKeyName(keyName);
    crosser.setKey(connectPar);
    connectToB(_topMux);
    toB(&crosser);
    pfConduit otherSide = crosser.getOtherSide();
    pfConduit thisSide(this);
    otherSide.connectToB(thisSide);
    thisSide.connectToB(otherSide);
    cout << "Done connecting." << endl;
    connectPar--; // update side.
    return;
}

void uniTestCCProtocol :: sendSETUPreq(sigSETUPreq *message_)
{
    sigSETUPreq *sr = new sigSETUPreq;
    sr->fetch(*message_);
    if (sr->isVariableDefined(sigConnectionIdentifierStr) == 0)
    {
        sr->defineStorage(sigConnectionIdentifierStr);
        (*sr)[sigConnectionIdentifierStr].defineInteger(sigCI_VPCIStr);
        (*sr)[sigConnectionIdentifierStr].defineInteger(sigCI_VCIStr);
    }
    (*sr)[sigConnectionIdentifierStr].setInteger(sigCI_VPCIStr, 0);
    (*sr)[sigConnectionIdentifierStr].setInteger(sigCI_VCIStr, vciPar);
    toA(sr);
    vciPar++; // Update for next connection
    return;
}

void uniTestCCProtocol :: sendSETUPreqToCC(sigSETUPind *message_)
{
    sigSETUPreq *sr = new sigSETUPreq;
    sr->copyIfPresent(*message_, sigAALParametersStr);
    sr->copyIfPresent(*message_, sigATDStr);
    sr->copyIfPresent(*message_, sigBroadbandBearerCapabilityStr);
    sr->copyIfPresent(*message_, sigBroadbandHighLayerInfoStr);
    sr->copyIfPresent(*message_, sigBroadbandLowLayerInfoStr);
    sr->copyIfPresent(*message_, sigCalledPartyNumberStr);
    sr->copyIfPresent(*message_, sigCalledPartySubaddressStr);
    sr->copyIfPresent(*message_, sigCallingPartyNumberStr);
    sr->copyIfPresent(*message_, sigCallingPartySubaddressStr);
    sr->copyIfPresent(*message_, sigQoSParametersStr);
    sr->copyIfPresent(*message_, sigBroadbandSendingCompleteStr);
    sr->copyIfPresent(*message_, sigTransitNetworkSelectionStr);
    toB(sr);
    return;
}

void uniTestCCProtocol :: sendPROCEEDINGreq(void)
{
    sigPROCEEDINGreq *pr = new sigPROCEEDINGreq;
    (*pr)[sigConnectionIdentifierStr].setInteger(sigCI_VPCIStr, 0);
    (*pr)[sigConnectionIdentifierStr].setInteger(sigCI_VCIStr, vciPar);
    toA(pr);
    return;
}

void uniTestCCProtocol :: sendSETUPresp(sigPrimitive *message_)
{
    sigSETUPresp *sr = new sigSETUPresp;
    sr->copyIfPresent(*message_, sigAALParametersStr);
    sr->copyIfPresent(*message_, sigBroadbandLowLayerInfoStr);
    sr->copyIfPresent(*message_, sigConnectionIdentifierStr);
    toA(sr);
    vciPar++; // update channel for next connection...
    return;
}

void uniTestCCProtocol :: sendSETUPrespToCC(sigSETUPconf *message_)
{
    sigSETUPresp *sr = new sigSETUPresp;
    sr->copyIfPresent(*message_, sigAALParametersStr);
    sr->copyIfPresent(*message_, sigBroadbandLowLayerInfoStr);
    sr->defineStorage(sigConnectionIdentifierStr);
    (*sr)[sigConnectionIdentifierStr].defineInteger(sigCI_VPCIStr);
    (*sr)[sigConnectionIdentifierStr].defineInteger(sigCI_VCIStr);
    (*sr)[sigConnectionIdentifierStr].setInteger(sigCI_VPCIStr, 0);
    (*sr)[sigConnectionIdentifierStr].setInteger(sigCI_VCIStr, vciPar);
    toB(sr);
    return;
}

void uniTestCCProtocol :: sendSETUP_COMPLETEreq(void)
{
    sigSETUP_COMPLETEreq *scr = new sigSETUP_COMPLETEreq;
    toA(scr);
    return;
}

void uniTestCCProtocol :: sendRELEASEreq(sigRELEASEreq *message_)
{
    sigRELEASEreq *rr = new sigRELEASEreq;
    rr->copyIfPresent(*message_, sigCauseStr);
    toA(rr);
    return;
}

void uniTestCCProtocol :: sendRELEASEreqToCC(sigRELEASEind *message_)
{
    sigRELEASEreq *rr = new sigRELEASEreq;
    rr->copyIfPresent(*message_, sigCauseStr);
    toB(rr);
    return;
}

void uniTestCCProtocol :: sendRELEASEresp(void)
{
    sigRELEASEresp *rr = new sigRELEASEresp;
    toA(rr);
    return;
}

pfConduit uniTestCCProtocol :: topMuxInstance(void)
{
    if (_topMux == pfConduit(0))
    {
        cout << "Created top mux" << endl;
        string keyName("REFERENCE");
        _topMux = pfMux::createMux(0xFFFFFF, keyName);

        //uniMuxCatcherTransporter cat = uniMuxCatcherTransporter::create();
        //topMuxConduit.accept(&cat);
        //pfMux *topMux = cat.catchedMux();
    }
    return _topMux;
}
   

