trikRuntime
trikScriptRunner::ScriptEngineWorker Class Reference

Worker object to be run in a separate thread for Qt Script execution. More...

#include <scriptEngineWorker.h>

Inheritance diagram for trikScriptRunner::ScriptEngineWorker:
Collaboration diagram for trikScriptRunner::ScriptEngineWorker:

Public Slots

void run (const QString &script, int scriptId)
 Starts script evaluation, emits startedScript() signal and returns. More...
 
void runDirect (const QString &command, int scriptId)
 Runs a command in a current context. More...
 
void brickBeep ()
 Plays "beep" sound. More...
 

Signals

void completed (const QString &error, int scriptId)
 Emitted when current script execution is completed or is aborted by reset() call. More...
 
void startedScript (int scriptId)
 Emitted when new script is started. More...
 
void getVariables (const QString &propertyName)
 Emitted when there is a request for variables values. More...
 
void variablesReady (const QJsonObject &data)
 Emitted when variables values are ready. More...
 

Public Member Functions

 ScriptEngineWorker (trikControl::BrickInterface &brick, trikNetwork::MailboxInterface *mailbox, ScriptExecutionControl &scriptControl)
 Constructor. More...
 
QScriptEngine * createScriptEngine (bool supportThreads=true)
 Create and initialize a new script engine. More...
 
QScriptEngine * copyScriptEngine (const QScriptEngine *original)
 Copies given script engine creating a new one with the same context as existing one. More...
 
void registerUserFunction (const QString &name, QScriptEngine::FunctionSignature function)
 Registers given C++ function as callable from script, with given name. More...
 
void addCustomEngineInitStep (const std::function< void(QScriptEngine *)> &step)
 Helper for adding custom initialization steps when creating script engine from outside of the TrikRuntime. More...
 
void resetBrick ()
 Clears execution state and stops robot. More...
 
void stopScript ()
 Stops script execution and resets script engine. More...
 
QStringList knownMethodNames () const
 Gets all method names from executive objects (brick, script, etc.) from ScriptEngineWorker (useful when used from outside of the TrikRuntime). More...
 

Detailed Description

Worker object to be run in a separate thread for Qt Script execution.

QScriptEngine calls ProcessEvents too infrequently even when ProcessEventsInterval is set to 1 ms, so there is a need for separate threads to run a script and listen for incoming connections.

Constructor & Destructor Documentation

ScriptEngineWorker::ScriptEngineWorker ( trikControl::BrickInterface brick,
trikNetwork::MailboxInterface mailbox,
ScriptExecutionControl scriptControl 
)

Constructor.

Parameters
brick- reference to trikControl::Brick instance.
mailbox- mailbox object used to communicate with other robots.
scriptControl- reference to script execution control object.

Member Function Documentation

void ScriptEngineWorker::addCustomEngineInitStep ( const std::function< void(QScriptEngine *)> &  step)

Helper for adding custom initialization steps when creating script engine from outside of the TrikRuntime.

void ScriptEngineWorker::brickBeep ( )
slot

Plays "beep" sound.

Can be safely called from other threads.

void trikScriptRunner::ScriptEngineWorker::completed ( const QString &  error,
int  scriptId 
)
signal

Emitted when current script execution is completed or is aborted by reset() call.

Parameters
error- localized error message or empty string.
scriptId- unique identifier of a script completed
QScriptEngine * ScriptEngineWorker::copyScriptEngine ( const QScriptEngine *  original)

Copies given script engine creating a new one with the same context as existing one.

Note that functions will not be copied to a new engine due to limitations of Qt Scripting engine, they need to be re-evaluated manually. Can be safely called from other threads (to some extent. Original engine shall not simultaneously evaluate).

QScriptEngine * ScriptEngineWorker::createScriptEngine ( bool  supportThreads = true)

Create and initialize a new script engine.

Can be safely called from other threads.

Parameters
supportThreads- true if created engine should support creation of threads.
void trikScriptRunner::ScriptEngineWorker::getVariables ( const QString &  propertyName)
signal

Emitted when there is a request for variables values.

Parameters
propertyName- name of variables prefix, i.e prefix "web" for variable "web.light"
QStringList ScriptEngineWorker::knownMethodNames ( ) const

Gets all method names from executive objects (brick, script, etc.) from ScriptEngineWorker (useful when used from outside of the TrikRuntime).

void ScriptEngineWorker::registerUserFunction ( const QString &  name,
QScriptEngine::FunctionSignature  function 
)

Registers given C++ function as callable from script, with given name.

Can be safely called from other threads (but it shall not be called simultaneously with engine creation).

void ScriptEngineWorker::resetBrick ( )

Clears execution state and stops robot.

Can be safely called from other threads.

void ScriptEngineWorker::run ( const QString &  script,
int  scriptId 
)
slot

Starts script evaluation, emits startedScript() signal and returns.

Script will be executed asynchronously. completed() signal is emitted upon script abortion or completion. It is a caller's responsibility to ensure that ScriptEngineWorker is in ready state before a call to run() by calling reset() first.

Parameters
script- QtScript code to evaluate
scriptId- an id of a script, used to distinguish between different scripts run by a worker Can be safely called from other threads.
void ScriptEngineWorker::runDirect ( const QString &  command,
int  scriptId 
)
slot

Runs a command in a current context.

Permits to run a script line by line. The command will be executed asynchronously. If called when an ordinary script is running, that script would be aborted before evaluation of a command. In this mode, threads and messages are unsupported. Can be safely called from other threads.

void trikScriptRunner::ScriptEngineWorker::startedScript ( int  scriptId)
signal

Emitted when new script is started.

Parameters
scriptId- unique identifier assigned to a newly started script.
void ScriptEngineWorker::stopScript ( )

Stops script execution and resets script engine.

Can be called from another thread. By the end of call the worker would be in a ready state. Can be safely called from other threads.

Todo:
: here script will continue to execute and may execute some statements before it will eventually be stopped by mThreading.reset(). But if we do mThreading.reset() before mMailbox->stopWaiting(), we will get deadlock, since mMailbox->stopWaiting() shall be executed in already stopped thread. Actually we shall stop script engines here, do mMailbox->stopWaiting(), then stop threads.
Todo:
: is it actually stopped?
void trikScriptRunner::ScriptEngineWorker::variablesReady ( const QJsonObject &  data)
signal

Emitted when variables values are ready.

Parameters
json- JSON container for variables values

The documentation for this class was generated from the following files: