-
Notifications
You must be signed in to change notification settings - Fork 15
/
csworker.js
2304 lines (2048 loc) · 101 KB
/
csworker.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
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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:
it is assumed that csworker _mmmk cud operations NEVER FAIL...
1. IMPLICATIONS
a) we don't check for failure
b) if failure were to occur, we wouldn't rollback the associated
asworker perations
c) *Icons.metamodels should NOT support actions, constraints or
multiplicities to ensure neither of these block cud operations...
they do however support parsing functions (designer code that
translates CS updates into AS operations) which are somewhat akin to
pre-edit actions: if the parsing function or the AS operations fail,
the CS update fails
2. REASONING
a) if we allowed csworker to rollback asworker operations, it would be
a natural extension for csworkers to be allowed to fail and "respond
negatively" to pushed changelogs and request asworker rollbacks...
this might cause severe user experience problems in collaboration
scenarios (e.g., A's changes could be repeatedly undone by failures
produced by B's csworker)
b) it doesn't make sense for logic that could overturn AS cud
operations to live anywhere else than in the AS spec
csworker cud operations are CHANGELOG-DRIVEN... in other words, when a client
makes PUT/POST/DELETE requests to a csworker, these are translated into
appropriate requests for an asworker...
1. on success, the asworker
a) returns a 20x status code to the csworker who made the request, in
turn, it forwards it to the client who made the request
b) returns a changelog that describes the AS impacts of the request...
this changelog is pushed to all of its csworker subscribers
(including the one who made the request) and which point their
internal CS models are adjusted to reflect AS changes... finally,
the result of these CS model modifications are themselves bundled
into changelogs that are forwarded to all subscribed clients
(subscription managment is performed in session_manager.js)
2. on failure, the asworker
a) returns a 40x|50x status code to the csworker...
so basically, when a client asks a csworker to change something, all that
csworker does is forward the request to its asworker... if and when the
csworker does comply, it will be in response to an asworker changelog
received sometime after it has responded a 20x status code to the client
'HITCHHIKERS' allow subscriber information exchange... for instance, if A
loads SC.purpleIcons.metamodel, we'd like for all csworkers subscribed to A's
associated asworker to be told (1) that their asworker has loaded
SC.metamodel *and* (2) that they should load SC.purpleIcons.metamodel... with
hitchhikers, 'subscriber-relevant' but 'worker-irrelevant' data is sent to
workers so that they may push it back to all of their subscribers upon
returning
technically, asworkers and csworkers could be run on DISTINCT MACHINES...
however, for the moment, this is neither supported nor recommended:
1. csworkers as they are now are not robust to asworker failures caused by
timeouts and/or other network problems
2. requests to asworkers would need to be prepended with the actual url of
wherever the asworker is being served from
there are at least 3 alternatives for EVALUATING A MAPPING FUNCTION
1. retrieve the full AS model and run the mapping function within this
csworker within some scope where the said model is accessible via the
designer API (ala. _mmmk.__runDesignerCode())
+ RESTful requests to asworker
- possibly very inefficient to transmit AS model
2. run the mapping function within this csworker within some scope where
calls to getAttr() are translated to REST that retrieve desired
information from the asworker
+ RESTful requests to asworker
- possibly numerous queries
3. send mapping function to asworker and run it there
- non-RESTful request
+ most efficient in terms of data traffic
we chose the 3rd approach for its efficiency... furthermore, when
regenerating icons (who may contain numerous VisualObjects with numerous
coded attributes), to avoid having to send many queries to the asworker
(i.e., one for each mappingf), we bundle together all of the icon's mappingfs
and send them to the asworker for evaluation in a single query
CREATING AN ICON AT A SPECIFIC POSITION brings up a few issues in the context
where icon position plays a role in AS attribute values... consider this
scenario:
1. user creates a BuildingIcon at (500,600)
2. request is received by csworker and forwarded to asworker
3. asworker creates a Building and sets abstract attribute 'address' to
metamodel-specified default (0,0)
4. later, __applyASWChanges() receives MKNODE
a) it creates a BuildingIcon
b) sets its 'position' to (500,600)
c) calls __regenIcon() who determines that 'position' should be (0,0)
after mapping AS attribute 'address'
d) emits changelog instructing client to create a BuildingIcon at (0,0)
the core issue here is that step 4b) is a hack... instead of 'position' being
set via 'PUT *.cs' which would have parsed 'position', appropriately updated
'address', and only later -- via AS changelog -- mapped 'address' and updated
'position', step 4b) bypasses this whole pipeline... this causes CS and AS to
be out-of-sync, which in turn causes the behaviour described for step 4c)...
to address this issue, during step 2 we perform a task similar to that of
'PUT *.cs'... step 2 from the above scenario thus becomes:
0. request is received by csworker
A. retrieve the BuildingIcon's parser
B. create a dummy context where a *Icon's parser can be run... within this
context, 'orientation' and 'scale' are set to their defaults, but
'position' is set to (500,600)
C. run the BuildingIcon's parser within this dummy context, this could
yield {'address':(5,6)}
D. forwarded creation request to asworker *and* bundle the result from
step C.
step 3 is also slightly changed: the asworker creates a new Building *and*
updates any specified attributes, in our example, 'address' would be set to
(5,6)... step 4 is left unchanged but step 4c) no longer causes any problems
because __regenIcon's mapping of 'address' will return (500,600)
supporting undo/redo requires REMEMBERING HITCHHIKERS... __applyASWChanges()
sometimes expects asworker changelogs to be bundled with hitchhikers... this
is the case when changelogs are the result of 'normal' requests getting
forwarded to the asworker by the csworker... however, for undos/redos, no
hitchhikers are bundled and as such, when __applyASWChanges() is called as a
result of undos and redos on the asworker, required information that would
normally be in hitchhikers is missing... to address this, we 'remember'
hitchhikers as we encounter them like so:
1. MKNODE hitchhikers are remembered by asid
2. LOADMM hitchhikers are remembered by asmm
3. RESETM hitchhikers are remembered by name *
* for this case, we also create and remember a hitchhiker to enable
undoing loading a model over an unsaved non-empty model
long story short, when handling asworker changelogs, missing hitchhikers, if
any, are retrieved from the __hitchhikerJournal
supporting FULL UNDO/REDO in our distributed environment presents a few
challenges
1. operations with no AS implications should be undone/redone by csworkers
2. operations with AS implications should be undone/redone by asworkers
*but* resulting changelogs should be handled specially to ensure that
csworkers undo/redo in response to asworer undo/redos
challenge 1 requires means to determine what kind of operation the client
wishes to undo/redo... we addressed this by logging handled sequence#s (via
__checkpointUserOperation()... when an undo/redo request is received, the
current sequence# to undo/redo dictates whether we're in case 1 or 2.
challenge 2 requires means to determine whether or not an asworker changelog
pertains to an undo/redo operation *and*, which csworker operations to undo/
redo in response to an asworker undo/redo... we addressed this in 3 parts
1. when DOing something in response to asworker changelogs, the csworker
sets a user-checkpoint (named after the asworker sequence#) in its
journal
2. when forwarding undo/redo requests to asworkers, the asworker sequence#
to undo/redo is bundled in a hitchhiker
3. when an asworker changelog has a bundled undo/redo hitchhiker, the
csworker handles it by undoing/redoing all of the changes the bundled
asworker sequence# had originally induced
step 3 is paramount... if the csworker responded to undo/redos like it would
any other request (e.g., respond to RMNODE by RMNODE), it would become out of
sync with the asworker... see example below:
1. client creates A/0, AIcon/0
2. client moves AIcon/0
3. client undoes move (csworker only, OK)
4. client undoes create (would trigger RMNODE A/0, AIcon/0)
5. client redoes create (would trigger MKNODE A/0, AIcon/1)
6. client redoes move (will fail because AIcon/0 doesn't exist)
in short, proper undo/redo requires that operations resultings from undo/redo
be distinguishable from normal ones
TBI:: undoing/redoing SYSOUT-only changelogs has no perceptible effect from
client... one inconvenient side-effect of this is that rules require 2
undos/redos to undo/redo: 1 to undo/redo the rule, 1 to undo/redo the
SYSOUT message announcing the launching of the rule... a sensible and
nice solution would be not to remember such changelogs in
__handledSeqNums */
const {
__batchCheckpoint,
__errorContinuable,
GET__current_state,
get__ids2uris,
set__ids2uris,
get__nextSequenceNumber,
set__nextSequenceNumber,
get__wtype,
__id_to_uri,
__wHttpReq,
__postInternalErrorMsg, __postMessage,
__postBadReqErrorMsg, __postForbiddenErrorMsg,
__sequenceNumber,
__successContinuable,
__uri_to_id
} = require("./__worker");
const _path = require('path');
const _siocl = require('socket.io-client');
const _do = require("./___do");
const _fs = _do.convert(require('fs'), ['readFile', 'writeFile', 'readdir']);
const _fspp = _do.convert(require('./___fs++'), ['mkdirs']);
const _mmmk = require("./mmmk");
const _mt = require('./libmt');
const _svg = require('./libsvg').SVG;
const _utils = require('./utils');
const logger = require('./logger.js')
module.exports = {
'__REGEN_ICON_RETRY_DELAY_MS': 200,
'__asmm2csmm': {},
'__asid2csid': {},
'__aswid': undefined,
'__handledSeqNums': {'i': undefined, '#s': []},
/*************************** ASWORKER INTERACTION **************************/
/* apply asworker changes
0. check the changelog's sequence number to know if we should handle it
now or later
1. iterate through the AS changelog setting up sync/async actions that
appropriately modify the CS while accumulating CS changelogs (for
pushing to subscribed clients)
2. launch sync/async action chain... on error, post error... on success,
a) flatten the CS changelogs into a single changelog
b) post message to server with flattened CS changelog, the server will
then push it to subscribed clients
c) apply next pending asworker changelog, if any and if applicable
__nextASWSequenceNumber
used to determine if a changelog is received out of order, and if a
pending changelog is now ready to be handled
__pendingChangelogs
stores out or order changelogs until we're ready to handle them
__hitchhikerJournal
stores encountered hithchikers for future use (see NOTES above) */
'__nextASWSequenceNumber': '/asworker#1',
'__pendingChangelogs': [],
'__hitchhikerJournal': {},
'__applyASWChanges':
function (changelog, aswSequenceNumber, hitchhiker, cid) {
let log_chs = _utils.collapse_changelog(changelog);
logger.debug('worker#' + __wid + ' << (' + aswSequenceNumber + ') ' + _utils.jsons(log_chs));
if (_utils.sn2int(aswSequenceNumber) >
_utils.sn2int(this.__nextASWSequenceNumber)) {
this.__pendingChangelogs.push(
{
'changelog': changelog,
'sequence#': aswSequenceNumber,
'hitchhiker': hitchhiker,
'cid':cid
});
let self = this;
this.__pendingChangelogs.sort(
function (a, b) {
return self.__sn2int(a['sequence#']) -
self.__sn2int(b['sequence#']);
});
return;
} else if (_utils.sn2int(aswSequenceNumber) <
_utils.sn2int(this.__nextASWSequenceNumber))
throw 'invalid changelog sequence#';
let cschangelogs = [];
let cshitchhiker;
let actions = [__successContinuable()];
let self = this;
/* special handling of undo/redo changelogs (see NOTES above) */
if (hitchhiker && 'undo' in hitchhiker) {
let chglg = _mmmk.undo(hitchhiker['undo'])['changelog']
cschangelogs.push(chglg);
} else if (hitchhiker && 'redo' in hitchhiker) {
let chglg = _mmmk.redo(hitchhiker['redo'])['changelog']
cschangelogs.push(chglg);
}
/* special handling of batchCheckpoint changelogs (see NOTES above) */
else if (changelog.length == 1 && changelog[0]['op'] == 'MKBTCCHKPT')
this.__checkpointUserOperation(changelog[0]['name']);
/* handle any other changelog */
else {
let manageHitchhiker =
function (hhid, hh) {
/* remember/restore a hitchhiker given specified id */
if (hh)
self.__hitchhikerJournal[hhid] = hh;
else if (hitchhiker)
self.__hitchhikerJournal[hhid] = hitchhiker;
else
hitchhiker = self.__hitchhikerJournal[hhid];
};
this.__checkpointUserOperation(aswSequenceNumber);
changelog.forEach(
function (step) {
/* no legal connections exist between *Icon types, so we
simply simulate the CS change such a [dis]connection would
incur */
if (step['op'] == 'MKEDGE' || step['op'] == 'RMEDGE')
actions.push(
function () {
let asid1 = step['id1'];
let asid2 = step['id2'];
let csid1 = self.__asid_to_csid(asid1);
let csid2 = self.__asid_to_csid(asid2);
cschangelogs.push(
[{'op': step['op'], 'id1': csid1, 'id2': csid2}]);
return __successContinuable();
});
/* create appropriate CS instance and associate it with new AS
instance (remember the association in __asid2csid to
optimize future operations) */
else if (step['op'] == 'MKNODE') {
actions.push(
function () {
manageHitchhiker(step['id']);
let asid = step['id'];
let node = _utils.jsonp(step['node']);
let isLink = ('segments' in hitchhiker);
let fullastype = node['$type'];
let fullcstype = self.__astype_to_cstype(
fullastype,
isLink);
let asuri = fullastype + '/' + asid + '.instance';
let attrs = {'$asuri': asuri};
if ('pos' in hitchhiker)
attrs['position'] = hitchhiker['pos'];
else if ('neighborhood' in hitchhiker) {
let nc = self.__nodesCenter(
hitchhiker['neighborhood']);
attrs['position'] =
[(nc[0] || 200), (nc[1] || 200)];
} else if ('clone' in hitchhiker)
attrs = _utils.mergeDicts(
[attrs, hitchhiker['clone']]);
else
attrs['position'] = [200, 200];
let res = _mmmk.create(fullcstype, attrs),
csid = res['id'];
self.__asid2csid[asid] = csid;
cschangelogs.push(res['changelog']);
if (isLink) {
let s = {},
src =
hitchhiker['src'] ||
self.__asuri_to_csuri(hitchhiker['asSrc']),
dest =
hitchhiker['dest'] ||
self.__asuri_to_csuri(hitchhiker['asDest']),
segments =
hitchhiker['segments'] ||
self.__defaultSegments(src, dest);
s[src + '--' + __id_to_uri(csid)] = segments[0];
s[__id_to_uri(csid) + '--' + dest] = segments[1];
let chglg = _mmmk.update(csid, {'$segments': s})['changelog'];
cschangelogs.push(chglg, self.__positionLinkDecorators(csid));
}
return self.__regenIcon(csid, undefined, cid);
},
function (riChangelog) {
cschangelogs.push(riChangelog);
return __successContinuable();
});
}
/* remove appropriate CS instance... update __asid2csid for it
to remain consistent */
else if (step['op'] == 'RMNODE')
actions.push(
function () {
let asid = step['id'];
let csid = self.__asid_to_csid(asid);
let chglg = _mmmk.delete(csid)['changelog']
cschangelogs.push(chglg);
delete self.__asid2csid[asid];
return __successContinuable();
});
/* regenerate the icon to re-evaluate any coded attributes
NOTE:: CS changes may be bundled (i.e., if an AS update was
simulated by a CS update)... if so, perform them
before regenerating the icon */
else if (step['op'] == 'CHATTR')
actions.push(
function () {
let asid = step['id'];
let csid = self.__asid_to_csid(asid);
if (hitchhiker && 'cschanges' in hitchhiker) {
let cschanges = hitchhiker['cschanges'];
let chglg = _mmmk.update(csid, cschanges)['changelog'];
cschangelogs.push(chglg,
('$segments' in cschanges ?
self.__positionLinkDecorators(csid) :
[]));
}
return self.__regenIcon(csid, undefined, cid);
},
function (riChangelog) {
cschangelogs.push(riChangelog);
return __successContinuable();
});
/* load appropriate CS metamodel (stored in hitchhiker)...
remember AS-to-CS metamodel mapping in __asmm2csmm to
optimize future operations */
else if (step['op'] == 'LOADMM')
actions.push(
function () {
manageHitchhiker(step['name']);
let asmm = step['name'];
let csmm = hitchhiker['name'];
let data = hitchhiker['csmm'];
let chglg = _mmmk.loadMetamodel(csmm, data)['changelog'];
cschangelogs.push(chglg,
{
'op': 'LOADASMM',
'name': asmm,
'mm': step['mm']
});
self.__asmm2csmm[asmm] = csmm;
return __successContinuable();
});
/* unload appropriate CS metamodel... update __asmm2csmm for
it to remain consistent */
else if (step['op'] == 'DUMPMM')
actions.push(
function () {
let asmm = step['name'];
let csmm = self.__asmm2csmm[asmm];
let chglg = _mmmk.unloadMetamodel(csmm)['changelog']
cschangelogs.push(chglg);
delete self.__asmm2csmm[asmm];
return __successContinuable();
});
/* load appropriate CS model (stored in hitchhiker) and
overwrite past hitchhiker associated to initial load of
current model, if any... when step['insert'] is specified,
adjust $asuris to compensate for offsetting of inserted asm
and $segments to compensate for upcoming offsetting of to-
be-inserted csm */
else if (step['op'] == 'RESETM')
actions.push(
function () {
manageHitchhiker(
step['old_name'],
{'csm': _mmmk.read()});
manageHitchhiker(step['new_name']);
let csm = hitchhiker['csm'];
let _csm = eval('(' + csm + ')');
if (step['insert']) {
let asoffset = parseInt(step['insert']);
let csoffset = _mmmk.next_id;
let incUri =
function (oldUri, offset) {
let matches = oldUri.match(/(.+\/)(.+)(\.instance)/);
return matches[1] +
(parseInt(matches[2]) + offset) +
matches[3];
};
for (let id in _csm.nodes) {
_csm.nodes[id]['$asuri']['value'] =
incUri(_csm.nodes[id]['$asuri']['value'],
asoffset);
if (!('$segments' in _csm.nodes[id]))
continue;
let segments = _csm.nodes[id]['$segments']['value'];
let _segments = {};
for (let edgeId in segments) {
let uris = edgeId.match(/^(.*\.instance)--(.*\.instance)$/);
let seg = incUri(uris[1], csoffset) + '--' + incUri(uris[2], csoffset);
_segments[seg] = segments[edgeId];
}
_csm.nodes[id]['$segments']['value'] = _segments;
}
csm = _utils.jsons(_csm, null, '\t');
}
//see if any cs metamodels are missing
//this fixes issue #28
//this loading should be done elsewhere in the model loading chain
for (let i in _csm.metamodels) {
let mm = _csm.metamodels[i];
if (!(_mmmk.model.metamodels.includes(mm))) {
logger.error("Last-minute loading for CS metamodel: " + mm);
let csmm = _fs.readFile('./users/' + mm, 'utf8');
_mmmk.loadMetamodel(mm, csmm);
}
}
let res = _mmmk.loadModel(
step['new_name'],
csm,
step['insert']);
if (res["$err"] == undefined) {
cschangelogs.push(res['changelog']);
return __successContinuable();
} else {
return __errorContinuable();
}
});
/* forward this SYSOUT command */
else if (step['op'] == 'SYSOUT')
actions.push(
function () {
cschangelogs.push([step]);
return __successContinuable();
});
});
actions.push(
function () {
cschangelogs.push(self.__solveLayoutContraints(changelog));
return __successContinuable();
});
}
_do.chain(actions)(
function () {
let cschangelog = _utils.flatten(cschangelogs);
let log_csc = _utils.jsons(_utils.collapse_changelog(cschangelog));
logger.debug('worker#' + __wid + ' >> (' + aswSequenceNumber + ') ' + log_csc);
__postMessage(
{
'statusCode': 200,
'changelog': cschangelog,
'sequence#': aswSequenceNumber,
'hitchhiker': cshitchhiker,
'cid':cid
});
self.__nextASWSequenceNumber =
_utils.incrementSequenceNumber(self.__nextASWSequenceNumber);
self.__applyPendingASWChanges();
},
function (err) {
throw 'unexpected error while applying changelogs :: ' + err;
}
);
},
/* apply pending asworker changelogs, if any and if applicable */
'__applyPendingASWChanges':
function () {
if (this.__pendingChangelogs.length > 0 &&
this.__nextASWSequenceNumber ==
this.__pendingChangelogs[0]['sequence#']) {
let pc = this.__pendingChangelogs.shift();
this.__applyASWChanges(
pc['changelog'],
pc['sequence#'],
pc['hitchhiker'],
pc['cid']
);
}
},
/* initialize a socket that will listen for and handle changelogs returned by
this csworker's associated asworker
1. onconnect() is triggered when 2 way communication is established, at
which point we attempt to subscribe for specified asworker
2. onmessage() is triggered once in response to our subscription attempt:
a) we set this.__aswid to specified aswid
b) if a cswid was provided (i.e., a shared model session is being
set up)
i. retrieve the specified csworker's internal state
ii. setup this csworker's state and _mmmk based on the
results from step ii.
iii. return the new state to the client (via callback())
iv. remove any obsolete changelogs received since step i.
and set __nextASWSequenceNumber to the same value as
that of the csworker whose state we're cloning
v. apply pending changelogs, if any
all future triggers of onmessage() are due to the asworker pushing
changelogs, these are handled by __applyASWChanges
NOTE : actually, there is a 3rd case where onmessage() is triggered...
between the moment where the socket is created and the moment
where we receive the response to our subscription attempt, we
will receive *all* messages broadcasted by the websocket server
in session_manager.js... these are detected and discarded */
'__aswSubscribe':
function (aswid, cswid, cid) {
let self = this;
return function (callback, errback) {
let io = _siocl('http://localhost:8124');
io.on('connect',
function () {
logger.http("socketio _ 'connect'", {
'from': "/csworker" + __wid,
'to': "/asworker" + aswid,
'type': "-->>"
});
let msg = {'method': 'POST', 'url': '/changeListener?wid=' + aswid + '&id=/csworker'+__wid};
io.emit('message', msg);
});
io.on('disconnect',
function () {
logger.http("socketio _ 'disconnect'", {
'from': "/asworker" + aswid,
'to': "/csworker" + __wid,
'type': "-->>"
});
self.__aswid = undefined;
});
io.on('message',
function (msg) {
let log_statusCode = (msg.statusCode === undefined) ? '' : msg.statusCode + "<br/>";
/* on POST /changeListener response */
if (msg.statusCode !== undefined) {
if (!_utils.isHttpSuccessCode(msg.statusCode))
return errback(msg.statusCode + ':' + msg.reason);
self.__aswid = aswid;
if (cswid !== undefined) {
let actions =
[__wHttpReq('GET', self.__build_url('/internal.state', cswid, cid),)];
logger.http("http _ GET internal.state", {
'from': "/csworker" + __wid,
'to': "/csworker" + cswid,
'type': "-)"
});
_do.chain(actions)(
function (respData) {
let state = respData['data'];
_mmmk.clone(state['_mmmk']);
self.__clone(state['_wlib']);
let __ids2uris = state['__ids2uris'];
set__ids2uris(__ids2uris);
let __nextSequenceNumber = state['__nextSequenceNumber'];
set__nextSequenceNumber(__nextSequenceNumber);
self.__pendingChangelogs =
self.__pendingChangelogs.filter(
function (pc) {
return self.__sn2int(pc['sequence#']) >
self.__sn2int(
self.__nextASWSequenceNumber)
});
callback();
self.__applyPendingASWChanges();
},
function (err) {
errback(err);
}
);
} else
callback();
}
/* on changelog reception (ignore changelogs while not
subscribed to an asworker... see NOTE) */
else if (self.__aswid !== undefined) {
let log_data = {};
if (msg.data) {
log_data = {'changelog': _utils.collapse_changelog(msg.data.changelog)};
}
logger.http("socketio _ 'recv chglg' <br/> " + log_statusCode + JSON.stringify(log_data), {
'from': "/asworker" + aswid,
'to': "/csworker" + __wid,
'type': "-->>"
});
self.__applyASWChanges(
msg.data.changelog,
msg.data['sequence#'],
msg.data.hitchhiker,
msg.data['cid']
);
}
});
};
},
/***************************** ICON GENERATION *****************************/
/* determine the correct positions and orientations of the given link's
decorators, adjust them and return changelogs
0. concatenate segments into a single path
1. for each link decorator (i.e., Link $contents)
*. do nothing if link decoration information is missing... this ensures
backward compatibility with pre-link decorator models
a. extract link decoration information, i.e., xratio and yoffset
b. determine point on path at xratio and its orientation
c. adjust yoffset given orientation (yoffset was specified for 0deg)
d. adjust endAt given orientation (endAt is specified for 0deg)... note
that endAt is only relevant for arrowtails
e. relativize point from step b. w.r.t. to Link center and adjust
position by adjusted yoffset
f. set new position and orientation in mmmk and remember changelogs
2. return flattened changelogs
NOTE:: the initial values of vobject geometric attribute values must
always be remembered... they are needed on the client to properly
support the drawing and transformation of vobjects... this is
captured by buildVobjGeomAttrVal(), which we use in step 1f */
'__positionLinkDecorators':
function (id) {
let link = _utils.jsonp(_mmmk.read(id));
let vobjs = link['$contents']['value'].nodes;
let segments = _utils.values(link['$segments']['value']);
let path = segments[0] + segments[1].substring(segments[1].indexOf('L'));
let changelogs = [];
for (let vid in vobjs) {
if (!('$linkDecoratorInfo' in vobjs[vid]))
continue;
let ldi = vobjs[vid]['$linkDecoratorInfo']['value'];
let pp = _svg.fns.getPointOnPathAtRatio(path, ldi.xratio);
if (pp == undefined)
continue;
let yoffset = new _svg.types.Point(0, ldi.yoffset).rotate(pp.O);
let endAt = (ldi.xratio >= 1 ?
new _svg.types.Point(100, 0).rotate(pp.O) :
new _svg.types.Point(0, 0));
let changes = {};
pp.x += yoffset.x - link['position']['value'][0];
pp.y += yoffset.y - link['position']['value'][1];
changes['$contents/value/nodes/' + vid + '/position'] =
[_utils.buildVobjGeomAttrVal(
vobjs[vid]['position']['value'][0], pp.x + ',' + endAt.x + '%'),
_utils.buildVobjGeomAttrVal(
vobjs[vid]['position']['value'][1], pp.y + ',' + endAt.y + '%')];
changes['$contents/value/nodes/' + vid + '/orientation'] =
_utils.buildVobjGeomAttrVal(
vobjs[vid]['orientation']['value'], pp.O);
let chglg = _mmmk.update(id, changes)['changelog'];
changelogs.push(chglg);
}
return _utils.flatten(changelogs);
},
/* regenerate specified icon... if newCsmm is specified, the regeneration
process essentially transforms the icon for it to conform to whatever is
specified by newCsmm... otherwise, it merely involves re-evaluating
VisualObject mappers
1. if newCSmm is defined
a) create a new instance I of node 'id''s icontype given newCsmm
b) copy node 'id''s '$asuri', 'position', etc. attributes to I
c) delete node 'id'
d) update__asid2csid and 'id' variable to be I's id
e) save the changelogs of steps a-c)
2. in either case, [re-]eval VisualObject mappers
a) retrieve Icon and VisualObject mappers...
i. fetch specified node from mmmk
ii. retrieve its '$contents' attribute
iii. retrieve the 'mapper' attribute for the node itself
iii. retrieve the 'mapper' attribute of VisualObjects within
'$contents'
b) return empty changelog if all mappers are empty
c) setup sync/async action chaining
i. ask asworker to evaluate a bunch of mappers
ii. save results for later access
d) launch chain...
on success, populate attributes with results from step 2ci and
'return' changelog OR 'return' SYSOUT error if evaluating mappers
raised exceptions
on failure (this only occurs if we were unable to obtain an
asworker read-lock), relaunch chain after short delay
TBI: step 2d) could potentially lead to an infinite loop if failure is due
to unforeseen error or to very long delays if lock holder takes a
long time to finish... we could address this by *not* relaunching the
chain if some number of tries have failed, and instead setting coded
attribute values to '<out-of-date>' */
'__regenIcon':
function (id, newCsmm, cid) {
let changelogs = [];
let self = this;
return function (callback, _errback) {
if (newCsmm != undefined) {
let node = _utils.jsonp(_mmmk.read(id));
let asuri = _mmmk.read(id, '$asuri');
let asid = __uri_to_id(asuri);
let dict1 = {
'$asuri': asuri,
'position': node['position']['value'],
'orientation': node['orientation']['value'],
'scale': node['scale']['value']
};
let s = _mmmk.read(id, '$segments');
let dict2 = {};
if (s['$err'] == undefined)
dict2 = {'$segments': s}
let attrs = _utils.mergeDicts([dict1, dict2]);
let cres = _mmmk.create(
newCsmm + '/' + node['$type'].match(/.*\/(.*)/)[1],
attrs);
let csid = cres['id'];
let dres = _mmmk['delete'](id);
self.__asid2csid[asid] = id = csid;
changelogs.push(
cres['changelog'],
dres['changelog']);
}
let csuri = __id_to_uri(id);
let asuri = self.__csuri_to_asuri(csuri);
let icon = _utils.jsonp(_mmmk.read(id));
let vobjects = icon['$contents']['value'];
let mappers = {};
if (icon['mapper']['value'] != '')
mappers[''] = icon['mapper']['value'];
for (let vid in vobjects['nodes'])
if ('mapper' in vobjects['nodes'][vid] && vobjects['nodes'][vid]['mapper']['value'] != '')
mappers['$contents/value/nodes/' + vid + '/'] = vobjects['nodes'][vid]['mapper']['value'];
if (_utils.keys(mappers).length > 0) {
let actions =
[__wHttpReq(
'POST',
self.__build_url('/GET/' + asuri + '.mappings', self.__aswid, cid),
mappers)];
let successf =
function (attrVals) {
if ('$err' in attrVals)
callback(
[{
'op': 'SYSOUT',
'text': 'ERROR :: ' + attrVals['$err']
}]);
else {
let changes = {};
for (let fullattr in attrVals)
changes[fullattr] = attrVals[fullattr];
let result = _mmmk.update(id, changes);
if ('$err' in result)
callback(
[{
'op': 'SYSOUT',
'text': 'ERROR :: ' + result['$err']
}]);
else {
changelogs.push(
result['changelog']);
callback(_utils.flatten(changelogs));
}
}
};
let failuref =
function (err) {
logger.error('"POST *.mappings" failed on :: ' + err +
'\n(will try again soon)');
setTimeout(
_do.chain(actions),
self.__REGEN_ICON_RETRY_DELAY_MS,
successf,
failuref);
};
_do.chain(actions)(successf, failuref);
} else
callback(_utils.flatten(changelogs));
};
},
'__solveLayoutContraints':
function (_changelog) {
// TBC actually implement this function
// use ids in changelog to determine what changed
// add 2 lines below to mmmk.__create() if necessary
// if( type in this.metamodels[metamodel]['connectorTypes'] )
// new_node['$linktype'] = this.metamodels[metamodel]['connectorTypes'][type];
return [];
// return [{'op':'SYSOUT',
// 'text':'WARNING :: '+
// 'a proper layout constraint solver has yet to be '+
// 'implemented... inter-VisualObject relationships are '+
// 'ignored and containers do not resize to fit their '+
// 'contents'}];
},
/* transform all icons from tgtCsmm into appropriate icons of newCsmm
1. read model and newCsmm from _mmmk
2. for each icon of tgtCsmm, if newCsmm defines a replacement icon, save
the icon's id in 'tgtIds'... otherwise, return error
3. init sync/async action chaining...
a) for each id in 'tgtIds', add 2 entries to chain
i. call __regenIcon on specified icon
ii. save resulting changelog and continue
4. launch chain...
on success,
a) adjust $segments attributes from all Links to account for edge
end change of id (and uri)
b) 'return' flattened changelogs
on failure, 'return' error */
'__transformIcons':
function (tgtCsmm, newCsmm, cid) {
let self = this;
return function (callback, errback) {
let m = _utils.jsonp(_mmmk.read());
let newCsmmData = _utils.jsonp(_mmmk.readMetamodels(newCsmm));
let tgtIds = [];
let newIds = {};
let changelogs = [];
let actions = [__successContinuable()];
for (let id in m.nodes) {
let matches = m.nodes[id]['$type'].match('^' + tgtCsmm + '/(.*)')
if (matches) {
let type = matches[1];
if (newCsmmData.types[type] == undefined) {
errback('Icons mm should define type :: ' + type);
return;
}
tgtIds.push(id);
}
}
tgtIds.forEach(
function (id) {
actions.push(
function () {
return self.__regenIcon(id, newCsmm, cid);
},
function (changelog) {
let newId = changelog[0]['id'];
newIds[id] = newId;
let chglg = _mmmk.read(newId, '$segments');
changelogs.push(
changelog.concat(
'$err' in chglg ? [] : self.__positionLinkDecorators(newId)));
return __successContinuable();
});
});
_do.chain(actions)(
function () {
let m = _utils.jsonp(_mmmk.read());
for (let id in m.nodes) {
let matches = m.nodes[id]['$type'].match(/Link$/);
if (matches) {
let s = _mmmk.read(id, '$segments');
let changed = false;
for (let edgeId in s) {
let ends = edgeId.match(/(.*\.instance)--(.*\.instance)/);
let id1 = __uri_to_id(ends[1]);
let id2 = __uri_to_id(ends[2]);
if (id1 in newIds) {
ends[1] = __id_to_uri(newIds[id1]);
changed = true;