OpenASIP  2.0
Public Member Functions | Private Attributes | List of all members
ProximSimulationThread Class Reference

#include <ProximSimulationThread.hh>

Inheritance diagram for ProximSimulationThread:
Inheritance graph
Collaboration diagram for ProximSimulationThread:
Collaboration graph

Public Member Functions

 ProximSimulationThread ()
 
virtual ~ProximSimulationThread ()
 
virtual ExitCode Entry ()
 
virtual void handleEvent (int event)
 
ProximLineReaderlineReader ()
 
TracedSimulatorFrontendfrontend ()
 
void initialize (ProximMainFrame *gui)
 
SimulatorInterpreterinterpreter ()
 
void requestStop ()
 
void killSimulation ()
 
void finishSimulation ()
 
bool isBusy ()
 
- Public Member Functions inherited from Listener
 Listener ()
 
virtual ~Listener ()
 
virtual void handleEvent ()
 

Private Attributes

SimulatorInterpreterContextinterpreterContext_
 SimulatorInterpreterContext. ? More...
 
SimulatorInterpreterinterpreter_
 SimulatorInterpreter where the user input is passed to. More...
 
TracedSimulatorFrontendsimulation_
 Simulator backend. More...
 
ProximLineReaderlineReader_
 Linereader used for reading the user input. More...
 
ProximMainFramegui_
 Proxim main frame where the simulator events are passed to. More...
 
ProximRuntimeErrorHandlerruntimeErrorHandler_
 Runtime error handler. More...
 

Detailed Description

ProximSimulationThread class wraps the simulation backend in a separate worker thread.

The worker thread utilizes ProximLinereader to read commands from the user. Simulator events and input requests are passed to the GUI thread as custom wxEvents (see SimulatorEvent class).

Definition at line 55 of file ProximSimulationThread.hh.

Constructor & Destructor Documentation

◆ ProximSimulationThread()

ProximSimulationThread::ProximSimulationThread ( )

The Constructor.

Definition at line 52 of file ProximSimulationThread.cc.

52  :
53  wxThread(wxTHREAD_JOINABLE),
54  simulation_(NULL),
56  gui_(NULL),
57  runtimeErrorHandler_(NULL) {
58 
59 }

◆ ~ProximSimulationThread()

ProximSimulationThread::~ProximSimulationThread ( )
virtual

The Destructor.

Definition at line 67 of file ProximSimulationThread.cc.

67  {
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 }

References interpreter_, interpreterContext_, lineReader_, runtimeErrorHandler_, and simulation_.

Member Function Documentation

◆ Entry()

void * ProximSimulationThread::Entry ( )
virtual

Entry point for the simulation thread execution.

The thread waits commands from the line reader, and executes them using the simulator interpreter. Commands are executed until the thread is terminated.

Definition at line 127 of file ProximSimulationThread.cc.

127  {
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 }

References assert, ScriptInterpreter::error(), SimulatorEvent::EVT_SIMULATOR_COMMAND, SimulatorEvent::EVT_SIMULATOR_COMMAND_DONE, SimulatorEvent::EVT_SIMULATOR_ERROR, SimulatorEvent::EVT_SIMULATOR_OUTPUT, SimulatorEvent::EVT_SIMULATOR_TERMINATED, gui_, TclInterpreter::interpret(), interpreter_, lineReader_, ProximLineReader::readLine(), ScriptInterpreter::result(), and StringTools::trim().

Here is the call graph for this function:

◆ finishSimulation()

void ProximSimulationThread::finishSimulation ( )

Finishes the current simulation.

Definition at line 218 of file ProximSimulationThread.cc.

218  {
221  }
223 }

References SimulatorFrontend::finishSimulation(), SimulatorFrontend::isSimulationRunning(), SimulatorFrontend::prepareToStop(), simulation_, and SRE_USER_REQUESTED.

Here is the call graph for this function:

◆ frontend()

TracedSimulatorFrontend * ProximSimulationThread::frontend ( )

Returns pointer to the simulator.

Definition at line 307 of file ProximSimulationThread.cc.

307  {
308  return simulation_;
309 }

References simulation_.

◆ handleEvent()

void ProximSimulationThread::handleEvent ( int  event)
virtual

Handles events from the simulator backend.

The events are passed to the GUI as SimulatorEvents, which is a class derived from the wxEvent class. This way responsibility of the event handling can be passed to the GUI thread.

Reimplemented from Listener.

Definition at line 258 of file ProximSimulationThread.cc.

258  {
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 }

References SimulatorEvent::EVT_SIMULATOR_LOADING_MACHINE, SimulatorEvent::EVT_SIMULATOR_LOADING_PROGRAM, SimulatorEvent::EVT_SIMULATOR_MACHINE_LOADED, SimulatorEvent::EVT_SIMULATOR_PROGRAM_LOADED, SimulatorEvent::EVT_SIMULATOR_RUN, SimulatorEvent::EVT_SIMULATOR_START, SimulatorEvent::EVT_SIMULATOR_STOP, gui_, ProximMainFrame::reset(), TracedSimulatorFrontend::SIMULATOR_LOADING_MACHINE, TracedSimulatorFrontend::SIMULATOR_LOADING_PROGRAM, TracedSimulatorFrontend::SIMULATOR_MACHINE_LOADED, TracedSimulatorFrontend::SIMULATOR_PROGRAM_LOADED, TracedSimulatorFrontend::SIMULATOR_RESET, TracedSimulatorFrontend::SIMULATOR_RUN, TracedSimulatorFrontend::SIMULATOR_START, and TracedSimulatorFrontend::SIMULATOR_STOP.

