-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudI.js
1250 lines (1195 loc) · 43.4 KB
/
CloudI.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
//-*-Mode:javascript;coding:utf-8;tab-width:4;c-basic-offset:4;indent-tabs-mode:()-*-
// ex: set ft=javascript fenc=utf-8 sts=4 ts=4 sw=4 et nomod:
//
// MIT License
//
// Copyright (c) 2014-2023 Michael Truog <mjtruog at protonmail dot com>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
exports.CloudI = new function() {
var CloudI = this; // namespace
'use strict';
var Erlang = require('./Erlang.js').Erlang;
var net = require('net');
var timers = require('timers');
var domain = require('domain');
var fs = require('fs');
var assert = require('assert');
var toNativeString = {}.toString;
CloudI.nodejs_version_after = Erlang.nodejs_version_after;
var bufferFrom;
if (Erlang.nodejs_version_after('5.10.0',true)) {
bufferFrom = Buffer.from;
}
else {
bufferFrom = Buffer;
}
var littleEndian = (
(new Uint16Array((new Uint8Array([0,1])).buffer))[0] === 0x0100);
var unpackUint8 = function unpackUint8 (i, data) {
return data[i];
};
var unpackInt8 = function unpackInt8 (i, data) {
var value = data[i];
if ((0x80 & value) != 0) {
value = -128 + (value & 0x7f);
}
return value;
};
var unpackUint32big = function unpackUint32big (i, data) {
return (data[i] << 24) |
(data[i + 1] << 16) |
(data[i + 2] << 8) |
data[i + 3];
};
var unpackUint32;
if (littleEndian) {
unpackUint32 = function unpackUint32 (i, data) {
return data[i] |
(data[i + 1] << 8) |
(data[i + 2] << 16) |
(data[i + 3] << 24);
};
}
else {
unpackUint32 = unpackUint32big;
}
var packUint32big = function packUint32big (value) {
return new bufferFrom([(value >>> 24) & 0xff,
(value >>> 16) & 0xff,
(value >>> 8) & 0xff,
value & 0xff]);
};
var unpackInt32 = function unpackInt32 (i, data) {
var value = unpackUint32(i, data);
if ((0x80000000 & value) != 0) {
value = -2147483648 + (value & 0x7fffffff);
}
return value;
};
if (Erlang.nodejs_version_after('10.0.0',true)) {
var originalEmitWarning = process.emitWarning;
process.emitWarning = function(warning, type, code, ctor) {
if (code === 'DEP0097') {
// Ignore the stderr output of the runtime deprecation:
// "Using a domain property in MakeCallback is deprecated.
// Use the async_context variant of MakeCallback or the
// AsyncResource class instead."
//
// Until the exception handling of the domain module is
// provided by a different module (like async_hooks) or
// exceptions (throw/try/catch) are removed from JavaScript,
// the use of the domain module needs to remain
// (to remain consistent with other CloudI API implementations).
// The stderr runtime deprecation information is ignored
// because it provides no useful information and is only an
// annoyance as a false negative.
return;
}
originalEmitWarning(warning, type, code, ctor);
};
}
var getEnvToUnsignedInt = function getEnvToUnsignedInt (name) {
var value_str = process.env[name];
if (value_str === undefined) {
throw new InvalidInputException();
}
var value = parseInt(value_str);
if (value < 0) {
throw new InvalidInputException();
}
return value;
};
var InvalidInputException = function InvalidInputException () {
var error = new Error('Invalid Input');
error.name = 'InvalidInputException';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
}
InvalidInputException.prototype = Object.create(Error.prototype, {
name: { value: 'InvalidInputException' }
});
var ReturnSyncException = function ReturnSyncException () {
var error = new Error('Synchronous Call Return Invalid');
error.name = 'ReturnSyncException';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
}
ReturnSyncException.prototype = Object.create(Error.prototype, {
name: { value: 'ReturnSyncException' }
});
var ReturnAsyncException = function ReturnAsyncException () {
var error = new Error('Asynchronous Call Return Invalid');
error.name = 'ReturnAsyncException';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
}
ReturnAsyncException.prototype = Object.create(Error.prototype, {
name: { value: 'ReturnAsyncException' }
});
var ForwardSyncException = function ForwardSyncException () {
var error = new Error('Synchronous Call Forward Invalid');
error.name = 'ForwardSyncException';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
}
ForwardSyncException.prototype = Object.create(Error.prototype, {
name: { value: 'ForwardSyncException' }
});
var ForwardAsyncException = function ForwardAsyncException () {
var error = new Error('Asynchronous Call Forward Invalid');
error.name = 'ForwardAsyncException';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
}
ForwardAsyncException.prototype = Object.create(Error.prototype, {
name: { value: 'ForwardAsyncException' }
});
var MessageDecodingException = function MessageDecodingException () {
var error = new Error('Message Decoding Error');
error.name = 'MessageDecodingException';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
}
MessageDecodingException.prototype = Object.create(Error.prototype, {
name: { value: 'MessageDecodingException' }
});
var TerminateException = function TerminateException (timeout) {
var error = new Error('Terminate');
error.name = 'TerminateException';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
this._timeout = timeout;
}
TerminateException.prototype = Object.create(Error.prototype, {
name: { value: 'TerminateException' }
});
TerminateException.prototype.timeout = function () {
return this._timeout;
};
var FatalError = function FatalError (message) {
var error = new Error(message);
error.name = 'FatalError';
this.message = error.message;
if (error.stack) {
this.stack = error.stack;
}
}
FatalError.prototype = Object.create(Error.prototype, {
name: { value: 'FatalError' }
});
CloudI.stdout_write = function stdout_write (s) {
fs.writeSync(1, s);
};
CloudI.stderr_write = function stderr_write (s) {
fs.writeSync(2, s);
};
var MESSAGE_INIT = 1;
var MESSAGE_SEND_ASYNC = 2;
var MESSAGE_SEND_SYNC = 3;
var MESSAGE_RECV_ASYNC = 4;
var MESSAGE_RETURN_ASYNC = 5;
var MESSAGE_RETURN_SYNC = 6;
var MESSAGE_RETURNS_ASYNC = 7;
var MESSAGE_KEEPALIVE = 8;
var MESSAGE_REINIT = 9;
var MESSAGE_SUBSCRIBE_COUNT = 10;
var MESSAGE_TERM = 11;
CloudI.API = function API (thread_index, callback) {
var API = this;
API.ASYNC = 1;
API.SYNC = -1;
if (typeof thread_index !== 'number') {
throw new InvalidInputException();
}
callback = typeof callback !== 'undefined' ?
callback : function (API_) {};
var protocol_str = process.env.CLOUDI_API_INIT_PROTOCOL;
if (protocol_str === undefined) {
CloudI.stderr_write('CloudI service execution must occur in CloudI\n');
throw new InvalidInputException();
}
var buffer_size = getEnvToUnsignedInt('CLOUDI_API_INIT_BUFFER_SIZE');
if (protocol_str == 'tcp') {
API._use_header = true;
}
else if (protocol_str == 'udp') {
API._use_header = false;
}
else if (protocol_str == 'local') {
API._use_header = true;
}
else {
throw new InvalidInputException();
}
API._socket = new net.Socket({fd: (thread_index + 3),
readable: true,
writable: true});
API._initialization_complete = false;
API._fatal_exceptions = false;
API._terminate = false;
API._terminate_callback = undefined;
API._size = buffer_size;
API._callbacks = {};
API._timeout_terminate = 10; // TIMEOUT_TERMINATE_MIN
API._poll_callback = callback;
API._poll_callbacks_pending = [];
API._poll_data = undefined;
API._poll_data_size = undefined;
API._socket.on('data', function(data) {
try {
if (! API._use_header) {
API._poll_request(data);
return;
}
if (API._poll_data === undefined) {
API._poll_data_size = 4 + unpackUint32big(0, data);
API._poll_data = data;
}
else {
data = Buffer.concat([API._poll_data, data]);
}
if (data.length == API._poll_data_size) {
API._poll_data = undefined;
API._poll_data_size = undefined;
API._poll_request(data);
}
else if (data.length < API._poll_data_size) {
API._poll_data = data;
}
else if (data.length > API._poll_data_size + 4) {
var next_data = data.slice(API._poll_data_size);
data = data.slice(0, API._poll_data_size);
API._poll_data = next_data;
API._poll_data_size = 4 + unpackUint32big(0, next_data);
API._poll_request(data);
}
}
catch (err) {
if (err instanceof InvalidInputException ||
err instanceof MessageDecodingException) {
CloudI.stderr_write(err.stack + '\n');
API._poll_terminate();
}
else if (err instanceof TerminateException) {
API._poll_terminate();
}
else {
API._exception(err);
API._poll_terminate();
}
}
});
API._socket.on('error', function(err) {
API._exception(err);
API._poll_terminate();
});
API._send(new Erlang.OtpErlangAtom('init'));
};
// class method
CloudI.API.thread_count = function thread_count () {
return getEnvToUnsignedInt('CLOUDI_API_INIT_THREAD_COUNT');
};
CloudI.API.prototype.subscribe = function (pattern, obj, obj_f, callback) {
if (obj_f.length != 9) {
throw new InvalidInputException();
}
callback = typeof callback !== 'undefined' ?
callback : function () {};
this._poll_wait(function (API) {
var f = {obj: obj, obj_f: obj_f};
var key = API._prefix + pattern;
var value = API._callbacks[key];
if (value === undefined) {
API._callbacks[key] = [f];
}
else {
value.push(f);
}
API._send([new Erlang.OtpErlangAtom('subscribe'), pattern]);
callback();
});
};
CloudI.API.prototype.subscribe_count = function (pattern, callback) {
callback = typeof callback !== 'undefined' ?
callback : function (count) {};
this._poll_wait(function (API) {
API._poll_callback = callback;
API._send([new Erlang.OtpErlangAtom('subscribe_count'), pattern]);
});
};
CloudI.API.prototype.unsubscribe = function (pattern, callback) {
callback = typeof callback !== 'undefined' ?
callback : function () {};
this._poll_wait(function (API) {
var key = API._prefix + pattern;
var value = API._callbacks[key];
assert(value !== undefined);
value.shift();
if (value.length == 0) {
delete API._callbacks[key];
}
API._send([new Erlang.OtpErlangAtom('unsubscribe'), pattern]);
callback();
});
};
CloudI.API.prototype.send_async = function (name, request, callback,
timeout, request_info, priority) {
callback = typeof callback !== 'undefined' ?
callback : function (trans_id) {};
request_info = typeof request_info !== 'undefined' ?
request_info : '';
this._poll_wait(function (API) {
timeout = typeof timeout !== 'undefined' ?
timeout : API._timeout_async;
priority = typeof priority !== 'undefined' ?
priority : API._priority_default;
API._poll_callback = callback;
API._send([new Erlang.OtpErlangAtom('send_async'), name,
new Erlang.OtpErlangBinary(request_info),
new Erlang.OtpErlangBinary(request),
timeout, priority]);
});
};
CloudI.API.prototype.send_sync = function (name, request, callback,
timeout, request_info, priority) {
callback = typeof callback !== 'undefined' ?
callback : function (request_info, request, trans_id) {};
request_info = typeof request_info !== 'undefined' ?
request_info : '';
this._poll_wait(function (API) {
timeout = typeof timeout !== 'undefined' ?
timeout : API._timeout_sync;
priority = typeof priority !== 'undefined' ?
priority : API._priority_default;
API._poll_callback = callback;
API._send([new Erlang.OtpErlangAtom('send_sync'), name,
new Erlang.OtpErlangBinary(request_info),
new Erlang.OtpErlangBinary(request),
timeout, priority]);
});
};
CloudI.API.prototype.mcast_async = function (name, request, callback,
timeout, request_info, priority) {
callback = typeof callback !== 'undefined' ?
callback : function (trans_ids) {};
request_info = typeof request_info !== 'undefined' ?
request_info : '';
this._poll_wait(function (API) {
timeout = typeof timeout !== 'undefined' ?
timeout : API._timeout_sync;
priority = typeof priority !== 'undefined' ?
priority : API._priority_default;
API._poll_callback = callback;
API._send([new Erlang.OtpErlangAtom('mcast_async'), name,
new Erlang.OtpErlangBinary(request_info),
new Erlang.OtpErlangBinary(request),
timeout, priority]);
});
};
CloudI.API.prototype.forward_ = function (request_type, name,
request_info, request,
timeout, priority,
trans_id, source) {
var API = this;
switch (request_type) {
case API.ASYNC:
API.forward_async(name, request_info, request,
timeout, priority, trans_id, source);
return;
case API.SYNC:
API.forward_sync(name, request_info, request,
timeout, priority, trans_id, source);
return;
default:
throw new InvalidInputException();
}
};
CloudI.API.prototype.forward_async = function (name, request_info, request,
timeout, priority,
trans_id, source) {
this._poll_wait(function (API) {
API._send([new Erlang.OtpErlangAtom('forward_async'), name,
new Erlang.OtpErlangBinary(request_info),
new Erlang.OtpErlangBinary(request),
timeout, priority,
new Erlang.OtpErlangBinary(trans_id), source]);
throw new ForwardAsyncException();
});
};
CloudI.API.prototype.forward_sync = function (name, request_info, request,
timeout, priority,
trans_id, source) {
this._poll_wait(function (API) {
API._send([new Erlang.OtpErlangAtom('forward_sync'), name,
new Erlang.OtpErlangBinary(request_info),
new Erlang.OtpErlangBinary(request),
timeout, priority,
new Erlang.OtpErlangBinary(trans_id), source]);
throw new ForwardSyncException();
});
};
CloudI.API.prototype.return_ = function (request_type, name, pattern,
response_info, response,
timeout, trans_id, source) {
var API = this;
switch (request_type) {
case API.ASYNC:
API.return_async(name, pattern, response_info, response,
timeout, trans_id, source);
return;
case API.SYNC:
API.return_sync(name, pattern, response_info, response,
timeout, trans_id, source);
return;
default:
throw new InvalidInputException();
}
};
CloudI.API.prototype.return_async = function (name, pattern,
response_info, response,
timeout, trans_id, source) {
this._poll_wait(function (API) {
API._send([new Erlang.OtpErlangAtom('return_async'), name, pattern,
new Erlang.OtpErlangBinary(response_info),
new Erlang.OtpErlangBinary(response),
timeout, new Erlang.OtpErlangBinary(trans_id), source]);
throw new ReturnAsyncException();
});
};
CloudI.API.prototype.return_sync = function (name, pattern,
response_info, response,
timeout, trans_id, source) {
this._poll_wait(function (API) {
API._send([new Erlang.OtpErlangAtom('return_sync'), name, pattern,
new Erlang.OtpErlangBinary(response_info),
new Erlang.OtpErlangBinary(response),
timeout, new Erlang.OtpErlangBinary(trans_id), source]);
throw new ReturnSyncException();
});
};
CloudI.API.prototype.recv_async = function (callback,
timeout, trans_id, consume) {
callback = typeof callback !== 'undefined' ?
callback : function (response_info, response, trans_id_) {};
trans_id = typeof trans_id !== 'undefined' ?
trans_id : '\x00\x00\x00\x00\x00\x00\x00\x00' +
'\x00\x00\x00\x00\x00\x00\x00\x00';
consume = typeof consume !== 'undefined' ?
consume : true;
this._poll_wait(function (API) {
timeout = typeof timeout !== 'undefined' ?
timeout : API._timeout_sync;
API._poll_callback = callback;
API._send([new Erlang.OtpErlangAtom('recv_async'), timeout,
new Erlang.OtpErlangBinary(trans_id), consume]);
});
};
CloudI.API.prototype.process_index = function () {
return this._process_index;
};
// class method
CloudI.API.process_index_ = function process_index_ () {
return getEnvToUnsignedInt('CLOUDI_API_INIT_PROCESS_INDEX');
};
CloudI.API.prototype.process_count = function () {
return this._process_count;
};
CloudI.API.prototype.process_count_max = function () {
return this._process_count_max;
};
// class method
CloudI.API.process_count_max_ = function process_count_max_ () {
return getEnvToUnsignedInt('CLOUDI_API_INIT_PROCESS_COUNT_MAX');
};
CloudI.API.prototype.process_count_min = function () {
return this._process_count_min;
};
// class method
CloudI.API.process_count_min_ = function process_count_min_ () {
return getEnvToUnsignedInt('CLOUDI_API_INIT_PROCESS_COUNT_MIN');
};
CloudI.API.prototype.prefix = function () {
return this._prefix;
};
CloudI.API.prototype.timeout_initialize = function () {
return this._timeout_initialize;
};
// class method
CloudI.API.timeout_initialize_ = function timeout_initialize_ () {
return getEnvToUnsignedInt('CLOUDI_API_INIT_TIMEOUT_INITIALIZE');
};
CloudI.API.prototype.timeout_async = function () {
return this._timeout_async;
};
CloudI.API.prototype.timeout_sync = function () {
return this._timeout_sync;
};
CloudI.API.prototype.timeout_terminate = function () {
return this._timeout_terminate;
};
// class method
CloudI.API.timeout_terminate_ = function timeout_terminate_ () {
return getEnvToUnsignedInt('CLOUDI_API_INIT_TIMEOUT_TERMINATE');
};
CloudI.API.prototype.priority_default = function () {
return this._priority_default;
};
CloudI.API.prototype._null_response = function (request_type, name, pattern,
request_info, request,
timeout, priority,
trans_id, source) {
return '';
};
CloudI.API.prototype._callback = function (command, name, pattern,
request_info, request,
timeout, priority,
trans_id, source) {
var API = this;
var function_queue = API._callbacks[pattern];
var f;
if (function_queue === undefined) {
f = {obj: API, obj_f: API._null_response};
}
else {
f = function_queue.shift();
function_queue.push(f);
}
switch (command) {
case MESSAGE_SEND_ASYNC:
var domain_async = domain.create();
domain_async.name = 'CloudI Async';
domain_async.on('error', function(err) {
domain_async.exit();
var return_null_response = false;
if (err instanceof MessageDecodingException) {
API._terminate = true;
return_null_response = true;
}
else if (err instanceof TerminateException) {
return_null_response = true;
}
else if (err instanceof ReturnAsyncException) {
return;
}
else if (err instanceof ReturnSyncException) {
API._terminate = true;
CloudI.stderr_write(err.stack + '\n');
}
else if (err instanceof ForwardAsyncException) {
return;
}
else if (err instanceof ForwardSyncException) {
API._terminate = true;
CloudI.stderr_write(err.stack + '\n');
}
else if (err instanceof assert.AssertionError ||
err instanceof FatalError) {
CloudI.stderr_write(err.stack + '\n');
process.exit(1);
}
else {
API._exception(err);
if (API._fatal_exceptions) {
process.exit(1);
}
return_null_response = true;
}
if (return_null_response) {
var response_info = '';
var response = '';
try {
API.return_async(name, pattern,
response_info, response,
timeout, trans_id, source);
}
catch (err_new) {
err_new = undefined;
}
}
if (API._terminate) {
API._poll_terminate();
}
});
domain_async.enter();
process.nextTick(function () {
var response = f.obj_f.call(f.obj,
API.ASYNC, name, pattern,
request_info, request,
timeout, priority,
trans_id, source);
if (typeof response === 'undefined') {
return;
}
domain_async.exit();
var response_info;
if (typeof response === 'object' &&
toNativeString.call(response) == '[object Array]') {
response_info = response[0];
response = response[1];
if (typeof response_info !== 'string') {
response_info = '';
}
}
else {
response_info = '';
}
if (typeof response !== 'string') {
response = '';
}
try {
API.return_async(name, pattern,
response_info, response,
timeout, trans_id, source);
}
catch (err_new) {
err_new = undefined;
}
});
return;
case MESSAGE_SEND_SYNC:
var domain_sync = domain.create();
domain_sync.name = 'CloudI Sync';
domain_sync.on('error', function(err) {
domain_sync.exit();
var return_null_response = false;
if (err instanceof MessageDecodingException) {
API._terminate = true;
return_null_response = true;
}
else if (err instanceof TerminateException) {
return_null_response = true;
}
else if (err instanceof ReturnAsyncException) {
API._terminate = true;
CloudI.stderr_write(err.stack + '\n');
}
else if (err instanceof ReturnSyncException) {
return;
}
else if (err instanceof ForwardAsyncException) {
API._terminate = true;
CloudI.stderr_write(err.stack + '\n');
}
else if (err instanceof ForwardSyncException) {
return;
}
else if (err instanceof assert.AssertionError ||
err instanceof FatalError) {
CloudI.stderr_write(err.stack + '\n');
process.exit(1);
}
else {
API._exception(err);
if (API._fatal_exceptions) {
process.exit(1);
}
return_null_response = true;
}
if (return_null_response) {
var response_info = '';
var response = '';
try {
API.return_sync(name, pattern,
response_info, response,
timeout, trans_id, source);
}
catch (err_new) {
err_new = undefined;
}
}
if (API._terminate) {
API._poll_terminate();
}
});
domain_sync.enter();
process.nextTick(function () {
var response = f.obj_f.call(f.obj,
API.SYNC, name, pattern,
request_info, request,
timeout, priority,
trans_id, source);
if (typeof response === 'undefined') {
return;
}
domain_sync.exit();
var response_info;
if (typeof response === 'object' &&
toNativeString.call(response) == '[object Array]') {
response_info = response[0];
response = response[1];
if (typeof response_info !== 'string') {
response_info = '';
}
}
else {
response_info = '';
}
if (typeof response !== 'string') {
response = '';
}
try {
API.return_sync(name, pattern,
response_info, response,
timeout, trans_id, source);
}
catch (err_new) {
err_new = undefined;
}
});
return;
default:
throw new MessageDecodingException();
}
};
CloudI.API.prototype._handle_events = function (data, data_size, i, command) {
var API = this;
if (typeof command === 'undefined') {
if (i > data_size) {
throw new MessageDecodingException();
}
command = unpackUint32(i, data);
i += 4;
}
while (true) {
switch (command) {
case MESSAGE_TERM:
API._terminate = true;
throw new TerminateException(API._timeout_terminate);
case MESSAGE_REINIT:
API._process_count = unpackUint32(i, data);
i += 4;
API._timeout_async = unpackUint32(i, data);
i += 4;
API._timeout_sync = unpackUint32(i, data);
i += 4;
API._priority_default = unpackInt8(i, data);
i += 1;
API._fatal_exceptions = unpackUint8(i, data) != 0;
i += 1;
break;
case MESSAGE_KEEPALIVE:
API._send(new Erlang.OtpErlangAtom('keepalive'));
break;
default:
throw new MessageDecodingException();
}
if (i > data_size) {
throw new MessageDecodingException();
}
else if (i == data_size) {
return;
}
command = unpackUint32(i, data);
i += 4;
}
};
CloudI.API.prototype._poll_request = function (data) {
var API = this;
if (API._terminate) {
throw new TerminateException(API._timeout_terminate);
}
var data_size = data.length;
var i = 4;
while (true) {
var command = unpackUint32(i, data);
i += 4;
switch (command) {
case MESSAGE_INIT:
API._process_index = unpackUint32(i, data);
i += 4;
API._process_count = unpackUint32(i, data);
i += 4;
API._process_count_max = unpackUint32(i, data);
i += 4;
API._process_count_min = unpackUint32(i, data);
i += 4;
var prefix_size = unpackUint32(i, data);
i += 4;
var j = i + prefix_size - 1;
API._prefix = data.slice(i, j).toString('binary');
i = j + 1;
API._timeout_initialize = unpackUint32(i, data);
i += 4;
API._timeout_async = unpackUint32(i, data);
i += 4;
API._timeout_sync = unpackUint32(i, data);
i += 4;
API._timeout_terminate = unpackUint32(i, data);
i += 4;
API._priority_default = unpackInt8(i, data);
i += 1;
API._fatal_exceptions = unpackUint8(i, data) != 0;
i += 1;
var bind = unpackInt32(i, data);
i += 4;
if (bind >= 0) {
throw new InvalidInputException();
}
if (i != data_size) {
API._handle_events(data, data_size, i);
}
API._poll_callback(API);
API._poll_callback = undefined;
return;
case MESSAGE_SEND_ASYNC:
case MESSAGE_SEND_SYNC:
var name_size = unpackUint32(i, data);
i += 4;
var j = i + name_size - 1;
var name = data.slice(i, j).toString('binary');
i = j + 1;
var pattern_size = unpackUint32(i, data);
i += 4;
j = i + pattern_size - 1;
var pattern = data.slice(i, j).toString('binary');
i = j + 1;
var request_info_size = unpackUint32(i, data);
i += 4;
j = i + request_info_size;
var request_info = data.slice(i, j);
i = j + 1;
var request_size = unpackUint32(i, data);
i += 4;
j = i + request_size;
var request = data.slice(i, j);
i = j + 1;
var request_timeout = unpackUint32(i, data);
i += 4;
var priority = unpackInt8(i, data);
i += 1;
j = i + 16;
var trans_id = data.slice(i, j);
i = j;
var source_size = unpackUint32(i, data);
i += 4;
j = i + source_size;
var source = data.slice(i, j);
i = j;
if (i != data_size) {
API._handle_events(data, data_size, i);
}
Erlang.binary_to_term(source, function (err, source_object) {
if (err) {
API._exception(err);
API._poll_terminate();
}
else {
API._callback(command, name, pattern,
request_info, request,
request_timeout, priority,
trans_id, source_object);
}
});
return;
case MESSAGE_RECV_ASYNC:
case MESSAGE_RETURN_SYNC:
var response_info_size = unpackUint32(i, data);
i += 4;
var j = i + response_info_size;
var response_info = data.slice(i, j);
i = j + 1;
var response_size = unpackUint32(i, data);
i += 4;
j = i + response_size;
var response = data.slice(i, j);
i = j + 1;
j = i + 16;
var trans_id = data.slice(i, j);
i = j;
if (i != data_size) {
API._handle_events(data, data_size, i);
}
API._poll_callback(response_info, response, trans_id);
API._poll_callback = undefined;
return;
case MESSAGE_RETURN_ASYNC:
var j = i + 16;
var trans_id = data.slice(i, j);
i = j;
if (i != data_size) {
API._handle_events(data, data_size, i);
}
API._poll_callback(trans_id);
API._poll_callback = undefined;
return;
case MESSAGE_RETURNS_ASYNC:
var trans_id_count = unpackUint32(i, data);
i += 4;
var j;
var trans_ids = [];
for (trans_id_index = 0; trans_id_index < trans_id_count;
trans_id_index++) {
j = i + 16;
var trans_id = data.slice(i, j);
i = j;
trans_ids.push(trans_id);
}
if (i != data_size) {
API._handle_events(data, data_size, i);