00001 //========================================================================== 00002 // CSIMUL.H - header for 00003 // OMNeT++ 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-2001 Andras Varga 00014 Technical University of Budapest, Dept. of Telecommunications, 00015 Stoczek u.2, H-1111 Budapest, Hungary. 00016 00017 This file is distributed WITHOUT ANY WARRANTY. See the file 00018 `license' for details on this and other legal matters. 00019 *--------------------------------------------------------------*/ 00020 00021 #ifndef __CSIMUL_H 00022 #define __CSIMUL_H 00023 00024 #include <time.h> // time_t, clock_t in cSimulation 00025 #include "defs.h" 00026 #include "util.h" 00027 #include "errmsg.h" 00028 #include "chead.h" 00029 #include "cmsgheap.h" 00030 #include "ccor.h" 00031 #include "coutvect.h" //FIXME 00032 00033 //=== classes mentioned: 00034 class cMessage; 00035 class cGate; 00036 class cModulePar; 00037 class cModule; 00038 class cSimpleModule; 00039 class cCompoundModule; 00040 class cNetMod; 00041 class cSimulation; 00042 class cNetworkType; 00043 class TModInspector; 00044 class cStatistic; 00045 00051 SIM_API extern cSimulation simulation; 00052 00053 //========================================================================== 00054 00067 class SIM_API cSimulation : public cObject 00068 { 00069 friend class cSimpleModule; 00070 00071 private: 00072 // variables of the module vector 00073 int size; // size of vector 00074 int delta; // if needed, grows by delta 00075 cModule **vect; // vector of modules, vect[0] is not used 00076 int last_id; // index of last used pos. in vect[] 00077 00078 // simulation global vars 00079 cModule *systemmodp; // pointer to system module 00080 cSimpleModule *runningmodp; // the currently executing module (NULL if in main) 00081 cModule *contextmodp; // module in context (or NULL) 00082 cHead *locallistp; // owner of newly created objects 00083 cHead locals; // "global" local objects list 00084 00085 cNetMod *netmodp; // if runs distributed; communications 00086 // (network interface) module 00087 00088 int err; // error code, 0 (== eOK) if no error 00089 bool warn; // if true, overrides individual warn flags 00090 00091 simtime_t sim_time; // simulation time (time of current event) 00092 long event_num; // sequence number of current event 00093 00094 int netif_check_freq; // (distributed execution:) frequency of processing 00095 int netif_check_cntr; // msgs from other segments 00096 00097 cNetworkType *networktype; // ptr to network creator object 00098 int run_number; // which simulation run 00099 time_t simbegtime; // real time when sim. started 00100 time_t simendtime; // real time when sim. ended 00101 time_t laststarted; // real time from where sim. was last cont'd 00102 time_t elapsedtime; // in seconds 00103 time_t tmlimit; // real time limit in seconds 00104 simtime_t simulatedtime; // sim.time at after finishing simulation 00105 simtime_t simtmlimit; // simulation time limit 00106 cSimpleModule *backtomod; // used in cSimpleModule::wait/sendmsg 00107 cCoroutine runningmod_deleter; // used when a simple module deletes itself 00108 00109 public: 00110 cMessageHeap msgQueue; // future messages (FES) 00111 public: 00114 00119 cSimulation(const cSimulation& r) : cObject(r) 00120 {setName(r.name());vect=NULL;operator=(r);} 00121 00125 explicit cSimulation(const char *name, cHead *h=NULL); 00126 00130 virtual ~cSimulation(); 00132 00135 00139 virtual const char *className() const {return "cSimulation";} 00140 00145 virtual cObject *dup() const {return new cSimulation(*this);} 00146 00151 virtual const char *inspectorFactoryName() const {return "cSimulationIFC";} 00152 00157 virtual void forEach(ForeachFunc f); 00158 00163 virtual void writeContents(ostream& os); 00164 00168 virtual const char *fullPath() const; 00169 00173 virtual const char *fullPath(char *buffer, int bufsize) const; 00174 00178 cSimulation& operator=(const cSimulation&) {copyNotSupported();return *this;} 00180 00181 // Internal: things that cannot be done from the constructor 00182 // (because it is called before main()). 00183 void init(); 00184 00187 00191 int add(cModule *mod); 00192 00196 void del(int id); 00197 00201 int lastModuleIndex() const {return last_id;} 00202 00206 cModule *moduleByPath(const char *modulepath) const; 00207 00211 cModule *module(int id) const 00212 {return id>=0 && id<size ? vect[id] : NO(cModule);} 00213 00217 cModule& operator[](int id) const 00218 {return id>=0 && id<size ? *vect[id] : *NO(cModule);} 00219 00223 void setSystemModule(cModule *p) 00224 {systemmodp = p;} 00225 00229 cModule *systemModule() const 00230 {return systemmodp;} 00232 00235 00239 void setNetInterface(cNetMod *netif); 00240 00244 cNetMod *netInterface() const {return netmodp;} 00245 00250 void resetClock(); 00251 00255 void startClock(); 00256 00260 void stopClock(); 00261 00265 bool setupNetwork(cNetworkType *net,int run_num); 00266 00270 void startRun(); 00271 00276 void callFinish(); 00277 00281 void endRun(); 00282 00286 void deleteNetwork(); 00287 00292 // FIXME: do time limits really belong to the simulation kernel??? why not in Envir? 00293 void setTimeLimit(long t) {tmlimit=(time_t)t;} 00294 00298 void setSimTimeLimit(simtime_t t) {simtmlimit=t;} 00299 00306 void setNetIfCheckFreq(int f) {netif_check_freq=f;} 00308 00311 00316 cNetworkType *networkType() const {return networktype;} 00317 00323 // FIXME: does run number really belong to the simulation kernel??? why not in Envir? 00324 int runNumber() const {return run_number;} 00325 00329 long timeLimit() const {return (long)tmlimit;} 00330 00334 simtime_t simTimeLimit() const {return simtmlimit;} 00335 00339 simtime_t simTime() const {return sim_time;} 00340 00344 long eventNumber() const {return event_num;} 00346 00349 00354 cSimpleModule *selectNextModule(); 00355 00362 void doOneEvent(cSimpleModule *m); 00363 00367 void incEventNumber() {event_num++;} 00368 00374 void checkTimes(); 00375 00380 int transferTo(cSimpleModule *p); 00381 00385 int transferToMain(); 00386 00390 void setContextModule(cModule *p); 00391 00395 void setGlobalContext() {contextmodp=NULL;locallistp=&locals;} 00396 00400 void setLocalList(cHead *p) {locallistp=p;} 00401 00405 cSimpleModule *runningModule() const {return runningmodp;} 00406 00410 cModule *contextModule() const {return contextmodp;} 00411 00417 // FIXME: implementation should check isSimple() and return NULL if not OK!!! 00418 cSimpleModule *contextSimpleModule() const; // cannot make inline! would require cmodule.h because of dynamic cast 00419 00427 cHead *localList() {return locallistp==NULL?(&locals):locallistp;} 00429 00432 00438 bool snapshot(cObject *obj, const char *label); 00440 00443 00447 bool warnings() const {return warn;} 00448 00452 void setWarnings(bool w) {warn=w;} 00453 00457 void terminate(int errcode,const char *message); 00458 00462 void error(int errcode,const char *message); 00463 00467 void warning(int errcode,const char *message); 00468 00472 bool ok() const {return err==eOK;} 00473 00477 int errorCode() const {return err;} 00478 00482 void setErrorCode(int e) {err=e;} 00483 00487 bool normalTermination() const; 00488 00492 void resetError() {err=eOK;} 00494 }; 00495 00496 //========================================================================== 00497 //=== operator new used by the NEW() macro: 00498 class ___nosuchclass; 00499 void *operator new(size_t m,___nosuchclass *); 00500 00501 #endif 00502