cameo-nomad-addon.cc 15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <node.h>
#include <iostream>
#include <unistd.h>
#include <nomadaccessor.h>
#include <uv.h>
#include <thread>
#include <sstream>
#include <boost/bind.hpp>

using namespace std;
using namespace nomad;

namespace nomad {

using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Number;
using v8::Integer;
using v8::Boolean;
using v8::Array;
using v8::Persistent;

NomadAccessor accessor;
Isolate * v8Isolate;

/**
 * Init function to initialise the Cameo Nomad addon.
 */
void Init(const FunctionCallbackInfo<Value>& args) {

	// Get the V8 isolate.
	v8Isolate = args.GetIsolate();

	// Get the args.
	v8::String::Utf8Value param1(args[0]->ToString());
	string electronArgs(*param1);

	// Parse the args.
	size_t pos = electronArgs.find_last_of(',') + 1;
	string lastArg = electronArgs.substr(pos, electronArgs.length() - pos);

	// We only need the last argument that we convert to an array.
	char *argv[1] = {(char *)lastArg.c_str()};

	// Initialise the Nomad accessor.
	accessor.init(1, argv);
	accessor.connectNomadServer("tcp://localhost:7000");

	args.GetReturnValue().Set(Undefined(v8Isolate));
}

/**
 * Terminate function to terminate the Cameo Nomad addon.
 */
void Terminate(const FunctionCallbackInfo<Value>& args) {
	accessor.terminate();
}

/**
 * Gets the property id.
 */
void GetPropertyId(const FunctionCallbackInfo<Value>& args) {

	v8::String::Utf8Value param0(args[0]->ToString());
	std::string servantName(*param0);

	v8::String::Utf8Value param1(args[1]->ToString());
	std::string propertyName(*param1);

	args.GetReturnValue().Set(Number::New(args.GetIsolate(), accessor.getPropertyId(servantName, propertyName)));
}

/**
 * Gets the float64 property value.
 */
void GetFloat64Property(const FunctionCallbackInfo<Value>& args) {
	args.GetReturnValue().Set(Number::New(args.GetIsolate(), accessor.getFloat64Value(Local<Integer>::Cast(args[0])->Value())));
}

/**
 * Gets the int32 property value.
 */
void GetInt32Property(const FunctionCallbackInfo<Value>& args) {
	args.GetReturnValue().Set(Number::New(args.GetIsolate(), accessor.getInt32Value(Local<Integer>::Cast(args[0])->Value())));
}

/**
 * Gets the boolean property value.
 */
void GetBooleanProperty(const FunctionCallbackInfo<Value>& args) {
	args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), accessor.getBooleanValue(Local<Integer>::Cast(args[0])->Value())));
}

/**
 * Gets the string property value.
 */
void GetStringProperty(const FunctionCallbackInfo<Value>& args) {

	string value = accessor.getStringValue(Local<Integer>::Cast(args[0])->Value());
	args.GetReturnValue().Set(String::NewFromUtf8(args.GetIsolate(), value.c_str()));
}

/**
 * Gets the int32 array property value.
 */
void GetInt32ArrayProperty(const FunctionCallbackInfo<Value>& args) {

113
	Local<Array> array = Array::New(args.GetIsolate(), 0);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

	vector<int32_t> arrayValue = accessor.getInt32Array(Local<Integer>::Cast(args[0])->Value());

	for (int i = 0; i < arrayValue.size(); ++i) {
		array->Set(i, Integer::New(args.GetIsolate(), arrayValue[i]));
	}

	args.GetReturnValue().Set(array);
}

/**
 * Gets the float64 array property value.
 */
void GetFloat64ArrayProperty(const FunctionCallbackInfo<Value>& args) {

129
	Local<Array> array = Array::New(args.GetIsolate(), 0);
130
131
132
133

	vector<double> arrayValue = accessor.getFloat64Array(Local<Integer>::Cast(args[0])->Value());

	for (int i = 0; i < arrayValue.size(); ++i) {
134
		array->Set(i, Number::New(args.GetIsolate(), arrayValue[i]));
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
	}

	args.GetReturnValue().Set(array);
}

/**
 * Sets the float64 property value.
 */
