ArnLib  4.0.x
Active Registry Network
ArnScriptJob.hpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #ifndef ARNSCRIPTJOB_HPP
33 #define ARNSCRIPTJOB_HPP
34 
35 #include "ArnLib_global.hpp"
36 #include "ArnScript.hpp"
37 #include <QObject>
38 #include <QAtomicInt>
39 #include <QMutex>
40 #include <QThread>
41 
42 class ArnScript;
43 class ArnScriptJobB;
46 class ArnScriptWatchdog;
47 class QTimer;
48 class QEvent;
49 
50 
52 
54 class ArnScriptJobB : public QObject
55 {
56  Q_OBJECT
57  friend class ArnScriptJobControl;
58 
59 public:
60  explicit ArnScriptJobB( int id, QObject* parent = arnNullptr);
61  bool evaluateScript( const QByteArray& script, const QString& idName);
62  bool evaluateScriptFile( const QString& fileName);
63  int id() const;
64  QString name() const;
65  bool setupScript();
66  void enterScript();
67  void leaveScript();
68  ArnScriptJobFactory* jobFactory() const;
69  ArnScriptWatchdog* watchdog() const;
70 
71  void setSleepState( bool isSleepState = true);
72  void setWatchDogTime( int time);
73  int watchDogTime();
74  void setWatchDog( int time = -1, bool persist = true);
75  void setPollTime( int time);
76  int pollTime();
77  bool isSleepState() const;
78  bool isRunable() const;
79  bool isStopped() const;
80  bool isRunning() const;
81  void errorLog( const QString& txt);
82 
83 signals:
84  void scheduleRequest( int callerId);
85  void quitRequest( int callerId);
86  void timeoutAbort( int id);
87  void errorText( const QString& txt);
88 
89 public slots:
90  void yield();
91  void quit();
92 
93 protected slots:
94  void doTimeoutAbort();
95 
96 protected:
97  virtual void customEvent( QEvent* ev);
98 
99  void installInterface( const QString& id, QObject* obj);
100  bool installExtension( const QString& id, ArnScriptJobControl* jobControl);
101  bool setConfig( const char* name, const QVariant& value);
102  void addConfig( QObject* obj);
103  void setJobFactory( ArnScriptJobFactory* jobFactory);
104 
105  ArnScript* _arnScr;
106  QObject* _configObj;
107 
108  ARN_JSVALUE _jobInit;
109  ARN_JSVALUE _jobEnter;
110  ARN_JSVALUE _jobLeave;
111 
112 private:
113  int _id;
114  int _watchDogTime; // Longest time to run continous script code until it is aborted
115  int _pollTime; // Longest time to run continous script code until events are processed. Don't exceed ...
116  bool _isSleepState;
117  bool _isStopped;
118  bool _isRunning;
119  bool _quitInProgress;
120  ArnScriptJobFactory* _jobFactory;
121  ArnScriptWatchdog* _watchdog;
122 };
123 
124 
125 class ArnScriptWatchdog : public QObject
126 {
127 Q_OBJECT
128 public:
129  ArnScriptWatchdog( ARN_JSENGINE* jsEngine, QObject* parent = arnNullptr);
130  ~ArnScriptWatchdog();
131 
132  void setup();
133  void setJsEngine( ARN_JSENGINE* jsEngine );
134 
135  void setTime( int timeMs);
136 
137 signals:
138  void timeout();
139 
140 private:
141  void setTimeNow( int timeMs);
142 
143  ARN_JSENGINE* _jsEngine;
144  QMutex _mutex;
145  QTimer* _timer;
146  int _lastTimeMs;
147  bool _isSetup;
148 };
150 
151 
153 class ARNLIBSHARED_EXPORT ArnScriptJob : public ArnScriptJobB
154 {
155  Q_OBJECT
156  Q_PROPERTY( bool sleepState WRITE setSleepState READ isSleepState )
157  Q_PROPERTY( bool running READ isRunning )
158  Q_PROPERTY( int watchDog WRITE setWatchDog READ watchDogTime )
159  Q_PROPERTY( int poll WRITE setPollTime READ pollTime )
160  Q_PROPERTY( QString name READ name )
161 public:
162  explicit ArnScriptJob( int id, QObject* parent = arnNullptr);
163 
164 signals:
165  void sigQuit();
166 
167 public slots:
168  void setWatchDogTime( int time) {ArnScriptJobB::setWatchDogTime( time);}
169  void yield() {ArnScriptJobB::yield();}
170  void quit() {ArnScriptJobB::quit();}
171  void errorLog( const QString& txt) {ArnScriptJobB::errorLog( txt);}
172 };
173 
174 
177 {
178 public:
179  explicit ArnScriptJobFactory();
180  virtual ~ArnScriptJobFactory();
181  virtual bool installExtension( const QString& id, ARN_JSENGINE& engine,
182  const ArnScriptJobControl* jobControl = arnNullptr) = 0;
183 
184 protected:
185  static void setupJsObj( const QString& id, const ARN_JSVALUE& jsObj, ARN_JSENGINE& engine);
186  static bool setupInterface( const QString& id, QObject* interface, ARN_JSENGINE& engine);
187 };
188 
189 
192 {
193  Q_OBJECT
194 public:
195  explicit ArnScriptJobControl( QObject* parent = arnNullptr);
196  int id();
197  QString name() const;
198  void setName( const QString& name);
199  void addInterface( const QString& id);
200  void addInterfaceList( const QStringList& interfaceList);
201  QByteArray script() const;
202  void loadScriptFile( const QString& fileName);
203  QVariant config( const char* name) const;
204  bool setConfig( const char* name, const QVariant& value);
205  void addConfig( QObject* obj);
206 
207  void setThreaded( bool isThreaded);
208  void doSetupJob( ArnScriptJob* job, ArnScriptJobFactory* jobFactory);
209 
210 signals:
211  void scriptChanged( int id);
212  void errorText( const QString& txt);
213 
214 public slots:
215  void setScript( const QByteArray& script);
216 
217 private:
218  // Source for unique id to all Jobs ..
219  static QAtomicInt _idCount;
220 
221  int _id;
222  QString _name;
223  QStringList _interfaceList;
224  QByteArray _script;
225  QObject* _configObj;
226 
227  bool _isThreaded;
228  mutable QMutex _mutex;
229 };
230 
231 
232 #endif // ARNSCRIPTJOB_HPP
QByteArray script() const
bool setConfig(const char *name, const QVariant &value)
void errorText(const QString &txt)
#define ARN_JSVALUE
Definition: ArnScript.hpp:55
#define ARN_JSENGINE
Definition: ArnScript.hpp:54
Must be thread-safe as subclassed.
void errorLog(const QString &txt)
Is thread-safe (except doSetupJob)
Interface class to be normally used, is also Script Job interface.
QString name() const
void addConfig(QObject *obj)
#define ARNLIBSHARED_EXPORT