OpenASIP 2.2
Loading...
Searching...
No Matches
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. ?
 
SimulatorInterpreterinterpreter_
 SimulatorInterpreter where the user input is passed to.
 
TracedSimulatorFrontendsimulation_
 Simulator backend.
 
ProximLineReaderlineReader_
 Linereader used for reading the user input.
 
ProximMainFramegui_
 Proxim main frame where the simulator events are passed to.
 
ProximRuntimeErrorHandlerruntimeErrorHandler_
 Runtime error handler.
 

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),
58
59}
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.

◆ ~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) {
76 }
77 if (interpreterContext_ != NULL) {
79 }
80}
SimulatorInterpreter * interpreter_
SimulatorInterpreter where the user input is passed to.
SimulatorInterpreterContext * interpreterContext_
SimulatorInterpreterContext. ?

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(
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}
#define assert(condition)
virtual std::string readLine(std::string prompt="")
virtual std::string result()
virtual bool error() const
static const wxEventType EVT_SIMULATOR_ERROR
Simulator error event.
static const wxEventType EVT_SIMULATOR_TERMINATED
Simulator thread terminated.
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 std::string trim(const std::string &source)
virtual bool interpret(const std::string &commandLine)

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}
@ SRE_USER_REQUESTED
User requested the simulation to stop explicitly, e.g., by pressing ctrl-c in the CLI.
bool isSimulationRunning() const
void prepareToStop(StopReason reason)

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}
static const wxEventType EVT_SIMULATOR_START
Simulation started.
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_LOADING_PROGRAM
Program loading started.
static const wxEventType EVT_SIMULATOR_STOP
Simulation stopped.

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.

87 {
88
89 gui_ = gui;
90
93
97
114
116}
virtual bool registerListener(int event, Listener *listener)
Definition Informer.cc:87
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)

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 ( )

◆ 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}
bool isSimulationStopped() const

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: