Project

General

Profile

Statistics
| Branch: | Revision:

root / src / tkenv / tkenv.h @ 3e29b8a0

History | View | Annotate | Download (11.5 KB)

1
//==========================================================================
2
//  TKENV.H - part of
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//==========================================================================
8

    
9
/*--------------------------------------------------------------*
10
  Copyright (C) 1992-2008 Andras Varga
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16

    
17
#ifndef __TKENV_H
18
#define __TKENV_H
19

    
20
#define WIN32_LEAN_AND_MEAN
21
#include <tk.h>
22
#include <vector>
23
#include <list>
24

    
25
#include "tkdefs.h"
26
#include "logbuffer.h"
27
#include "envirbase.h"
28
#include "cchannel.h"
29
#include "cmodule.h"
30

    
31
NAMESPACE_BEGIN
32

    
33
class Speedometer;
34
class TInspector;
35

    
36
#define MAX_CLASSNAME  100
37

    
38

    
39
/**
40
 * A Tcl/Tk-based user interface.
41
 */
42
class TKENV_API Tkenv : public EnvirBase
43
{
44
   public:
45
      //
46
      // state transitions:
47
      //    SIM_NONET -> SIM_NEW -> (SIM_RUNNING <-> SIM_READY) -> SIM_TERMINATED -> SIM_FINISHCALLED -> SIM_NONET
48
      //                                               `-> SIM_ERROR
49
      // plus, at any time it may be temporarily BUSY inside an in idle() call
50
      //
51
      enum eState {
52
          SIM_NONET = 0,
53
          SIM_NEW = 1,
54
          SIM_RUNNING = 2,
55
          SIM_READY = 3,
56
          SIM_TERMINATED = 4,
57
          SIM_ERROR = 5,
58
          SIM_FINISHCALLED = 6,
59
          SIM_BUSY = 7  // busy doing active wait
60
      };
61

    
62
      enum eRunMode {
63
          RUNMODE_SLOW = 0,
64
          RUNMODE_NORMAL = 1,
65
          RUNMODE_FAST = 2,
66
          RUNMODE_EXPRESS = 3
67
      };
68

    
69
      struct sPathEntry {
70
         cModule *from; // NULL if descent
71
         cModule *to;   // NULL if ascent
72
         sPathEntry(cModule *f, cModule *t) {from=f; to=t;}
73
      };
74
      typedef std::vector<sPathEntry> PathVec;
75

    
76
      enum LayouterChoice {
77
          LAYOUTER_FAST,
78
          LAYOUTER_ADVANCED,
79
          LAYOUTER_AUTO
80
      };
81

    
82
   public:
83
      // options
84
      opp_string opt_default_config; // automatically set up this config at startup
85
      int  opt_default_run;        // automatically set up this run (of the default config) at startup
86
      bool opt_init_banners;       // print "initializing..." banners
87
      bool opt_event_banners;      // print event banners
88
      bool opt_short_banners;      // controls detail of event banners
89
      bool opt_use_mainwindow;     // dump modules' ev << ... stuff into main window
90
      bool opt_animation_enabled;  // msg animation
91
      bool opt_nexteventmarkers;   // display next event marker (red frame around modules)
92
      bool opt_senddirect_arrows;  // flash arrows when doing sendDirect() animation
93
      bool opt_anim_methodcalls;   // animate method calls
94
      int  opt_methodcalls_delay;  // hold animation of method calls (millisec)
95
      bool opt_animation_msgnames; // msg animation: display message name or not
96
      bool opt_animation_msgclassnames; // msg animation: display message class name or not
97
      bool opt_animation_msgcolors;// msg animation: display msg kind as color code or not
98
      bool opt_penguin_mode;       // msg animation: message appearance
99
      bool opt_showlayouting;      // show layouting process in graphical module inspectors
100
      LayouterChoice opt_layouterchoice;  // which new layouting algorithm to use
101
      bool opt_arrangevectorconnections;     // arrange connections on vector gates parallel to each outher
102
      int opt_iconminsize;         // minimum size of icons when zooming out
103
      bool opt_bubbles;            // show result of bubble() calls
104
      double opt_animation_speed;  // msg animation speed: 0=slow 1=norm 2=fast
105
      long opt_stepdelay;          // Delay between steps in ms
106
      long opt_updatefreq_fast;    // Fast Run updates display every N milliseconds
107
      long opt_updatefreq_express; // Express Run updates display every N milliseconds
108
      size_t opt_extrastack;       // per-module extra stack for activity() modules
109
      bool opt_expressmode_autoupdate;// update inspectors at every display refresh in EXPRESS mode or not
110
      opp_string opt_image_path;   // directory of module icon files
111
      opp_string opt_plugin_path;  // path for loading Tcl and binary plugins
112
      bool opt_stoponmsgcancel;    // with rununtil_msg: whether to stop when the message gets cancelled
113

    
114
      // state variables
115
      bool animating;              // while execution, do message animation or not
116
      bool hasmessagewindow;       // whether the message window is currently open
117

    
118
   protected:
119
      Tcl_Interp *interp;          // Tcl interpreter
120
      opp_string windowtitleprefix;// contains "procId=.." when using parsim
121

    
122
      opp_string tkenv_dir;        // directory of Tkenv's *.tcl files
123

    
124
      bool isconfigrun;            // true after newRun(), and false after newConfig()
125
      eState simstate;             // state of the simulation run
126
      int runmode;                 // the current mode the simulation is executing under
127
      simtime_t rununtil_time;     // time limit in current "Run Until" execution, or zero
128
      eventnumber_t rununtil_eventnum;// event number in current "Run Until" execution, or zero
129
      cMessage *rununtil_msg;      // stop before this event; also when this message gets cancelled
130
      cModule *rununtil_module;    // stop before and after events in this module; ignored with EXPRESS mode
131

    
132
      bool stopsimulation_flag;    // indicates that the simulation should be stopped (STOP button pressed in the UI)
133
      timeval idleLastUICheck;     // gettimeofday() time when idle() last run the Tk "update" command
134

    
135
      typedef std::list<TInspector*> TInspectorList;
136
      TInspectorList inspectors;   // list of inspector objects
137

    
138
      LogBuffer logBuffer;         // text window contents
139
      std::set<int> mainWindowExcludedModuleIds;
140

    
141
      typedef std::map<std::string,std::string> StringMap;
142
      StringMap answers;           // key: <ModuleType>:<paramName>, value: <interactively-given-paramvalue>
143

    
144
   public:
145
      Tkenv();
146
      virtual ~Tkenv();
147

    
148
      virtual void objectDeleted(cObject *object); // notify environment
149
      virtual void componentInitBegin(cComponent *component, int stage);
150
      virtual void simulationEvent(cMessage *msg);
151
      virtual void messageSent_OBSOLETE(cMessage *msg, cGate *directToGate);
152
      virtual void messageScheduled(cMessage *msg);
153
      virtual void messageCancelled(cMessage *msg);
154
      virtual void beginSend(cMessage *msg);
155
      virtual void messageSendDirect(cMessage *msg, cGate *toGate, simtime_t propagationDelay, simtime_t transmissionDelay);
156
      virtual void messageSendHop(cMessage *msg, cGate *srcGate);
157
      virtual void messageSendHop(cMessage *msg, cGate *srcGate, simtime_t propagationDelay, simtime_t transmissionDelay);
158
      virtual void endSend(cMessage *msg);
159
      virtual void messageDeleted(cMessage *msg);
160
      virtual void componentMethodBegin(cComponent *from, cComponent *to, const char *methodFmt, va_list va, bool silent);
161
      virtual void componentMethodEnd();
162
      virtual void moduleCreated(cModule *newmodule);
163
      virtual void moduleDeleted(cModule *module);
164
      virtual void moduleReparented(cModule *module, cModule *oldparent);
165
      virtual void connectionCreated(cGate *srcgate);
166
      virtual void connectionDeleted(cGate *srcgate);
167
      virtual void displayStringChanged(cComponent *component);
168

    
169
      virtual bool isGUI() const {return true;}
170
      virtual void bubble(cComponent *component, const char *text);
171

    
172
      virtual void putsmsg(const char *s);
173
      virtual void sputn(const char *s, int n);
174
      virtual cEnvir& flush();
175
      virtual std::string gets(const char *promt, const char *defaultReply);
176
      virtual bool askyesno(const char *question);
177

    
178
      virtual bool idle();
179

    
180
      // with Tkenv, activity() modules need extra stack
181
      virtual unsigned getExtraStackForEnvir() const;
182

    
183
  protected:
184
      // redefined virtual functions from EnvirBase
185
      virtual void run();
186
      virtual void printUISpecificHelp();
187

    
188
      virtual void readOptions();
189
      virtual void readPerRunOptions();
190
      virtual void askParameter(cPar *par, bool unassigned);
191
      virtual void printLastLogLine();
192
      virtual void displayException(std::exception& e);
193

    
194
  public:
195
      // New functions:
196
      void newNetwork(const char *networkname);
197
      void newRun(const char *configname, int runnumber);
198
      void createSnapshot(const char *label);
199

    
200
      void rebuildSim();
201
      void doOneStep();
202
      void runSimulation(int mode, simtime_t until_time=0, eventnumber_t until_eventnum=0, cMessage *until_msg=NULL, cModule *until_module=NULL);
203
      void setSimulationRunMode(int runmode);
204
      int getSimulationRunMode() const {return runmode;}
205
      void setSimulationRunUntil(simtime_t until_time, eventnumber_t until_eventnum, cMessage *until_msg);
206
      void setSimulationRunUntilModule(cModule *until_module);
207
      bool doRunSimulation();
208
      bool doRunSimulationExpress();
209

    
210
      void startAll();
211
      void finishSimulation(); // wrapper around simulation.callFinish() and simulation.endRun()
212

    
213
      void loadNedFile(const char *fname, const char *expectedPackage=NULL, bool isXML=false);
214

    
215
      void updateInspectors();
216
      TInspector *inspect(cObject *obj, int type, const char *geometry, void *dat);
217
      TInspector *findInspector(cObject *obj, int type);
218
      void deleteInspector(TInspector *insp);
219

    
220
      int getSimulationState() {return simstate;}
221
      void setStopSimulationFlag() {stopsimulation_flag = true;}
222
      bool getStopSimulationFlag() {return stopsimulation_flag;}
223
      Tcl_Interp *getInterp() {return interp;}
224
      const LogBuffer& getLogBuffer() const {return logBuffer;}
225
      const std::set<int>& getMainWindowExcludedModuleIds() const {return mainWindowExcludedModuleIds;}
226
      void setMainWindowExcludedModuleIds(const std::set<int>& ids);
227

    
228
      void updateGraphicalInspectorsBeforeAnimation();
229

    
230
      void channelDisplayStringChanged(cChannel *channel);
231
      void moduleDisplayStringChanged(cModule *module);
232

    
233
      // small functions:
234
      void updateNetworkRunDisplay();
235
      void updateSimtimeDisplay();
236
      void updateNextModuleDisplay();
237
      void clearNextModuleDisplay();
238
      void updatePerformanceDisplay(Speedometer& speedometer);
239
      void clearPerformanceDisplay();
240

    
241
      void confirm(const char *msg); // messagebox with OK button
242
      bool inputDialog(const char *title, const char *prompt,
243
                       const char *checkboxLabel, const char *defaultValue,
244
                       std::string& outResult, bool& inoutCheckState);
245

    
246
      void printEventBanner(cMessage *msg, cSimpleModule *mod);
247
      void animateSend(cMessage *msg, cGate *fromgate, cGate *togate);
248
      void animateSendDirect(cMessage *msg, cModule *frommodule, cGate *togate);
249
      void animateDelivery(cMessage *msg);
250
      void animateDeliveryDirect(cMessage *msg);
251
      void performAnimations();
252

    
253
      void findDirectPath(cModule *frommodule, cModule *tomodule, PathVec& pathvec);
254

    
255
      std::string getLocalPackage()      {return simulation.getNedPackageForFolder(opt_inifile_network_dir.c_str());}
256
      const char *getIniFileName()       {return getConfigEx()->getFileName();}
257
      const char *getOutVectorFileName() {return outvectormgr->getFileName();}
258
      const char *getOutScalarFileName() {return outscalarmgr->getFileName();}
259
      const char *getSnapshotFileName()  {return snapshotmgr->getFileName();}
260
      const char *getWindowTitlePrefix() {return windowtitleprefix.c_str();}
261

    
262
};
263

    
264

    
265
/**
266
 * Utility function
267
 */
268
inline Tkenv *getTkenv()
269
{
270
    return (Tkenv *)(&ev);
271
}
272

    
273
NAMESPACE_END
274

    
275
#endif