void SetFloat64Property(const FunctionCallbackInfo<Value>& args) {
	args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), accessor.setFloat64Value(Local<Integer>::Cast(args[0])->Value(), Local<Number>::Cast(args[1])->Value())));
}

/**
 * Sets the int32 property value.
 */
void SetInt32Property(const FunctionCallbackInfo<Value>& args) {
	args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), accessor.setInt32Value(Local<Integer>::Cast(args[0])->Value(), Local<Integer>::Cast(args[1])->Value())));
}

/**
 * Sets the boolean property value.
 */
void SetBooleanProperty(const FunctionCallbackInfo<Value>& args) {
	args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), accessor.setBooleanValue(Local<Integer>::Cast(args[0])->Value(), Local<Boolean>::Cast(args[1])->Value())));
}

/**
 * Sets the string property value.
 */
void SetStringProperty(const FunctionCallbackInfo<Value>& args) {
	v8::String::Utf8Value param1(args[1]->ToString());
	std::string value(*param1);
	args.GetReturnValue().Set(Boolean::New(args.GetIsolate(), accessor.setStringValue(Local<Integer>::Cast(args[0])->Value(), value)));
}

/**
 * Work structure is be used to pass the callback function and data
 * from the initiating function to the function which triggers the callback.
 */
template<typename Type>
struct Work {
	uv_work_t request;
	Persistent<Function> callback;
	Type result;
};

static void WorkAsync(uv_work_t *req) {
	// Do nothing.
}

/**
 * WorkAsyncComplete function is called once we are ready to trigger the callback
 * function in JS.
 */
template<typename Type, typename JSType>
static void WorkAsyncComplete(uv_work_t *req, int status) {

	Isolate * isolate = Isolate::GetCurrent();

	v8::HandleScope handleScope(isolate);

	Work<Type> *work = static_cast<Work<Type> *>(req->data);

	// Get the result and pass it to the JS callback.
	Local<Value> argv[1] = {JSType::New(isolate, work->result)};

	// https://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8/28554065#28554065
	Local<Function>::New(isolate, work->callback)->Call(isolate->GetCurrentContext()->Global(), 1, argv);

	work->callback.Reset();
	delete work;
}

/**
 * StringWorkAsyncComplete function is called once we are ready to trigger the callback
 * function in JS.
 */
static void StringWorkAsyncComplete(uv_work_t *req, int status) {

	Isolate * isolate = Isolate::GetCurrent();

	v8::HandleScope handleScope(isolate);

	Work<string> *work = static_cast<Work<string> *>(req->data);

	// Get the result and pass it to the JS callback.
	Local<Value> argv[1] = {String::NewFromUtf8(isolate, work->result.c_str())};

	// https://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8/28554065#28554065
	Local<Function>::New(isolate, work->callback)->Call(isolate->GetCurrentContext()->Global(), 1, argv);

	work->callback.Reset();
	delete work;
}

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
 * Float64ArrayWorkAsyncComplete function is called once we are ready to trigger the callback
 * function in JS.
 */
static void Float64ArrayWorkAsyncComplete(uv_work_t *req, int status) {

	Isolate * isolate = Isolate::GetCurrent();

	v8::HandleScope handleScope(isolate);

	Work<vector<double> > *work = static_cast<Work<vector<double> > *>(req->data);

	// Get the result and pass it to the JS callback.
	Local<Array> array = Array::New(isolate, 0);

	const vector<double>& arrayValue = work->result;

	for (int i = 0; i < arrayValue.size(); ++i) {
		array->Set(i, Number::New(isolate, arrayValue[i]));
	}

	Local<Value> argv[1] = {array};

	// https://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8/28554065#28554065
	Local<Function>::New(isolate, work->callback)->Call(isolate->GetCurrentContext()->Global(), 1, argv);

	work->callback.Reset();
	delete work;
}

/**
 * Int32ArrayWorkAsyncComplete function is called once we are ready to trigger the callback
 * function in JS.
 */
