00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __CMODULE_H
00024 #define __CMODULE_H
00025
00026 #include <time.h>
00027 #include "cobject.h"
00028 #include "ccor.h"
00029 #include "chead.h"
00030 #include "carray.h"
00031 #include "cqueue.h"
00032 #include "cgate.h"
00033 #include "csimul.h"
00034
00035
00036 enum {
00037 sENDED,
00038 sREADY
00039 };
00040
00041
00042 enum {
00043 dispSUBMOD=0,
00044 dispENCLOSINGMOD=1,
00045 dispNUMTYPES
00046 };
00047
00048
00049 #define opp_new new((___nosuchclass *)NULL)
00050 #define opp_delete memFree((void *&)x)
00051
00052
00053 class cMessage;
00054 class cGate;
00055 class cModulePar;
00056 class cModule;
00057 class cSimpleModule;
00058 class cCompoundModule;
00059 class cNetMod;
00060 class cSimulation;
00061 class cNetworkType;
00062
00068
00072 SIM_API void connect(cModule *frm, int frg,
00073 cLinkType *linkp,
00074 cModule *tom, int tog);
00075
00079 SIM_API void connect(cModule *frm, int frg,
00080 cPar *delayp, cPar *errorp, cPar *dataratep,
00081 cModule *tom, int tog);
00083
00084
00085
00086 class ___nosuchclass;
00087 void *operator new(size_t m,___nosuchclass *);
00088
00089
00098 typedef void (*DisplayStringNotifyFunc)(cModule*,bool,void*);
00099
00100
00101
00111 class SIM_API cModule : public cObject
00112 {
00113 friend class cGate;
00114 friend class cSimulation;
00115
00116 public:
00117 static bool pause_in_sendmsg;
00118
00119 protected:
00120 mutable char *fullname;
00121 cModuleType *mod_type;
00122 int mod_id;
00123 cModule *parentmodp;
00124
00125 public:
00126
00127
00128 cArray gatev;
00129 cArray paramv;
00130 cArray machinev;
00131 cHead members;
00132
00133 protected:
00134 bool warn;
00135
00136 int idx;
00137 int vectsize;
00138
00139 opp_string dispstr;
00140 opp_string parentdispstr;
00141
00142 DisplayStringNotifyFunc notify_inspector;
00143 void *data_for_inspector;
00144
00145 protected:
00146
00147 virtual void arrived(cMessage *msg,int n) = 0;
00148
00149 protected:
00172
00177 virtual void initialize(int stage) {if(stage==0) initialize();}
00178
00184 virtual int numInitStages() const {return 1;}
00185
00190 virtual void initialize();
00191
00196 virtual void finish();
00198
00199 public:
00202
00206 cModule(const cModule& mod);
00207
00211 cModule(const char *name, cModule *parentmod);
00212
00216 virtual ~cModule();
00217
00222 cModule& operator=(const cModule& mod);
00224
00227
00231 virtual const char *className() const {return "cModule";}
00232
00233
00234
00239 virtual void forEach(ForeachFunc f);
00240
00245 virtual const char *inspectorFactoryName() const {return "cModuleIFC";}
00246
00252 virtual const char *fullName() const;
00253
00257 virtual const char *fullPath() const;
00258
00264 virtual const char *fullPath(char *buffer, int bufsize) const;
00266
00269
00273
00274 virtual void setId(int n);
00275
00280 void setIndex(int i, int n);
00281
00286
00287 void setModuleType(cModuleType *mtype);
00288
00293 void addGate(const char *s, char tp);
00294
00298 void setGateSize(const char *s, int size);
00299
00303 void addPar(const char *s);
00304
00308 void addMachinePar(const char *pnam);
00309
00313 void setMachinePar(const char *pnam, const char *val);
00314
00319 virtual void buildInside() {}
00321
00324
00330 virtual bool isSimple() const = 0;
00331
00335 cModuleType *moduleType() const {return mod_type;}
00336
00344 int id() const {return mod_id;}
00345
00350 cModule *parentModule() const {return parentmodp;}
00351
00356 bool isOnLocalMachine() const;
00357
00361 bool isVector() const {return vectsize>=0;}
00362
00366 int index() const {return idx;}
00367
00371 int size() const {return vectsize<0?1:vectsize;}
00373
00376
00382 int findSubmodule(const char *submodname, int idx=-1);
00383
00389 cModule *submodule(const char *submodname, int idx=-1);
00390
00397 cModule *moduleByRelativePath(const char *path);
00399
00402
00407 int gates() const {return gatev.items();}
00408
00412 cGate *gate(int g) {return (cGate*)gatev[g];}
00413
00417 const cGate *gate(int g) const {return (const cGate*)gatev[g];}
00418
00423 cGate *gate(const char *gatename,int sn=-1);
00424
00429 const cGate *gate(const char *gatename,int sn=-1) const;
00430
00435 int findGate(const char *gatename, int sn=-1) const;
00436
00440 bool hasGate(const char *gatename, int sn=-1) const {return findGate(gatename,sn)>=0;}
00441
00448 bool checkInternalConnections() const;
00450
00453
00457 int params() const {return paramv.items();}
00458
00463 cPar& par(int p);
00464
00469 cPar& par(const char *parname);
00470
00475 int findPar(const char *parname) const;
00476
00481 cPar& ancestorPar(const char *parname);
00482
00486 bool hasPar(const char *s) const {return findPar(s)>=0;}
00488
00491
00495 int machinePars() const {return machinev.items();}
00496
00500 const char *machinePar(int i);
00501
00505 const char *machinePar(const char *machinename);
00507
00513
00518 virtual void callInitialize();
00519
00524 virtual bool callInitialize(int stage) = 0;
00525
00529 virtual void callFinish() = 0;
00531
00534
00541 virtual void scheduleStart(simtime_t t) = 0;
00542
00548 virtual void deleteModule() = 0;
00550
00553
00559 bool warnings() const {return warn;}
00560
00564 void setWarnings(bool wr) {warn=wr;}
00566
00569
00575 const char *displayString();
00576
00592 void setDisplayString(const char *dispstr, bool immediate=true);
00593
00599 const char *displayStringAsParent();
00600
00616 void setDisplayStringAsParent(const char *dispstr, bool immediate=true);
00617
00622 const char *displayString(int type);
00623
00628 void setDisplayString(int type, const char *dispstr, bool immediate=true);
00629
00636 void setDisplayStringNotify(DisplayStringNotifyFunc notify_func, void *data);
00638 };
00639
00640
00641
00642
00643
00644
00645 struct sBlock
00646 {
00647 sBlock *next;
00648 sBlock *prev;
00649 cSimpleModule *mod;
00650 };
00651
00652
00653
00669 class SIM_API cSimpleModule : public cCoroutine, public cModule
00670 {
00671 friend class cModule;
00672 friend class cSimulation;
00673 friend class TSimpleModInspector;
00674 private:
00675 bool usesactivity;
00676 int state;
00677 opp_string phasestr;
00678 sBlock *heap;
00679 cMessage *timeoutmsg;
00680
00681 private:
00682
00683 static void activate(void *p);
00684
00685 protected:
00686
00687 virtual void arrived(cMessage *msg,int n);
00688
00689 public:
00690 cHead locals;
00691 cQueue putAsideQueue;
00692
00693 protected:
00704
00709 virtual void activity();
00710
00715 virtual void handleMessage(cMessage *msg);
00717
00718 public:
00721
00725 cSimpleModule(const cSimpleModule& mod);
00726
00730 cSimpleModule(const char *name, cModule *parentmod, unsigned stk);
00731
00735 virtual ~cSimpleModule();
00736
00740 cSimpleModule& operator=(const cSimpleModule& mod);
00742
00745
00749 virtual const char *className() const {return "cSimpleModule";}
00750
00755 virtual cObject *dup() const {return new cSimpleModule(*this);}
00756
00761 virtual void info(char *buf);
00762
00767 virtual const char *inspectorFactoryName() const {return "cSimpleModuleIFC";}
00768
00773 virtual void forEach(ForeachFunc f);
00775
00778
00782 virtual void setId(int n);
00783
00787 virtual bool isSimple() const {return true;}
00788
00794 virtual bool callInitialize(int stage);
00795
00799 virtual void callFinish();
00800
00804 virtual void scheduleStart(simtime_t t);
00805
00809 virtual void deleteModule();
00811
00814
00818 bool usesActivity() const {return usesactivity;}
00820
00823
00828 simtime_t simTime() const;
00830
00833
00841 void setPhase(const char *phase) {phasestr=phase;}
00842
00846 const char *phase() const {return correct(phasestr);}
00847
00864 bool snapshot(cObject *obj=&simulation, const char *label=NULL);
00865
00872 void breakpoint(const char *label);
00873
00884 void pause(const char *phase=NULL);
00886
00889
00893 int send(cMessage *msg, int gateid);
00894
00899 int send(cMessage *msg, const char *gatename, int sn=-1);
00900
00904 int send(cMessage *msg, cGate *outputgate);
00905
00910 int sendDelayed(cMessage *msg, double delay, int gateid);
00911
00917 int sendDelayed(cMessage *msg, double delay, const char *gatename, int sn=-1);
00918
00923 int sendDelayed(cMessage *msg, double delay, cGate *outputgate);
00924
00928 int sendDirect(cMessage *msg, double delay, cModule *mod, int inputgateid);
00929
00933 int sendDirect(cMessage *msg, double delay, cModule *mod, const char *inputgatename, int sn=-1);
00934
00938 int sendDirect(cMessage *msg, double delay, cGate *inputgate);
00940
00943
00948 int scheduleAt(simtime_t t, cMessage *msg);
00949
00955 cMessage *cancelEvent(cMessage *msg);
00957
00960
00965 int syncpoint(simtime_t t, int gateid);
00966
00971 int syncpoint(simtime_t t, const char *gatename, int sn=-1);
00972
00977 int cancelSyncpoint(simtime_t t, int gateid);
00978
00983 int cancelSyncpoint(simtime_t t, const char *gatename, int sn=-1);
00985
00992
00998 bool isThereMessage() const;
00999
01003 cMessage *receive();
01004
01012 cMessage *receive(simtime_t timeout);
01013
01022 cMessage *receiveOn(const char *gatename, int sn=-1, simtime_t timeout=MAXTIME);
01023
01030 cMessage *receiveOn(int gateid, simtime_t timeout=MAXTIME);
01031
01036 cMessage *receiveNew();
01037
01045 cMessage *receiveNew(simtime_t timeout);
01046
01058 cMessage *receiveNewOn(const char *gatename, int sn=-1, simtime_t timeout=MAXTIME);
01059
01066 cMessage *receiveNewOn(int gateid, simtime_t timeout=MAXTIME);
01068
01071
01078 void wait(simtime_t time);
01080
01083
01088 void end();
01089
01093 void endSimulation();
01094
01098 void error(const char *fmt,...) const;
01100
01102
01103
01106
01110 void recordScalar(const char *name, double value);
01111
01115 void recordScalar(const char *name, const char *text);
01116
01120 void recordStats(const char *name, cStatistic *stats);
01122
01125
01137 void *memAlloc(size_t m);
01138
01143 void memFree(void *&p);
01145
01146
01147 void clearHeap();
01148
01152 int moduleState() const {return state;}
01153 };
01154
01155
01156
01164 class SIM_API cCompoundModule : public cModule
01165 {
01166 friend class TCompoundModInspector;
01167
01168 protected:
01169
01170 virtual void arrived(cMessage *msg,int n);
01171
01172 public:
01175
01179 cCompoundModule(const cCompoundModule& mod);
01180
01184 cCompoundModule(const char *name, cModule *parentmod);
01185
01189 virtual ~cCompoundModule();
01190
01194 cCompoundModule& operator=(const cCompoundModule& mod);
01196
01199
01203 virtual const char *className() const {return "cCompoundModule";}
01204
01209 virtual cObject *dup() const {return new cCompoundModule(*this);}
01210
01215 virtual void info(char *buf);
01216
01221 virtual const char *inspectorFactoryName() const {return "cCompoundModuleIFC";}
01223
01226
01230 virtual bool isSimple() const {return false;}
01231
01237 virtual bool callInitialize(int stage);
01238
01243 virtual void callFinish();
01244
01249 virtual void scheduleStart(simtime_t t);
01250
01255 virtual void deleteModule();
01257 };
01258
01259
01260
01264 class SIM_API cSubModIterator
01265 {
01266 private:
01267 const cModule *parent;
01268 int i;
01269
01270 public:
01274 cSubModIterator(const cModule& p) {parent=&p;i=0;operator++(0);}
01275
01279 void init(const cModule& p) {parent=&p;i=0;operator++(0);}
01280
01284
01285 cModule& operator[](int) {return simulation[i];}
01286
01292
01293 cModule *operator()() {return &simulation[i];}
01294
01298 bool end() const {return (i==-1);}
01299
01305 cModule *operator++(int);
01306 };
01307
01308 #endif
01309