BurstApplLayerBattery.cc

00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        BurstApplLayer.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 like the TestApplLayer but sends burst of
00019  *               messages
00020  **************************************************************************/
00021 
00022 
00023 #include "BurstApplLayerBattery.h"
00024 
00025 Define_Module(BurstApplLayerBattery);
00026 
00027 // do some initialization
00028 void BurstApplLayerBattery::initialize(int stage)
00029 {
00030     BurstApplLayer::initialize(stage);
00031 
00032     if(stage==0) {
00033         bcastOut = replyOut = replyIn = 0;
00034     }
00035 }
00036 
00037 void BurstApplLayerBattery::handleSelfMsg(cMessage *msg)
00038 {
00039   switch(msg->getKind()) {
00040   case SEND_BROADCAST_TIMER:
00041     for(int i=0; i<burstSize; i++) {
00042       bcastOut += 1;
00043       debugEV << "bcastQueued = " << bcastOut << endl;
00044       sendBroadcast();
00045     }
00046     break;
00047   default:
00048     EV <<" Unkown selfmessage! kind: "<<msg->getKind() << endl;
00049     break;
00050   }
00051 }
00052 
00053 void BurstApplLayerBattery::handleLowerMsg( cMessage* msg )
00054 {
00055   ApplPkt *m;
00056 
00057   switch( msg->getKind() ) {
00058   case BROADCAST_MESSAGE:
00059     m = static_cast<ApplPkt *>(msg);
00060     debugEV << "Received a broadcast packet from host["<<m->getSrcAddr()<<"] -> sending reply\n";
00061     replyOut += 1;
00062     debugEV << simTime() << ": replyOut = " << replyOut << endl;
00063     sendReply(m);
00064     break;
00065   case BROADCAST_REPLY_MESSAGE:
00066     m = static_cast<ApplPkt *>(msg);
00067     debugEV << "Received reply from host["<<m->getSrcAddr()<<"]; delete msg\n";
00068     replyIn += 1;
00069     debugEV << simTime() << ": replyIn = " << replyIn << endl;
00070     delete msg;
00071     break;
00072   default:
00073     EV <<"Error! got packet with unknown kind: " << msg->getKind()<<endl;
00074     break;
00075   }
00076 }
00077 
00078 void BurstApplLayerBattery::handleHostState(const HostState& state)
00079 {
00080   HostState::States hostState = state.get();
00081 
00082   switch (hostState) {
00083   case HostState::FAILED:
00084     debugEV << "t = " << simTime() << " host state FAILED" << endl;
00085     recordScalar("HostState::FAILED", simTime());
00086 
00087     // BurstApplLayer sends all its frames to the MAC layer at
00088     // once, so this happens only if the battery fails before the
00089     // burst.  Frames that are already sendDown() are stopped at
00090     // the SnrEval.
00091 
00092     // battery is depleted, stop events
00093     if (delayTimer->isScheduled()) {
00094       debugEV << "canceling frame burst" << endl;
00095       cancelEvent(delayTimer);
00096     }
00097     break;
00098   default:
00099     // ON/OFF aren't used
00100     break;
00101   }
00102 }
00103 
00104 void BurstApplLayerBattery::finish()
00105 {
00106     BurstApplLayer::finish();
00107     recordScalar("broadcast queued", bcastOut);
00108     recordScalar("replies sent", replyOut);
00109     recordScalar("replies received", replyIn);
00110 }