FACT++  1.0
InterpreterV8.h
Go to the documentation of this file.
1 #ifndef FACT_InterpreterV8
2 #define FACT_InterpreterV8
3 
4 #include <map>
5 #include <set>
6 #include <list>
7 #include <string>
8 #include <thread>
9 
10 #ifdef HAVE_V8
11 #include <v8.h>
12 #endif
13 
14 #include "State.h"
15 #include "Service.h"
16 #include "Description.h"
17 #include "EventImp.h"
18 
19 class Database;
20 
21 #ifdef HAVE_NOVA
22 struct ln_rst_time;
23 #endif
24 
26 {
28 
29  // The main thread id, needed to be able to terminate
30  // the thread forcefully from 'the outside'
31  int fThreadId;
32  std::set<int> fThreadIds;
33 
34  // A loookup table which allows to indentify the
35  // the JavaScript object corrsponding to the
36  // service name (for checking of an .onchange
37  // subscription exists for that object)
38  std::map<std::string, v8::Persistent<v8::Object>> fReverseMap;
39 
40  // Lookup table for the callbacks in cases of state changes
41  std::map<std::string, v8::Persistent<v8::Object>> fStateCallbacks;
42 
43  // List of all threads
44  std::vector<std::thread> fThreads;
45 
46  // List of all states already set
47  std::vector<std::pair<int, std::string>> fStates;
48 
49  // Interrupt handler
50  v8::Persistent<v8::Object> fInterruptCallback;
51 
52  static v8::Handle<v8::FunctionTemplate> fTemplateLocal;
53  static v8::Handle<v8::FunctionTemplate> fTemplateSky;
54  static v8::Handle<v8::FunctionTemplate> fTemplateEvent;
55  static v8::Handle<v8::FunctionTemplate> fTemplateDescription;
56 
57 #ifdef HAVE_SQL
58  std::list<Database*> fDatabases;
59 #endif
60 
61 #ifdef HAVE_V8
62  bool HandleException(v8::TryCatch &try_catch, const char *where);
63  void ExecuteConsole();
64  v8::Handle<v8::Value> ExecuteCode(const std::string &code, const std::string &file="internal");
65  v8::Handle<v8::Value> ExecuteInternal(const std::string &code);
66 
67  void Thread(int &id, v8::Persistent<v8::Object> _this, v8::Persistent<v8::Function> func, uint32_t ms);
68 
69  std::vector<std::string> ValueToArray(const v8::Handle<v8::Value> &val, bool only=true);
70 
71  v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
72  v8::Handle<v8::Value> FuncSend(const v8::Arguments& args);
73  v8::Handle<v8::Value> FuncSleep(const v8::Arguments& args);
74  v8::Handle<v8::Value> FuncTimeout(const v8::Arguments& args);
75  v8::Handle<v8::Value> FuncThread(const v8::Arguments& args);
76  v8::Handle<v8::Value> FuncKill(const v8::Arguments& args);
77  v8::Handle<v8::Value> FuncLog(const v8::Arguments& args);
78  v8::Handle<v8::Value> FuncAlarm(const v8::Arguments& args);
79  v8::Handle<v8::Value> FuncOut(const v8::Arguments& args);
80  v8::Handle<v8::Value> FuncWarn(const v8::Arguments& args);
81  v8::Handle<v8::Value> FuncFile(const v8::Arguments& args);
82  v8::Handle<v8::Value> FuncSendMail(const v8::Arguments& args);
83  v8::Handle<v8::Value> FuncSendCurl(const v8::Arguments& args);
84  v8::Handle<v8::Value> FuncInclude(const v8::Arguments& args);
85  v8::Handle<v8::Value> FuncExit(const v8::Arguments& args);
86  v8::Handle<v8::Value> FuncState(const v8::Arguments& args);
87  v8::Handle<v8::Value> FuncSetState(const v8::Arguments& args);
88  v8::Handle<v8::Value> FuncGetState(const v8::Arguments& args);
89  v8::Handle<v8::Value> FuncGetStates(const v8::Arguments& args);
90  v8::Handle<v8::Value> FuncGetDescription(const v8::Arguments& args);
91  v8::Handle<v8::Value> FuncGetServices(const v8::Arguments& args);
92  v8::Handle<v8::Value> FuncNewState(const v8::Arguments& args);
93  v8::Handle<v8::Value> FuncSetInterrupt(const v8::Arguments& args);
94  v8::Handle<v8::Value> FuncTriggerInterrupt(const v8::Arguments& args);
95  //v8::Handle<v8::Value> FuncOpen(const v8::Arguments& args);
96  v8::Handle<v8::Value> FuncSubscription(const v8::Arguments& args);
97  v8::Handle<v8::Value> FuncGetData(const v8::Arguments &args);
98  v8::Handle<v8::Value> FuncClose(const v8::Arguments &args);
99  v8::Handle<v8::Value> FuncQuery(const v8::Arguments &args);
100  v8::Handle<v8::Value> FuncDatabase(const v8::Arguments &args);
101  v8::Handle<v8::Value> FuncDbQuery(const v8::Arguments &args);
102  v8::Handle<v8::Value> FuncDbClose(const v8::Arguments &args);
103  v8::Handle<v8::Value> OnChangeSet(v8::Local<v8::String>, v8::Local<v8::Value>, const v8::AccessorInfo &);
104 
105  static v8::Handle<v8::Value> Constructor(const v8::Arguments &args);
106 
107  static v8::Handle<v8::Value> ConstructorMail(const v8::Arguments &args);
108  static v8::Handle<v8::Value> ConstructorCurl(const v8::Arguments &args);
109 
110 #ifdef HAVE_NOVA
111  static double GetDataMember(const v8::Arguments &args, const char *name);
112 
113  static v8::Handle<v8::Value> CalcDist(const v8::Arguments &args, const bool);
114 
115  static v8::Handle<v8::Value> LocalToString(const v8::Arguments &args);
116  static v8::Handle<v8::Value> SkyToString(const v8::Arguments &args);
117  static v8::Handle<v8::Value> MoonToString(const v8::Arguments &args);
118  static v8::Handle<v8::Value> LocalDist(const v8::Arguments &args);
119  static v8::Handle<v8::Value> SkyDist(const v8::Arguments &args);
120  static v8::Handle<v8::Value> MoonDisk(const v8::Arguments &args);
121  static v8::Handle<v8::Value> LocalToSky(const v8::Arguments &args);
122  static v8::Handle<v8::Value> SkyToLocal(const v8::Arguments &args);
123  static v8::Handle<v8::Value> MoonToLocal(const v8::Arguments &args);
124  static v8::Handle<v8::Value> ConstructorMoon(const v8::Arguments &args);
125  static v8::Handle<v8::Value> ConstructorSky(const v8::Arguments &args);
126  static v8::Handle<v8::Value> ConstructorLocal(const v8::Arguments &args);
127  static v8::Handle<v8::Value> MoonHorizon(const v8::Arguments &args);
128  static v8::Handle<v8::Value> SunHorizon(const v8::Arguments &args);
129  static v8::Handle<v8::Object> ConstructRiseSet(const v8::Handle<v8::Value>, const ln_rst_time &, const bool &);
130 #endif
131 
132  static v8::Handle<v8::Value> WrapInclude(const v8::Arguments &args) { if (This) return This->FuncInclude(args); else return v8::Undefined(); }
133  static v8::Handle<v8::Value> WrapFile(const v8::Arguments &args) { if (This) return This->FuncFile(args); else return v8::Undefined(); }
134  static v8::Handle<v8::Value> WrapSendMail(const v8::Arguments &args) { if (This) return This->FuncSendMail(args); else return v8::Undefined(); }
135  static v8::Handle<v8::Value> WrapSendCurl(const v8::Arguments &args) { if (This) return This->FuncSendCurl(args); else return v8::Undefined(); }
136  static v8::Handle<v8::Value> WrapLog(const v8::Arguments &args) { if (This) return This->FuncLog(args); else return v8::Undefined(); }
137  static v8::Handle<v8::Value> WrapAlarm(const v8::Arguments &args) { if (This) return This->FuncAlarm(args); else return v8::Undefined(); }
138  static v8::Handle<v8::Value> WrapOut(const v8::Arguments &args) { if (This) return This->FuncOut(args); else return v8::Undefined(); }
139  static v8::Handle<v8::Value> WrapWarn(const v8::Arguments &args) { if (This) return This->FuncWarn(args); else return v8::Undefined(); }
140  static v8::Handle<v8::Value> WrapWait(const v8::Arguments &args) { if (This) return This->FuncWait(args); else return v8::Undefined(); }
141  static v8::Handle<v8::Value> WrapSend(const v8::Arguments &args) { if (This) return This->FuncSend(args); else return v8::Undefined(); }
142  static v8::Handle<v8::Value> WrapSleep(const v8::Arguments &args) { if (This) return This->FuncSleep(args); else return v8::Undefined(); }
143  static v8::Handle<v8::Value> WrapTimeout(const v8::Arguments &args) { if (This) return This->FuncTimeout(args); else return v8::Undefined(); }
144  static v8::Handle<v8::Value> WrapThread(const v8::Arguments &args) { if (This) return This->FuncThread(args); else return v8::Undefined(); }
145  static v8::Handle<v8::Value> WrapKill(const v8::Arguments &args) { if (This) return This->FuncKill(args); else return v8::Undefined(); }
146  static v8::Handle<v8::Value> WrapExit(const v8::Arguments &args) { if (This) return This->FuncExit(args); else return v8::Undefined(); }
147  static v8::Handle<v8::Value> WrapState(const v8::Arguments &args) { if (This) return This->FuncState(args); else return v8::Undefined(); }
148  static v8::Handle<v8::Value> WrapNewState(const v8::Arguments &args) { if (This) return This->FuncNewState(args); else return v8::Undefined(); }
149  static v8::Handle<v8::Value> WrapSetState(const v8::Arguments &args) { if (This) return This->FuncSetState(args); else return v8::Undefined(); }
150  static v8::Handle<v8::Value> WrapGetState(const v8::Arguments &args) { if (This) return This->FuncGetState(args); else return v8::Undefined(); }
151  static v8::Handle<v8::Value> WrapGetStates(const v8::Arguments &args){ if (This) return This->FuncGetStates(args);else return v8::Undefined(); }
152  static v8::Handle<v8::Value> WrapGetDescription(const v8::Arguments &args){ if (This) return This->FuncGetDescription(args);else return v8::Undefined(); }
153  static v8::Handle<v8::Value> WrapGetServices(const v8::Arguments &args){ if (This) return This->FuncGetServices(args);else return v8::Undefined(); }
154  static v8::Handle<v8::Value> WrapSetInterrupt(const v8::Arguments &args){ if (This) return This->FuncSetInterrupt(args);else return v8::Undefined(); }
155  static v8::Handle<v8::Value> WrapTriggerInterrupt(const v8::Arguments &args){ if (This) return This->FuncTriggerInterrupt(args);else return v8::Undefined(); }
156  //static v8::Handle<v8::Value> WrapOpen(const v8::Arguments &args) { if (This) return This->FuncOpen(args); else return v8::Undefined(); }
157  static v8::Handle<v8::Value> WrapSubscription(const v8::Arguments &args){ if (This) return This->FuncSubscription(args);else return v8::Undefined(); }
158  static v8::Handle<v8::Value> WrapGetData(const v8::Arguments &args) { if (This) return This->FuncGetData(args); else return v8::Undefined(); }
159  static v8::Handle<v8::Value> WrapClose(const v8::Arguments &args) { if (This) return This->FuncClose(args); else return v8::Undefined(); }
160  static v8::Handle<v8::Value> WrapQuery(const v8::Arguments &args) { if (This) return This->FuncQuery(args); else return v8::Undefined(); }
161  static v8::Handle<v8::Value> WrapDatabase(const v8::Arguments &args) { if (This) return This->FuncDatabase(args); else return v8::Undefined(); }
162  static v8::Handle<v8::Value> WrapDbQuery(const v8::Arguments &args) { if (This) return This->FuncDbQuery(args); else return v8::Undefined(); }
163  static v8::Handle<v8::Value> WrapDbClose(const v8::Arguments &args) { if (This) return This->FuncDbClose(args); else return v8::Undefined(); }
164  static v8::Handle<v8::Value> WrapOnChangeSet(v8::Local<v8::String> prop, v8::Local<v8::Value> value, const v8::AccessorInfo &info)
165  {
166  if (This) return This->OnChangeSet(prop, value, info); else return v8::Undefined();
167  }
168 
169  static v8::Handle<v8::Value> OnChangeGet(v8::Local<v8::String>, const v8::AccessorInfo &)
170  {
171  return v8::Handle<v8::Value>();
172  }
173 
174  static v8::Handle<v8::Value> Convert(char type, const char* &ptr);
175  v8::Handle<v8::Value> ConvertEvent(const EventImp *evt, uint64_t, const char *str);
176 #endif
177 
178  v8::Handle<v8::Value> HandleInterruptImp(std::string, uint64_t);
179 
180 public:
181  InterpreterV8();
182  virtual ~InterpreterV8()
183  {
184  This = 0;
185 
186 #ifdef HAVE_V8
187  //v8::Locker locker;
188  v8::V8::Dispose();
189 #endif
190  }
191 
192  std::vector<std::string> JsGetCommandList(const char *, int) const;
193 
194  virtual void JsLoad(const std::string & = "");
195  virtual void JsStart(const std::string &) { }
196  virtual void JsEnd(const std::string & = "");
197  virtual void JsPrint(const std::string & = "") { }
198  virtual void JsAlarm(const std::string & = "") { }
199  virtual void JsOut(const std::string &) { }
200  virtual void JsWarn(const std::string &) { }
201  virtual void JsResult(const std::string &) { }
202  virtual void JsException(const std::string &) { }
203  virtual bool JsSend(const std::string &) { return true; }
204  //virtual void JsSleep(uint32_t) { }
205  //virtual int JsWait(const std::string &, int32_t, uint32_t) { return -1; };
206  virtual State JsState(const std::string &) { return State(); };
207  virtual void *JsSubscribe(const std::string &) { return 0; };
208  virtual bool JsUnsubscribe(const std::string &) { return false; };
209 
210  virtual bool JsNewState(int, const std::string&, const std::string&) { return false; }
211  virtual bool JsSetState(int) { return false; }
212  virtual bool JsHasState(int) const { return false; }
213  virtual bool JsHasState(const std::string &) const { return false; }
214  virtual int JsGetState(const std::string &) const { return -2; }
215  virtual State JsGetCurrentState() const { return State(); }
216  virtual std::vector<State> JsGetStates(const std::string &) { return std::vector<State>(); }
217  virtual std::set<Service> JsGetServices() { return std::set<Service>(); }
218  virtual std::vector<Description> JsGetDescription(const std::string &) { return std::vector<Description>(); }
219 
220  virtual std::vector<Description> JsDescription(const std::string &) { return std::vector<Description>(); };
221  virtual std::pair<uint64_t, EventImp *> JsGetEvent(const std::string &) { return std::make_pair(0, (EventImp*)0); };
222 
223  int JsHandleInterrupt(const EventImp &);
224  void JsHandleEvent(const EventImp &, uint64_t, const std::string &);
225  void JsHandleState(const std::string &, const State &);
226 
227  void AddFormatToGlobal();
228 
229  bool JsRun(const std::string &, const std::map<std::string,std::string> & = std::map<std::string,std::string>());
230  static void JsStop();
231 };
232 
233 #ifndef HAVE_V8
234 inline bool InterpreterV8::JsRun(const std::string &, const std::map<std::string,std::string> &) { return false; }
235 inline void InterpreterV8::JsStop() { }
236 #endif
237 
238 #endif
virtual bool JsSetState(int)
void JsHandleState(const std::string &, const State &)
static v8::Handle< v8::FunctionTemplate > fTemplateEvent
Definition: InterpreterV8.h:54
virtual std::vector< Description > JsDescription(const std::string &)
std::vector< std::pair< int, std::string > > fStates
Definition: InterpreterV8.h:47
virtual std::set< Service > JsGetServices()
A general base-class describing events issues in a state machine.
Definition: EventImp.h:11
virtual void JsStart(const std::string &)
virtual int JsGetState(const std::string &) const
char str[80]
Definition: test_client.c:7
v8::Persistent< v8::Object > fInterruptCallback
Definition: InterpreterV8.h:50
static v8::Handle< v8::FunctionTemplate > fTemplateDescription
Definition: InterpreterV8.h:55
virtual bool JsSend(const std::string &)
virtual void JsOut(const std::string &)
virtual void JsEnd(const std::string &="")
virtual std::pair< uint64_t, EventImp * > JsGetEvent(const std::string &)
void AddFormatToGlobal()
void JsHandleEvent(const EventImp &, uint64_t, const std::string &)
std::map< std::string, v8::Persistent< v8::Object > > fReverseMap
Definition: InterpreterV8.h:38
virtual bool JsHasState(int) const
virtual std::vector< Description > JsGetDescription(const std::string &)
virtual std::vector< State > JsGetStates(const std::string &)
virtual bool JsHasState(const std::string &) const
virtual void JsException(const std::string &)
virtual bool JsUnsubscribe(const std::string &)
int JsHandleInterrupt(const EventImp &)
static v8::Handle< v8::FunctionTemplate > fTemplateLocal
Definition: InterpreterV8.h:52
int type
static v8::Handle< v8::FunctionTemplate > fTemplateSky
Definition: InterpreterV8.h:53
virtual void JsWarn(const std::string &)
std::set< int > fThreadIds
Definition: InterpreterV8.h:32
static void JsStop()
std::vector< std::string > JsGetCommandList(const char *, int) const
virtual void JsAlarm(const std::string &="")
virtual bool JsNewState(int, const std::string &, const std::string &)
bool JsRun(const std::string &, const std::map< std::string, std::string > &=std::map< std::string, std::string >())
void Thread(MainImp *io_service, bool dummy, int &rc)
Definition: Main.h:79
virtual void JsPrint(const std::string &="")
virtual void JsLoad(const std::string &="")
std::map< std::string, v8::Persistent< v8::Object > > fStateCallbacks
Definition: InterpreterV8.h:41
virtual State JsGetCurrentState() const
v8::Handle< v8::Value > HandleInterruptImp(std::string, uint64_t)
virtual void * JsSubscribe(const std::string &)
virtual void JsResult(const std::string &)
virtual ~InterpreterV8()
std::vector< std::thread > fThreads
Definition: InterpreterV8.h:44
static InterpreterV8 * This
Definition: InterpreterV8.h:27
virtual State JsState(const std::string &)