Statistics
| Branch: | Revision:

root / src / envir / envirbase.h @ 08285dff

History | View | Annotate | Download (11.2 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_per_module;
112
        std::vector<cRNG*> rngs;
113

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

    
120
    // Data for getUniqueNumber()
121
    unsigned long nextuniquenumber;
122

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

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

    
134
  public:
135
    /**
136
     * Constructor.
137
     */
138
    EnvirBase();
139

    
140
    /**
141
     * Destructor.
142
     */
143
    virtual ~EnvirBase();
144

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

    
150
    virtual void setEventlogRecording(bool enabled);
151
    virtual bool hasEventlogRecordingIntervals() const;
152
    virtual void clearEventlogRecordingIntervals();
153

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

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

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

    
195
    // RNGs
196
    virtual int getNumRNGsPerModule() const;
197
    virtual cRNG *getRNG(int k);
198
    virtual void getRNGMappingFor(cComponent *component);
199

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

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

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

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

    
223
        virtual void createRNGs();
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
  protected:
276
    // Utility function: checks simulation fingerprint and displays a message accordingly
277
    void checkFingerprint();
278

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

    
289
    /**
290
     * Original command-line args.
291
     */
292
    ArgList *argList()  {return args;}
293

    
294
    /**
295
     * Display the exception.
296
     */
297
    virtual void displayException(std::exception& e);
298

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

    
307
    /**
308
     * Resets the clock measuring the elapsed (real) time spent in this
309
     * simulation run.
310
     */
311
    void resetClock();
312

    
313
    /**
314
     * Start measuring elapsed (real) time spent in this simulation run.
315
     */
316
    void startClock();
317

    
318
    /**
319
     * Stop measuring elapsed (real) time spent in this simulation run.
320
     */
321
    void stopClock();
322

    
323
    /**
324
     * Elapsed time
325
     */
326
    timeval totalElapsed();
327
    //@}
328

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

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

    
342

    
343
    //@}
344
};
345

    
346
NAMESPACE_END
347

    
348

    
349
#endif
350