TestApplLayer.cc

00001 /***************************************************************************
00002  * file:        TestApplLayer.cc
00003  *
00004  * author:      Daniel Willkomm
00005  *
00006  * copyright:   (C) 2004 Telecommunication Networks Group (TKN) at
00007  *              Technische Universitaet Berlin, Germany.
00008  *
00009  *              This program is free software; you can redistribute it
00010  *              and/or modify it under the terms of the GNU General Public
00011  *              License as published by the Free Software Foundation; either
00012  *              version 2 of the License, or (at your option) any later
00013  *              version.
00014  *              For further information see file COPYING
00015  *              in the top level directory
00016  ***************************************************************************
00017  * part of:     framework implementation developed by tkn
00018  * description: application layer: test class for the application layer
00019  ***************************************************************************/
00020 
00021 
00022 #include "TestApplLayer.h"
00023 #include "NetwControlInfo.h"
00024 
00025 #include <SimpleAddress.h>
00026 
00027 
00036 void TestApplLayer::initialize(int stage)
00037 {
00038     BaseApplLayer::initialize(stage);
00039     if(stage == 0) {
00040       hasPar("coreDebug") ? coreDebug = par("coreDebug").boolValue() : coreDebug = false;
00041         delayTimer = new cMessage( "delay-timer", SEND_BROADCAST_TIMER );
00042     }
00043     else if(stage==1) {
00044         scheduleAt(simTime() + dblrand() * 10, delayTimer);
00045     }
00046 }
00047 
00048 
00056 void TestApplLayer::handleLowerMsg( cMessage* msg )
00057 {
00058     ApplPkt *m;
00059     switch( msg->getKind() ){
00060     case BROADCAST_MESSAGE:
00061         m = static_cast<ApplPkt *>(msg);
00062         coreEV << "Received a broadcast packet from host["<<m->getSrcAddr()<<"] -> sending reply\n";
00063         sendReply(m);
00064         break;
00065     case BROADCAST_REPLY_MESSAGE:
00066         m = static_cast<ApplPkt *>(msg);
00067         coreEV << "Received reply from host["<<m->getSrcAddr()<<"]; delete msg\n";
00068         delete msg;
00069   break;
00070     default:
00071   EV <<"Error! got packet with unknown kind: " << msg->getKind()<<endl;
00072         delete msg;
00073     }
00074 }
00075 
00084 void TestApplLayer::handleSelfMsg(cMessage *msg) {
00085     switch( msg->getKind() ){
00086     case SEND_BROADCAST_TIMER:
00087         sendBroadcast();
00088     delete msg;
00089     delayTimer = NULL;
00090   break;
00091     default:
00092       EV << "Unknown selfmessage! -> delete, kind: "<<msg->getKind() <<endl;
00093   delete msg;
00094     }
00095 }
00096 
00101 void TestApplLayer::sendBroadcast()
00102 {
00103     ApplPkt *pkt = new ApplPkt("BROADCAST_MESSAGE", BROADCAST_MESSAGE);
00104     pkt->setDestAddr(-1);
00105     // we use the host modules getIndex() as a appl address
00106     pkt->setSrcAddr( myApplAddr() );
00107     pkt->setBitLength(headerLength);
00108 
00109     // set the control info to tell the network layer the layer 3
00110     // address;
00111     pkt->setControlInfo( new NetwControlInfo(L3BROADCAST) );
00112 
00113     coreEV << "Sending broadcast packet!\n";
00114     sendDown( pkt );
00115 }
00116 
00117 void TestApplLayer::sendReply(ApplPkt *msg)
00118 {
00119     simtime_t delay;
00120 
00121     delay = uniform(0, 0.01);
00122 
00123     msg->setDestAddr(msg->getSrcAddr());
00124     msg->setSrcAddr(myApplAddr());
00125     msg->setKind(BROADCAST_REPLY_MESSAGE);
00126     msg->setName("BROADCAST_REPLY_MESSAGE");
00127     sendDelayedDown(msg, delay);
00128 
00129     coreEV << "sent message with delay " << delay << endl;
00130 
00131     //NOTE: the NetwControl info was already ste by the network layer
00132     //and stays the same
00133 }
00134 
00135 TestApplLayer::~TestApplLayer()
00136 {
00137   cancelAndDelete(delayTimer);
00138 }