Here is the call graph for this function:

◆ initialize()

void ProximSimulationThread::initialize ( ProximMainFrame gui)

Initializes the thread.

Definition at line 87 of file ProximSimulationThread.cc.

References gui_, ProximLineReader::initialize(), interpreter_, interpreterContext_, lineReader_, Informer::registerListener(), runtimeErrorHandler_, simulation_, TracedSimulatorFrontend::SIMULATOR_LOADING_MACHINE, TracedSimulatorFrontend::SIMULATOR_LOADING_PROGRAM, TracedSimulatorFrontend::SIMULATOR_MACHINE_LOADED, TracedSimulatorFrontend::SIMULATOR_PROGRAM_LOADED, TracedSimulatorFrontend::SIMULATOR_RESET, TracedSimulatorFrontend::SIMULATOR_RUN, TracedSimulatorFrontend::SIMULATOR_START, and TracedSimulatorFrontend::SIMULATOR_STOP.

Referenced by Proxim::OnInit().

Here is the call graph for this function:

◆ interpreter()

SimulatorInterpreter * ProximSimulationThread::interpreter ( )

Returns reference to the script interpreter of the simulator.

Returns
Simulator script interpreter of the simulator.

Definition at line 244 of file ProximSimulationThread.cc.

244  {
245  return interpreter_;
246 
247 }

References interpreter_.

Referenced by ProximToolbox::interpreter(), and Proxim::OnInit().

◆ isBusy()

bool ProximSimulationThread::isBusy ( )

Returns true if the thread is busy running the simulation.

Returns
True, if the thread is currently running simulation.

Definition at line 318 of file ProximSimulationThread.cc.

318  {
320  return true;
321  } else {
322  return false;
323  }
324 }

References SimulatorFrontend::isSimulationRunning(), and simulation_.

Here is the call graph for this function:

◆ killSimulation()

void ProximSimulationThread::killSimulation ( )

Kills the current simulation.

Definition at line 205 of file ProximSimulationThread.cc.

205  {
208  }
211  wxPostEvent(gui_, simEvent);
212 }

References SimulatorEvent::EVT_SIMULATOR_STOP, gui_, SimulatorFrontend::isSimulationRunning(), TracedSimulatorFrontend::killSimulation(), SimulatorFrontend::prepareToStop(), simulation_, and SRE_USER_REQUESTED.

Here is the call graph for this function:

◆ lineReader()

ProximLineReader & ProximSimulationThread::lineReader ( )

Returns reference to the line reader which the thread uses to read user input.

Returns
LineReader used by the simulator.

Definition at line 233 of file ProximSimulationThread.cc.

233  {
234  return *lineReader_;
235 }

References lineReader_.

◆ requestStop()

void ProximSimulationThread::requestStop ( )

Requests the simulation backend to stop the simulation.

Definition at line 192 of file ProximSimulationThread.cc.

192  {
196  wxPostEvent(gui_, simEvent);
197  }
198 }

References SimulatorEvent::EVT_SIMULATOR_STOP, gui_, SimulatorFrontend::isSimulationStopped(), SimulatorFrontend::prepareToStop(), simulation_, and SRE_USER_REQUESTED.

Here is the call graph for this function:

Member Data Documentation

◆ gui_

ProximMainFrame* ProximSimulationThread::gui_
private

Proxim main frame where the simulator events are passed to.

Definition at line 81 of file ProximSimulationThread.hh.

Referenced by Entry(), handleEvent(), initialize(), killSimulation(), and requestStop().

◆ interpreter_

SimulatorInterpreter* ProximSimulationThread::interpreter_
private

SimulatorInterpreter where the user input is passed to.

Definition at line 75 of file ProximSimulationThread.hh.

Referenced by Entry(), initialize(), interpreter(), and ~ProximSimulationThread().

◆ interpreterContext_

SimulatorInterpreterContext* ProximSimulationThread::interpreterContext_
private

◆ lineReader_

ProximLineReader* ProximSimulationThread::lineReader_
private

Linereader used for reading the user input.

Definition at line 79 of file ProximSimulationThread.hh.

Referenced by Entry(), initialize(), lineReader(), and ~ProximSimulationThread().

◆ runtimeErrorHandler_

ProximRuntimeErrorHandler* ProximSimulationThread::runtimeErrorHandler_
private

Runtime error handler.

Definition at line 83 of file ProximSimulationThread.hh.

Referenced by initialize(), and ~ProximSimulationThread().

◆ simulation_

TracedSimulatorFrontend* ProximSimulationThread::simulation_
private

The documentation for this class was generated from the following files:
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
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
SimulatorFrontend::finishSimulation
void finishSimulation()
Definition: SimulatorFrontend.cc:1538
TracedSimulatorFrontend::SIMULATOR_RUN
@ SIMULATOR_RUN
Definition: TracedSimulatorFrontend.hh:74
SimulatorFrontend::isSimulationStopped
bool isSimulationStopped() const
Definition: SimulatorFrontend.cc:1271
TracedSimulatorFrontend::SIMULATOR_LOADING_PROGRAM
@ SIMULATOR_LOADING_PROGRAM
Definition: TracedSimulatorFrontend.hh:75
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
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
ScriptInterpreter::error
virtual bool error() const
Definition: ScriptInterpreter.cc:229
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
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::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
SimulatorEvent::EVT_SIMULATOR_TERMINATED
static const wxEventType EVT_SIMULATOR_TERMINATED
Simulator thread terminated.
Definition: SimulatorEvent.hh:57
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