//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project/CC
//
//File: binappdus.cpp
//
//Version: $Revision: 1.25 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/11/24 12:57:24 $
//
//Organisation:
//      University of Technology
// 
//Author:
//      Pasi Nummisalo
//
//Description:
//      See header.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//      
//Licence:
//     
//
//History:
//


#include "protocol/cc/ccprotocol.h"
#include "protocol/cc/cccrossconnectormux.h"
#include "iface/binapif/binappdus.h"
#if CC_DEBUG
#include "pf/debug.h"
#endif    


binapPdu :: binapPdu(void)
    : pfMessenger()
{
    return;
}

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

void binapANALYSE_INFORMATIONpdu :: apply(
     pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapANALYSE_INFORMATIONpduAct(this,(ccProtocol*) protocol_);
    
    return;    
}

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

void binapAUTHORIZE_ORIGINATIONpdu :: apply(
    pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapAUTHORIZE_ORIGINATIONpduAct(this,(ccProtocol*) protocol_);
    
    return;
}

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

void binapCOLLECT_INFORMATIONpdu :: apply(
    pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapCOLLECT_INFORMATIONpduAct(this,(ccProtocol*) protocol_);
    
    return;
}


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

binapREQUEST_REPORT_BCSM_EVENTpdu :: binapREQUEST_REPORT_BCSM_EVENTpdu(void)
    : binapPdu(),
      _args()
{
    return;
}


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


void binapREQUEST_REPORT_BCSM_EVENTpdu :: apply(
    pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    ccProtocol *ccprotocol = dynamic_cast<ccProtocol*>(protocol_);
    assert(ccprotocol != 0);
    input->binapREQUEST_REPORT_BCSM_EVENTpduAct(this, ccprotocol);
    
    return;
}

void binapREQUEST_REPORT_BCSM_EVENTpdu :: setDPValues(ccProtocol *protocol_)
{
    
    // BCSM Event List can contain many Events
    unsigned int length = _args.bcsmEvents.length();
    toveinap_MonitorModeType monitorMode;
    
    for (unsigned int i=0; i < length; i++)
    {
        ccDp &dp = protocol_->getDp(_args.bcsmEvents[i].eventTypeBCSM);
        monitorMode = _args.bcsmEvents[i].monitorMode;
        dp.setMonitorMode(monitorMode);
        // If type = EDP_R then SCF and SSF has control relationship
        // else monitor relationship (default)
        if (monitorMode == toveinap_interrupted)
        {
            protocol_->setControlRelationship();
        }

#if CC_DEBUG        
        cout << "Armed " <<  dp.getType() << endl;
#endif     
    }

    protocol_->setArmedEDPcount(length);
    return;
}

void binapREQUEST_REPORT_BCSM_EVENTpdu :: setArguments(
    const toveinap_RequestReportBCSMEventArgType  &args_)
{
    _args = args_;

    return;
}
    

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

void binapCONTINUEpdu :: apply(
    pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapCONTINUEpduAct(this,(ccProtocol*) protocol_);
    
    return;       
}

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

binapSELECT_ROUTEpdu :: binapSELECT_ROUTEpdu(void)
    : binapPdu(),
      _args(),
      _hasArguments(false),
      _currentNumberIndex(0) 
{
    return;
}


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

const binapSELECT_ROUTEpdu &binapSELECT_ROUTEpdu :: 
    operator = (const binapSELECT_ROUTEpdu &other_)
{
    if (this != &other_) // if not x = x situation
    {
        _hasArguments = other_._hasArguments;
        _currentNumberIndex = 0;
        _args = other_._args;
    }
    return *this;
}

void binapSELECT_ROUTEpdu :: apply(
    pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    ccProtocol *ccprotocol = dynamic_cast<ccProtocol*>(protocol_);
    assert(ccprotocol != 0);
    ccprotocol->saveSelectRoute(*this);
    input->binapSELECT_ROUTEpduAct(this, ccprotocol);
    
    return;
}


void binapSELECT_ROUTEpdu :: setArguments(
    const toveinap_DestinationRoutingAddressType &args_)
{
    _hasArguments = true;
    _args = args_;
    return;
}


bool binapSELECT_ROUTEpdu :: hasArguments() const
{
    return _hasArguments;
}

string binapSELECT_ROUTEpdu :: getNextNumber()
{
    string numberStr;

    toveinap_CalledPartyNumberType number;
    number = _args[_currentNumberIndex];
    _currentNumberIndex++;
    
    for(unsigned int i=0; i < number.length(); i++)
    {
        numberStr+= number[i];
    }
    
    if (_currentNumberIndex >= _args.length())
    {
        _hasArguments = false;
    }
    
    return numberStr;
}

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

void binapPRESENT_CALLpdu :: apply(
     pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapPRESENT_CALLpduAct(this,(ccProtocol*) protocol_);
    
    return;   
}

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

void binapSELECT_FACILITYpdu :: apply(
     pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapSELECT_FACILITYpduAct(this,(ccProtocol*) protocol_);
    
    return;   
}

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

void binapAUTHORIZE_TERMINATIONpdu :: apply(
     pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapAUTHORIZE_TERMINATIONpduAct(this,(ccProtocol*) protocol_);
    
    return;    
}

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

void binapRELEASE_CALLpdu :: apply(
     pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    binapInputs *input = dynamic_cast<binapInputs*>(state_);
    assert(input != 0);
    input->binapRELEASE_CALLpduAct(this,(ccProtocol*) protocol_);
    
    return;     
}


