BaseModule.cc

00001 /* -*- mode:c++ -*- ********************************************************
00002  * file:        BaseModule.cc
00003  *
00004  * author:      Steffen Sroka
00005  *              Andreas Koepke
00006  *
00007  * copyright:   (C) 2004 Telecommunication Networks Group (TKN) at
00008  *              Technische Universitaet Berlin, Germany.
00009  *
00010  *              This program is free software; you can redistribute it
00011  *              and/or modify it under the terms of the GNU General Public
00012  *              License as published by the Free Software Foundation; either
00013  *              version 2 of the License, or (at your option) any later
00014  *              version.
00015  *              For further information see file COPYING
00016  *              in the top level directory
00017  ***************************************************************************
00018  * part of:     framework implementation developed by tkn
00019  **************************************************************************/
00020 
00021 #include "BaseModule.h"
00022 #include "BaseUtility.h"
00023 #include <cassert>
00024 
00025 BaseModule::BaseModule():
00026   cSimpleModule(),
00027   utility(NULL)
00028 {}
00029 
00037 void BaseModule::initialize(int stage) {
00038     if (stage == 0) {
00039       notAffectedByHostState =  hasPar("notAffectedByHostState")
00040                  && par("notAffectedByHostState").boolValue();
00041 
00042         hasPar("debug") ? debug = par("debug").boolValue() : debug = true;
00043         utility = FindModule<BaseUtility*>::findSubModule(findHost());
00044 
00045         if(!utility) {
00046           error("No BaseUtility module found!");
00047         } 
00048 
00049         hostId = findHost()->getId();
00050 
00051         /* host failure notification */
00052     HostState hs;
00053     hostStateCat = utility->subscribe(this, &hs, hostId);
00054     }
00055 }
00056 
00057 void BaseModule::receiveBBItem(int category, const BBItem *details, int scopeModuleId) {
00058   Enter_Method_Silent();
00059 
00060   if (category == hostStateCat) {
00061 
00062     handleHostState(*(HostState*)details);
00063   }
00064 }
00065 
00066 void BaseModule::handleHostState(const HostState& state)
00067 {
00068   if(notAffectedByHostState)
00069     return;
00070 
00071   if(state.get() != HostState::ACTIVE) {
00072     error("Hosts state changed to something else than active which"
00073         " is not handled by this module. Either handle this state"
00074         " correctly or if this module really isn't affected by the"
00075         " hosts state set the parameter \"notAffectedByHostState\""
00076         " of this module to true.");
00077   }
00078 }
00079 
00080 void BaseModule::switchHostState(HostState::States state)
00081 {
00082   HostState hostState(state);
00083   utility->publishBBItem(hostStateCat, &hostState, hostId);
00084 }
00085 
00086 cModule *BaseModule::findHost(void)
00087 {
00088    return FindModule<>::findHost(this);
00089 }
00090 
00091 
00104 //std::string BaseModule::getLogName(int id)
00105 //{
00106 //    BaseModule *mod;
00107 //    std::string lname;
00108 //    mod = check_and_cast<BaseModule *>(simulation.getModule(id));
00109 //    if (mod->isSimple()) {
00110 //        lname = mod->logName();
00111 //    }
00112 //    else if(mod->getSubmodule("phy")) {
00113 //        lname = check_and_cast<BaseModule *>(mod->getSubmodule("phy"))->logName();
00114 //    }
00115 //    return lname;
00116 //};
00117 
00118 
00119 std::string BaseModule::logName(void)
00120 {
00121         std::ostringstream ost;
00122   if (hasPar("logName")) // let modules override
00123   {
00124     ost << par("logName").stringValue();
00125   }
00126   else
00127   {
00128     cModule *parent = findHost();
00129     parent->hasPar("logName") ?
00130       ost << parent->par("logName").stringValue() : ost << parent->getName();
00131     ost << "[" << parent->getIndex() << "]";
00132   }
00133   return ost.str();
00134 }
00135