-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvxa-node.cc
475 lines (399 loc) · 14.7 KB
/
vxa-node.cc
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <node.h>
#include <v8.h>
#include "Vk.h"
#include "V_VQueue.h"
#include "Vision_Evaluation_Gofer.h"
#include "va_node_callback.h"
#include "va_node_export.h"
#include "va_node_handle_scope.h"
#include "Vca_VcaGofer.h"
#include "Vca_IDirectory.h"
#include "Vca_IPipeFactory.h"
#include "Vca_VServerApplication.h"
namespace VA {
using V::VString;
using v8::FunctionCallbackInfo;
using v8::Value;
/*********************
*---- Helpers ----*
*********************/
namespace VE = Vision::Evaluation;
namespace VN = VA::Node;
char const *ValueOrElse (char const *pName, char const *pElse) {
char const *pValue = getenv (pName);
return pValue ? pValue : pElse;
}
VE::evaluator_gofer_t *EvaluatorGoferFor (VString const &rEvaluatorName) {
VString iProtocol, iEvaluatorName;
bool const bUsingNet = rEvaluatorName.getPrefix (
':', iProtocol, iEvaluatorName
) && iProtocol.equalsIgnoreCase ("net");
if (bUsingNet)
return new Vca::Gofer::Named<Vsa::IEvaluator,Vca::IPipeFactory> (iEvaluatorName);
else
return new Vca::Gofer::Named<Vsa::IEvaluator,Vca::IDirectory> (iEvaluatorName);
}
VE::evaluator_gofer_t *DefaultEvaluator () {
static VE::evaluator_gofer_t::Reference const pGofer (
EvaluatorGoferFor (ValueOrElse ("NodeEvaluator", "dir:NodeEvaluator"))
);
return pGofer;
}
/**********************************
*---- class ServerContext -----*
**********************************/
class ServerContext final : public VReferenceable {
DECLARE_CONCRETE_RTTLITE (ServerContext, VReferenceable);
// Aliases
public:
typedef VkDynamicArrayOf<VString> arg_storage_t;
// Construction
public:
static ServerContext *New (
VN::Isolate *pIsolate, FunctionCallbackInfo<Value> const& args, int xFirstArg
);
ServerContext (arg_storage_t const &rArgs);
// Destruction
private:
~ServerContext () {
}
// Application Context
public:
Vca::VApplicationContext *applicationContext () const;
// State
private:
arg_storage_t m_aArgs;
argv_t m_pArgs;
};
/****************/
ServerContext *ServerContext::New (
VN::Isolate *pIsolate, FunctionCallbackInfo<Value> const& args, int xFirstArg
) {
int const cArgs = args.Length () > xFirstArg ? args.Length () - xFirstArg : 0;
ServerContext::arg_storage_t aServerArgs (cArgs + 1);
aServerArgs[0] = "-node-";
for (int xArg = 0; xArg < cArgs; xArg++) {
if (!pIsolate->UnwrapString (aServerArgs[xArg + 1], args[xArg + xFirstArg])) {
VString iMessage;
iMessage << "Invalid Argument: " << xArg;
pIsolate->ThrowTypeError (iMessage);
}
}
return new ServerContext (aServerArgs);
}
ServerContext::ServerContext (
arg_storage_t const &rArgs
) : m_aArgs (rArgs), m_pArgs (new char* [rArgs.elementCount ()]) {
for (unsigned int xArg = 0; xArg < m_aArgs.elementCount (); xArg++) {
m_pArgs[xArg] = m_aArgs[xArg].storage ();
}
}
/****************/
Vca::VApplicationContext *ServerContext::applicationContext () const {
return new Vca::VApplicationContext (m_aArgs.elementCount (), m_pArgs);
}
/**************************
*---- class Server ----*
**************************/
class Server final : public Vca::VServerApplication {
DECLARE_CONCRETE_RTTLITE (Server, VServerApplication);
// StopCallback
public:
class StopCallback final : public VN::Callback {
DECLARE_CONCRETE_RTTLITE (StopCallback, VN::Callback);
// Construction
public:
StopCallback (
VN::Export *pCallback, VString const &rStateName
) : m_pCallback (pCallback), m_iStateName (rStateName) {
retain (); {
trigger ();
} untain ();
}
private:
~StopCallback () {
}
// Execution
private:
void run () override {
VN::HandleScope iHS (m_pCallback);
VN::local_value_t hResult;
node::async_context aContext = {0,0};
m_pCallback->Call (hResult, aContext, m_pCallback->NewObject (), m_iStateName);
}
// State
private:
VN::Export::Reference const m_pCallback;
VString const m_iStateName;
};
// Offer
public:
static void Offer1(FunctionCallbackInfo<Value> const& args);
static void Offer2(FunctionCallbackInfo<Value> const& args);
// Construction
private:
Server (
ServerContext *pContext,
Vxa::export_return_t const &rExport,
VN::Export *pStopCallback = 0
);
// Destruction
private:
~Server ();
// Control
private:
virtual bool start_() override;
virtual bool stop_(bool bHardStop) override;
// State
private:
ServerContext::Reference const m_pServerContext;
VN::Export::Reference const m_pStopCallback;
};
/****************/
Server::Server (
ServerContext *pContext, Vxa::export_return_t const &rExport, VN::Export *pStopCallback
) : BaseClass (pContext->applicationContext ()), m_pServerContext (
pContext
), m_pStopCallback (pStopCallback) {
aggregate (rExport);
}
/****************/
Server::~Server () {
}
/****************/
bool Server::start_() {
return BaseClass::start_() && offerSelf () && isStarting ();
}
bool Server::stop_(bool bHardStop) {
BaseClass::stop_(bHardStop);
if (m_pStopCallback)
(new StopCallback (m_pStopCallback, runStateName ()))->discard ();
return isStopping (bHardStop);
}
/*************************************************************************
*----- Arguments:
*
* arg[0]: offered object
* arg[1+]: server command options
*
*************************************************************************/
void Server::Offer1(FunctionCallbackInfo<Value> const& args) {
static const int xa_ExportedObject = 0;
static const int xa_ServerOptions = 1;
Vca::VCohortClaim cohortClaim;
VN::Isolate::Reference pIsolate;
VN::Isolate::GetInstance (pIsolate, args.GetIsolate());
// Access the server export...
if (args.Length () <= xa_ExportedObject) {
pIsolate->ThrowTypeError ("Missing Object");
return;
}
Vxa::export_return_t pExport;
pIsolate->GetExport (pExport, args[0]);
// ... and start the server:
Server::Reference pServer (
new Server (
ServerContext::New (pIsolate, args, xa_ServerOptions), pExport
)
);
args.GetReturnValue ().Set (pServer->start ());
}
/*************************************************************************
*----- Arguments:
*
* arg[0]: on stop callback
* arg[1]: offered object
* arg[2+]: server command options
*
*************************************************************************/
void Server::Offer2(FunctionCallbackInfo<Value> const& args) {
static const int xa_StopCallback = 0;
static const int xa_ExportedObject = 1;
static const int xa_ServerOptions = 2;
Vca::VCohortClaim cohortClaim;
VN::Isolate::Reference pIsolate;
VN::Isolate::GetInstance (pIsolate, args.GetIsolate());
// Access the onStop callback (required) ...
VN::Export::Reference pStopCallback;
if (args.Length () <= xa_StopCallback || !pIsolate->Attach (
pStopCallback, args[xa_StopCallback]
)
) {
pIsolate->ThrowTypeError ("Missing Stop Callback");
return;
}
// Access the server export (required) ...
if (args.Length () <= xa_ExportedObject) {
pIsolate->ThrowTypeError ("Missing Object");
return;
}
Vxa::export_return_t pExport;
pIsolate->GetExport (pExport, args[0]);
// ... and start the server:
Server::Reference pServer (
new Server (
ServerContext::New (pIsolate, args, xa_ServerOptions), pExport, pStopCallback
)
);
args.GetReturnValue ().Set (pServer->start ());
}
/*******************************
*---- ExternalEvaluator ----*
*******************************/
class ExternalEvaluator : public Vca::VRolePlayer {
DECLARE_CONCRETE_RTTLITE (ExternalEvaluator, Vca::VRolePlayer);
// Resolution
public:
class Resolution;
// Evaluate
public:
static void Evaluate(FunctionCallbackInfo<Value> const& args);
// Construction
private:
ExternalEvaluator (
VN::Export *pRejectCallback, VN::Export *pResolveCallback, VE::Gofer *pGofer
);
~ExternalEvaluator () {
}
// Resolution
private:
void onData (VE::Value iResult);
void onError (Vca::IError *pInterface, VString const &rMessage);
// State
private:
VN::Export::Reference m_pRejectCallback, m_pResolveCallback;
};
/******************************************************************/
class ExternalEvaluator::Resolution final : public VN::Callback {
DECLARE_CONCRETE_RTTLITE (Resolution, VN::Callback);
// Construction
public:
Resolution (
VN::Export *pCallback, VString const &rResult
) : m_pCallback (pCallback), m_iResult (rResult) {
retain (); {
trigger ();
} untain ();
}
private:
~Resolution () {
}
// Execution
private:
void run () override {
VN::HandleScope iHS (m_pCallback);
VN::local_value_t hResult;
node::async_context aContext = {0,0};
m_pCallback->Call (hResult, aContext, m_pCallback->NewObject (), m_iResult);
}
// State
private:
VN::Export::Reference const m_pCallback;
VString const m_iResult;
};
/******************************************************************/
ExternalEvaluator::ExternalEvaluator (
VN::Export *pRejectCallback, VN::Export *pResolveCallback, VE::Gofer *pGofer
) : m_pRejectCallback (pRejectCallback), m_pResolveCallback (pResolveCallback) {
retain (); {
pGofer->supplyMembers (this, &ThisClass::onData, &ThisClass::onError);
} untain ();
};
/******************************************************************/
void ExternalEvaluator::onData (VE::Value iResult) {
(new Resolution (m_pResolveCallback, iResult.output()))->discard ();
}
void ExternalEvaluator::onError (Vca::IError *pInterface, VString const &rMessage) {
(new Resolution (m_pRejectCallback, rMessage))->discard ();
}
/*************************************************************************
*----- Arguments:
*
* arg[0]: promise constructor's 'resolve' callback
* arg[1]: promise constructor's 'reject' callback
* arg[2]: vision expression
* arg[3]: optional client object
* arg[4]: optional evaluator name
*
*************************************************************************/
void ExternalEvaluator::Evaluate(FunctionCallbackInfo<Value> const& args) {
static const int xa_RejectCallback = 0;
static const int xa_ResolveCallback = 1;
static const int xa_VisionExpression = 2;
static const int xa_ExportedObject = 3;
static const int xa_EvaluatorName = 4;
Vca::VCohortClaim cohortClaim;
VN::Isolate::Reference pIsolate;
VN::Isolate::GetInstance (pIsolate, args.GetIsolate());
// Access the promise reject callback (required) ...
VN::Export::Reference pRejectCallback;
if (args.Length () <= xa_RejectCallback || !pIsolate->Attach (
pRejectCallback, args[xa_RejectCallback]
)
) {
pIsolate->ThrowTypeError ("Missing Reject Callback");
return;
}
// Access the promise resolve callback (required) ...
VN::Export::Reference pResolveCallback;
if (args.Length () <= xa_ResolveCallback || !pIsolate->Attach (
pResolveCallback, args[xa_ResolveCallback]
)
) {
VN::local_value_t hResult;
pRejectCallback->Call (
hResult, pIsolate->LocalUndefined (), "Missing Resolve Callback"
);
return;
}
// Access the Vision expression (required)...
VString iExpression;
if (args.Length () <= xa_VisionExpression || !pIsolate->UnwrapString (
iExpression, args[xa_VisionExpression]
)
) {
VN::local_value_t hResult;
pRejectCallback->Call (
hResult, pIsolate->LocalUndefined (), "Missing Expression"
);
return;
}
// Access the client export if supplied...
Vxa::export_return_t pExport;
if (args.Length () > xa_ExportedObject) {
pIsolate->GetExport (pExport, args[xa_ExportedObject]);
}
// Prepare the evaluation...
VE::Gofer::Reference const pGofer (
new VE::Gofer (
[&]() {
VString iEvaluatorName;
return args.Length () > xa_EvaluatorName && pIsolate->UnwrapString (
iEvaluatorName, args[xa_EvaluatorName]
) ? EvaluatorGoferFor (iEvaluatorName) : DefaultEvaluator ();
}(), iExpression, pExport
)
);
// ... and start it:
(new ExternalEvaluator (pRejectCallback, pResolveCallback, pGofer))->discard ();
}
/**************************
*---- State Access ----*
**************************/
void CachedIsolateCount (const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue ().Set (
static_cast<uint32_t>(VA::Node::Process::IsolateCacheSize ())
);
}
/**********************************
*---- Module Initialization ----*
**********************************/
void Init (VN::local_object_t exports, VN::local_object_t module) {
NODE_SET_METHOD(exports, "v" , ExternalEvaluator::Evaluate);
NODE_SET_METHOD(exports, "o" , Server::Offer1);
NODE_SET_METHOD(exports, "o1", Server::Offer1);
NODE_SET_METHOD(exports, "o2", Server::Offer2);
NODE_SET_METHOD(exports, "cachedIsolateCount", CachedIsolateCount);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Init)
} // anonymous namespace