OMNeT++ Simulation Library  5.6.1
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 "cexception.h"
23 
24 namespace omnetpp {
25 
26 class cEvent;
27 class cMessage;
28 class cGate;
29 class cComponent;
30 class cModule;
31 class cChannel;
32 class cSimpleModule;
33 class cSimulation;
34 class cException;
35 class cFutureEventSet;
36 class cScheduler;
37 class cParsimPartition;
38 class cNedFileLoader;
39 class cFingerprintCalculator;
40 class cModuleType;
41 class cEnvir;
42 class cDefaultList;
43 
44 SIM_API extern cDefaultList defaultList; // also in globals.h
45 
46 
62 class SIM_API cSimulation : public cNamedObject, noncopyable
63 {
64  friend class cSimpleModule;
65  private:
66  // global variables
67  static cSimulation *activeSimulation;
68  static cEnvir *activeEnvir;
69  static cEnvir *staticEnvir; // the environment to activate when activeSimulation becomes nullptr
70 
71  // variables of the module vector
72  int size; // size of componentv[]
73  int delta; // if needed, grows by delta
74  cComponent **componentv; // vector of modules/channels, componentv[0] is not used
75  int lastComponentId; // index of last used pos. in componentv[]
76 #ifdef USE_OMNETPP4x_FINGERPRINTS
77  int lastVersion4ModuleId; // last used OMNeT++ V4.x compatible module ID
78 #endif
79 
80  // simulation vars
81  cEnvir *envir; // the environment that belongs to this simulation object
82  cModule *systemModule; // pointer to system (root) module
83  cSimpleModule *currentActivityModule; // the module currently executing activity() (nullptr if handleMessage() or in main)
84  cComponent *contextComponent; // component in context (or nullptr)
85  int contextType; // the innermost context type (one of CTX_BUILD, CTX_EVENT, CTX_INITIALIZE, CTX_FINISH)
86  cModuleType *networkType; // network type
87  cFutureEventSet *fes; // stores future events
88  cScheduler *scheduler; // event scheduler
89  simtime_t warmupPeriod; // warm-up period
90 
91  int simulationStage; // simulation stage (one of CTX_NONE, CTX_BUILD, CTX_EVENT, CTX_INITIALIZE, CTX_FINISH or CTX_CLEANUP)
92  simtime_t currentSimtime; // simulation time (time of current event)
93  eventnumber_t currentEventNumber; // sequence number of current event
94 
95  cMessage *msgForActivity; // helper variable to pass the received message into activity()
96  cException *exception; // helper variable to get exceptions back from activity()
97  bool trapOnNextEvent; // when set, next handleMessage or activity() will execute debugger interrupt
98 
99  cFingerprintCalculator *fingerprint; // used for fingerprint calculation
100 
101  private:
102  // internal
103  void checkActive() {if (getActiveSimulation()!=this) throw cRuntimeError(this, E_WRONGSIM);}
104 
105  // helper for executeEvent()
106  void doMessageEvent(cMessage *msg, cSimpleModule *mod);
107 
108  public:
115  cSimulation(const char *name, cEnvir *env);
116 
120  virtual ~cSimulation();
122 
129  virtual void forEachChild(cVisitor *v) override;
130 
134  virtual std::string getFullPath() const override;
136 
142  static cSimulation *getActiveSimulation() {return activeSimulation;}
143 
148  static cEnvir *getActiveEnvir() {return activeEnvir;}
149 
155  static void setActiveSimulation(cSimulation *sim);
156 
161  static void setStaticEnvir(cEnvir *env);
162 
166  static cEnvir *getStaticEnvir() {return staticEnvir;}
167 
171  cEnvir *getEnvir() const {return envir;}
173 
176 
183  int registerComponent(cComponent *component);
184 
189  void deregisterComponent(cComponent *component);
190 
194  int getLastComponentId() const {return lastComponentId;}
195 
203  cModule *getModuleByPath(const char *modulePath) const;
204 
209  cComponent *getComponent(int id) const {return id<0 || id>=size ? nullptr : componentv[id];}
210 
215  cModule *getModule(int id) const {return id<0 || id>=size || !componentv[id] ? nullptr : componentv[id]->isModule() ? (cModule *)componentv[id] : nullptr;}
216 
221  cChannel *getChannel(int id) const {return id<0 || id>=size || !componentv[id] ? nullptr : componentv[id]->isChannel() ? (cChannel *)componentv[id] : nullptr;}
222 
226  void setSystemModule(cModule *module);
227 
231  cModule *getSystemModule() const {return systemModule;}
233 
243 
253  static int loadNedSourceFolder(const char *folderName);
254 
262  static void loadNedFile(const char *nedFilename, const char *expectedPackage=nullptr, bool isXML=false);
263 
274  static void loadNedText(const char *name, const char *nedText, const char *expectedPackage=nullptr, bool isXML=false);
275 
282  static void doneLoadingNedFiles();
283 
288  static std::string getNedPackageForFolder(const char *folder);
290 
293 
299  void setScheduler(cScheduler *scheduler);
300 
304  cScheduler *getScheduler() const {return scheduler;}
305 
311  void setFES(cFutureEventSet *fes);
312 
316  cFutureEventSet *getFES() const {return fes;}
317 
322  void setSimulationTimeLimit(simtime_t simTimeLimit);
323 
327  void setupNetwork(cModuleType *networkType);
328 
334  void callInitialize();
335 
340  void callFinish();
341 
346  void deleteNetwork();
348 
357  int getSimulationStage() const {return simulationStage;}
358 
363  cModuleType *getNetworkType() const {return networkType;}
364 
370  void setSimTime(simtime_t time) {currentSimtime = time;}
371 
377  simtime_t_cref getSimTime() const {return currentSimtime;}
378 
383  eventnumber_t getEventNumber() const {return currentEventNumber;}
384 
394  simtime_t_cref getWarmupPeriod() const {return warmupPeriod;}
395 
399  void setWarmupPeriod(simtime_t t) {warmupPeriod = t;}
401 
409  cEvent *guessNextEvent();
410 
416  cSimpleModule *guessNextModule();
417 
422  simtime_t guessNextSimtime();
423 
434  cEvent *takeNextEvent();
435 
439  void putBackEvent(cEvent *event);
440 
445  void executeEvent(cEvent *event);
446 
451  void transferTo(cSimpleModule *module);
452 
456  void transferToMain();
457 
463  void insertEvent(cEvent *event);
464 
468  void setContext(cComponent *component);
469 
473  void setContextType(int type) {contextType = type;}
474 
478  void setGlobalContext() {contextComponent=nullptr; cOwnedObject::setDefaultOwner(&defaultList);}
479 
485  cSimpleModule *getActivityModule() const {return currentActivityModule;}
486 
490  cComponent *getContext() const {return contextComponent;}
491 
499  int getContextType() const {return contextType;}
500 
505  cModule *getContextModule() const;
506 
512  cSimpleModule *getContextSimpleModule() const;
513 
519  void requestTrapOnNextEvent() {trapOnNextEvent = true;}
520 
524  void clearTrapOnNextEvent() {trapOnNextEvent = false;}
525 
530  bool isTrapOnNextEventRequested() const {return trapOnNextEvent;}
532 
541  unsigned long getUniqueNumber();
542 
548  void snapshot(cObject *obj, const char *label);
549 
555 
559  void setFingerprintCalculator(cFingerprintCalculator *fingerprint);
561 };
562 
569 
576 
584 
585 } // namespace omnetpp
586 
587 
588 #endif
589 
Abstract base class for the future event set (FES), a central data structure for discrete event simul...
Definition: cfutureeventset.h:32
int getLastComponentId() const
Definition: csimulation.h:194
The message class in OMNeT++. cMessage objects may represent events, messages, jobs or other entities...
Definition: cmessage.h:95
void setWarmupPeriod(simtime_t t)
Definition: csimulation.h:399
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:283
Common base for module and channel classes.
Definition: ccomponent.h:48
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
Base class for all simple module classes.
Definition: csimplemodule.h:62
cModule * getModule(int id) const
Definition: csimulation.h:215
cSimpleModule * getActivityModule() const
Definition: csimulation.h:485
cChannel * getChannel(int id) const
Definition: csimulation.h:221
Simulation manager class.
Definition: csimulation.h:62
Represents an event in the discrete event simulator.
Definition: cevent.h:43
Extends cObject with a name string. Also includes a "flags" member, with bits open for use by subclas...
Definition: cnamedobject.h:36
simtime_t_cref getWarmupPeriod() const
Definition: csimulation.h:394
static cEnvir * getActiveEnvir()
Definition: csimulation.h:148
cFutureEventSet * getFES() const
Definition: csimulation.h:316
cEnvir represents the "environment" or user interface of the simulation.
Definition: cenvir.h:73
simtime_t simTime()
Returns the current simulation time.
Definition: csimulation.h:568
bool isModule() const
Definition: ccomponent.h:384
cModuleType * getNetworkType() const
Definition: csimulation.h:363
This class represents modules in the simulation.
Definition: cmodule.h:47
static cEnvir * getStaticEnvir()
Definition: csimulation.h:166
simtime_t_cref getSimTime() const
Definition: csimulation.h:377
eventnumber_t getEventNumber() const
Definition: csimulation.h:383
void setContextType(int type)
Definition: csimulation.h:473
void setSimTime(simtime_t time)
Definition: csimulation.h:370
Abstract class to encapsulate event scheduling.
Definition: cscheduler.h:48
static cSimulation * getActiveSimulation()
Definition: csimulation.h:142
cEnvir * getEnvir() const
Definition: csimulation.h:171
void setGlobalContext()
Definition: csimulation.h:478
void requestTrapOnNextEvent()
Definition: csimulation.h:519
int getSimulationStage() const
Definition: csimulation.h:357
cFingerprintCalculator * getFingerprintCalculator()
Definition: csimulation.h:554
cComponent * getContext() const
Definition: csimulation.h:490
int64_t eventnumber_t
Sequence number of events during the simulation. Events are numbered from one. (Event number zero is ...
Definition: simkerneldefs.h:78
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:311
int getContextType() const
Definition: csimulation.h:499
This class defines the interface for fingerprint calculators.
Definition: cfingerprint.h:34
Definition: cabstracthistogram.h:21
cModule * getSystemModule() const
Definition: csimulation.h:231
cComponent * getComponent(int id) const
Definition: csimulation.h:209
cEnvir * getEnvir()
Returns the environment object for the currently active simulation. This function never returns nullp...
Definition: csimulation.h:583
Abstract class for creating a module of a specific type.
Definition: ccomponenttype.h:192
cScheduler * getScheduler() const
Definition: csimulation.h:304
void clearTrapOnNextEvent()
Definition: csimulation.h:524
Exception class.
Definition: cexception.h:49
bool isChannel() const
Definition: ccomponent.h:389
Base class for channels.
Definition: cchannel.h:34
cSimulation * getSimulation()
Returns the currently active simulation, or nullptr if there is none.
Definition: csimulation.h:575
bool isTrapOnNextEventRequested() const
Definition: csimulation.h:530