OpenASIP  2.0
ProximSimulationThread.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file ProximSimulationThread.cc
26  *
27  * Implementation of ProximSimulationThread class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2005 (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <sstream>
34 #include <string>
35 #include <iostream>
36 #include <wx/thread.h>
39 #include "SimulatorInterpreter.hh"
40 #include "StringTools.hh"
41 #include "Application.hh"
42 #include "SimulatorEvent.hh"
45 
46 using std::string;
47 
48 
49 /**
50  * The Constructor.
51  */
53  wxThread(wxTHREAD_JOINABLE),
54  simulation_(NULL),
55  lineReader_(new ProximLineReader),
56  gui_(NULL),
57  runtimeErrorHandler_(NULL) {
58 
59 }
60 
61 
62 
63 
64 /**
65  * The Destructor.
66  */
68  delete lineReader_;
69  delete interpreter_;
70 
71  if (simulation_ != NULL) {
72  delete simulation_;
73  }
74  if (runtimeErrorHandler_ != NULL) {
75  delete runtimeErrorHandler_;
76  }
77  if (interpreterContext_ != NULL) {
78  delete interpreterContext_;
79  }
80 }
81 
82 
83 /**
84  * Initializes the thread.
85  */
86 void
88 
89  gui_ = gui;
90 
91  lineReader_->initialize(">");
93 
96  0, NULL, *interpreterContext_, *lineReader_);
97 
114 
116 }
117 
118 
119 /**
120  * Entry point for the simulation thread execution.
121  *
122  * The thread waits commands from the line reader, and executes
123  * them using the simulator interpreter. Commands are executed
124  * until the thread is terminated.
125  */
126 void*
128 
129  assert(gui_ != NULL);
130 
131  while (!TestDestroy()) {
132 
133  std::string command = "";
134 
135  command = lineReader_->readLine();
136  command = StringTools::trim(command);
137 
138  if (command == "") {
139  continue;
140  }
141 
142  if (command == "quit") {
143  // Post an event which tells the gui thread to delete this thread.
144  SimulatorEvent eventCmd(
146  wxPostEvent(gui_, eventCmd);
147 
148  // Wait for the gui thread to call Delete() for this thread.
149  while (!TestDestroy()) {
150  Sleep(100);
151  }
152  return 0;
153  }
154 
155  // Inform GUI about the command being executed.
156  SimulatorEvent eventCmd(
158  wxPostEvent(gui_, eventCmd);
159 
160  interpreter_->interpret(command);
161 
162  // Inform GUI about the command execution being done.
163  SimulatorEvent eventCmdDone(
165  wxPostEvent(gui_, eventCmdDone);
166 
167  // If the command resulted in error, inform the GUI about the error.
168  if (interpreter_->error()) {
169  SimulatorEvent eventError(
171  interpreter_->result());
172  wxPostEvent(gui_, eventError);
173  continue;
174  }
175 
176  // Inform GUI about the simulator interpreter result.
177  if (interpreter_->result().size() > 0) {
178  SimulatorEvent event(
180  (interpreter_->result() + "\n"));
181  wxPostEvent(gui_, event);
182  }
183  }
184  return 0;
185 }
186 
187 
188 /**
189  * Requests the simulation backend to stop the simulation.
190  */
191 void
196  wxPostEvent(gui_, simEvent);
197  }
198 }
199 
200 
201 /**
202  * Kills the current simulation.
203  */
204 void
208  }
211  wxPostEvent(gui_, simEvent);
212 }
213 
214 /**
215  * Finishes the current simulation.
216  */
217 void
221  }
223 }
224 
225 
226 /**
227  * Returns reference to the line reader which the thread uses to read
228  * user input.
229  *
230  * @return LineReader used by the simulator.
231  */
234  return *lineReader_;
235 }
236 
237 
238 /**
239  * Returns reference to the script interpreter of the simulator.
240  *
241  * @return Simulator script interpreter of the simulator.
242  */
245  return interpreter_;
246 
247 }
248 
249 
250 /**
251  * Handles events from the simulator backend.
252  *
253  * The events are passed to the GUI as SimulatorEvents, which is a class
254  * derived from the wxEvent class. This way responsibility of the event
255  * handling can be passed to the GUI thread.
256  */
257 void
259 
260  switch (event) {
263  wxPostEvent(gui_, simEvent);
264  return;
265  }
268  wxPostEvent(gui_, simEvent);
269  return;
270  }
273  wxPostEvent(gui_, simEvent);
274  return;
275  }
278  wxPostEvent(gui_, simEvent);
279  return;
280  }
283  wxPostEvent(gui_, simEvent);
284  return;
285  }
288  wxPostEvent(gui_, simEvent);
289  return;
290  }
293  wxPostEvent(gui_, simEvent);
294  return;
295  }
297  gui_->reset();
298  return;
299  }
300  };
301 }
302 
303 /**
304  * Returns pointer to the simulator.
305  */
308  return simulation_;
309 }
310 
311 
312 /**
313  * Returns true if the thread is busy running the simulation.
314  *
315  * @return True, if the thread is currently running simulation.
316  */
317 bool
320  return true;
321  } else {
322  return false;
323  }
324 }
ProximMainFrame
Definition: ProximMainFrame.hh:58
TracedSimulatorFrontend
Definition: TracedSimulatorFrontend.hh:49
ProximSimulationThread::simulation_
TracedSimulatorFrontend * simulation_
Simulator backend.
Definition: ProximSimulationThread.hh:77
SimulatorInterpreterContext
Definition: SimulatorInterpreterContext.hh:45
SimulatorEvent::EVT_SIMULATOR_STOP
static const wxEventType EVT_SIMULATOR_STOP
Simulation stopped.
Definition: SimulatorEvent.hh:71
TracedSimulatorFrontend::SIMULATOR_START
@ SIMULATOR_START
Definition: TracedSimulatorFrontend.hh:72
SimulatorInterpreterContext.hh
ScriptInterpreter::result
virtual std::string result()
Definition: ScriptInterpreter.cc:191
SRE_USER_REQUESTED
@ SRE_USER_REQUESTED
User requested the simulation to stop explicitly, e.g., by pressing ctrl-c in the CLI.
Definition: SimulatorConstants.hh:66
ProximSimulationThread::~ProximSimulationThread
virtual ~ProximSimulationThread()
Definition: ProximSimulationThread.cc:67
SimulatorEvent.hh
SimulatorFrontend::finishSimulation
void finishSimulation()
Definition: SimulatorFrontend.cc:1538
TracedSimulatorFrontend::SIMULATOR_RUN
@ SIMULATOR_RUN
Definition: TracedSimulatorFrontend.hh:74
ProximSimulationThread::killSimulation
void killSimulation()
Definition: ProximSimulationThread.cc:205
SimulatorFrontend::isSimulationStopped
bool isSimulationStopped() const
Definition: SimulatorFrontend.cc:1271
TracedSimulatorFrontend::SIMULATOR_LOADING_PROGRAM
@ SIMULATOR_LOADING_PROGRAM
Definition: TracedSimulatorFrontend.hh:75
ProximSimulationThread::interpreter
SimulatorInterpreter * interpreter()
Definition: ProximSimulationThread.cc:244
ProximSimulationThread::isBusy
bool isBusy()
Definition: ProximSimulationThread.cc:318
ProximSimulationThread.hh
ProximRuntimeErrorHandler.hh
ProximSimulationThread::lineReader
ProximLineReader & lineReader()
Definition: ProximSimulationThread.cc:233
ProximMainFrame::reset
void reset()
Definition: ProximMainFrame.cc:1016
ProximSimulationThread::interpreter_
SimulatorInterpreter * interpreter_
SimulatorInterpreter where the user input is passed to.
Definition: ProximSimulationThread.hh:75
SimulatorEvent::EVT_SIMULATOR_OUTPUT
static const wxEventType EVT_SIMULATOR_OUTPUT
Textual output event from simulator interpreter.
Definition: SimulatorEvent.hh:60
TracedSimulatorFrontend::SIMULATOR_MACHINE_LOADED
@ SIMULATOR_MACHINE_LOADED
Definition: TracedSimulatorFrontend.hh:70
ProximSimulationThread::frontend
TracedSimulatorFrontend * frontend()
Definition: ProximSimulationThread.cc:307
Listener::handleEvent
virtual void handleEvent()
Definition: Listener.cc:70
StringTools.hh
assert
#define assert(condition)
Definition: Application.hh:86
TracedSimulatorFrontend::SIMULATOR_STOP
@ SIMULATOR_STOP
Definition: TracedSimulatorFrontend.hh:73
SimulatorInterpreter
Definition: SimulatorInterpreter.hh:49
SimulatorEvent::EVT_SIMULATOR_LOADING_MACHINE
static const wxEventType EVT_SIMULATOR_LOADING_MACHINE
Machine loading started.
Definition: SimulatorEvent.hh:77
ProximSimulationThread::Entry
virtual ExitCode Entry()
Definition: ProximSimulationThread.cc:127
ProximSimulationThread::finishSimulation
void finishSimulation()
Definition: ProximSimulationThread.cc:218
ScriptInterpreter::error
virtual bool error() const
Definition: ScriptInterpreter.cc:229
Application.hh
SimulatorEvent::EVT_SIMULATOR_LOADING_PROGRAM
static const wxEventType EVT_SIMULATOR_LOADING_PROGRAM
Program loading started.
Definition: SimulatorEvent.hh:81
Informer::registerListener
virtual bool registerListener(int event, Listener *listener)
Definition: Informer.cc:87
ProximLineReader
Definition: ProximLineReader.hh:60
ProximLineReader::initialize
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
Definition: ProximLineReader.cc:76
TclInterpreter::interpret
virtual bool interpret(const std::string &commandLine)
Definition: TclInterpreter.cc:138
SimulatorEvent::EVT_SIMULATOR_PROGRAM_LOADED
static const wxEventType EVT_SIMULATOR_PROGRAM_LOADED
Program loaded event.
Definition: SimulatorEvent.hh:83
ProximSimulationThread::interpreterContext_
SimulatorInterpreterContext * interpreterContext_
SimulatorInterpreterContext. ?
Definition: ProximSimulationThread.hh:73
ProximSimulationThread::ProximSimulationThread
ProximSimulationThread()
Definition: ProximSimulationThread.cc:52
ProximLineReader::readLine
virtual std::string readLine(std::string prompt="")
Definition: ProximLineReader.cc:98
SimulatorFrontend::isSimulationRunning
bool isSimulationRunning() const
Definition: SimulatorFrontend.cc:1260
StringTools::trim
static std::string trim(const std::string &source)
Definition: StringTools.cc:55
ProximSimulationThread::initialize
void initialize(ProximMainFrame *gui)
Definition: ProximSimulationThread.cc:87
TracedSimulatorFrontend.hh
ProximSimulationThread::lineReader_
ProximLineReader * lineReader_
Linereader used for reading the user input.
Definition: ProximSimulationThread.hh:79
TracedSimulatorFrontend::SIMULATOR_LOADING_MACHINE
@ SIMULATOR_LOADING_MACHINE
Definition: TracedSimulatorFrontend.hh:76
SimulatorInterpreter.hh
SimulatorEvent::EVT_SIMULATOR_TERMINATED
static const wxEventType EVT_SIMULATOR_TERMINATED
Simulator thread terminated.
Definition: SimulatorEvent.hh:57
ProximSimulationThread::requestStop
void requestStop()
Definition: ProximSimulationThread.cc:192
SimulatorEvent::EVT_SIMULATOR_ERROR
static const wxEventType EVT_SIMULATOR_ERROR
Simulator error event.
Definition: SimulatorEvent.hh:62
TracedSimulatorFrontend::SIMULATOR_RESET
@ SIMULATOR_RESET
Definition: TracedSimulatorFrontend.hh:77
SimulatorEvent::EVT_SIMULATOR_START
static const wxEventType EVT_SIMULATOR_START
Simulation started.
Definition: SimulatorEvent.hh:69
SimulatorEvent
Definition: SimulatorEvent.hh:42
TracedSimulatorFrontend::SIMULATOR_PROGRAM_LOADED
@ SIMULATOR_PROGRAM_LOADED
Definition: TracedSimulatorFrontend.hh:71
SimulatorEvent::EVT_SIMULATOR_COMMAND
static const wxEventType EVT_SIMULATOR_COMMAND
Command received from the GUI.
Definition: SimulatorEvent.hh:53
TracedSimulatorFrontend::killSimulation
virtual void killSimulation()
Definition: TracedSimulatorFrontend.cc:94
SimulatorEvent::EVT_SIMULATOR_RUN
static const wxEventType EVT_SIMULATOR_RUN
Simulation ran/resumed.
Definition: SimulatorEvent.hh:73
SimulatorEvent::EVT_SIMULATOR_MACHINE_LOADED
static const wxEventType EVT_SIMULATOR_MACHINE_LOADED
Machine loaded event.
Definition: SimulatorEvent.hh:79
ProximSimulationThread::gui_
ProximMainFrame * gui_
Proxim main frame where the simulator events are passed to.
Definition: ProximSimulationThread.hh:81
SimulatorEvent::EVT_SIMULATOR_COMMAND_DONE
static const wxEventType EVT_SIMULATOR_COMMAND_DONE
Command execution completed.
Definition: SimulatorEvent.hh:55
ProximSimulationThread::runtimeErrorHandler_
ProximRuntimeErrorHandler * runtimeErrorHandler_
Runtime error handler.
Definition: ProximSimulationThread.hh:83
SimulatorFrontend::prepareToStop
void prepareToStop(StopReason reason)
Definition: SimulatorFrontend.cc:1331
ProximRuntimeErrorHandler
Definition: ProximRuntimeErrorHandler.hh:47