static void Int32ArrayWorkAsyncComplete(uv_work_t *req, int status) {

	Isolate * isolate = Isolate::GetCurrent();

	v8::HandleScope handleScope(isolate);

	Work<vector<int32_t> > *work = static_cast<Work<vector<int32_t> > *>(req->data);

	// Get the result and pass it to the JS callback.
	Local<Array> array = Array::New(isolate, 0);

	const vector<int32_t>& arrayValue = work->result;

	for (int i = 0; i < arrayValue.size(); ++i) {
		array->Set(i, Integer::New(isolate, arrayValue[i]));
	}

	Local<Value> argv[1] = {array};

	// https://stackoverflow.com/questions/13826803/calling-javascript-function-from-a-c-callback-in-v8/28554065#28554065
	Local<Function>::New(isolate, work->callback)->Call(isolate->GetCurrentContext()->Global(), 1, argv);

	work->callback.Reset();
	delete work;
}

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
template<typename Type, typename JSType>
void PropertyChanged(Persistent<Function>* callback, Type value) {

	Work<Type> * work = new Work<Type>();

	work->request.data = work;
	work->callback.Reset(v8Isolate, *callback);
	work->result = value;

	uv_queue_work(uv_default_loop(), &work->request, WorkAsync, WorkAsyncComplete<Type, JSType>);
}

void StringPropertyChanged(Persistent<Function>* callback, const std::string& value) {

	Work<string> * work = new Work<string>();

	work->request.data = work;
	work->callback.Reset(v8Isolate, *callback);
	work->result = value;

	uv_queue_work(uv_default_loop(), &work->request, WorkAsync, StringWorkAsyncComplete);
}

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
void Float64ArrayPropertyChanged(Persistent<Function>* callback, const std::vector<double>& value) {

	Work<std::vector<double> > * work = new Work<std::vector<double> >();

	work->request.data = work;
	work->callback.Reset(v8Isolate, *callback);
	work->result = value;

	uv_queue_work(uv_default_loop(), &work->request, WorkAsync, Float64ArrayWorkAsyncComplete);
}

void Int32ArrayPropertyChanged(Persistent<Function>* callback, const std::vector<int32_t>& value) {

	Work<std::vector<int32_t> > * work = new Work<std::vector<int32_t> >();

	work->request.data = work;
	work->callback.Reset(v8Isolate, *callback);
	work->result = value;

	uv_queue_work(uv_default_loop(), &work->request, WorkAsync, Int32ArrayWorkAsyncComplete);
}

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
void RegisterFloat64PropertyChanged(const FunctionCallbackInfo<Value>& args) {

	Isolate * isolate = args.GetIsolate();

	int propertyId = Local<Integer>::Cast(args[0])->Value();
	Local<Function> callback = Local<Function>::Cast(args[1]);

	// The persistent function callback is allocated (should not) to allow to use it through the boost function parameter.
	// That could lead to a memory leak.
	Persistent<Function>* pCallback = new Persistent<Function>();
	pCallback->Reset(isolate, callback);
	accessor.registerFloat64PropertyChanged(propertyId, boost::bind(&PropertyChanged<double, Number>, pCallback, _1));

	args.GetReturnValue().Set(Undefined(isolate));
}

void RegisterInt32PropertyChanged(const FunctionCallbackInfo<Value>& args) {

	Isolate * isolate = args.GetIsolate();

	int propertyId = Local<Integer>::Cast(args[0])->Value();
	Local<Function> callback = Local<Function>::Cast(args[1]);

	// The persistent function callback is allocated (should not) to allow to use it through the boost function parameter.
	// That could lead to a memory leak.
	Persistent<Function>* pCallback = new Persistent<Function>();
	pCallback->Reset(isolate, callback);
	accessor.registerInt32PropertyChanged(propertyId, boost::bind(&PropertyChanged<int32_t, Integer>, pCallback, _1));

	args.GetReturnValue().Set(Undefined(isolate));
}

void RegisterBooleanPropertyChanged(const FunctionCallbackInfo<Value>& args) {

	Isolate * isolate = args.GetIsolate();

	int propertyId = Local<Integer>::Cast(args[0])->Value();
	Local<Function> callback = Local<Function>::Cast(args[1]);

	// The persistent function callback is allocated (should not) to allow to use it through the boost function parameter.
	// That could lead to a memory leak.
	Persistent<Function>* pCallback = new Persistent<Function>();
	pCallback->Reset(isolate, callback);
	accessor.registerBooleanPropertyChanged(propertyId, boost::bind(&PropertyChanged<bool, Boolean>, pCallback, _1));

	args.GetReturnValue().Set(Undefined(isolate));
}

