Statistics
| Branch: | Revision:

root / src / envir / envirbase.h @ 2f5cc443

History | View | Annotate | Download (11.4 KB)

1
//==========================================================================
2
//  ENVIRBASE.H - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    EnvirBase:  abstract base class for simulation applications
9
//
10
//  Author: Andras Varga
11
//
12
//==========================================================================
13

    
14
/*--------------------------------------------------------------*
15
  Copyright (C) 1992-2008 Andras Varga
16
  Copyright (C) 2006-2008 OpenSim Ltd.
17

18
  This file is distributed WITHOUT ANY WARRANTY. See the file
19
  `license' for details on this and other legal matters.
20
*--------------------------------------------------------------*/
21

    
22
#ifndef __ENVIRBASE_H
23
#define __ENVIRBASE_H
24

    
25
#include "carray.h"
26
#include "ccomponent.h"
27
#include "globals.h"
28
#include "cenvir.h"
29
#include "cexception.h"
30
#include "args.h"
31
#include "envirdefs.h"
32
#include "envirext.h"
33
#include "eventlogfilemgr.h"
34
#include "cconfiguration.h"
35
#include "timeutil.h"
36
#include "resultlistener.h"
37

    
38
NAMESPACE_BEGIN
39

    
40

    
41
class cXMLDocCache;
42
class cScheduler;
43
class cModuleType;
44
class cIListener;
45
class cProperty;
46
// WITH_PARSIM:
47
class cParsimCommunications;
48
class cParsimPartition;
49
class cParsimSynchronizer;
50
// endif
51
class ResultRecorder;
52

    
53
// assumed maximum length for getFullPath() string.
54
// note: this maximum actually not enforced anywhere
55
#define MAX_OBJECTFULLPATH  1024
56

    
57
// maximum length of string passed in Enter_Method() (longer strings will be truncated)
58
#define MAX_METHODCALL 1024
59

    
60

    
61
/**
62
 * Abstract base class for the user interface. Concrete user interface
63
 * implementations (Cmdenv, Tkenv) should be derived from this class.
64
 */
