//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / OVOPS++
//
//File: pftest.cpp
//
//Version: $Revision: 1.3 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/06/10 00:14:21 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//      See corresponding header file
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include <typeinfo>
#include <iostream.h>
#include "pf/transp.h"
#include "pftest.h"

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


pfTestUpAdapter :: pfTestUpAdapter(int counter_)
    : _counter(counter_)
{
//    cerr << "Constructor : pfTestUpAdapter" << endl;
    changeState(pfTestAdapterState::instance());
    return;
}

int pfTestUpAdapter :: decrementCounter(void)
{
    _counter--;
    return _counter;
}

int pfTestUpAdapter :: getCounter(void) const
{
    return _counter;
}

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

pfTestDownAdapter :: pfTestDownAdapter(void)
{
//   cerr << "Constructor : pfTestDownAdapter " << endl;
    changeState(pfTestAdapterState::instance());    
    return;
}

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

//pfTestUpMux :: pfTestUpMux(void)
//    : pfMux(100)
//{
//    cerr << "Constructor : pfTestUpMux " << endl;
//    changeState(pfTestUpMuxState::instance());    
//    return;
//}

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

//pfTestDownMux :: pfTestDownMux(void)
//    : pfMux(100)    
//{
//    cerr << "Contstructor : pfTestDownMux" << endl;
//    changeState(pfTestDownMuxState::instance());        
//    return;
//}

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

pfTestUpProtocol :: pfTestUpProtocol(void)
{
//    cerr << "Contstructor : pfTestUpProtocol" << endl;
    changeState(pfTestUpProtocolState::instance());        
    return;
}

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

pfTestDownProtocol :: pfTestDownProtocol(void)
{
//    cerr << "Contstructor : pfTestDownProtocol" << endl;
    changeState(pfTestDownProtocolState::instance());            
    return;
}

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


void pfTestState :: testSETUPindAct(pfTestSETUPind *,
                                    pfProtocol *)
{
    return;
}

void pfTestState :: testDATAindAct(pfTestDATAind *,
                                   pfProtocol *)
{
    return;
}

void pfTestState :: testRELEASEindAct(pfTestRELEASEind *,
                                      pfProtocol *)
{
    return;
}

void pfTestState :: testSETUPreqAct(pfTestSETUPreq *,
                                    pfProtocol *)
{
    return;
}

void pfTestState :: testDATAreqAct(pfTestDATAreq *,
                                   pfProtocol *)
{
    return;
}

void pfTestState :: testRELEASEreqAct(pfTestRELEASEreq *,
                                      pfProtocol *)
{
    return;
}

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

pfTestUpProtocolState *pfTestUpProtocolState :: _only = 0;

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

pfTestUpProtocolState :: pfTestUpProtocolState(void)
{
    return;
}

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

void pfTestUpProtocolState :: testDATAindAct(pfTestDATAind *messenger_,
                                             pfProtocol *protocol_)
{
    cout << "DATA_up" << endl;

    pfTestDATAind *messenger = new pfTestDATAind;
    messenger->setKey(messenger_->getKey());
    protocol_->toB(messenger);    

//    pfMsgTransporter *message =
//        pfMsgTransporter::createMsgTransporter(messenger_);
//    message->setAcceptMethodToSynchronous();
//    protocol_->toB(message);
    return;
}

void pfTestUpProtocolState :: testRELEASEindAct(pfTestRELEASEind *messenger_,
                                                pfProtocol *protocol_)
{
    cout << "RELEASE_up" << endl;
    pfCloseRequestTransporter *transporter =
        pfCloseRequestTransporter::createCloseRequestTransporter();
    transporter->setKey(messenger_->getKey());
    
// set sideB to NULL    
//    pfConduit null;
//    protocol_->connectToB(null);

    protocol_->toA(transporter);
    return;
}

void pfTestUpProtocolState :: testSETUPreqAct(pfTestSETUPreq *,
                                              pfProtocol *)
{
    return;
}

void pfTestUpProtocolState :: testDATAreqAct(pfTestDATAreq *messenger_,
                                             pfProtocol *protocol_)
{
    pfTestDATAreq *messenger = new pfTestDATAreq;
    messenger->setKey(messenger_->getKey());
    protocol_->toA(messenger);
    return;
}

void pfTestUpProtocolState :: testRELEASEreqAct(pfTestRELEASEreq *messenger_,
                                                pfProtocol *protocol_)
{
    pfTestRELEASEreq *messenger = new pfTestRELEASEreq;
    messenger->setKey(messenger_->getKey());
    protocol_->toB(messenger);
  
    return;
}

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

pfTestDownProtocolState *pfTestDownProtocolState :: _only = 0;

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

pfTestDownProtocolState :: pfTestDownProtocolState(void)
{
    return;
}

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

void pfTestDownProtocolState :: testSETUPindAct(pfTestSETUPind *,
                                                pfProtocol *)
{
    cout << "SETUP ala-protokollassa" << endl;    
    return;
}