void RegisterStringPropertyChanged(const FunctionCallbackInfo<Value>& args) {

	Isolate * isolate = args.GetIsolate();

	int propertyId = Local<Integer>::Cast(args[0])->Value();
	Local<Function> callback = Local<Function>::Cast(args[1]);

	// The persistent function callback is allocated (should not) to allow to use it through the boost function parameter.
	// That could lead to a memory leak.
	Persistent<Function>* pCallback = new Persistent<Function>();
	pCallback->Reset(isolate, callback);
	accessor.registerStringPropertyChanged(propertyId, boost::bind(&StringPropertyChanged, pCallback, _1));

	args.GetReturnValue().Set(Undefined(isolate));
}

399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
void RegisterFloat64ArrayPropertyChanged(const FunctionCallbackInfo<Value>& args) {

	Isolate * isolate = args.GetIsolate();

	int propertyId = Local<Integer>::Cast(args[0])->Value();
	Local<Function> callback = Local<Function>::Cast(args[1]);

	// The persistent function callback is allocated (should not) to allow to use it through the boost function parameter.
	// That could lead to a memory leak.
	Persistent<Function>* pCallback = new Persistent<Function>();
	pCallback->Reset(isolate, callback);
	accessor.registerFloat64ArrayPropertyChanged(propertyId, boost::bind(&Float64ArrayPropertyChanged, pCallback, _1));

	args.GetReturnValue().Set(Undefined(isolate));
}

void RegisterInt32ArrayPropertyChanged(const FunctionCallbackInfo<Value>& args) {

	Isolate * isolate = args.GetIsolate();

	int propertyId = Local<Integer>::Cast(args[0])->Value();
	Local<Function> callback = Local<Function>::Cast(args[1]);

	// The persistent function callback is allocated (should not) to allow to use it through the boost function parameter.
	// That could lead to a memory leak.
	Persistent<Function>* pCallback = new Persistent<Function>();
	pCallback->Reset(isolate, callback);
	accessor.registerInt32ArrayPropertyChanged(propertyId, boost::bind(&Int32ArrayPropertyChanged, pCallback, _1));

	args.GetReturnValue().Set(Undefined(isolate));
}

431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
 * The init function declares what we will make visible to node.
 */
void init(Local<Object> exports) {

	// Register the functions.
	NODE_SET_METHOD(exports, "init", Init);
	NODE_SET_METHOD(exports, "terminate", Terminate);
	NODE_SET_METHOD(exports, "getPropertyId", GetPropertyId);
	NODE_SET_METHOD(exports, "getFloat64Property", GetFloat64Property);
	NODE_SET_METHOD(exports, "getInt32Property", GetInt32Property);
	NODE_SET_METHOD(exports, "getBooleanProperty", GetBooleanProperty);
	NODE_SET_METHOD(exports, "getStringProperty", GetStringProperty);
	NODE_SET_METHOD(exports, "getInt32ArrayProperty", GetInt32ArrayProperty);
	NODE_SET_METHOD(exports, "getFloat64ArrayProperty", GetFloat64ArrayProperty);
	NODE_SET_METHOD(exports, "setFloat64Property", SetFloat64Property);
	NODE_SET_METHOD(exports, "setInt32Property", SetInt32Property);
	NODE_SET_METHOD(exports, "setStringProperty", SetStringProperty);
	NODE_SET_METHOD(exports, "setBooleanProperty", SetBooleanProperty);
	NODE_SET_METHOD(exports, "registerFloat64PropertyChanged", RegisterFloat64PropertyChanged);
	NODE_SET_METHOD(exports, "registerInt32PropertyChanged", RegisterInt32PropertyChanged);
	NODE_SET_METHOD(exports, "registerBooleanPropertyChanged", RegisterBooleanPropertyChanged);
	NODE_SET_METHOD(exports, "registerStringPropertyChanged", RegisterStringPropertyChanged);
454
455
	NODE_SET_METHOD(exports, "registerFloat64ArrayPropertyChanged", RegisterFloat64ArrayPropertyChanged);
	NODE_SET_METHOD(exports, "registerInt32ArrayPropertyChanged", RegisterInt32ArrayPropertyChanged);
456
457
458
459
460
}

NODE_MODULE(cameonomadAddon, init)

}