OpenASIP 2.2
Loading...
Searching...
No Matches
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>
40#include "StringTools.hh"
41#include "Application.hh"
42#include "SimulatorEvent.hh"
45
46using 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) {
76 }
77 if (interpreterContext_ != NULL) {
79 }
80}
81
82
83/**
84 * Initializes the thread.
85 */
86void
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 */
126void*
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(
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 */
191void
199
200
201/**
202 * Kills the current simulation.
203 */
204void
213
214/**
215 * Finishes the current simulation.
216 */
217void
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 */
236
237
238/**
239 * Returns reference to the script interpreter of the simulator.
240 *
241 * @return Simulator script interpreter of the simulator.
242 */
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 */
257void
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 */
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 */
317bool
320 return true;
321 } else {
322 return false;
323 }
324}
#define assert(condition)
@ SRE_USER_REQUESTED
User requested the simulation to stop explicitly, e.g., by pressing ctrl-c in the CLI.
virtual bool registerListener(int event, Listener *listener)
Definition Informer.cc:87
virtual void handleEvent()
Definition Listener.cc:70
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)
virtual std::string readLine(std::string prompt="")
ProximRuntimeErrorHandler * runtimeErrorHandler_
Runtime error handler.
TracedSimulatorFrontend * simulation_
Simulator backend.
ProximLineReader * lineReader_
Linereader used for reading the user input.
ProximMainFrame * gui_
Proxim main frame where the simulator events are passed to.
TracedSimulatorFrontend * frontend()
void initialize(ProximMainFrame *gui)
SimulatorInterpreter * interpreter_
SimulatorInterpreter where the user input is passed to.
SimulatorInterpreterContext * interpreterContext_
SimulatorInterpreterContext. ?
SimulatorInterpreter * interpreter()
ProximLineReader & lineReader()
virtual std::string result()
virtual bool error() const
static const wxEventType EVT_SIMULATOR_START
Simulation started.
static const wxEventType EVT_SIMULATOR_ERROR
Simulator error event.
static const wxEventType EVT_SIMULATOR_TERMINATED
Simulator thread terminated.
static const wxEventType EVT_SIMULATOR_MACHINE_LOADED
Machine loaded event.
static const wxEventType EVT_SIMULATOR_RUN
Simulation ran/resumed.
static const wxEventType EVT_SIMULATOR_PROGRAM_LOADED
Program loaded event.
static const wxEventType EVT_SIMULATOR_LOADING_MACHINE
Machine loading started.
static const wxEventType EVT_SIMULATOR_COMMAND_DONE
Command execution completed.
static const wxEventType EVT_SIMULATOR_COMMAND
Command received from the GUI.
static const wxEventType EVT_SIMULATOR_OUTPUT
Textual output event from simulator interpreter.
static const wxEventType EVT_SIMULATOR_LOADING_PROGRAM
Program loading started.
static const wxEventType EVT_SIMULATOR_STOP
Simulation stopped.
bool isSimulationStopped() const
bool isSimulationRunning() const
void prepareToStop(StopReason reason)
static std::string trim(const std::string &source)
virtual bool interpret(const std::string &commandLine)