-
Notifications
You must be signed in to change notification settings - Fork 15
/
__worker.js
908 lines (781 loc) · 26.2 KB
/
__worker.js
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
/* This file is part of AToMPM - A Tool for Multi-Paradigm Modelling
* Copyright 2011 by the AToMPM team and licensed under the LGPL
* See COPYING.lesser and README.md in the root of this project for full details
*/
/* NOTES:
because of the *asynchronous* nature of numerous operations in our system,
brought about by client requests coming to csworkers who asynchronously
forward them to asworkers that asynchronously return changelogs to subscribed
csworkers who later asynchronously return their own changelogs to subscribed
clients, CONCURRENCY CONTROL is needed to ensure weird interleavings of
operations and/or responses don't bring the system (either *worker or the
client) into incoherent or undesirable states...
our approach is inspired by TCP and Readers-Writer locks :
1. 'read' requests increment the number of readers (read requests are
either GETs or have uris that start with "/GET/")
2. 'write' requests increment the number of writers
3. locking read requests also read-lock this *worker (the only such query
is 'POST /GET/batchRead')
4. locking write requests also write-lock this *worker (there is currently
no such query... see *)
5. before processing incoming requests, onmessage() calls __canProceed()..
this method succeeds when the current configuration of readers, writers
and locks allows the incoming query to run immediately... otherwise,
(e.g., if there is more than one reader or writer and a write lock is
needed, or if the request queue is not-empty), this method fails, which
triggers the queueing of the incoming request for handling asap... this
essentially buys us a big 'synchronize' block around operations that
change the state of _mmmk (and its journal), operations which are
meant to be atomic...
6. individual requests that make up batchEdits bypass __canProceed():
these may need to write to a *worker that the initial 'POST /batchEdit'
locked... specifying a valid 'backstagePass' in the uri allows them to
skip over __canProceed()
7. other than possible delays in query handling, queriers (be they clients
or csworkers) are oblivious to the whole locking scheme... they are not
expected to ask for locks (these are granted automatically) or to
explicitly release them (locks and increments to reader/writer counts
produced by a request are appropriately 'adjusted' upon receiving a
response to the said request)
*. although one might think that 'POST /batchEdit' requests would write-
lock this *worker, they don't... instead, they read-lock this *worker
and increment the number of writers... this has 2 side-effects
a) no one can write to this *worker during a batchEdit
b) people can still read from this *worker... thus, the
'intermediate' state of the system is visible to all
this is important since effects of batchEdits may include the re-
evaluation of VisualObject mapping functions... if the asworker were to
reject such reads, the user would need to 'refresh' after each
batchEdit to sync up his icons... this all stems from the fact that we
preferred not to 'catch/queue' changelogs emitted by the asworker
during batchEdits
TBI:
perhaps the most important point to improve on here is that locking
queries could be made to lock individual objects (e.g., one or more AS
node) rather than the entire *worker... this is not necessarily difficult
to implement: before accessing any object for reading or writing in
_mmmk, check if its locked and/or lock it
the use of our verbose URIs in HTTP requests is mostly useful for debugging
and to enhance the RESTful feel of our HTTP exchanges: in most cases, IDs are
sufficient to uniquely identify and refer to nodes, and their associated URIs
can easily be computed... this fact, as well as performance concerns (e.g.,
minimizing bandwith needs and string matching) is behind our decision not to
'URIZE' changelogs sent from asworker to csworker, i.e., changelogs refer to
nodes by ID rather than by URI... although this is acceptable in the backend,
referring to nodes by ID in client-bound changelogs is not... thus, before
sending changelogs to the client, any IDs they may contain are replaced by
URIs (via __urizeChangelog())
TBI:
eventually, it may be possible to entirely strip out the sequence
numbering mechanism... this is contingent (at least) on the WebSocket
protocol and its implementations guaranteeing that messages are always
delivered in order
supporting ATOMIC UNDO/REDO OF BATCHEDITS requires that we keep track of
which operations happened as part of the same batchEdit... this can get hairy
in several cases, e.g., when:
1. batched requests are individually piped through the csworker to the
asworker who thus has no way of knowing it should remember they're
batched
2. certain requests are handled by the csworker while others are forwarded
to the asworker which prevents any of the workers of really knowing
which are the first and last requests in the batchEdit
to address this, we place easily identifiable user-checkpoints in both the
asworker and csworker journals at the start and end of any batchEdit via POST
/batchCheckpoint (in practice, these are handled by asworkers who report
setting these checkpoints via changelogs such that all subscribed csworkers
can set them as well)... given that possibly dispersed and/or delocalized
operations now all reside between identically named user-checkpoints on both
asworker and csworker, we can undo/redo them atomically on both workers by
undoing/redoing everything between the start and end checkpoints... this
special undo/redo behavior is implemented in csworker.__undoredo (see its
comments for details)
it is assumed that csworker, asworker, mmmk and libmt are only ever imported
from this file and as such inherit all of its imported libraries */
/**************************** LIBRARIES and GLOBALS ****************************/
let _http = require('http'),
_do = require('./___do'),
_fs = _do.convert(require('fs'), ['readFile', 'writeFile', 'readdir']),
_utils = require('./utils'),
logger = require('./logger.js');
let _wlib,
_mmmk,
_mt,
_plugins,
__wtype;
//have worker id global so that workers can detect it when loaded
global.__wid = null;
let keepaliveAgent = new _http.Agent({keepAlive: true, maxSockets: 10, maxFreeSockets: 5}); // proposed by yentl to improve performance
/*********************************** UTILS ************************************/
/***************************** BASIC CONTINUABLES *****************************/
/* return a failure continuable */
function __errorContinuable(err)
{
return function(callback,errback) {errback(err);};
}
/* return a success continuable */
function __successContinuable(arg)
{
return function(callback,errback) {callback(arg);};
}
/******************************* HTTP REQUESTS ********************************/
/* make an HTTP request to 127.0.0.1:port */
function __httpReq(method,url,data,port)
{
if( port === undefined )
port = 8124;
return function(callback,errback)
{
let options = {'port': port, 'host': '127.0.0.1', 'path': url, 'method': method, 'agent': keepaliveAgent}; // agent proposed by yentl to improve performance
if( data !== undefined )
{
data = _utils.jsons(data);
options['headers'] = {'Content-Length':unescape(encodeURIComponent(data)).length,
'Access-Control-Allow-Origin': '*'};
}
let uri_s = url.split("?");
let uri = uri_s.length == 1? uri_s[0] : uri_s[0] + '<br/>' + uri_s[1];
logger.http("http _ request " + "<br/>" + method + " " + uri,{'from':__wtype+__wid, 'to':'server'});
let request =
_http.request(options,
function(resp)
{
let resp_data = '';
resp.on('data', function(chunk) {
resp_data += chunk;});
resp.on('end',
function()
{
if( _utils.isHttpSuccessCode(resp.statusCode) )
callback(resp_data);
else
errback(resp.statusCode+':'+resp_data);
});
});
request.on('error',
function(err)
{
logger.http("http _ error <br/> " +url+ "<br/>"+err,{'at':__wtype+__wid});
errback('HTTP request ('+method+' '+url+':'+port+') '+
'failed on ::\n'+err);
});
request.end(data);
};
}
/* make an http request to a *worker... this is basically just a wrapper than
takes into account the fact that *workers respond data in respData.data and
sometimes include sequence numbers in respData.sequence# */
function __wHttpReq(method,url,data,port)
{
return function(callback,errback)
{
__httpReq(method,url,data,port)(
function(respData)
{
respData = eval('('+respData+')');
respData =
(respData == undefined ||
respData['sequence#'] != undefined ?
respData :
respData.data);
callback(respData);
},
function(respData) {errback(respData);}
);
};
}
/******************************* URI PROCESSING *******************************/
/* optimize __id_to_uri() by remembering computed mappings */
let __ids2uris = {};
/* try to construct a uri from an instance id */
function __id_to_uri(id)
{
if (id == undefined)
return undefined;
else if (id in __ids2uris)
return __ids2uris[id];
let res = _mmmk.read(id);
if (res['$err'])
return res;
let uri = _utils.jsonp(res)['$type'] + '/' + id + '.instance';
__ids2uris[id] = uri;
return uri;
}
/* try to extract an instance id from a uri */
function __uri_to_id(uri)
{
let matches = uri.match(/.*\/(.*).instance/);
if( matches != null )
return matches[1];
return {'$err':'bad instance uri :: '+uri};
}
/* replace ids in the given changelog by corresponding uris... see above NOTES
on IDs vs. URIs for more on this
NOTE:: when RESETM steps are encountered, we additionaly flush all currently
remembered id-to-uri mappings */
function __urizeChangelog(chlog)
{
chlog.forEach(
function(step)
{
if( step['op'] == 'RESETM' )
{
__ids2uris = {};
let newModel = _utils.jsonp(step['new_model']);
for( let id in newModel.nodes )
{
newModel.nodes[__id_to_uri(id)] = newModel.nodes[id];
delete newModel.nodes[id];
}
step['new_model'] = _utils.jsons(newModel);
}
else
['id','id1','id2'].forEach(
function(x)
{
if( x in step )
step[x] = __id_to_uri(step[x]);
});
});
}
/****************************** POST MESSAGE... *******************************/
/* wrapper for : 400 Bad Request Syntax */
function __postBadReqErrorMsg(respIndex,reason)
{
__postErrorMessage(respIndex,400,reason);
}
/* wrapper for all error messages */
function __postErrorMessage(respIndex,statusCode,reason)
{
__postMessage(
{'statusCode':statusCode,
'reason':reason,
'respIndex':respIndex});
}
/* wrapper for : 403 Forbidden */
function __postForbiddenErrorMsg(respIndex,reason)
{
__postErrorMessage(respIndex,403,reason);
}
/* wrapper for : 500 Internal Server Error */
function __postInternalErrorMsg(respIndex,reason)
{
__postErrorMessage(respIndex,500,reason);
}
/* wrapper for all messages */
function __postMessage(msg)
{
//make sure that reason is a string
if (typeof msg.reason == 'object'){
msg.reason = _utils.jsons(msg.reason);
}
if( 'respIndex' in msg )
__onRequestResponse(msg.respIndex);
if( __wtype == '/csworker' && 'changelog' in msg )
__urizeChangelog(msg['changelog']);
let s = "process _ RI" + msg.respIndex + "<br/>changelog: ";
for (let ch of _utils.collapse_changelog(msg['changelog'])){
s += JSON.stringify(ch) + "<br/>"
}
logger.http(s,{'from':__wtype+__wid, 'to': "session_mngr", 'type': "-x"});
process.send(msg);
}
/* wrapper for : 501 Not Implemented */
function __postUnsupportedErrorMsg(respIndex)
{
__postErrorMessage(respIndex,501);
}
/********************************** LOCKING ***********************************/
let __wLocked = false,
__rLocks = 0,
__numWriters = 0,
__numReaders = 0,
__reqs2lockInfo = {},
__reqQueue = [];
const __NO_LOCK = 0,
__LOCK = 1,
__WLOCK = __LOCK | 2,
__RLOCK = __LOCK | 4;
/* determine whether this worker can proceed with the specified request given
its current readers/writers/locks/queue... returns false if the worker can't
proceed... otherwise, grants needed locks, increments number of readers/
writers, and returns true
NOTE:: the 'ignoreQueue' parameter disables queue-emptyness as a condition
for this function's success */
function __canProceed(method,uri,respIndex,ignoreQueue)
{
function __isRead(method,uri)
{
/* returns true if request is a read */
return (method == 'GET' || uri.match(/^\/GET\//));
}
function __needsLock(method,uri)
{
/* returns lock type needed by request */
if( method == 'POST' && uri.match(/^batch/) )
return __RLOCK;
return __NO_LOCK;
}
let isReader = __isRead(method,uri);
let needsLock = __needsLock(method,uri);
/* disallow concurrent writes and queue if queue (see NOTES above) */
if( (!isReader && __numWriters > 0) ||
(!ignoreQueue && __reqQueue.length > 0) )
return false;
/* check current locks */
if( __wLocked ||
(__rLocks > 0 && !isReader) ||
(__numReaders > 0 && (needsLock & __WLOCK)) ||
(__numWriters > 0 && (needsLock & __LOCK)) )
return false;
/* access granted... */
if( needsLock & __RLOCK )
__rLocks++;
else if( needsLock & __WLOCK )
__wLocked = true;
if( isReader )
__numReaders++;
else
__numWriters++;
__reqs2lockInfo[respIndex] = {'isReader':isReader,'needsLock':needsLock};
return true;
}
/* unlock this *worker (if request had locked it), decrement number of readers/
writers, and launch queued requests, if any... ignore requests that have no
entry in __reqs2lockInfo (i.e., requests with backstage passes) */
function __onRequestResponse(respIndex)
{
let li = __reqs2lockInfo[respIndex];
if (li == undefined)
return;
if (li['needsLock'] & __RLOCK)
__rLocks = Math.max(--__rLocks, 0);
else if (li['needsLock'] & __WLOCK)
__wLocked = false;
if (li['isReader'])
__numReaders = Math.max(--__numReaders, 0);
else
__numWriters = Math.max(--__numWriters, 0);
__runQueuedRequests();
}
/* run proceedable queued requests in FIFO order until a non-proceedable request
is encountered...
NOTE:: this function doesn't wait for request responses, if all queued
responses can run concurrently (e.g., all reads), it will launch them
all s.t. they can all be handled in parallel */
function __runQueuedRequests()
{
if (__reqQueue.length > 0) {
let head = __reqQueue[0],
uri = head['uri'],
method = head['method'],
reqData = head['reqData'],
respIndex = head['respIndex'],
cid = head['cid']
;
if (__canProceed(method, uri, respIndex, true)) {
__reqQueue.shift();
__handleClientRequest(uri, method, reqData, respIndex, cid);
__runQueuedRequests();
}
}
}
/* push given request onto request queue for future handling */
function __queueRequest(uri,method,reqData,respIndex,cid)
{
__reqQueue.push(
{
'uri': uri,
'method': method,
'reqData': reqData,
'respIndex': respIndex,
'cid': cid
});
}
/****************************** SEQUENCE NUMBERS ******************************/
let __nextSequenceNumber = 0;
function __sequenceNumber(inc)
{
if( inc == undefined || inc == 1 )
inc = 1;
else if( inc != 0 )
throw '__sequenceNumber increment must be 0, 1 or undefined';
return __wtype+'#'+(__nextSequenceNumber+=inc);
}
function __batchCheckpoint(id,start)
{
return 'bchkpt@'+id+(start ? '>>' : '<<');
}
process.on('message',
function(msg)
{
let log_id = __wtype+__wid;
/* initial setup */
if( _wlib === undefined )
{
__wtype = msg['workerType'];
__wid = msg['workerId'];
//log_id = msg['workerType'] + msg['workerId'];
//logger.http("process _ worker creation", {'at': log_id});
if (__wtype === "/asworker") {
_wlib = require("./asworker");
}else if (__wtype === "/csworker") {
_wlib = require("./csworker");
}else {
throw "Error! Unknown worker type: " + __wtype;
}
_mmmk = require('./mmmk');
_mt = require('./libmt');
_plugins = {};
_fs.readdirSync('./plugins').forEach(
function(p)
{
try
{
if( ! p.match(/.*\.js$/) )
return;
//throw 'invalid plugin filename, see user\'s manual';
p = p.match(/(.*)\.js$/)[1];
_plugins[p] = require('./plugins/' + p);
if( ! ('interfaces' in _plugins[p]) ||
! ('csworker' in _plugins[p]) ||
! ('asworker' in _plugins[p]) )
throw 'invalid plugin specification, see user\'s manual';
}
catch(err)
{
logger.error('failed to load plugin ('+p+') on :: '+err);
}
});
return;
}
/* parse msg */
let uri = msg['uri'];
let method = msg['method'];
let uriData = msg['uriData'];
let reqData = msg['reqData'];
let respIndex = msg['respIndex'];
let cid = msg['cid'];
let s = "process _ message RI" + msg.respIndex + "<br/>" + method + " " + uri;
if (cid != undefined) s+= "<br/>" + cid;
logger.http(s, {'from':"session_mngr", 'to': log_id, 'type': "-->>"});
/* concurrent access control */
if( uriData != undefined && uriData['backstagePass'] != undefined )
{
if( uriData['backstagePass'] != __backstagePass )
return __postErrorMessage(respIndex,401,'invalid backstage pass');
}
else if( ! __canProceed(method,uri,respIndex) )
return __queueRequest(
uri,
method,
(method === 'GET' ? uriData : reqData),
respIndex,
cid
);
/* handle client requests
POST <> create
GET <> retrieve
PUT <> update
DELETE <> delete */
__handleClientRequest(
uri,
method,
(method == 'GET' ? uriData : reqData),
respIndex,
cid
);
});
/* handle a request described by the given parameters */
function __handleClientRequest(uri,method,reqData,respIndex,cid)
{
/********************** SHARED AS-CS WORKER BEHAVIOR ***********************/
if( method == 'GET' && uri.match(/^\/current.model$/) )
GET__current_model(respIndex);
else if( method == 'GET' && uri.match(/^\/current.state$/) )
GET__current_state(respIndex);
else if( method == 'POST' && uri.match(/^\/GET\/batchRead$/) )
POST_GET_batchread(respIndex,reqData);
else if( method == 'POST' && uri.match(/^\/batchEdit$/) )
POST_batchedit(respIndex,reqData);
/********************* DISTINCT AS-CS WORKER BEHAVIOR **********************/
else if( (method == 'DELETE' && uri.match(/\.metamodel$/)) ||
(method == 'POST' && uri.match(/\.type$/)) ||
(method == 'GET' && uri.match(/\.instance$/)) ||
(method == 'PUT' && uri.match(/\.instance$/)) ||
(method == 'DELETE' && uri.match(/\.instance$/)) ||
(method == 'PUT' && uri.match(/\.instance.cs$/)) ||
(method == 'PUT' && uri.match(/\.vobject$/)) ||
(method == 'POST' && uri.match(/^\/GET\/.*\.mappings$/)) ||
(method == 'PUT' && uri.match(/^\/GET\/.*\.metamodel$/)) ||
(method == 'PUT' && uri.match(/^\/GET\/.*\.model$/)) )
{
let func = method+' *'+uri.match(/.*(\..*)$/)[1];
if( _wlib[func] == undefined )
return __postUnsupportedErrorMsg(respIndex);
_wlib[func](respIndex,uri,reqData,cid);
}
else if( (method == 'GET' && uri.match(/^\/internal.state$/)) ||
(method == 'PUT' && uri.match(/^\/aswSubscription$/)) ||
(method == 'PUT' && uri.match(/^\/current.metamodels$/)) ||
(method == 'PUT' && uri.match(/^\/current.model$/)) ||
(method == 'GET' && uri.match(/^\/validatem$/)) ||
(method == 'POST' && uri.match(/^\/undo$/)) ||
(method == 'POST' && uri.match(/^\/redo$/)) ||
(method == 'PUT' && uri.match(/^\/GET\/console$/)) ||
(method == 'POST' && uri.match(/^\/batchCheckpoint$/)) )
{
let func = method+' '+uri;
if( _wlib[func] == undefined )
return __postUnsupportedErrorMsg(respIndex);
_wlib[func](respIndex,uri,reqData,cid);
}
else if( uri.match(/^\/__mt\/.*$/) )
_wlib.mtwRequest(respIndex,method,uri,reqData,cid);
/* plugin request */
else if( uri.match(/^\/plugins\/.*$/) )
{
let matches = uri.match(/^\/plugins\/(.*?)(\/.*)$/),
plugin = matches[1],
requrl = matches[2];
if( ! (plugin in _plugins) ||
! _plugins[plugin].interfaces.some(
function(ifc)
{
if( method == ifc.method &&
('url=' in ifc && ifc['url='] == requrl) ||
('urlm' in ifc && requrl.match(ifc['urlm'])) )
{
_plugins[plugin][__wtype.substring(1)](
respIndex,
method,
uri,
reqData,
_wlib);
return true;
}
}) )
__postUnsupportedErrorMsg(respIndex);
}
/* unsupported request */
else
__postUnsupportedErrorMsg(respIndex);
}
/************************ SHARED AS-CS WORKER BEHAVIOR ************************/
/* returns the current model to the querier
1. ask _mmmk for a copy of the current model
2. return said copy to the querier */
function GET__current_model(resp)
{
let res = _mmmk.read();
if (res['$err'])
__postInternalErrorMsg(resp, res['$err']);
else
__postMessage(
{
'statusCode': 200,
'data': res,
'sequence#': __sequenceNumber(0),
'respIndex': resp
});
}
/* returns the current 'state' of this *worker's _mmmk (i.e., its model,
loaded metamodels, current sequence#, and next expected sequence#, if any)
to the querier
1. ask _mmmk for a copy of its model, loaded metamodels and name
2. return said copies to the querier */
function GET__current_state(resp)
{
let mms = _mmmk.readMetamodels();
if (mms['$err']) {
__postInternalErrorMsg(resp, mms['$err']);
return;
}
let m = _mmmk.read();
if (m['$err']) {
__postInternalErrorMsg(resp, m['$err']);
return;
}
__postMessage(
{
'statusCode': 200,
'data': {
'mms': mms,
'm': m,
'name': _mmmk.readName(),
'asn': _wlib['__nextASWSequenceNumber'],
'asw': _wlib['__aswid']
},
'sequence#': __sequenceNumber(0),
'respIndex': resp
});
}
/* returns an array containing the results of a number of bundled read
requests */
function POST_GET_batchread(resp,reqData)
{
let actions = [__successContinuable()];
let results = [];
reqData.forEach(
function(r)
{
actions.push(
function()
{
return __wHttpReq(r['method'],r['uri']+'?wid='+__wid);
},
function(res)
{
results.push(res);
return __successContinuable();
});
});
_do.chain(actions)(
function()
{
__postMessage(
{'statusCode':200,
'data':{'results':results},
'sequence#':__sequenceNumber(0),
'respIndex':resp});
},
function(err) {__postInternalErrorMsg(resp,err);}
);
}
/* returns an array containing the results of a number of bundled edit
requests (these results are mostly just statusCodes)... if any of the
requests fail, every preceding request is undone (this is facilitated by
setting a user-checkpoint before beginning)
NOTE: requests may refer to the results of previously completed requests
in their uri and reqData : all occurrences of '$i$' are replaced by
the result of request #i
NOTE: to enable undoing/redoing batchEdits atomically, easily identifiable
user-checkpoints are set before performing any of the batched requests
and after they've all been completed... more on this in NOTES above
NOTE: nested batchEdits are not supported */
function POST_batchedit(resp,reqData)
{
for (let i in reqData)
if (reqData[i]['method'] == 'POST' && reqData[i]['uri'].match(/^\/batchEdit$/))
return __postBadReqErrorMsg('nested batchEdit requests are not supported');
let results = [];
let currtime = Date.now();
let startchkpt = __batchCheckpoint(currtime, true);
let endchkpt = __batchCheckpoint(currtime);
let setbchkpt =
function (name) {
return function () {
__backstagePass = Math.random();
return __wHttpReq(
'POST',
'/batchCheckpoint?wid=' + __wid +
'&backstagePass=' + __backstagePass,
{'name': name});
};
},
actions = [__successContinuable(), setbchkpt(startchkpt)];
reqData.forEach(
function(r)
{
actions.push(
function()
{
__backstagePass = Math.random();
let replace = function (s, p1) {
return results[p1]['data'];
};
let uri = r['uri'].replace(/\$(\d+)\$/g, replace);
if (r['reqData'] != undefined)
var reqData =
_utils.jsonp(
_utils.jsons(r['reqData']).replace(/\$(\d+)\$/g, replace));
return __wHttpReq(
r['method'],
uri + '?wid=' + __wid +
'&backstagePass=' + __backstagePass,
reqData);
},
function(res)
{
results.push(res);
return __successContinuable();
});
});
actions.push(setbchkpt(endchkpt));
_do.chain(actions)(
function()
{
__backstagePass = undefined;
__postMessage(
{'statusCode':200,
'data':{'results':results},
'sequence#':__sequenceNumber(0),
'respIndex':resp});
},
function(err)
{
let undoActions =
[__successContinuable(),
function()
{
if( results.length == 0 )
return __successContinuable();
return __wHttpReq(
'POST',
'/undo?wid='+__wid+'&backstagePass='+__backstagePass,
{'undoUntil':startchkpt,
'hitchhiker':{'undo':startchkpt}});
}];
_do.chain(undoActions)(
function()
{
__backstagePass = undefined;
__postInternalErrorMsg(resp,err);
},
function(undoErr)
{
__backstagePass = undefined;
__postInternalErrorMsg(
resp,
'unexpected error occured on rollback :: '+undoErr);
}
);
}
);
}
//required so that csworker has access to these variables
function get__ids2uris(){
return __ids2uris;
}
function set__ids2uris(new__ids2uris){
__ids2uris = new__ids2uris;
}
function get__nextSequenceNumber(){
return get__nextSequenceNumber;
}
function set__nextSequenceNumber(new__nextSequenceNumber){
__nextSequenceNumber = new__nextSequenceNumber;
}
function get__wtype(){
return __wtype;
}
module.exports = {
__errorContinuable,
__successContinuable,
__httpReq,
__wHttpReq,
__postInternalErrorMsg,
__postForbiddenErrorMsg,
__postBadReqErrorMsg,
__sequenceNumber,
__postMessage,
__uri_to_id,
__id_to_uri,
__batchCheckpoint,
GET__current_state,
//GLOBAL VARS
get__ids2uris,
set__ids2uris,
get__nextSequenceNumber,
set__nextSequenceNumber,
get__wtype,
};