OMNeT++ API 6.1
Discrete Event Simulation Library
csimulation.h
1 //==========================================================================
2 // CSIMULATION.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_CSIMULATION_H
17 #define __OMNETPP_CSIMULATION_H
18 
19 #include "simkerneldefs.h"
20 #include "simtime_t.h"
21 #include "ccomponent.h"
22 #include "ccontextswitcher.h"
23 #include "cexception.h"
24 
25 namespace omnetpp {
26 
27 class cEvent;
28 class cMessage;
29 class cGate;
30 class cComponent;
31 class cModule;
32 class cChannel;
33 class cSimpleModule;
34 class cSimulation;
35 class cException;
36 class cFutureEventSet;
37 class cScheduler;
38 class cParsimPartition;
39 class cNedFileLoader;
40 class cFingerprintCalculator;
41 class cModuleType;
42 class cEnvir;
43 class cSoftOwner;
44 class cUsageCollector; // (internal)
45 
46 SIM_API extern cSoftOwner globalOwningContext; // also in globals.h
47 
48 
64 class SIM_API cSimulation : public cNamedObject, noncopyable
65 {
66  friend class cSimpleModule;
67  private:
68  // global variables
69  static cSimulation *activeSimulation;
70  static cEnvir *activeEnvir;
71  static cEnvir *staticEnvir; // the environment to activate when activeSimulation becomes nullptr
72 
73  // variables of the module vector
74  int size = 0; // size of componentv[]
75  int delta = 32; // if needed, grows by delta
76  cComponent **componentv = nullptr; // vector of modules/channels, componentv[0] is not used for historical reasons
77  int lastComponentId = 0; // index of last used pos. in componentv[]
78 
79  // simulation vars
80  cEnvir *envir = nullptr; // the environment that belongs to this simulation object
81  cModule *systemModule = nullptr; // pointer to system (root) module
82  cSimpleModule *currentActivityModule = nullptr; // the module currently executing activity() (nullptr if handleMessage() or in main)
83  cComponent *contextComponent = nullptr; // component in context (or nullptr)
84  ContextType contextType; // the innermost context type
85  cModuleType *networkType = nullptr; // network type
86  cFutureEventSet *fes = nullptr; // stores future events
87  cScheduler *scheduler = nullptr; // event scheduler
88  simtime_t warmupPeriod; // warm-up period
89 
90  ContextType simulationStage; // simulation stage
91  simtime_t currentSimtime; // simulation time (time of current event)
92  eventnumber_t currentEventNumber = 0; // sequence number of current event
93 
94  cException *exception; // helper variable to get exceptions back from activity()
95  bool trapOnNextEvent = false; // when set, next handleMessage or activity() will execute debugger interrupt
96 
97  bool parameterMutabilityCheck = true; // when disabled, module parameters can be set without them being declared @mutable
98 
99  cFingerprintCalculator *fingerprint = nullptr; // used for fingerprint calculation
100  cUsageCollector *usageCollector = nullptr; // used for coverage tests and for detecting unread parameters
101 
102  private:
103  // internal
104  void checkActive() {if (getActiveSimulation()!=this) throw cRuntimeError(this, E_WRONGSIM);}
105 
106  public:
107  // internal
108  void setParameterMutabilityCheck(bool b) {parameterMutabilityCheck = b;}
109  bool getParameterMutabilityCheck() const {return parameterMutabilityCheck;}
110  // internal, for cUsageCollector
111  void parametersAdded(cComponent *component);
112  void parameterAccessed(cPar *par);
113 
114  public:
121  cSimulation(const char *name, cEnvir *env);
122 
126  virtual ~cSimulation();
128 
135  virtual void forEachChild(cVisitor *v) override;
136 
140  virtual std::string getFullPath() const override;
142 
148  static cSimulation *getActiveSimulation() {return activeSimulation;}
149 
154  static cEnvir *getActiveEnvir() {return activeEnvir;}
155 
161  static void setActiveSimulation(cSimulation *sim);
162 
167  static void setStaticEnvir(cEnvir *env);
168 
172  static cEnvir *getStaticEnvir() {return staticEnvir;}
173 
177  cEnvir *getEnvir() const {return envir;}
179 
182 
189  int registerComponent(cComponent *component);
190 
195  void deregisterComponent(cComponent *component);
196 
200  int getLastComponentId() const {return lastComponentId;}
201 
214  cModule *getModuleByPath(const char *modulePath) const;
215 
228  cModule *findModuleByPath(const char *modulePath) const;
229 
234  cComponent *getComponent(int id) const {return id<0 || id>=size ? nullptr : componentv[id];}
235 
240  cModule *getModule(int id) const {return id<0 || id>=size || !componentv[id] ? nullptr : componentv[id]->isModule() ? (cModule *)componentv[id] : nullptr;}
241 
246  cChannel *getChannel(int id) const {return id<0 || id>=size || !componentv[id] ? nullptr : componentv[id]->isChannel() ? (cChannel *)componentv[id] : nullptr;}
247 
251  void setSystemModule(cModule *module);
252 
256  cModule *getSystemModule() const {return systemModule;}
258 
268 
281  static int loadNedSourceFolder(const char *folderName, const char *excludedPackages="");
282 
290  static void loadNedFile(const char *nedFilename, const char *expectedPackage=nullptr, bool isXML=false);
291 
302  static void loadNedText(const char *name, const char *nedText, const char *expectedPackage=nullptr, bool isXML=false);
303 
310  static void doneLoadingNedFiles();
311 
316  static std::string getNedPackageForFolder(const char *folder);
318 
321 
327  void setScheduler(cScheduler *scheduler);
328 
332  cScheduler *getScheduler() const {return scheduler;}
333 
339  void setFES(cFutureEventSet *fes);
340 
344  cFutureEventSet *getFES() const {return fes;}
345 
350  void setSimulationTimeLimit(simtime_t simTimeLimit);
351 
355  void setupNetwork(cModuleType *networkType);
356 
362  void callInitialize();
363 
368  void callFinish();
369 
374  void deleteNetwork();
376 
385  int getSimulationStage() const {return simulationStage;}
386 
391  cModuleType *getNetworkType() const {return networkType;}
392 
398  void setSimTime(simtime_t time) {currentSimtime = time;}
399 
405  simtime_t_cref getSimTime() const {return currentSimtime;}
406 
411  eventnumber_t getEventNumber() const {return currentEventNumber;}
412 
422  simtime_t_cref getWarmupPeriod() const {return warmupPeriod;}
423 
427  void setWarmupPeriod(simtime_t t) {warmupPeriod = t;}
429 
437  cEvent *guessNextEvent();
438 
444  cSimpleModule *guessNextModule();
445 
450  simtime_t guessNextSimtime();
451 
462  cEvent *takeNextEvent();
463 
467  void putBackEvent(cEvent *event);
468 
473  void executeEvent(cEvent *event);
474 
478  void callRefreshDisplay();
479 
484  void transferTo(cSimpleModule *module);
485 
489  void transferToMain();
490 
496  void insertEvent(cEvent *event);
497 
501  void setContext(cComponent *component);
502 
506  void setContextType(ContextType type) {contextType = type;}
507 
511  void setGlobalContext() {contextComponent=nullptr; cOwnedObject::setOwningContext(&globalOwningContext);}
512 
518  cSimpleModule *getActivityModule() const {return currentActivityModule;}
519 
523  cComponent *getContext() const {return contextComponent;}
524 
532  ContextType getContextType() const {return contextType;}
533 
538  cModule *getContextModule() const;
539 
545  cSimpleModule *getContextSimpleModule() const;
546 
552  void requestTrapOnNextEvent() {trapOnNextEvent = true;}
553 
557  void clearTrapOnNextEvent() {trapOnNextEvent = false;}
558 
563  bool isTrapOnNextEventRequested() const {return trapOnNextEvent;}
565 
574  unsigned long getUniqueNumber();
575 
581  void snapshot(cObject *obj, const char *label);
582 
588 
592  void setFingerprintCalculator(cFingerprintCalculator *fingerprint);
594 };
595 
602 
609 
617 
618 } // namespace omnetpp
619 
620 
621 #endif
622 
omnetpp::simtime_t_cref
const typedef simtime_t & simtime_t_cref
Constant reference to a simtime_t.
Definition: simtime_t.h:48
omnetpp::cException
Exception class.
Definition: cexception.h:49
omnetpp::cModule
This class represents modules in the simulation.
Definition: cmodule.h:48
omnetpp::cSimulation::getSimTime
simtime_t_cref getSimTime() const
Definition: csimulation.h:405
omnetpp::cSimulation::getContext
cComponent * getContext() const
Definition: csimulation.h:523
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cSimulation::getFingerprintCalculator
cFingerprintCalculator * getFingerprintCalculator()
Definition: csimulation.h:587
omnetpp::cSimulation::getSimulationStage
int getSimulationStage() const
Definition: csimulation.h:385
omnetpp::cSimulation::requestTrapOnNextEvent
void requestTrapOnNextEvent()
Definition: csimulation.h:552
omnetpp::cFingerprintCalculator
This class defines the interface for fingerprint calculators.
Definition: cfingerprint.h:35
omnetpp::getEnvir
cEnvir * getEnvir()
Returns the environment object for the currently active simulation. This function never returns nullp...
Definition: csimulation.h:616
omnetpp::noncopyable
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:415
omnetpp::cEvent
Represents an event in the discrete event simulator.
Definition: cevent.h:46
omnetpp::cSimulation
Simulation manager class.
Definition: csimulation.h:64
omnetpp::cSimulation::clearTrapOnNextEvent
void clearTrapOnNextEvent()
Definition: csimulation.h:557
omnetpp::cComponent::isChannel
bool isChannel() const
Definition: ccomponent.h:475
omnetpp::cFutureEventSet
Abstract base class for the future event set (FES), a central data structure for discrete event simul...
Definition: cfutureeventset.h:32
omnetpp::cScheduler
Abstract class to encapsulate event scheduling.
Definition: cscheduler.h:47
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cChannel
Base class for channels.
Definition: cchannel.h:46
omnetpp::cModuleType
Abstract class for creating a module of a specific type.
Definition: ccomponenttype.h:206
omnetpp::simTime
simtime_t simTime()
Returns the current simulation time.
Definition: csimulation.h:601
omnetpp::cComponent::isModule
bool isModule() const
Definition: ccomponent.h:470
omnetpp::SimTime
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
omnetpp::cSimulation::getSystemModule
cModule * getSystemModule() const
Definition: csimulation.h:256
omnetpp::cSimulation::getComponent
cComponent * getComponent(int id) const
Definition: csimulation.h:234
omnetpp::cSimulation::isTrapOnNextEventRequested
bool isTrapOnNextEventRequested() const
Definition: csimulation.h:563
omnetpp::cSimulation::getScheduler
cScheduler * getScheduler() const
Definition: csimulation.h:332
omnetpp::eventnumber_t
int64_t eventnumber_t
Sequence number of events during the simulation. Events are numbered from one. (Event number zero is ...
Definition: simkerneldefs.h:78
omnetpp::cSimulation::getModule
cModule * getModule(int id) const
Definition: csimulation.h:240
omnetpp::getSimulation
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:608
omnetpp::cSimulation::getLastComponentId
int getLastComponentId() const
Definition: csimulation.h:200
omnetpp::cPar
Represents a module or channel parameter.
Definition: cpar.h:70
omnetpp::cSimulation::getWarmupPeriod
simtime_t_cref getWarmupPeriod() const
Definition: csimulation.h:422
omnetpp::cSimulation::getContextType
ContextType getContextType() const
Definition: csimulation.h:532
omnetpp::cSimulation::setWarmupPeriod
void setWarmupPeriod(simtime_t t)
Definition: csimulation.h:427
omnetpp::cNamedObject
Extends cObject with a name string. Also includes a "flags" member, with bits open for use by subclas...
Definition: cnamedobject.h:34
omnetpp::cComponent
Common base for module and channel classes.
Definition: ccomponent.h:49
omnetpp::cSimulation::getFES
cFutureEventSet * getFES() const
Definition: csimulation.h:344
omnetpp::cSimulation::getChannel
cChannel * getChannel(int id) const
Definition: csimulation.h:246
omnetpp::cSimulation::getActivityModule
cSimpleModule * getActivityModule() const
Definition: csimulation.h:518
omnetpp::cRuntimeError
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:286
omnetpp::cSimpleModule
Base class for all simple module classes.
Definition: csimplemodule.h:202
omnetpp::cSimulation::getNetworkType
cModuleType * getNetworkType() const
Definition: csimulation.h:391
omnetpp::cSimulation::getActiveEnvir
static cEnvir * getActiveEnvir()
Definition: csimulation.h:154
omnetpp::cEnvir
cEnvir represents the "environment" or user interface of the simulation.
Definition: cenvir.h:75
omnetpp::cSimulation::getStaticEnvir
static cEnvir * getStaticEnvir()
Definition: csimulation.h:172
omnetpp::cSimulation::getActiveSimulation
static cSimulation * getActiveSimulation()
Definition: csimulation.h:148
omnetpp::cSimulation::setSimTime
void setSimTime(simtime_t time)
Definition: csimulation.h:398
omnetpp::cSimulation::getEventNumber
eventnumber_t getEventNumber() const
Definition: csimulation.h:411
omnetpp::cSimulation::setContextType
void setContextType(ContextType type)
Definition: csimulation.h:506
omnetpp::cSimulation::setGlobalContext
void setGlobalContext()
Definition: csimulation.h:511
omnetpp::cSimulation::getEnvir
cEnvir * getEnvir() const
Definition: csimulation.h:177