OMNeT++ Simulation Library
5.6.1
|
#include <csimplemodule.h>
Base class for all simple module classes.
cSimpleModule, although packed with simulation-related functionality, does not do anything useful by itself: one has to subclass from it and redefine one or more virtual member functions to make it do useful work. These functions are:
initialize() is called after OMNeT++ created the module. Multi-stage initialization can be achieved by redefining the initialize(int stage) method instead, and also redefining the numInitStages() const method to return the required number of stages.
One has to redefine handleMessage() to contain the internal logic of the module. handleMessage() is called by the simulation kernel when the module receives a message. (An alternative to handleMessage() is activity(), but activity() is not recommended for serious model development because of scalability and debugging issues. activity() also tends to lead to messy module implementations.)
You can send() messages to other modules, or use scheduleAt()+cancelEvent() to implement delays, timers or timeouts. Messages sent or scheduled (but not cancelled) are delivered to modules via handleMessage(), or, when using activity(), via receive().
The finish() functions are called when the simulation terminates successfully. Typical use of finish() is recording statistics collected during simulation.
Public Member Functions | |
Constructors, destructor, assignment. | |
cSimpleModule (unsigned stacksize=0) | |
virtual | ~cSimpleModule () |
Redefined cObject member functions. | |
virtual std::string | str () const override |
virtual void | forEachChild (cVisitor *v) override |
Redefined cModule functions. | |
virtual void | scheduleStart (simtime_t t) override |
Information about the module. | |
bool | usesActivity () const |
bool | isTerminated () const |
Debugging aids. | |
virtual void | snapshot (cObject *obj=nullptr, const char *label=nullptr) |
Message sending. | |
virtual void | send (cMessage *msg, int gateid) |
virtual void | send (cMessage *msg, const char *gatename, int gateindex=-1) |
virtual void | send (cMessage *msg, cGate *outputgate) |
virtual void | sendDelayed (cMessage *msg, simtime_t delay, int gateid) |
virtual void | sendDelayed (cMessage *msg, simtime_t delay, const char *gatename, int gateindex=-1) |
virtual void | sendDelayed (cMessage *msg, simtime_t delay, cGate *outputgate) |
virtual void | sendDirect (cMessage *msg, cModule *mod, const char *inputGateName, int gateIndex=-1) |
virtual void | sendDirect (cMessage *msg, cModule *mod, int inputGateId) |
virtual void | sendDirect (cMessage *msg, cGate *inputGate) |
virtual void | sendDirect (cMessage *msg, simtime_t propagationDelay, simtime_t duration, cModule *mod, const char *inputGateName, int gateIndex=-1) |
virtual void | sendDirect (cMessage *msg, simtime_t propagationDelay, simtime_t duration, cModule *mod, int inputGateId) |
virtual void | sendDirect (cMessage *msg, simtime_t propagationDelay, simtime_t duration, cGate *inputGate) |
Self-messages. | |
virtual void | scheduleAt (simtime_t t, cMessage *msg) |
virtual cMessage * | cancelEvent (cMessage *msg) |
virtual void | cancelAndDelete (cMessage *msg) |
Receiving messages. | |
These methods may only be invoked from activity()-based simple modules. | |
virtual cMessage * | receive () |
virtual cMessage * | receive (simtime_t timeout) |
Waiting. | |
virtual void | wait (simtime_t time) |
virtual void | waitAndEnqueue (simtime_t time, cQueue *queue) |
Stopping the module or the simulation. | |
virtual void | endSimulation () |
virtual void | halt () |
virtual void | error (const char *format,...) const |
Coroutine stack info. Useful only if module uses activity(). | |
virtual bool | hasStackOverflow () const |
virtual unsigned | getStackSize () const |
virtual unsigned | getStackUsage () const |
Public Member Functions inherited from cModule | |
cModule () | |
virtual | ~cModule () |
virtual void | setName (const char *s) override |
virtual const char * | getFullName () const override |
virtual std::string | getFullPath () const override |
virtual cGate * | addGate (const char *gatename, cGate::Type type, bool isvector=false) |
virtual void | setGateSize (const char *gatename, int size) |
virtual cGate * | getOrCreateFirstUnconnectedGate (const char *gatename, char suffix, bool inside, bool expand) |
virtual void | getOrCreateFirstUnconnectedGatePair (const char *gatename, bool inside, bool expand, cGate *&gatein, cGate *&gateout) |
virtual void | finalizeParameters () override |
void | buildInside () |
virtual bool | isSimple () const |
virtual ComponentKind | getComponentKind () const override |
virtual bool | isPlaceholder () const |
virtual cModule * | getParentModule () const override |
cModuleType * | getModuleType () const |
virtual cProperties * | getProperties () const override |
bool | isVector () const |
int | getIndex () const |
int | getVectorSize () const |
_OPPDEPRECATED int | size () const |
virtual bool | hasSubmodules () const |
virtual int | findSubmodule (const char *name, int index=-1) const |
virtual cModule * | getSubmodule (const char *name, int index=-1) const |
virtual cModule * | getModuleByPath (const char *path) const |
virtual bool | containsModule (cModule *module) const |
virtual cGate * | gate (const char *gatename, int index=-1) |
const cGate * | gate (const char *gatename, int index=-1) const |
virtual cGate * | gateHalf (const char *gatename, cGate::Type type, int index=-1) |
const cGate * | gateHalf (const char *gatename, cGate::Type type, int index=-1) const |
virtual bool | hasGate (const char *gatename, int index=-1) const |
virtual int | findGate (const char *gatename, int index=-1) const |
virtual cGate * | gate (int id) |
const cGate * | gate (int id) const |
virtual void | deleteGate (const char *gatename) |
virtual std::vector< const char * > | getGateNames () const |
virtual cGate::Type | gateType (const char *gatename) const |
virtual bool | isGateVector (const char *gatename) const |
virtual int | gateSize (const char *gatename) const |
virtual int | gateBaseId (const char *gatename) const |
virtual bool | checkInternalConnections () const |
virtual cPar & | getAncestorPar (const char *parname) |
virtual cCanvas * | getCanvas () const |
virtual cOsgCanvas * | getOsgCanvas () const |
virtual void | setBuiltinAnimationsAllowed (bool enabled) |
virtual bool | getBuiltinAnimationsAllowed () const |
virtual void | callInitialize () override |
virtual bool | callInitialize (int stage) override |
virtual void | callFinish () override |
virtual void | deleteModule () |
virtual void | changeParentTo (cModule *mod) |
Public Member Functions inherited from cComponent | |
cComponent (const char *name=nullptr) | |
virtual | ~cComponent () |
const cComponent * | getThisPtr () const |
virtual cComponentType * | getComponentType () const |
cSimulation * | getSimulation () const |
int | getId () const |
virtual const char * | getNedTypeName () const |
bool | isModule () const |
bool | isChannel () const |
virtual cModule * | getSystemModule () const |
virtual int | getNumParams () const |
virtual cPar & | par (int k) |
const cPar & | par (int k) const |
virtual cPar & | par (const char *parname) |
const cPar & | par (const char *parname) const |
virtual int | findPar (const char *parname) const |
bool | hasPar (const char *s) const |
virtual cRNG * | getRNG (int k) const |
virtual unsigned long | intrand (long r, int rng=0) const |
virtual double | dblrand (int rng=0) const |
virtual double | uniform (double a, double b, int rng=0) const |
virtual SimTime | uniform (SimTime a, SimTime b, int rng=0) const |
virtual double | exponential (double mean, int rng=0) const |
virtual SimTime | exponential (SimTime mean, int rng=0) const |
virtual double | normal (double mean, double stddev, int rng=0) const |
virtual SimTime | normal (SimTime mean, SimTime stddev, int rng=0) const |
virtual double | truncnormal (double mean, double stddev, int rng=0) const |
virtual SimTime | truncnormal (SimTime mean, SimTime stddev, int rng=0) const |
virtual double | gamma_d (double alpha, double theta, int rng=0) const |
virtual double | beta (double alpha1, double alpha2, int rng=0) const |
virtual double | erlang_k (unsigned int k, double mean, int rng=0) const |
virtual double | chi_square (unsigned int k, int rng=0) const |
virtual double | student_t (unsigned int i, int rng=0) const |
virtual double | cauchy (double a, double b, int rng=0) const |
virtual double | triang (double a, double b, double c, int rng=0) const |
virtual double | lognormal (double m, double w, int rng=0) const |
virtual double | weibull (double a, double b, int rng=0) const |
virtual double | pareto_shifted (double a, double b, double c, int rng=0) const |
virtual int | intuniform (int a, int b, int rng=0) const |
virtual int | intuniformexcl (int a, int b, int rng=0) const |
virtual int | bernoulli (double p, int rng=0) const |
virtual int | binomial (int n, double p, int rng=0) const |
virtual int | geometric (double p, int rng=0) const |
virtual int | negbinomial (int n, double p, int rng=0) const |
virtual int | poisson (double lambda, int rng=0) const |
virtual void | subscribe (simsignal_t signalID, cIListener *listener) |
virtual void | subscribe (const char *signalName, cIListener *listener) |
virtual void | unsubscribe (simsignal_t signalID, cIListener *listener) |
virtual void | unsubscribe (const char *signalName, cIListener *listener) |
virtual bool | isSubscribed (simsignal_t signalID, cIListener *listener) const |
virtual bool | isSubscribed (const char *signalName, cIListener *listener) const |
virtual std::vector< simsignal_t > | getLocalListenedSignals () const |
virtual std::vector< cIListener * > | getLocalSignalListeners (simsignal_t signalID) const |
virtual bool | hasGUI () const |
virtual cDisplayString & | getDisplayString () const |
virtual void | setDisplayString (const char *dispstr) |
virtual void | bubble (const char *text) const |
virtual std::string | resolveResourcePath (const char *fileName) const |
virtual void | recordScalar (const char *name, double value, const char *unit=nullptr) |
virtual void | recordScalar (const char *name, SimTime value, const char *unit=nullptr) |
virtual void | recordStatistic (cStatistic *stats, const char *unit=nullptr) |
virtual void | recordStatistic (const char *name, cStatistic *stats, const char *unit=nullptr) |
virtual void | emit (simsignal_t signalID, bool b, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, long l, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, unsigned long l, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, double d, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, const SimTime &t, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, const char *s, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, cObject *obj, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, const cObject *obj, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, char c, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, unsigned char c, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, short i, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, unsigned short i, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, int i, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, unsigned int i, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, float f, cObject *details=nullptr) |
virtual void | emit (simsignal_t signalID, long double d, cObject *details=nullptr) |
bool | mayHaveListeners (simsignal_t signalID) const |
bool | hasListeners (simsignal_t signalID) const |
Public Member Functions inherited from cDefaultList | |
cDefaultList (const char *name=nullptr) | |
virtual | ~cDefaultList () |
virtual bool | isSoftOwner () const override |
virtual void | parsimPack (cCommBuffer *buffer) const override |
virtual void | parsimUnpack (cCommBuffer *buffer) override |
bool | getPerformFinalGC () const |
virtual void | setPerformFinalGC (bool b) |
int | defaultListSize () const |
cOwnedObject * | defaultListGet (int k) |
bool | defaultListContains (cOwnedObject *obj) const |
Public Member Functions inherited from cNoncopyableOwnedObject | |
cNoncopyableOwnedObject (const char *name=nullptr, bool namepooling=true) | |
virtual cNoncopyableOwnedObject * | dup () const override |
Public Member Functions inherited from cOwnedObject | |
cOwnedObject () | |
cOwnedObject (const char *name, bool namepooling=true) | |
cOwnedObject (const cOwnedObject &obj) | |
virtual | ~cOwnedObject () |
cOwnedObject & | operator= (const cOwnedObject &o) |
virtual cObject * | getOwner () const override |
virtual bool | isOwnedObject () const override |
Public Member Functions inherited from cNamedObject | |
cNamedObject () | |
cNamedObject (const char *name, bool namepooling=true) | |
cNamedObject (const cNamedObject &obj) | |
virtual | ~cNamedObject () |
cNamedObject & | operator= (const cNamedObject &o) |
virtual const char * | getName () const override |
virtual void | setNamePooling (bool b) |
virtual bool | getNamePooling () |
Public Member Functions inherited from cObject | |
cObject () | |
cObject (const cObject &other) | |
virtual | ~cObject () |
virtual const char * | getClassName () const |
bool | isName (const char *s) const |
const cObject * | getThisPtr () const |
virtual _OPPDEPRECATED std::string | info () const |
virtual _OPPDEPRECATED std::string | detailedInfo () const |
cObject * | findObject (const char *name, bool deep=true) |
void | copyNotSupported () const |
Protected Member Functions | |
virtual void | arrived (cMessage *msg, cGate *ongate, simtime_t t) override |
Hooks for defining module behavior. | |
Exactly one of activity() and handleMessage() must be redefined by the user to add functionality to the simple module. See the manual for detailed guidance on how use to these two methods. These methods are made protected because they shouldn't be called directly from outside. | |
virtual void | activity () |
virtual void | handleMessage (cMessage *msg) |
Protected Member Functions inherited from cComponent | |
virtual void | initialize (int stage) |
virtual int | numInitStages () const |
virtual void | initialize () |
virtual void | finish () |
virtual void | handleParameterChange (const char *parname) |
virtual void | refreshDisplay () const |
Protected Member Functions inherited from cDefaultList | |
virtual void | take (cOwnedObject *obj) override |
virtual void | drop (cOwnedObject *obj) override |
Protected Member Functions inherited from cObject | |
void | dropAndDelete (cOwnedObject *obj) |
Additional Inherited Members | |
Static Public Member Functions inherited from cComponent | |
static simsignal_t | registerSignal (const char *name) |
static const char * | getSignalName (simsignal_t signalID) |
Static Public Member Functions inherited from cOwnedObject | |
static long | getTotalObjectCount () |
static long | getLiveObjectCount () |
static void | resetObjectCounters () |
static cDefaultList * | getDefaultOwner () |
cSimpleModule | ( | unsigned | stacksize = 0 | ) |
Constructor. Note that module objects should not be created directly, only via their cModuleType objects. See cModule constructor for more info. The stacksize parameter should only be used with simple modules using activity().
|
virtual |
Destructor. Note: it is not allowed delete modules directly, only via the deleteModule() method.
This method is invoked as part of a send() call in another module. It is called when the message arrives at a gates in this module which is not further connected, that is, the gate's getNextGate() method returns nullptr. The default, cModule implementation reports an error ("message arrived at a compound module"), and the cSimpleModule implementation inserts the message into the FES after some processing.
Reimplemented from cModule.
|
protectedvirtual |
Should be redefined to contain the module activity function. For several good reasons, you should prefer handleMessage() to activity(). This default implementation issues an error message (throws cRuntimeError).
|
protectedvirtual |
Should be redefined to contain the module's message handling function. This default implementation issues an error message (throws cRuntimeError).
|
overridevirtual |
|
overridevirtual |
|
overridevirtual |
Creates a starting message for the module.
Reimplemented from cModule.
|
inline |
Returns the event handling scheme: activity() or handleMessage().
|
inline |
Returns true if the module has already terminated, by having called end() or returning from the activity() method.
|
virtual |
To be called from module functions. Outputs textual information about all objects of the simulation (including the objects created in module functions by the user!) into the snapshot file. The output is detailed enough to be used for debugging the simulation: by regularly calling snapshot(), one can trace how the values of variables, objects changed over the simulation. The arguments: label is a string that will appear in the output file; obj is the object whose inside is of interest. By default, the whole simulation (all modules etc) will be written out.
Tkenv also supports making snapshots manually, from menu.
See also class cWatch and the WATCH() macro.
|
inlinevirtual |
Sends a message through the gate given with its ID.
References SIMTIME_ZERO.
|
inlinevirtual |
Sends a message through the gate given with its name and index (if multiple gate).
References SIMTIME_ZERO.
Sends a message through the gate given with its pointer.
References SIMTIME_ZERO.
Delayed sending. Sends a message through the gate given with its index as if it was sent delay seconds later.
|
virtual |
Delayed sending. Sends a message through the gate given with its name and index (if multiple gate) as if it was sent delay seconds later.
Sends a message through the gate given with its pointer as if it was sent delay seconds later.
|
virtual |
Sends a message directly to another module, with zero propagation delay and duration. See sendDirect(cMessage *, simtime_t, simtime_t, cGate *) for a more detailed description.
Sends a message directly to another module, with zero propagation delay and duration. See sendDirect(cMessage *, simtime_t, simtime_t, cGate *) for a more detailed description.
Sends a message directly to another module, with zero propagation delay and duration. See sendDirect(cMessage *, simtime_t, simtime_t, cGate *) for a more detailed description.
|
virtual |
Sends a message directly to another module. See sendDirect(cMessage *, simtime_t, simtime_t, cGate *) for a more detailed description.
|
virtual |
See sendDirect(cMessage *, simtime_t, simtime_t, cGate *) for a more detailed description.
|
virtual |
Send a message directly to another module.
If the target gate is further connected (i.e. getNextGate()!=nullptr), the message will follow the connections that start at that gate. For example, when sending to an input gate of a compound module, the message will follow the connections inside the compound module.
It is permitted to send to an output gate, which will also cause the message to follow the connections starting at that gate. This can be useful, for example, when several submodules are sending to a single output gate of their parent module.
It is not permitted to send to a gate of a compound module which is not further connected (i.e. getNextGate()==nullptr), as this would cause the message to arrive at a compound module.
Also, it is not permitted to send to a gate which is otherwise connected i.e. where getPreviousGate()!=nullptr. This means that modules MUST have dedicated gates for receiving via sendDirect(). You cannot have a gate which receives messages via both connections and sendDirect().
When a nonzero duration is given, that signifies the duration of the packet transmission, that is, the time difference between the transmission (or reception) of the start of the packet and that of the end of the packet. The destination module can choose whether it wants the simulation kernel to deliver the packet object to it at the start or at the end of the reception. The default is the latter; the module can change it by calling setDeliverOnReceptionStart() on the final input gate (that is, on inputGate->getPathEndGate()). setDeliverOnReceptionStart() needs to be called in advance, for example in the initialize() method of the module. When a module receives a packet, it can call the isReceptionStart() and getDuration() methods on the packet to find out whether it represents the start or the end of the reception, and the duration of the transmission.
For messages that are not packets (i.e. not subclassed from cPacket), the duration parameter must be zero.
Schedules a self-message. It will be delivered back to the module via receive() or handleMessage() at simulation time t. This method is the way you can implement timers or timeouts. Timers can also be cancelled via cancelEvent() (See below.)
When the message is delivered at the module, you can call msg->isSelfMessage()
to tell it apart from messages arriving from other modules. msg->getKind()
can be used to further classify it, or of you need to manage an unbounded number of timers, you can set msg->getContextPointer()
before scheduling to point to the data structure the message belongs to – this way you can avoid having to search through lists or other data structures to find out where a just-arrived self-message belongs.
cancelEvent() can be used to cancel the self-message before it arrives. This is useful for implementing timeouts: if the event occurs "in time" (before timeout), the scheduled self-message can be cancelled.
Given a cMessage pointer, you can check whether it is currently scheduled by calling msg->isScheduled()
. If it is scheduled, you cannot schedule it again without calling cancelEvent() first. However, after the message was delivered to the module or cancelled, you can schedule it again – so you can reuse the same message object for timeouts over and over during the whole simulation.
Removes the given message from the future events. The message needs to have been sent using the scheduleAt() function. This function can be used to cancel a timer implemented with scheduleAt(). If the message is not currently scheduled, nothing happens.
|
virtual |
Invokes cancelEvent() on the message (in case it is scheduled), then deletes it. nullptr is also accepted, then the method does nothing. This method is especially useful in simple module destructors, to dispose of self-messages that the module has allocated.
|
virtual |
Remove the next message from the event queue and return a pointer to it.
Removes the next message from the event queue and returns a pointer to it. If there is no message in the event queue, the function waits with t timeout until a message will be available. If the timeout expires and there is still no message in the queue, the function returns nullptr.
|
virtual |
Waits for the given interval. (Some other simulators call this functionality hold()). It is intended for use only if you do not expect other messages to arrive at the module during the wait period. To assert this, it throws an exception if a message arrives during the wait.
If you expect to receive messages during the call, you should use waitAndEnqueue() instead.
This function can only be used with activity(), but not with handleMessage().
Waits for the given interval. The messages received during the wait period are inserted into the queue passed as argument.
This function can only be used with activity(), but not with handleMessage().
|
virtual |
Causes the whole simulation to stop. The implementation simply throws a cTerminationException.
|
virtual |
May only be invoked from activity()-based simple modules. Execution of the simple module stops in this call, and any further messages sent to module will cause a runtime error.
|
virtual |
Equivalent to throw cRuntimeError(same argument list)
.
|
virtual |
Returns true if there was a stack overflow during execution of the coroutine. (Not implemented for every coroutine package - see cCoroutine documentation for more info.) If the module uses handleMessage(), this method always returns false.
|
virtual |
Returns the stack size of the coroutine. If the module uses handleMessage(), this method always returns 0.
|
virtual |
Returns the amount of stack actually used by the coroutine. (Not implemented for every coroutine package - see cCoroutine documentation for more info.) If the module uses handleMessage(), this method always returns 0.