//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / NNI SSCF protocol
//
//File: nsscfstate3_10_5.cpp
//
//Version: $Revision: 1.9 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/05/26 14:29:51 $
//
//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 "nsscfstate3_10_5.h"
#include "nsscfprotocol.h"
#include "protocol/sscop/sscop.h"
#include "iface/aaif/aaupprimitives.h"
#include "iface/naalif/naaldownprimitives.h"
#include "pf/frame.h"


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

nsscfInService_DataTransferReady *
nsscfInService_DataTransferReady :: _only = 0;

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


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

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


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady :: naalSTOPreqAct()
// Implemented as in Q.2140 page 62 SDL description

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


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady ::
//     naalMESSAGE_FOR_TRANSMISSIONreqAct()
// Implemented as in Q.2140 page 62 SDL description

void nsscfInService_DataTransferReady ::
naalMESSAGE_FOR_TRANSMISSIONreqAct
(naalMESSAGE_FOR_TRANSMISSIONreq *messenger_, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    nsscf->sendAaDATAreq(messenger_->getMessageUnit());
    nsscf->sendMaalREPORTind(LBC_EMPTY, UBC_EMPTY, R_PDUT);
    return;
}


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady :: aaRELEASEindAct()
// Implemented as in Q.2140 page 62 SDL description

void nsscfInService_DataTransferReady ::
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->sendNaalOUT_OF_SERVICEind();
    nsscf->setUPSnormal();
    nsscf->toNsscfOutOfService_OutgoingDisconnectionPending();
    return;
}


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady :: aaDATAindAct()
// Implemented as in Q.2140 page 62 SDL description

void nsscfInService_DataTransferReady ::
aaDATAindAct(aaDATAind *messenger_, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    pfFrame mu = messenger_->getMessageUnit();
    if (mu.length() > NSSCF_PDU_LENGTH)
    {
        nsscf->setBSNT(messenger_->getSequenceNumber());
        nsscf->sendNaalRECEIVED_MESSAGEind(mu);
    }
    else if ((mu.length() == NSSCF_PDU_LENGTH) &&
             ((nsscf->decodePDU(mu) == nsscfINSpdu) ||
              (nsscf->decodePDU(mu) == nsscfNMpdu)))
    {
        releaseConnection(nsscf, nsscfPEpdu, 1);
        nsscf->sendMaalREPORTind(LBC_LR, UBC_OOS, R_PE);
        nsscf->setUPSnormal();
        nsscf->toNsscfOutOfService_OutgoingDisconnectionPending();
    }
    return;
}


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady :: aaRESYNCindAct()
// Implemented as in Q.2140 page 63 SDL description

void nsscfInService_DataTransferReady ::
aaRESYNCindAct(aaRESYNCind *, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    releaseConnection(nsscf, nsscfPEpdu, 1);
    nsscf->sendMaalREPORTind(LBC_LR, UBC_OOS, R_PE);
    nsscf->setUPSnormal();
    nsscf->toNsscfOutOfService_OutgoingDisconnectionPending();
    return;
}


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady :: aaRECOVERindAct()
// Implemented as in Q.2140 page 63 SDL description

void nsscfInService_DataTransferReady ::
aaRECOVERindAct(aaRECOVERind *, pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    nsscf->sendAaRECOVERresp();
    nsscf->sendMaalREPORTind(LBC_EMPTY, UBC_EMPTY, R_SREC);
    return;
}


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady :: maalRELEASEreqAct()
// Implemented as in Q.2140 page 63 SDL description

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


//////////////////////////////////////////////////////////////////////////
// void nsscfInService_DataTransferReady :: maalLOCAL_PROCESSOR_OUTAGEreqAct()
// Implemented as in Q.2140 page 63 SDL description

void nsscfInService_DataTransferReady ::
maalLOCAL_PROCESSOR_OUTAGEreqAct(maalLOCAL_PROCESSOR_OUTAGEreq *,
                                 pfProtocol *protocol_)
{
    nsscfProtocol *nsscf = (nsscfProtocol *) protocol_;
    releaseConnection(nsscf, nsscfPOpdu, 1);
    nsscf->setUPSnormal();
    nsscf->toNsscfOutOfService_OutgoingDisconnectionPending();
    return;
}

