WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZScriptThread.h
Go to the documentation of this file.
1 /*
2 * Zuble - A run-time system for QML/Javascript applications
3 * Copyright (C) 2013, 2014 Bob Dinitto
4 *
5 * Filename: ZScriptThread.h
6 * Created on: 11/9/2014
7 * Author: Bob Dinitto
8 *
9 * Zuble is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24 
25 #ifndef ZSCRIPTTHREAD_H
26 #define ZSCRIPTTHREAD_H
27 
28 #include <QThread>
29 #include <QJSValue>
30 #include <QVariant>
31 #include "zglobal.h"
32 #include "zblcore_global.h"
33 
34 namespace Zbl
35 {
36 
37 class ZScriptWorker;
38 class ZblJsonHelper;
39 
62 class ZScriptThread : public QObject
63 {
64  friend class ZblApp;
65 
66  Q_OBJECT
67 public:
68 
69  typedef enum alertConversion {
70 
76 
78 
79  Q_ENUM(AlertConversion)
80 
81 
82  explicit ZScriptThread(QObject *parent = nullptr);
83 
84  virtual ~ZScriptThread();
85 
90  static void registerType();
91 
104  Q_PROPERTY(QString initScript READ getInitScript WRITE setInitScript)
105 
106 
112  Q_PROPERTY(QVariant initError READ getInitError)
113 
114 
123  Q_PROPERTY(bool ready READ getReady NOTIFY initComplete)
124 
131  Q_PROPERTY(bool complete READ getCompleted NOTIFY finished)
132 
133 
134  QString getInitScript();
135  void setInitScript(const QString& script);
136  QVariant getInitError();
137  bool getReady();
138  bool getCompleted();
139 
149  Q_INVOKABLE bool isInterrupted() const;
150 
151 signals:
152 
163  void execComplete(qulonglong requestID, QVariant result, QVariant userData);
164 
170  //TBD: initComplete send userData too!
171 
172  void initComplete(QVariant status);
173 
179  void beginShutdown(); //TBD: ARE WE ISSUING THIS?
180 
185  void finished();
186 
187 
188 
199  void alert(const QString signalID, QVariant payload);
200 
204  void readyChanged();
205 
209  void completeChanged();
210 
211 
212 
213 
214 
215 public slots:
216 
236  void execScript(qulonglong requestID, const QString& script, QVariantMap userData = QVariantMap());
237 
247  bool waitThreadComplete(unsigned long time = ULONG_MAX);
248 
249 
256  void start();
257 
263  void quit();
264 
271  void sendInterruptRequest();
272 
273 
274 protected:
275 
276 
286  void onWorkerExecComplete(qulonglong requestID, QString result, QString userData);
287 
288 
304  Q_INVOKABLE void onThreadAlert(QString signalID,
305  QString payload,
306  int conversion);
307 
308 
309 
313  void onStarted();
314 
318  void onFinished();
319 
325  void destroyThread();
326 
333  static QString variantToJson(QVariant& var);
334 
342  static QVariant jsonToVariant(const QString& json);
343 
344 
346 
351  QThread* m_thread;
352 
359 
368  QString m_initScript;
369 
375  QVariant m_initError;
376 
382 
388 
389 };
390 
391 //typedef QMap<QString, ZScriptThread*> ZScriptMap;
392 
393 
394 } // Zbl
395 
396 //Q_DECLARE_METATYPE(Zbl::ZScriptThread::AlertConversion)
397 
398 #endif // ZSCRIPTTHREAD_H
void execComplete(qulonglong requestID, QVariant result, QVariant userData)
Sent when an asynchronous background script execution has completed.
virtual ~ZScriptThread()
#define ZBL_DECLARE_LOGGED_OBJECT
Definition: zglobal.h:94
void finished()
Sent from the associated thread right before it finishes executing.
QVariant m_initError
The error message generated when running the initScript, or an empty string if initScript was success...
static void registerType()
Registers ZScriptThread as a QML type.
enum Zbl::ZScriptThread::alertConversion AlertConversion
void setInitScript(const QString &script)
void onWorkerExecComplete(qulonglong requestID, QString result, QString userData)
Receives and propagates the ZScriptWorker::execComplete signal.
void execScript(qulonglong requestID, const QString &script, QVariantMap userData=QVariantMap())
Execute the specified javascript program fragment asynchronously in the background thread&#39;s javascrip...
void onFinished()
Called when QThread event loop has exited.
Q_INVOKABLE bool isInterrupted() const
Calls the QThread::isInterruptionRequested() method for this thread. Can be used to determine if this...
ZScriptThread(QObject *parent=nullptr)
bool ready
True if thread is ready to run scripts, false otherwise.
A thread class to support Zuble&#39;s background Javascript processing.
Definition: ZScriptThread.h:62
A class for executing javascript programs in a background worker thread.
Definition: ZScriptWorker.h:51
void quit()
Causes the thread to stop processing events and exit the event loop.
static QString variantToJson(QVariant &var)
Converts QVariant to json string.
Q_INVOKABLE void onThreadAlert(QString signalID, QString payload, int conversion)
Called by ZblApp in background thread to trigger ZScriptThread::alert signal in foreground thread...
void beginShutdown()
Sent when the javascript thread is about to begin it&#39;s shutdown sequence.
ZScriptWorker * m_worker
Script worker object maintains this thread&#39;s javascript context.
Definition: ZAndGate.cpp:6
void onStarted()
Called when QThread has started running.
The primary QML API to the Zuble plugin library. Zuble applications access this object through the ja...
Definition: ZblApp.h:81
void readyChanged()
Sent when ready property value changes.
static QVariant jsonToVariant(const QString &json)
Converts json string to QVariant.
void sendInterruptRequest()
Calls the QThread::requestInterruption() method for this thread. If the thread is checking Zbl...
bool m_initReady
true if thread initialization is complete, false otherwise
void completeChanged()
Sent when complete property value changes.
void destroyThread()
Stops the QThread, waits for it to exit and then deletes it and the ZScriptWorker object...
void alert(const QString signalID, QVariant payload)
Background scripts send this signal by calling the ZblApp::alert() method.
bool waitThreadComplete(unsigned long time=ULONG_MAX)
Blocks the current thread until the background thread finishes running its event loop or a timeout oc...
ZBL_DECLARE_LOGGED_OBJECT QThread * m_thread
QThread object in which ZScriptWorker object will run.
QString m_initScript
Thread&#39;s javascript context will be initialized by running this script.
void initComplete(QVariant status)
Sent when the javascript execution environment has completed initialization and is ready for use...
bool m_threadCompleted
true if thread has been shut down, false otherwise
void start()
Causes the tread to start execution.
QVariant initError
The error message generated when executing the initScript program, or the empty string if no error wa...
QString initScript
Thread initialization script.
bool complete
True when thread has been shut down, false otherwise.