//Editor-Info: -*- C++ -*-
//
//Subject: SF + PF + OmniBroker test
//
//File: democlasses.cpp
//
//Version: $Revision: 1.3 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/06/10 00:13:58 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Juhana Räsänen
//
//Description:
//      See corresponding header file
//
//Copyright:
//
//
//Licence:
//
//
//History: 

#include <typeinfo>
#include <iostream.h>
#include "democlasses.h"


loopProtocol :: loopProtocol(int nTimeouts_, pfUlong timeout_)
    : pfProtocol(),
      _nTimeouts(nTimeouts_)
{
    loopTimeout *messenger = new loopTimeout;
    defineTimer(TIMERNAME, messenger, timeout_);
    return;
}

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

void loopState :: loopMessageAct(
    loopMessage *messenger_,
    loopProtocol *protocol_)
{
    if (messenger_->getCounter() > 0)
    {
        loopMessage *newMessage = new loopMessage(*messenger_);
        newMessage->decrementCounter();
        protocol_->toB(newMessage);
    }
    else
    { 
        cout << "LoopProtocol " << protocol_->getId() << ": Message "
             << messenger_->getId() << " done!" << endl;
    }
    return;
}

void loopState :: loopTimeoutAct(loopProtocol *protocol_)
{
    loopMessage *newMessage = new loopMessage(protocol_->getN());
    protocol_->toB(newMessage);
    cout << "pfConduitTest: Sent message " << protocol_->getN() << endl;
    
    if (protocol_->getN() > 0)
    {
        protocol_->decrementN();
        protocol_->startTimer(TIMERNAME);
    }
    return;
}

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

void loopMessage :: apply(pfState *state_, pfProtocol *protocol_)
{
    (dynamic_cast<loopState *> (state_))->
        loopMessageAct(this, (loopProtocol *) protocol_);
    return;
}

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

loopTimeout :: loopTimeout(void)
    : pfTimerMessenger()
{
    return;
}

loopTimeout :: loopTimeout(const loopTimeout &)
    : pfTimerMessenger()
{
    return;
}

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

pfTimerMessenger *loopTimeout :: clone(void) const
{
    pfTimerMessenger *messenger = new loopTimeout(*this);
    return messenger;
}

void loopTimeout :: apply(
    pfState *state_,
    pfProtocol *protocol_)
{
    (dynamic_cast<loopState *> (state_))->
        loopTimeoutAct((loopProtocol *) protocol_);
    return;
}
