Public Member Functions | Protected Member Functions | Protected Attributes

BaseModule Class Reference
[baseModules - base module classes of MiXiM]

Base class for all simple modules of a host that want to have access to the BaseUtility module. More...

#include <BaseModule.h>

Inherits ImNotifiable.

Inherited by ArpHost, BaseArp, BaseBattery, BatteryAccess, BatteryStats, phyPER, RepeatTimerCore, SimpleMacLayer, SimpleNetwLayer, TestApplication, and TimerCore.

Collaboration diagram for BaseModule:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 BaseModule ()
 Function to get the logging name of id.
virtual void initialize (int)
 Basic initialization for all modules.
virtual int numInitStages () const
 Divide initialization into two stages.
std::string logName (void)
 Function to get the logging name of the host.
cModule * getNode ()
 Get a reference to the local node module.
virtual void receiveBBItem (int category, const BBItem *details, int scopeModuleId)
 Called by the Blackboard whenever a change of a category occurs to which we have subscribed. Redefined from ImNotifiable. In this base class just handle the host state switching and some debug notifications.

Protected Member Functions

virtual void handleHostState (const HostState &state)
 Called whenever the hosts state changes.
void switchHostState (HostState::States state)
 Switches the host to the passed state.
cModule * findHost (void)
 Function to get a pointer to the host module.

Protected Attributes

BaseUtilityutility
 Cached pointer to the utility module.
bool debug
 Debug switch for all other modules.
bool notAffectedByHostState
 Stores if this module is affected by changes in the hosts state. If not explicitly set this module has to capture changes in the host state.
int hostStateCat
 Stores the category of the HostState.
int hostId
 The hosts id.

Detailed Description

Base class for all simple modules of a host that want to have access to the BaseUtility module.

BaseModule listens for HostState changes broadcasted over BaseUtility and calls its own "handleHostState()" method. This method raises an error if the host state changes to something else than ACTIVE. Therefore that a sub-classing module can be used in a simulation where the host state can change it has to override that method which forces the author to make sure the module reacts well to host state changes. Alternatively one can also set a "notAffectedByHostState" parameter of the module to true.

The base module additionally provides a function findHost which returns a pointer to the host module and a function hostIndex to return the index of the host module. The latter one correspondes to the index shown in tkenv and comes in very handy for testing and debugging using tkenv. It is used e.g. in all the 'print' macros used for debugging.

There will never be a stand-alone BaseModule module.

Note: most modules wont derive from BaseModule directly but from its sub class "BatteryAccess" which extends BaseModule by several methods for accessing the battery module.

See also:
BaseUtility
BatteryAccess
Author:
Steffen Sroka
Andreas Koepke

Definition at line 79 of file BaseModule.h.


Member Function Documentation

void BaseModule::handleHostState ( const HostState state  )  [protected, virtual]

Called whenever the hosts state changes.

Default implementation of this method throws an error whenever the host state changes and the "notAffectedbyHostState" variable is not explicitly set. This is because every module of a host has to make sure to react well to changes in the host state. Or it has to explicitly set its parameter "notAffectedbyHostState" to true.

Reimplemented in BurstApplLayerBattery, and SimpleBattery.

Definition at line 66 of file BaseModule.cc.

References HostState::get(), and notAffectedByHostState.

Referenced by receiveBBItem().

{
  if(notAffectedByHostState)
    return;

  if(state.get() != HostState::ACTIVE) {
    error("Hosts state changed to something else than active which"
        " is not handled by this module. Either handle this state"
        " correctly or if this module really isn't affected by the"
        " hosts state set the parameter \"notAffectedByHostState\""
        " of this module to true.");
  }
}

void BaseModule::initialize ( int  stage  )  [virtual]

Basic initialization for all modules.

Subscription to Blackboard should be in stage==0, and firing notifications in stage==1 or later.

NOTE: You have to call this in the initialize() function of the inherited class!

Reimplemented in ChannelAccess, BaseApplLayer, BaseArp, BaseLayer, BaseMacLayer, BaseMobility, BaseNetwLayer, TestApplLayer, BurstApplLayer, BurstApplLayerBattery, SensorApplLayer, TestApplication, TrafficGen, AlohaMacLayer, BMacLayer, csma, CSMA802154, CSMAMacLayer, LMacLayer, Mac80211, Mac80211MultiChannel, UWBIRMac, ANSimMobility, BonnMotionMobility, CircleMobility, ConstSpeedMobility, LinearMobility, LineSegmentsMobilityBase, MassMobility, MoBANLocal, RectangleMobility, TractorMobility, TurtleMobility, AdaptiveProbabilisticBroadcast, ArpHost, DummyRoute, Flood, ProbabilisticBroadcast, WiseRoute, BatteryStats, SimpleBattery, Aggregation, phyPER, SimpleMacLayer, SimpleNetwLayer, and NetworkStackTrafficGen.

Definition at line 37 of file BaseModule.cc.

References debug, findHost(), hostId, hostStateCat, notAffectedByHostState, Blackboard::subscribe(), and utility.

                                     {
    if (stage == 0) {
      notAffectedByHostState =  hasPar("notAffectedByHostState")
                 && par("notAffectedByHostState").boolValue();

        hasPar("debug") ? debug = par("debug").boolValue() : debug = true;
        utility = FindModule<BaseUtility*>::findSubModule(findHost());

        if(!utility) {
          error("No BaseUtility module found!");
        } 

        hostId = findHost()->getId();

        /* host failure notification */
    HostState hs;
    hostStateCat = utility->subscribe(this, &hs, hostId);
    }
}

std::string BaseModule::logName ( void   ) 

Function to get the logging name of the host.

The logging name is the ned module name of the host (unless the host ned variable loggingName is specified). It can be used for logging messages to simplify debugging in TKEnv.

This function returns the logging name of the module with the specified id. It can be used for logging messages to simplify debugging in TKEnv.

Only supports ids from simple module derived from the BaseModule or the nic compound module id.

Parameters:
id Id of the module for the desired logging name
Returns:
logging name of module id or NULL if not found
See also:
logName

Definition at line 119 of file BaseModule.cc.

References findHost().

{
        std::ostringstream ost;
  if (hasPar("logName")) // let modules override
  {
    ost << par("logName").stringValue();
  }
  else
  {
    cModule *parent = findHost();
    parent->hasPar("logName") ?
      ost << parent->par("logName").stringValue() : ost << parent->getName();
    ost << "[" << parent->getIndex() << "]";
  }
  return ost.str();
}

virtual int BaseModule::numInitStages (  )  const [inline, virtual]

Divide initialization into two stages.

In the first stage (stage==0), modules subscribe to notification categories at Blackboard. The first notifications (e.g. about the initial values of some variables such as RadioState) should take place earliest in the second stage (stage==1), when everyone interested in them has already subscribed. Further one should try to keep calls to other modules out of stage 0 to assure that the other module had at least once the chance to initialize itself in stage 0.

Definition at line 149 of file BaseModule.h.

                                      {
      return 2;
    }

void BaseModule::switchHostState ( HostState::States  state  )  [protected]

Switches the host to the passed state.

This is a convenience method for publishing the host state to BaseUtility.

If the hosts state is switched to anything else than "ACTIVE" every module of the host has to handle this explicitly (see method "handleHostState()")!

Definition at line 80 of file BaseModule.cc.

References hostId, hostStateCat, Blackboard::publishBBItem(), and utility.

{
  HostState hostState(state);
  utility->publishBBItem(hostStateCat, &hostState, hostId);
}


The documentation for this class was generated from the following files: