BaseLayer.cc

00001 /***************************************************************************
00002  * file:        BaseLayer.cc
00003  *
00004  * author:      Andreas Koepke
00005  *
00006  * copyright:   (C) 2006 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: basic MAC layer class
00019  *              subclass to create your own MAC layer
00020  ***************************************************************************
00021  * changelog:   $Revision: 250 $
00022  *              last modified:   $Date: 2006-04-04 18:53:02 +0200 (Tue, 04 Apr 2006) $
00023  *              by:              $Author: koepke $
00024  **************************************************************************/
00025 
00026 
00027 #include "BaseLayer.h"
00028 #include <assert.h>
00029 
00036 void BaseLayer::initialize(int stage)
00037 {
00038   BatteryAccess::initialize(stage);
00039     if(stage==0){
00040         if (hasPar("stats") && par("stats").boolValue()) {
00041             doStats = true;
00042             passedMsg = new PassedMessage();
00043             catPassedMsg = utility->getCategory(passedMsg);
00044             passedMsg->fromModule = getId();
00045             hostId = findHost()->getId();
00046         }
00047         else {
00048             doStats = false;
00049         }
00050         upperGateIn  = findGate("upperGateIn");
00051         upperGateOut = findGate("upperGateOut");
00052         lowerGateIn  = findGate("lowerGateIn");
00053         lowerGateOut = findGate("lowerGateOut");
00054         upperControlIn  = findGate("upperControlIn");
00055         upperControlOut = findGate("upperControlOut");
00056         lowerControlIn  = findGate("lowerControlIn");
00057         lowerControlOut = findGate("lowerControlOut");
00058     }
00059 }
00060 
00061 
00073 void BaseLayer::handleMessage(cMessage* msg)
00074 {
00075     if (msg->isSelfMessage()){
00076         handleSelfMsg(msg);
00077     } else if(msg->getArrivalGateId()==upperGateIn) {
00078         recordPacket(PassedMessage::INCOMING,PassedMessage::UPPER_DATA,msg);
00079         handleUpperMsg(msg);
00080     } else if(msg->getArrivalGateId()==upperControlIn) {
00081         recordPacket(PassedMessage::INCOMING,PassedMessage::UPPER_CONTROL,msg);
00082         handleUpperControl(msg);
00083     } else if(msg->getArrivalGateId()==lowerControlIn){
00084         recordPacket(PassedMessage::INCOMING,PassedMessage::LOWER_CONTROL,msg);
00085         handleLowerControl(msg);
00086     } else if(msg->getArrivalGateId()==lowerGateIn) {
00087         recordPacket(PassedMessage::INCOMING,PassedMessage::LOWER_DATA,msg);
00088         handleLowerMsg(msg);
00089     }
00090     else if(msg->getArrivalGateId()==-1) {
00091         /* Classes extending this class may not use all the gates, f.e.
00092          * BaseApplLayer has no upper gates. In this case all upper gate-
00093          * handles are initialized to -1. When getArrivalGateId() equals -1,
00094          * it would be wrong to forward the message to one of these gates,
00095          * as they actually don't exist, so raise an error instead.
00096          */
00097         opp_error("No self message and no gateID?? Check configuration.");
00098     } else {
00099         /* msg->getArrivalGateId() should be valid, but it isn't recognized
00100          * here. This could signal the case that this class is extended
00101          * with extra gates, but handleMessage() isn't overridden to
00102          * check for the new gate(s).
00103          */
00104         opp_error("Unknown gateID?? Check configuration or override handleMessage().");
00105     }
00106 }
00107 
00108 void BaseLayer::sendDown(cMessage *msg) {
00109     recordPacket(PassedMessage::OUTGOING,PassedMessage::LOWER_DATA,msg);
00110     send(msg, lowerGateOut);
00111 }
00112 
00113 void BaseLayer::sendUp(cMessage *msg) {
00114     recordPacket(PassedMessage::OUTGOING,PassedMessage::UPPER_DATA,msg);
00115     send(msg, upperGateOut);
00116 }
00117 
00118 void BaseLayer::sendControlUp(cMessage *msg) {
00119     recordPacket(PassedMessage::OUTGOING,PassedMessage::UPPER_CONTROL,msg);
00120     send(msg, upperControlOut);
00121 }
00122 
00123 void BaseLayer::sendControlDown(cMessage *msg) {
00124     recordPacket(PassedMessage::OUTGOING,PassedMessage::LOWER_CONTROL,msg);
00125     send(msg, lowerControlOut);
00126 }
00127 
00128 void BaseLayer::recordPacket(PassedMessage::direction_t dir,
00129                              PassedMessage::gates_t gate,
00130                              const cMessage * msg) {
00131     if (!doStats) return;
00132     passedMsg->direction = dir;
00133     passedMsg->gateType = gate;
00134     passedMsg->kind = msg->getKind();
00135     passedMsg->name = msg->getName();
00136     utility->publishBBItem(catPassedMsg, passedMsg, hostId);
00137 }
00138 
00139 void BaseLayer::finish() {
00140 
00141 }
00142 
00143 BaseLayer::~BaseLayer()
00144 {
00145     if (doStats) {
00146         delete passedMsg;
00147     }
00148 }