OMNeT++ Simulation Library  6.0.3
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 
45 SIM_API extern cSoftOwner globalOwningContext; // also in globals.h
46 
47 
63 class SIM_API cSimulation : public cNamedObject, noncopyable
64 {
65  friend class cSimpleModule;
66  private:
67  // global variables
68  static cSimulation *activeSimulation;
69  static cEnvir *activeEnvir;
70  static cEnvir *staticEnvir; // the environment to activate when activeSimulation becomes nullptr
71 
72  // variables of the module vector
73  int size = 0; // size of componentv[]
74  int delta = 32; // if needed, grows by delta
75  cComponent **componentv = nullptr; // vector of modules/channels, componentv[0] is not used for historical reasons
76  int lastComponentId = 0; // index of last used pos. in componentv[]
77 
78  // simulation vars
79  cEnvir *envir = nullptr; // the environment that belongs to this simulation object
80  cModule *systemModule = nullptr; // pointer to system (root) module
81  cSimpleModule *currentActivityModule = nullptr; // the module currently executing activity() (nullptr if handleMessage() or in main)
82  cComponent *contextComponent = nullptr; // component in context (or nullptr)
83  ContextType contextType; // the innermost context type
84  cModuleType *networkType = nullptr; // network type
85  cFutureEventSet *fes = nullptr; // stores future events
86  cScheduler *scheduler = nullptr; // event scheduler
87  simtime_t warmupPeriod; // warm-up period
88 
89  ContextType simulationStage; // simulation stage
90  simtime_t currentSimtime; // simulation time (time of current event)
91  eventnumber_t currentEventNumber = 0; // sequence number of current event
92 
93  cException *exception; // helper variable to get exceptions back from activity()
94  bool trapOnNextEvent = false; // when set, next handleMessage or activity() will execute debugger interrupt
95 
96  bool parameterMutabilityCheck = true; // when disabled, module parameters can be set without them being declared @mutable
97 
98  cFingerprintCalculator *fingerprint = nullptr; // used for fingerprint calculation
99 
100  private:
101  // internal
102  void checkActive() {if (getActiveSimulation()!=this) throw cRuntimeError(this, E_WRONGSIM);}
103 
104  public:
105  // internal
106  void setParameterMutabilityCheck(bool b) {parameterMutabilityCheck = b;}
107  bool getParameterMutabilityCheck() const {return parameterMutabilityCheck;}
108 
109  public:
116  cSimulation(const char *name, cEnvir *env);
117 
121  virtual ~cSimulation();
123 
130  virtual void forEachChild(cVisitor *v) override;
131 
135  virtual std::string getFullPath() const override;
137 
143  static cSimulation *getActiveSimulation() {return activeSimulation;}
144 
149  static cEnvir *getActiveEnvir() {return activeEnvir;}
150 
156  static void setActiveSimulation(cSimulation *sim);
157 
162  static void setStaticEnvir(cEnvir *env);
163 
167  static cEnvir *getStaticEnvir() {return staticEnvir;}
168 
172  cEnvir *getEnvir() const {return envir;}
174 
177 
184  int registerComponent(cComponent *component);
185 
190  void deregisterComponent(cComponent *component);
191 
195  int getLastComponentId() const {return lastComponentId;}
196 
209  cModule *getModuleByPath(const char *modulePath) const;
210 
223  cModule *findModuleByPath(const char *modulePath) const;
224 
229  cComponent *getComponent(int id) const {return id<0 || id>=size ? nullptr : componentv[id];}
230 
235  cModule *getModule(int id) const {return id<0 || id>=size || !componentv[id] ? nullptr : componentv[id]->isModule() ? (cModule *)componentv[id] : nullptr;}
236 
241  cChannel *getChannel(int id) const {return id<0 || id>=size || !componentv[id] ? nullptr : componentv[id]->isChannel() ? (cChannel *)componentv[id] : nullptr;}
242 
246  void setSystemModule(cModule *module);
247 
251  cModule *getSystemModule() const {return systemModule;}
253 
263 
276  static int loadNedSourceFolder(const char *folderName, const char *excludedPackages="");
277 
285  static void loadNedFile(const char *nedFilename, const char *expectedPackage=nullptr, bool isXML=false);
286 
297  static void loadNedText(const char *name, const char *nedText, const char *expectedPackage=nullptr, bool isXML=false);
298 
305  static void doneLoadingNedFiles();
306 
311  static std::string getNedPackageForFolder(const char *folder);
313 
316 
322  void setScheduler(cScheduler *scheduler);
323 
327  cScheduler *getScheduler() const {return scheduler;}
328 
334  void setFES(cFutureEventSet *fes);
335 
339  cFutureEventSet *getFES() const {return fes;}
340 
345  void setSimulationTimeLimit(simtime_t simTimeLimit);
346 
350  void setupNetwork(cModuleType *networkType);
351 
357  void callInitialize();
358 
363  void callFinish();
364 
369  void deleteNetwork();
371 
380  int getSimulationStage() const {return simulationStage;}
381 
386  cModuleType *getNetworkType() const {return networkType;}
387 
393  void setSimTime(simtime_t time) {currentSimtime = time;}
394 
400  simtime_t_cref getSimTime() const {return currentSimtime;}
401 
406  eventnumber_t getEventNumber() const {return currentEventNumber;}
407 
417  simtime_t_cref getWarmupPeriod() const {return warmupPeriod;}
418 
422  void setWarmupPeriod(simtime_t t) {warmupPeriod = t;}
424 
432  cEvent *guessNextEvent();
433 
439  cSimpleModule *guessNextModule();
440 
445  simtime_t guessNextSimtime();
446 
457  cEvent *takeNextEvent();
458 
462  void putBackEvent(cEvent *event);
463 
468  void executeEvent(cEvent *event);
469 
473  void callRefreshDisplay();
474 
479  void transferTo(cSimpleModule *module);
480 
484  void transferToMain();
485 
491  void insertEvent(cEvent *event);
492 
496  void setContext(cComponent *component);
497 
501  void setContextType(ContextType type) {contextType = type;}
502 
506  void setGlobalContext() {contextComponent=nullptr; cOwnedObject::setOwningContext(&globalOwningContext);}
507 
513  cSimpleModule *getActivityModule() const {return currentActivityModule;}
514 
518  cComponent *getContext() const {return contextComponent;}
519 
527  ContextType getContextType() const {return contextType;}
528 
533  cModule *getContextModule() const;
534 
540  cSimpleModule *getContextSimpleModule() const;
541 
547  void requestTrapOnNextEvent() {trapOnNextEvent = true;}
548 
552  void clearTrapOnNextEvent() {trapOnNextEvent = false;}
553 
558  bool isTrapOnNextEventRequested() const {return trapOnNextEvent;}
560 
569  unsigned long getUniqueNumber();
570 
576  void snapshot(cObject *obj, const char *label);
577 
583 
587  void setFingerprintCalculator(cFingerprintCalculator *fingerprint);
589 };
590 
597 
604 
612 
613 } // namespace omnetpp
614 
615 
616 #endif
617 
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:400
omnetpp::cSimulation::getContext
cComponent * getContext() const
Definition: csimulation.h:518
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:582
omnetpp::cSimulation::getSimulationStage
int getSimulationStage() const
Definition: csimulation.h:380
omnetpp::cSimulation::requestTrapOnNextEvent
void requestTrapOnNextEvent()
Definition: csimulation.h:547
omnetpp::cFingerprintCalculator
This class defines the interface for fingerprint calculators.
Definition: cfingerprint.h:34
omnetpp::getEnvir
cEnvir * getEnvir()
Returns the environment object for the currently active simulation. This function never returns nullp...
Definition: csimulation.h:611
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:63
omnetpp::cSimulation::clearTrapOnNextEvent
void clearTrapOnNextEvent()
Definition: csimulation.h:552
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:596
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:251
omnetpp::cSimulation::getComponent
cComponent * getComponent(int id) const
Definition: csimulation.h:229
omnetpp::cSimulation::isTrapOnNextEventRequested
bool isTrapOnNextEventRequested() const
Definition: csimulation.h:558
omnetpp::cSimulation::getScheduler
cScheduler * getScheduler() const
Definition: csimulation.h:327
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:235
omnetpp::getSimulation
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:603
omnetpp::cSimulation::getLastComponentId
int getLastComponentId() const
Definition: csimulation.h:195
omnetpp::cSimulation::getWarmupPeriod
simtime_t_cref getWarmupPeriod() const
Definition: csimulation.h:417
omnetpp::cSimulation::getContextType
ContextType getContextType() const
Definition: csimulation.h:527
omnetpp::cSimulation::setWarmupPeriod
void setWarmupPeriod(simtime_t t)
Definition: csimulation.h:422
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:339
omnetpp::cSimulation::getChannel
cChannel * getChannel(int id) const
Definition: csimulation.h:241
omnetpp::cSimulation::getActivityModule
cSimpleModule * getActivityModule() const
Definition: csimulation.h:513
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:386
omnetpp::cSimulation::getActiveEnvir
static cEnvir * getActiveEnvir()
Definition: csimulation.h:149
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:167
omnetpp::cSimulation::getActiveSimulation
static cSimulation * getActiveSimulation()
Definition: csimulation.h:143
omnetpp::cSimulation::setSimTime
void setSimTime(simtime_t time)
Definition: csimulation.h:393
omnetpp::cSimulation::getEventNumber
eventnumber_t getEventNumber() const
Definition: csimulation.h:406
omnetpp::cSimulation::setContextType
void setContextType(ContextType type)
Definition: csimulation.h:501
omnetpp::cSimulation::setGlobalContext
void setGlobalContext()
Definition: csimulation.h:506
omnetpp::cSimulation::getEnvir
cEnvir * getEnvir() const
Definition: csimulation.h:172