65
class ENVIR_API EnvirBase : public cRunnableEnvir
66
{
67
  protected:
68
    cConfigurationEx *cfg;
69
    ArgList *args;
70
    cXMLDocCache *xmlcache;
71
    int exitcode;
72

    
73
    //
74
    // Configuration options
75
    //
76
    size_t opt_total_stack;
77
    opp_string opt_scheduler_class;
78
    bool opt_parsim;
79
// WITH_PARSIM (note: no #ifdef to preserve class layout!)
80
    opp_string opt_parsimcomm_class; // if opt_parsim: cParsimCommunications class to use
81
    opp_string opt_parsimsynch_class; // if opt_parsim: cParsimSynchronizer class to use
82
// end
83
    opp_string opt_network_name;
84
    opp_string opt_inifile_network_dir; // dir of the inifile containing "network="
85

    
86
    int opt_num_rngs;
87
    opp_string opt_rng_class;
88
    int opt_seedset; // which set of seeds to use
89

    
90
    opp_string opt_outputvectormanager_class;
91
    opp_string opt_outputscalarmanager_class;
92
    opp_string opt_snapshotmanager_class;
93
    bool opt_debug_statistics_recording;
94
    bool opt_fname_append_host;
95

    
96
    bool opt_warnings;
97
    bool opt_print_undisposed;
98

    
99
    simtime_t opt_simtimelimit;
100
    long opt_cputimelimit;
101
    simtime_t opt_warmupperiod;
102

    
103
    opp_string opt_fingerprint;
104

    
105
// WITH_PARSIM (note: no #ifdef to preserve class layout!)
106
    cParsimCommunications *parsimcomm;
107
    cParsimPartition *parsimpartition;
108
// end
109

    
110
    // Random number generators. Module RNG's map to these RNG objects.
111
    int num_rngs;
112
    cRNG **rngs;
113

    
114
    cRNG *seedGenerator; // central (and private) random num gen for generating seeds for all public ones
115

    
116
    // Output file managers
117
    EventlogFileManager *eventlogmgr;  // NULL if no event log is being written, must be non NULL if record_eventlog is true
118
    cOutputVectorManager *outvectormgr;
119
    cOutputScalarManager *outscalarmgr;
120
    cSnapshotManager *snapshotmgr;
121

    
122
    // Data for getUniqueNumber()
123
    unsigned long nextuniquenumber;
124

    
125
    timeval simbegtime;  // real time when sim. started
126
    timeval simendtime;  // real time when sim. ended
127
    timeval laststarted; // real time from where sim. was last cont'd
128
    timeval elapsedtime; // time spent simulating
129
    simtime_t simulatedtime;  // sim. time after finishing simulation
130

    
131
  protected:
132
    // leave to subclasses: virtual void putsmsg(const char *msg);
133
    // leave to subclasses: virtual bool askyesno(const char *msg);
134
    virtual void sputn(const char *s, int n);
135

    
136
  public:
137
    /**
138
     * Constructor.
139
     */
140
    EnvirBase();
141

    
142
    /**
143
     * Destructor.
144
     */
145
    virtual ~EnvirBase();
146

    
147
    /** @name Functions called from cEnvir's similar functions */
148
    //@{
149
    // life cycle
150
    virtual int run(int argc, char *argv[], cConfiguration *config);
151

    
152
    virtual void setEventlogRecording(bool enabled);
153
    virtual bool hasEventlogRecordingIntervals() const;
154
    virtual void clearEventlogRecordingIntervals();
155

    
156
    // eventlog callback interface
157
    virtual void objectDeleted(cObject *object);
158
    virtual void simulationEvent(cMessage *msg);
159
    virtual void simulationEventEnd(double complexity);
160
    // leave to subclasses: virtual void messageSent_OBSOLETE(cMessage *msg, cGate *directToGate=NULL);
161
    virtual void messageScheduled(cMessage *msg);
162
    virtual void messageCancelled(cMessage *msg);
163
    virtual void beginSend(cMessage *msg);
164
    virtual void messageSendDirect(cMessage *msg, cGate *toGate, simtime_t propagationDelay, simtime_t transmissionDelay);
165
    virtual void messageSendHop(cMessage *msg, cGate *srcGate);
166
    virtual void messageSendHop(cMessage *msg, cGate *srcGate, simtime_t propagationDelay, simtime_t transmissionDelay);
167
    virtual void endSend(cMessage *msg);
168
    virtual void messageDeleted(cMessage *msg);
169
    virtual void moduleReparented(cModule *module, cModule *oldparent);
170
    virtual void componentMethodBegin(cComponent *from, cComponent *to, const char *methodFmt, va_list va, bool silent);
171
    virtual void componentMethodEnd();
172
    virtual void moduleCreated(cModule *newmodule);
173
    virtual void moduleDeleted(cModule *module);
174
    virtual void gateCreated(cGate *newgate);
175
    virtual void gateDeleted(cGate *gate);
176
    virtual void connectionCreated(cGate *srcgate);
177
    virtual void connectionDeleted(cGate *srcgate);
178
    virtual void displayStringChanged(cComponent *component);
179
    virtual void undisposedObject(cObject *obj);
180

    
181
    // configuration, model parameters
182
    virtual void configure(cComponent *component);
183
    virtual void readParameter(cPar *parameter);
184
    virtual bool isModuleLocal(cModule *parentmod, const char *modname, int index);
185
    virtual cXMLElement *getXMLDocument(const char *filename, const char *path=NULL);
186
    virtual void forgetXMLDocument(const char *filename);
187
    virtual void flushXMLDocumentCache();
188
    // leave to subclasses: virtual unsigned getExtraStackForEnvir();
189
    virtual cConfiguration *getConfig();
190
    virtual cConfigurationEx *getConfigEx();
191

    
192
    // UI functions
193
    virtual void bubble(cComponent *component, const char *text);
194
    // leave to subclasses: virtual std::string gets(const char *prompt, const char *defaultreply=NULL);
195
    // leave to subclasses: virtual cEnvir& flush();
196

    
197
    // RNGs
198
    virtual int getNumRNGs() const;
199
    virtual cRNG *getRNG(int k);
200
    virtual void getRNGMappingFor(cComponent *component);
201

    
202
    // output vectors
203
    virtual void *registerOutputVector(const char *modulename, const char *vectorname);
204
    virtual void deregisterOutputVector(void *vechandle);
205
    virtual void setVectorAttribute(void *vechandle, const char *name, const char *value);
206
    virtual bool recordInOutputVector(void *vechandle, simtime_t t, double value);
207

    
208
    // output scalars
209
    virtual void recordScalar(cComponent *component, const char *name, double value, opp_string_map *attributes=NULL);
210
    virtual void recordStatistic(cComponent *component, const char *name, cStatistic *statistic, opp_string_map *attributes=NULL);
211

    
212
    // snapshot file
213
    virtual std::ostream *getStreamForSnapshot();
214
    virtual void releaseStreamForSnapshot(std::ostream *os);
215

    
216
    // misc
217
    virtual int getArgCount() const;
218
    virtual char **getArgVector() const;
219
    virtual int getParsimProcId() const;
220
    virtual int getParsimNumPartitions() const;
221
    virtual unsigned long getUniqueNumber();
222
    virtual bool idle();
223
    //@}
224

    
225
  protected:
226
    // functions added locally
227
    virtual bool simulationRequired();
228
    virtual bool setup();  // true==OK
229
    virtual void run() = 0;
230
    virtual void shutdown();
231

    
232
    virtual void startRun();
233
    virtual void endRun();
234

    
235
    // utility function; never returns NULL
236
    cModuleType *resolveNetwork(const char *networkname);
237

    
238
    void printHelp();
239
    void setupEventLog();
240

    
241
    /**
242
     * Prints the contents of a registration list to the standard output.
243
     */
244
    void dumpComponentList(const char *category);
245

    
246
    /**
247
     * To be redefined to print Cmdenv or Tkenv-specific help on available
248
     * command-line options. Invoked from printHelp().
249
     */
250
    virtual void printUISpecificHelp() = 0;
251

    
252
    /**
253
     * Used internally to read opt_xxxxx setting from ini file.
254
     * Can be overloaded in subclasses, to support new options.
255
     */
256
    virtual void readOptions();
257
    virtual void readPerRunOptions();
258

    
259
    /**
260
     * Called internally from readParameter(), to interactively prompt the
261
     * user for a parameter value.
262
     */
263
    virtual void askParameter(cPar *par, bool unassigned) = 0;
264

    
265
    /**
266
     * Called from configure(component); adds result recording listeners
267
     * for each declared signal (@statistic property) in the component.
268
     */
269
    virtual void addResultRecorders(cComponent *component);
270

    
271
  public:
272
    // Utility function: optionally appends host name to fname
273
    virtual void processFileName(opp_string& fname);
274

    
275

    
276
    cRNG *getSeedGenerator() const { return seedGenerator; }
277

    
278
  protected:
279
    // Utility function: checks simulation fingerprint and displays a message accordingly
280
    void checkFingerprint();
281

    
282
    // Utility function for addResultRecorders()
283
    std::vector<std::string> extractRecorderList(const char *modesOption, cProperty *statisticProperty);
284
    // Utility function for addResultRecorders()
285
    SignalSource doStatisticSource(cComponent *component, const char *statisticName, const char *sourceSpec, bool needWarmupFilter);
286
    // Utility function for addResultRecorders()
287
    void doResultRecorder(const SignalSource& source, const char *mode, bool scalarsEnabled, bool vectorsEnabled, cComponent *component, const char *statisticName);
288
    // Utility function for addResultRecorders()
289
    void dumpResultRecorders(cComponent *component);
290
    void dumpResultRecorderChain(ResultListener *listener, int depth);
291

    
292
    /**
293
     * Original command-line args.
294
     */
295
    ArgList *argList()  {return args;}
296

    
297
    /**
298
     * Display the exception.
299
     */
300
    virtual void displayException(std::exception& e);
301

    
302
    /** @name Measuring elapsed time. */
303
    //@{
304
    /**
305
     * Checks if the current simulation has reached the simulation
306
     * or real time limits, and if so, throws an appropriate exception.
307
     */
308
    void checkTimeLimits();
309

    
310
    /**
311
     * Resets the clock measuring the elapsed (real) time spent in this
312
     * simulation run.
313
     */
314
    void resetClock();
315

    
316
    /**
317
     * Start measuring elapsed (real) time spent in this simulation run.
318
     */
319
    void startClock();
320

    
321
    /**
322
     * Stop measuring elapsed (real) time spent in this simulation run.
323
     */
324
    void stopClock();
325

    
326
    /**
327
     * Elapsed time
328
     */
329
    timeval totalElapsed();
330
    //@}
331

    
332
    //@{
333
    /**
334
     * Hook called when the simulation terminates normally.
335
     * Its current use is to notify parallel simulation part.
336
     */
337
    void stoppedWithTerminationException(cTerminationException& e);
338

    
339
    /**
340
     * Hook called when the simulation is stopped with an error.
341
     * Its current use is to notify parallel simulation part.
342
     */
343
    void stoppedWithException(std::exception& e);
344

    
345

    
346
    //@}
347
};
348

    
349
NAMESPACE_END
350

    
351

    
352
#endif
353