void pfTestDownProtocolState :: testDATAindAct(pfTestDATAind *messenger_,
                                               pfProtocol *protocol_)
{
    cout << "DATA" << endl;
    pfTestDATAind *messenger = new pfTestDATAind;
    messenger->setKey(messenger_->getKey());
    protocol_->toB(messenger);
    return;
}

void pfTestDownProtocolState :: testRELEASEindAct(pfTestRELEASEind *messenger_,
                                                  pfProtocol *protocol_)
{
    cout << "RELEASE" << endl;
    pfTestRELEASEind *messenger = new pfTestRELEASEind;
    messenger->setKey(messenger_->getKey());
    protocol_->toB(messenger);
    return;
}

void pfTestDownProtocolState :: testDATAreqAct(pfTestDATAreq *messenger_,
                                               pfProtocol *protocol_)
{
    pfTestDATAreq *messenger = new pfTestDATAreq;
    messenger->setKey(messenger_->getKey());
    pfMsgTransporter *message =
        pfMsgTransporter::createMsgTransporter(messenger_);        
    protocol_->toA(message);
    return;
}

void pfTestDownProtocolState :: testRELEASEreqAct(pfTestRELEASEreq *messenger_,
                                                  pfProtocol *protocol_)
{
    pfCloseRequestTransporter *transporter =    
        pfCloseRequestTransporter::createCloseRequestTransporter();    
    transporter->setKey(messenger_->getKey());
    
    // set sideA to NULL    
    pfConduit null;
    protocol_->connectToA(null);

    protocol_->toB(transporter);
    return;
}

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

pfTestAdapterState *pfTestAdapterState :: _only = 0;

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

pfTestAdapterState :: pfTestAdapterState(void)
{
    return;
}

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

void pfTestAdapterState :: testDATAreqAct(pfTestDATAreq *messenger_,
                                          pfProtocol *protocol_)
{
    pfTestDATAind *messenger = new pfTestDATAind;
    messenger->setKey(messenger_->getKey());
    cout << "DATAreq(" << messenger->getKey() << ")" << endl;        
    protocol_->toA(messenger);
    return;
}

void pfTestAdapterState :: testDATAindAct(pfTestDATAind *messenger_,
                                          pfProtocol *protocol_)
{
    assert(protocol_ != 0);
    pfTestUpAdapter *adapter = dynamic_cast<pfTestUpAdapter*>(protocol_);
    assert(adapter != 0);
    cout << "DATAind(" << messenger_->getKey() << ")" << endl;

/*    
    if (adapter->decrementCounter() > 0)
    {
        pfTestDATAreq *messenger = new pfTestDATAreq;
        messenger->setKey(messenger_->getKey());
        protocol_->toA(messenger);
    }
*/    
    return;
}

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

pfTestVisitor :: pfTestVisitor(pfMessenger *messenger_)
    : pfTransporter(),
      _messenger(messenger_)
{
    return;
}

void pfTestVisitor :: atAdapter(pfAdapter *const adapter_)
{
    adapter_->toA(_messenger);
    return;
}

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

pfTestBaseMessage :: pfTestBaseMessage(void)
    : pfMessenger()
{
    defineInteger("testKey");
    return;
}

void pfTestBaseMessage :: setKey(pfKey key_)
{
    setInteger("testKey", key_);
    return;
}

pfKey pfTestBaseMessage :: getKey(void) const
{
    pfKey key = getInteger("testKey");
    return key;
}

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

void pfTestSETUPind :: apply(pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    pfTestState *state = dynamic_cast<pfTestState*>(state_);
    assert(state != 0);

    state->testSETUPindAct(this, protocol_);
    return;
}

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

void pfTestDATAind :: apply(pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    pfTestState *state = dynamic_cast<pfTestState*>(state_);
    assert(state != 0);

    state->testDATAindAct(this, protocol_);
    return;
}

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

void pfTestRELEASEind :: apply(pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    pfTestState *state = dynamic_cast<pfTestState*>(state_);
    assert(state != 0);

    state->testRELEASEindAct(this, protocol_);
    return;
}

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

void pfTestSETUPreq :: apply(pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    pfTestState *state = dynamic_cast<pfTestState*>(state_);
    assert(state != 0);

    state->testSETUPreqAct(this, protocol_);
    return;
}

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

void pfTestDATAreq :: apply(pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    pfTestState *state = dynamic_cast<pfTestState*>(state_);
    assert(state != 0);

    state->testDATAreqAct(this, protocol_);
    return;
}

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

void pfTestRELEASEreq :: apply(pfState *state_, pfProtocol *protocol_)
{
    assert(state_ != 0);
    pfTestState *state = dynamic_cast<pfTestState*>(state_);
    assert(state != 0);

    state->testRELEASEreqAct(this, protocol_);
    return;
}

// END OF FILE
