csimulation.h

00001 //==========================================================================
00002 //   CSIMULATION.H  -  header for
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cSimulation  : simulation management class; only one instance
00009 //
00010 //==========================================================================
00011 
00012 /*--------------------------------------------------------------*
00013   Copyright (C) 1992-2008 Andras Varga
00014   Copyright (C) 2006-2008 OpenSim Ltd.
00015 
00016   This file is distributed WITHOUT ANY WARRANTY. See the file
00017   `license' for details on this and other legal matters.
00018 *--------------------------------------------------------------*/
00019 
00020 #ifndef __CSIMULATION_H
00021 #define __CSIMULATION_H
00022 
00023 #include "simkerneldefs.h"
00024 #include "simtime_t.h"
00025 #include "cmessageheap.h"
00026 #include "cexception.h"
00027 
00028 NAMESPACE_BEGIN
00029 
00030 //=== classes mentioned:
00031 class  cMessage;
00032 class  cGate;
00033 class  cModule;
00034 class  cSimpleModule;
00035 class  cSimulation;
00036 class  cException;
00037 class  cScheduler;
00038 class  cParsimPartition;
00039 class  cNEDFileLoader;
00040 class  cHasher;
00041 class  cModuleType;
00042 class  cEnvir;
00043 class  cDefaultList;
00044 
00045 SIM_API extern cDefaultList defaultList; // also in globals.h
00046 
00052 #define simulation  (*cSimulation::getActiveSimulation())
00053 
00054 
00070 class SIM_API cSimulation : public cNamedObject, noncopyable
00071 {
00072     friend class cSimpleModule;
00073   private:
00074     // global variables
00075     static cSimulation *simPtr; // the active cSimulation instance
00076     static cEnvir *evPtr;       // the active cEnvir instance
00077     static cEnvir *staticEvPtr; // the environment to activate when simPtr becomes NULL
00078 
00079     // variables of the module vector
00080     int size;                 // size of vector
00081     int delta;                // if needed, grows by delta
00082     cModule **vect;           // vector of modules, vect[0] is not used
00083     int last_id;              // index of last used pos. in vect[]
00084 
00085     // simulation vars
00086     cEnvir *ownEvPtr;         // the environment that belongs to this simulation object
00087     cModule *systemmodp;      // pointer to system (root) module
00088     cSimpleModule *activitymodp; // the module currently executing activity() (NULL if handleMessage() or in main)
00089     cComponent *contextmodp;  // component in context (or NULL)
00090     int contexttype;          // the innermost context type (one of CTX_BUILD, CTX_EVENT, CTX_INITIALIZE, CTX_FINISH)
00091     cModuleType *networktype; // network type
00092     cScheduler *schedulerp;   // event scheduler
00093     simtime_t warmup_period;  // warm-up period
00094 
00095     int simulationstage;      // simulation stage (one of CTX_NONE, CTX_BUILD, CTX_EVENT, CTX_INITIALIZE, CTX_FINISH or CTX_CLEANUP)
00096     simtime_t sim_time;       // simulation time (time of current event)
00097     eventnumber_t event_num;  // sequence number of current event
00098 
00099     cMessage *msg_for_activity; // helper variable to pass the received message into activity()
00100     cException *exception;    // helper variable to get exceptions back from activity()
00101     bool trap_on_next_event;  // when set, next handleMessage or activity() will execute debugger interrupt
00102 
00103     cHasher *hasherp;         // used for fingerprint calculation
00104 
00105   private:
00106     // internal
00107     void checkActive()  {if (getActiveSimulation()!=this) throw cRuntimeError(this, eWRONGSIM);}
00108 
00109   public:
00110     // internal: FES
00111     cMessageHeap msgQueue;    // future messages (FES)
00112     cMessageHeap& getMessageQueue() {return msgQueue;}  // accessor for sim_std.msg
00113 
00114   public:
00121     cSimulation(const char *name, cEnvir *env);
00122 
00126     virtual ~cSimulation();
00128 
00135     virtual void forEachChild(cVisitor *v);
00136 
00140     virtual std::string getFullPath() const;
00142 
00148     static cSimulation *getActiveSimulation()  {return simPtr;}
00149 
00154     static cEnvir *getActiveEnvir()  {return evPtr;}
00155 
00161     static void setActiveSimulation(cSimulation *sim);
00162 
00167     static void setStaticEnvir(cEnvir *env);
00168 
00172     static cEnvir *getStaticEnvir()  {return staticEvPtr;}
00173 
00177     cEnvir *getEnvir() const  {return ownEvPtr;}
00179 
00182 
00189     int registerModule(cModule *mod);
00190 
00195     void deregisterModule(cModule *mod);
00196 
00200     int getLastModuleId() const    {return last_id;}
00201 
00207     cModule *getModuleByPath(const char *modulepath) const;
00208 
00212     cModule *getModule(int id) const  {return id>=0 && id<size ? vect[id] : NULL;}
00213 
00219     _OPPDEPRECATED cModule& operator[](int id) const  {return id>=0 && id<size ? *vect[id] : *(cModule *)NULL;}
00220 
00224     void setSystemModule(cModule *p);
00225 
00229     cModule *getSystemModule() const  {return systemmodp;}
00231 
00241 
00251     static int loadNedSourceFolder(const char *foldername);
00252 
00260     static void loadNedFile(const char *nedfname, const char *expectedPackage=NULL, bool isXML=false);
00261 
00272     static void loadNedText(const char *name, const char *nedtext, const char *expectedPackage=NULL, bool isXML=false);
00273 
00280     static void doneLoadingNedFiles();
00281 
00286     static std::string getNedPackageForFolder(const char *folder);
00288 
00291 
00297     void setScheduler(cScheduler *scheduler);
00298 
00302     cScheduler *getScheduler() const  {return schedulerp;}
00303 
00307     void setupNetwork(cModuleType *networkType);
00308 
00314     void startRun();
00315 
00320     void callFinish();
00321 
00325     void endRun();
00326 
00331     void deleteNetwork();
00333 
00342     int getSimulationStage() const  {return simulationstage;}
00343 
00348     cModuleType *getNetworkType() const  {return networktype;}
00349 
00355     void setSimTime(simtime_t time)  {sim_time = time;}
00356 
00361     simtime_t_cref getSimTime() const  {return sim_time;}
00362 
00366     eventnumber_t getEventNumber() const  {return event_num;}
00367 
00377     simtime_t_cref getWarmupPeriod() const  {return warmup_period;}
00378 
00382     void setWarmupPeriod(simtime_t t)  {warmup_period = t;}
00384 
00387 
00399     cSimpleModule *selectNextModule();
00400 
00415     cMessage *guessNextEvent();
00416 
00426     cSimpleModule *guessNextModule();
00427 
00437     simtime_t guessNextSimtime();
00438 
00446     void doOneEvent(cSimpleModule *m);
00447 
00452     void transferTo(cSimpleModule *p);
00453 
00457     void transferToMain();
00458 
00464     void insertMsg(cMessage *msg);
00465 
00469     void setContext(cComponent *p);
00470 
00474     void setContextType(int ctxtype)  {contexttype = ctxtype;}
00475 
00479     void setGlobalContext()  {contextmodp=NULL; cOwnedObject::setDefaultOwner(&defaultList);}
00480 
00486     cSimpleModule *getActivityModule() const {return activitymodp;}
00487 
00491     cComponent *getContext() const {return contextmodp;}
00492 
00500     int getContextType() const {return contexttype;}
00501 
00506     cModule *getContextModule() const;
00507 
00513     cSimpleModule *getContextSimpleModule() const;
00514 
00520     void requestTrapOnNextEvent() {trap_on_next_event = true;}
00521 
00525     void clearTrapOnNextEvent() {trap_on_next_event = false;}
00526 
00531     bool isTrapOnNextEventRequested() const {return trap_on_next_event;}
00533 
00542     unsigned long getUniqueNumber();
00543 
00549     bool snapshot(cObject *obj, const char *label);
00550 
00555     cHasher *getHasher() {return hasherp;}
00556 
00560     void setHasher(cHasher *hasher);
00562 };
00563 
00567 inline simtime_t simTime() {return cSimulation::getActiveSimulation()->getSimTime();}
00568 
00569 
00570 NAMESPACE_END
00571 
00572 
00573 #endif
00574 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3