//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / NNI SSCF protocol
//
//File: nsscfstate2_2_2.cpp
//
//Version: $Revision: 1.9 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/05/26 14:29:50 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications and Multimedia
//
//Author:
//      Juhana Räsänen
//
//Description:
//      See corresponding header file
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 


#include <assert.h>
#include "nsscfstate2_2_2.h"
#include "nsscfprotocol.h"
#include "protocol/sscop/sscop.h"
#include "iface/aaif/aaupprimitives.h"
#include "iface/naalif/naaldownprimitives.h"
#include "pf/frame.h"


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

nsscfAlignment_OutgoingConnectionPending *
nsscfAlignment_OutgoingConnectionPending :: _only = 0;

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


nsscfAlignment_OutgoingConnectionPending ::
nsscfAlignment_OutgoingConnectionPending(void)
    : nsscfState()
{
    return;
}

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


////////////////////////////////////////////////////////////////////
// void nsscfAlignment_OutgoingConnectionPending :: naalSTOPreqAct()
// Implemented as in Q.2140 page 52 SDL description

void nsscfAlignment_OutgoingConnectionPending ::
naalSTOPreqAct(naalSTOPreq *, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    releaseConnection(nsscf, nsscfOOSpdu, 0);
    nsscf->sendMaalREPORTind(LBC_EMPTY, UBC_OOS);
    nsscf->timerT2stop();
    nsscf->setUPSnormal();
    nsscf->toNsscfOutOfService_OutgoingDisconnectionPending();
    return;
}


////////////////////////////////////////////////////////////////////////
// void nsscfAlignment_OutgoingConnectionPending :: aaESTABLISHconfAct()
// Implemented as in Q.2140 page 52 SDL description

void nsscfAlignment_OutgoingConnectionPending ::
aaESTABLISHconfAct(aaESTABLISHconf *messenger_, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    nsscfPDU pdu = nsscf->decodePDU(messenger_->getSSCOP_UU());
    if ((pdu != nsscfEMpdu) && (pdu != nsscfNMpdu))
    {
        releaseConnection(nsscf, nsscfPEpdu, 0);
        nsscf->sendMaalREPORTind(LBC_LR, UBC_EMPTY, R_PE);
        nsscf->toNsscfAlignment_OutgoingDisconnectionPending();
    }
    else
    {
        nsscf->sendMaalPROVINGind();
        nsscf->setC1(pdu);
        nsscf->setNotInService();
        nsscf->timerT3start();
        nsscf->toNsscfProving_DataTransferReady();
    }
    return;
}


/////////////////////////////////////////////////////////////////////
// void nsscfAlignment_OutgoingConnectionPending :: aaRELEASEindAct()
// Implemented as in Q.2140 page 52 SDL description

void nsscfAlignment_OutgoingConnectionPending ::
aaRELEASEindAct(aaRELEASEind *messenger_, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    pfUlong source = messenger_->getSource();
    if (source == SSCOP_RELEASE_USER)
    {
        nsscfPDU pdu = nsscf->decodePDU(messenger_->getSSCOP_UU());
        nsscf->sendMaalREPORTind(LBC_RR, UBC_EMPTY, R_SSCOP_UU, pdu);
    }
    else
    {
        nsscf->sendMaalREPORTind(LBC_SR);
    }
    nsscf->timerT1start();
    nsscf->toNsscfAlignment_Idle();
    return;
}


///////////////////////////////////////////////////////////////////////
// void nsscfAlignment_OutgoingConnectionPending :: maalRELEASEreqAct()
// Implemented as in Q.2140 page 53 SDL description

void nsscfAlignment_OutgoingConnectionPending ::
maalRELEASEreqAct(maalRELEASEreq *, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    releaseConnection(nsscf, nsscfMIpdu, 1);
    nsscf->timerT2stop();
    nsscf->setUPSnormal();
    nsscf->toNsscfOutOfService_OutgoingDisconnectionPending();
    return;
}


///////////////////////////////////////////////////////////////////////
// void nsscfAlignment_OutgoingConnectionPending :: nsscfT2timeoutAct()
// Implemented as in Q.2140 page 51 SDL description

void nsscfAlignment_OutgoingConnectionPending ::
nsscfT2timeoutAct(nsscfProtocol *protocol_)
{
    releaseConnection(protocol_, nsscfANSpdu, 1);
    protocol_->sendMaalREPORTind(LBC_LR, UBC_OOS, R_ANS);
    protocol_->setUPSnormal();
    protocol_->toNsscfOutOfService_OutgoingDisconnectionPending();
    return;
}

