OMNeT++ Simulation Library  6.0.3
csimplemodule.h
1 //==========================================================================
2 // CSIMPLEMODULE.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CSIMPLEMODULE_H
17 #define __OMNETPP_CSIMPLEMODULE_H
18 
19 #include "cmodule.h"
20 
21 namespace omnetpp {
22 
23 class cQueue;
24 class cCoroutine;
25 
82 struct SIM_API SendOptions {
83  simtime_t sendDelay = SIMTIME_ZERO; // for after()
84  simtime_t propagationDelay_ = SIMTIME_ZERO; // for sendDirect()
85  simtime_t duration_ = DURATION_UNSPEC; // for packets not using channel datarate
86  bool isUpdate = false;
87  txid_t transmissionId_ = -1; // if not -1, specifies the transmission id
88  simtime_t remainingDuration = DURATION_UNSPEC; // when updating an earlier transmission
89 
90  static const simtime_t DURATION_UNSPEC;
91  static const SendOptions DEFAULT;
92 
97 
108  SendOptions& after(simtime_t delay) {this->sendDelay = delay; return *this;}
109 
114  SendOptions& propagationDelay(simtime_t delay) {this->propagationDelay_ = delay; return *this;}
115 
124  SendOptions& duration(simtime_t duration) {this->duration_ = duration; return *this;}
125 
133  SendOptions& transmissionId(txid_t transmissionId) {this->transmissionId_ = transmissionId; return *this;}
134 
142  SendOptions& finishTx(txid_t transmissionId) {this->isUpdate = true; this->transmissionId_ = transmissionId; remainingDuration = SIMTIME_ZERO; return *this;}
143 
151  SendOptions& updateTx(txid_t transmissionId) {this->isUpdate = true; this->transmissionId_ = transmissionId; remainingDuration = DURATION_UNSPEC; return *this;}
152 
158  SendOptions& updateTx(txid_t transmissionId, simtime_t remainingDuration) {this->isUpdate = true; this->transmissionId_ = transmissionId; this->remainingDuration = remainingDuration; return *this;}
159 
163  std::string str() const;
164 };
165 
202 class SIM_API cSimpleModule : public cModule //implies noncopyable
203 {
204  friend class cModule;
205  friend class cSimulation;
206  friend class cMessage;
207 
208  private:
209  enum {
210  FL_USESACTIVITY = 1 << 13, // uses activity() or handleMessage()
211  FL_ISTERMINATED = 1 << 14, // for both activity and handleMessage modules
212  FL_STACKALREADYUNWOUND = 1 << 15, // only for activity modules
213  FL_SUPPORTSTXUPDATES = 1 << 16, // whether module is prepared to receive tx updates (see SendOptions)
214  };
215 
216  cMessage *timeoutMessage; // msg used in wait() and receive() with timeout
217  cCoroutine *coroutine;
218 
219  static cMessage *msgForActivity; // helper variable to pass the received message into activity()
220  static bool stackCleanupRequested; // 'true' value asks activity() to throw a cStackCleanupException
221  static cSimpleModule *afterCleanupTransferTo; // transfer back to this module (or to main)
222 
223  private:
224  // internal use
225  static void activate(void *p);
226 
227  // internal utility methods
228  cGate *resolveSendDirectGate(cModule *mod, int gateId);
229  cGate *resolveSendDirectGate(cModule *mod, const char *gateName, int gateIndex);
230  static SendOptions resolveSendDirectOptions(simtime_t propagationDelay, simtime_t duration);
231  void deleteObsoletedTransmissionFromFES(txid_t transmissionId, cPacket *updatePkt);
232  void throwNotOwnerOfMessage(const char *sendOp, cMessage *msg);
233 
234  protected:
235  // internal: handle a message event, basically dispatch to handleMessage() or activity(); not supposed to be overridden by models
236  virtual void doMessageEvent(cMessage *msg);
237 
238  protected:
249 
255  virtual void activity();
256 
261  virtual void handleMessage(cMessage *msg);
263 
264  public:
273  cSimpleModule(unsigned stacksize = 0);
274 
279  virtual ~cSimpleModule();
281 
288  virtual std::string str() const override;
289 
293  virtual bool isSimple() const override;
294 
299  virtual void forEachChild(cVisitor *v) override;
301 
304 
308  virtual void scheduleStart(simtime_t t) override;
309 
315  virtual void arrived(cMessage *msg, cGate *ongate, const SendOptions& options, simtime_t t) override;
317 
320 
324  bool usesActivity() const {return flags&FL_USESACTIVITY;}
325 
341  void setTxUpdateSupport(bool b) {setFlag(FL_SUPPORTSTXUPDATES, b);}
342 
348  bool supportsTxUpdates() const {return flags&FL_SUPPORTSTXUPDATES;}
349 
354  bool isTerminated() const {return flags&FL_ISTERMINATED;}
356 
375  virtual void snapshot(cObject *obj=nullptr, const char *label=nullptr);
377 
380 
384  virtual void send(cMessage *msg, int gateid) {send(msg, gate(gateid));}
385 
390  virtual void send(cMessage *msg, const char *gatename, int gateindex=-1) {send(msg, gate(gatename, gateindex));}
391 
395  virtual void send(cMessage *msg, cGate *outputgate) {send(msg, SendOptions::DEFAULT, outputgate);}
396 
400  virtual void send(cMessage *msg, const SendOptions& options, int gateid) {send(msg, options, gate(gateid));}
401 
406  virtual void send(cMessage *msg, const SendOptions& options, const char *gatename, int gateindex=-1) {send(msg, options, gate(gatename, gateindex));}
407 
411  virtual void send(cMessage *msg, const SendOptions& options, cGate *outputgate);
412 
416  virtual void sendDelayed(cMessage *msg, simtime_t delay, int gateid) {send(msg, SendOptions().after(delay), gate(gateid));}
417 
421  virtual void sendDelayed(cMessage *msg, simtime_t delay, const char *gatename, int gateindex=-1) {send(msg, SendOptions().after(delay), gate(gatename, gateindex));}
422 
426  virtual void sendDelayed(cMessage *msg, simtime_t delay, cGate *outputgate) {send(msg, SendOptions().after(delay), outputgate);}
427 
433  virtual void sendDirect(cMessage *msg, cModule *mod, const char *inputGateName, int gateIndex=-1) {sendDirect(msg, SendOptions::DEFAULT, resolveSendDirectGate(mod, inputGateName, gateIndex));}
434 
440  virtual void sendDirect(cMessage *msg, cModule *mod, int inputGateId) {sendDirect(msg, SendOptions::DEFAULT, resolveSendDirectGate(mod, inputGateId));}
441 
447  virtual void sendDirect(cMessage *msg, cGate *inputGate) {sendDirect(msg, SendOptions::DEFAULT, inputGate);}
448 
454  virtual void sendDirect(cMessage *msg, const SendOptions& options, cModule *mod, const char *inputGateName, int gateIndex=-1) {sendDirect(msg, options, resolveSendDirectGate(mod, inputGateName, gateIndex));}
455 
461  virtual void sendDirect(cMessage *msg, const SendOptions& options, cModule *mod, int inputGateId) {sendDirect(msg, options, resolveSendDirectGate(mod, inputGateId));}
462 
485  virtual void sendDirect(cMessage *msg, const SendOptions& options, cGate *inputGate);
486 
490  virtual void sendDirect(cMessage *msg, simtime_t propagationDelay, simtime_t duration, cModule *mod, const char *inputGateName, int gateIndex=-1) {sendDirect(msg, resolveSendDirectOptions(propagationDelay, duration), resolveSendDirectGate(mod, inputGateName, gateIndex));}
491 
495  virtual void sendDirect(cMessage *msg, simtime_t propagationDelay, simtime_t duration, cModule *mod, int inputGateId) {sendDirect(msg, resolveSendDirectOptions(propagationDelay, duration), resolveSendDirectGate(mod, inputGateId));}
496 
500  virtual void sendDirect(cMessage *msg, simtime_t propagationDelay, simtime_t duration, cGate *inputGate) {sendDirect(msg, resolveSendDirectOptions(propagationDelay, duration), inputGate);}
502 
505 
534  virtual void scheduleAt(simtime_t t, cMessage *msg);
535 
543  virtual void scheduleAfter(simtime_t delay, cMessage *msg);
544 
552  virtual void rescheduleAt(simtime_t t, cMessage *msg);
553 
561  virtual void rescheduleAfter(simtime_t delay, cMessage *msg);
562 
569  virtual cMessage *cancelEvent(cMessage *msg);
570 
577  virtual void cancelAndDelete(cMessage *msg);
579 
585 
590  virtual cMessage *receive();
591 
599  virtual cMessage *receive(simtime_t timeout);
601 
604 
618  virtual void wait(simtime_t time);
619 
627  virtual void waitAndEnqueue(simtime_t time, cQueue *queue);
629 
636  virtual void endSimulation();
637 
643  virtual void halt();
644 
648  _OPP_GNU_ATTRIBUTE(format(printf, 2, 3))
649  virtual void error(const char *format,...) const;
651 
654 
663  virtual bool hasStackOverflow() const;
664 
669  virtual unsigned getStackSize() const;
670 
679  virtual unsigned getStackUsage() const;
681 };
682 
683 } // namespace omnetpp
684 
685 
686 #endif
687 
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, cModule *mod, int inputGateId)
Definition: csimplemodule.h:440
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::SendOptions::propagationDelay
SendOptions & propagationDelay(simtime_t delay)
Definition: csimplemodule.h:114
omnetpp::SendOptions::updateTx
SendOptions & updateTx(txid_t transmissionId, simtime_t remainingDuration)
Definition: csimplemodule.h:158
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, const SendOptions &options, cModule *mod, int inputGateId)
Definition: csimplemodule.h:461
omnetpp::cSimpleModule::sendDelayed
virtual void sendDelayed(cMessage *msg, simtime_t delay, cGate *outputgate)
Definition: csimplemodule.h:426
omnetpp::cSimpleModule::send
virtual void send(cMessage *msg, const SendOptions &options, int gateid)
Definition: csimplemodule.h:400
omnetpp::SendOptions::SendOptions
SendOptions()
Definition: csimplemodule.h:96
omnetpp::cSimulation
Simulation manager class.
Definition: csimulation.h:63
omnetpp::SendOptions::finishTx
SendOptions & finishTx(txid_t transmissionId)
Definition: csimplemodule.h:142
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cSimpleModule::sendDelayed
virtual void sendDelayed(cMessage *msg, simtime_t delay, int gateid)
Definition: csimplemodule.h:416
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, cGate *inputGate)
Definition: csimplemodule.h:447
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, simtime_t propagationDelay, simtime_t duration, cModule *mod, const char *inputGateName, int gateIndex=-1)
Definition: csimplemodule.h:490
omnetpp::SendOptions::transmissionId
SendOptions & transmissionId(txid_t transmissionId)
Definition: csimplemodule.h:133
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, simtime_t propagationDelay, simtime_t duration, cModule *mod, int inputGateId)
Definition: csimplemodule.h:495
omnetpp::SimTime
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, cModule *mod, const char *inputGateName, int gateIndex=-1)
Definition: csimplemodule.h:433
omnetpp::cSimpleModule::send
virtual void send(cMessage *msg, const char *gatename, int gateindex=-1)
Definition: csimplemodule.h:390
omnetpp::cSimpleModule::supportsTxUpdates
bool supportsTxUpdates() const
Definition: csimplemodule.h:348
omnetpp::SendOptions
Options for the cSimpleModule::send() and cSimpleModule::sendDirect() calls.
Definition: csimplemodule.h:82
omnetpp::cGate
Represents a module gate.
Definition: cgate.h:62
omnetpp::SendOptions::duration
SendOptions & duration(simtime_t duration)
Definition: csimplemodule.h:124
omnetpp::cSimpleModule::send
virtual void send(cMessage *msg, int gateid)
Definition: csimplemodule.h:384
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, simtime_t propagationDelay, simtime_t duration, cGate *inputGate)
Definition: csimplemodule.h:500
omnetpp::cSimpleModule::sendDelayed
virtual void sendDelayed(cMessage *msg, simtime_t delay, const char *gatename, int gateindex=-1)
Definition: csimplemodule.h:421
SIMTIME_ZERO
#define SIMTIME_ZERO
Zero simulation time.
Definition: simtime_t.h:70
omnetpp::txid_t
int64_t txid_t
Transmission ID. See SendOptions::transmissionId(), cMessage::getTransmissionId().
Definition: simkerneldefs.h:92
omnetpp::cCoroutine
Low-level coroutine library. Coroutines are used by cSimpleModule.
Definition: ccoroutine.h:66
omnetpp::cMessage
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
omnetpp::cSimpleModule::usesActivity
bool usesActivity() const
Definition: csimplemodule.h:324
omnetpp::SendOptions::updateTx
SendOptions & updateTx(txid_t transmissionId)
Definition: csimplemodule.h:151
omnetpp::cSimpleModule
Base class for all simple module classes.
Definition: csimplemodule.h:202
omnetpp::cPacket
A subclass of cMessage to represent packets, frames, datagrams, application messages,...
Definition: cpacket.h:52
omnetpp::cSimpleModule::sendDirect
virtual void sendDirect(cMessage *msg, const SendOptions &options, cModule *mod, const char *inputGateName, int gateIndex=-1)
Definition: csimplemodule.h:454
omnetpp::cSimpleModule::isTerminated
bool isTerminated() const
Definition: csimplemodule.h:354
omnetpp::SendOptions::after
SendOptions & after(simtime_t delay)
Definition: csimplemodule.h:108
omnetpp::cQueue
Queue class for objects derived from cObject.
Definition: cqueue.h:42
omnetpp::cSimpleModule::send
virtual void send(cMessage *msg, cGate *outputgate)
Definition: csimplemodule.h:395
omnetpp::cSimpleModule::send
virtual void send(cMessage *msg, const SendOptions &options, const char *gatename, int gateindex=-1)
Definition: csimplemodule.h:406
omnetpp::cSimpleModule::setTxUpdateSupport
void setTxUpdateSupport(bool b)
Definition: csimplemodule.h:341