-
Notifications
You must be signed in to change notification settings - Fork 0
/
teocli_init.js
981 lines (782 loc) · 28.6 KB
/
teocli_init.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/* global angular, device, cordova */
var teocli = undefined;
angular.module('app.services.Teocli', [])
.factory('showInConnect',function () {
return {
message: function(msg) {
var div = document.createElement('div');
div.innerHTML = msg;
div.className = "msg";
var el = document.getElementById('stage');
if(el !== undefined && el !== null)
el.appendChild(div);
},
line: function() {
this.message("----------");
}
};
})
.factory('initTeocli', function($location, $rootScope, $localStorage,
authTeocli, dataTeocli, showInConnect) {
var teocli_change_page_to = undefined;
var teocli_last_page = undefined;
var teocli_connect_to_idx = 0;
// Initialize teonet client connection
return {
teocli_reconnect_now: false,
teocli_login: false,
teocli_init: function(done) {
var self = this;
/**
* On client connected callback function example
*
* @param {type} ev
* @returns {undefined}
*/
function open_func(ev) {
console.log('onopen', teocli.client_name, ev);
var success = true;
//onotherTeocli.register(onotherTeocli._onother, onother_func);
// Send login to L0 server
var auth = new authTeocli();
if(!auth._checkUserLogin()) {
// Client name (random created)
teocli.client_name = "teo-cli-ws-" + Math.floor((Math.random() * 100) + 1);
teocli.login(teocli.client_name); // Send login command to L0 server
success = false;
}
else {
// Client name (userid:clientid)
teocli.client_name = $localStorage.user.userId + ':' + $localStorage.user.clientId;
//teocli.client_name = $localStorage.user.accessToken;
teocli.login($localStorage.user.accessToken); // Send login command to L0 server
}
// Send teocli-init event
// self.teocli_login = true;
// $rootScope.$broadcast('teocli-init');
if(teocli_last_page !== undefined) {
if(teocli_change_page_to !== undefined)
$location.path(teocli_last_page);
teocli_last_page = undefined;
}
// Call done function
if(typeof done === 'function') {
if(success) done(undefined, "success");
else done("err", undefined);
}
}
/**
* On error callback function example
*
* @param {type} ev
* @returns {undefined}
*/
function error_func(ev) {
console.log('error_func', ev);
}
/**
* On close callback function example
*
* @param {type} ev
* @returns {undefined}
*/
function close_func(ev) {
console.log('close_func', ev);
teocli = undefined;
self.teocli_login = false;
if(teocli_last_page === undefined)
teocli_last_page = $location.path();
if(teocli_change_page_to !== undefined)
$location.path(teocli_change_page_to);
// Send teocli-close event
$rootScope.$broadcast('teocli-close');
console.log('close_func.reconnect now: ', self.teocli_reconnect_now);
// Set timeout
var timeout = 5000;
if(self.teocli_reconnect_now || $rootScope.teocli_reconnect_now) {
timeout = 100;
self.teocli_reconnect_now = false;
}
// Reconnect
console.log('close_func.reconnect after', timeout, "ms");
setTimeout(init, timeout);
}
/**
* On message callback function example
*
* @param {type} ev
* @returns {undefined}
*/
function message_func(ev) {
//console.log('message_func', ev, ev.data.slice(0,20));
showInConnect.message(ev.data);
}
/**
* Check login answer in Other callback function
*
* @param {type} err
* @param {type} data
* @returns {int}
*/
function other_func (err, data) {
console.log("other_func" , data);
var processed = 0;
// Check login answer
if(data && data.cmd === 96) {
console.log("check login answer" , data);
// Send teocli-init event
self.teocli_login = true;
$rootScope.$broadcast('teocli-init');
$rootScope.networksItems = data.data.networks;
processed = 1;
}
return processed;
}
/**
* Set default local storage values
*
* @returns {undefined}
*/
function set_settings_default() {
console.log("mset_settings_default()");
$localStorage.settings = {
ws_server_addr: "teomac.ksproject.org",
ws_server_port: 80,
auth_separate: false,
auth_server_addr: "teomac.ksproject.org",
auth_server_port: 1234,
rememberMe: true,
autoLogin: false
};
}
function init() {
console.log("initTeocli" , "Start");
if($localStorage.settings === undefined) set_settings_default();
// Connect to websocket server
var ws = new WebSocket(dataTeocli.connect_to[teocli_connect_to_idx]);
// Create Teocli object
teocli = new Teocli(ws);
// Set netx connection index
if(++teocli_connect_to_idx >= dataTeocli.connect_to.length)
teocli_connect_to_idx = 0;
// Define received data callbacks
// teocli.onecho = echo_func; // Calls when echo_answer received
// teocli.onpeers = peers_func; // Calls when peers_answer received
teocli.onother = other_func; // Calls when some other command received
//
// Define common websocket callbacks
teocli.onopen = open_func; // Calls when client connected to websocket
teocli.onclose = close_func; // Calls when client disconnected from websocket
teocli.onerror = error_func; // Calls when websocket error hapend
teocli.onmessage = message_func; // Calls when websocket message received
}
init();
}
};
//return teocli_init;
})
/**
* Teonet event callback function registration
*
* @param {type} $rootScope
* @returns {onotherTeocli}
*/
.factory('onotherTeocli', function($rootScope) {
var self; // To save this
// Literal constants
var _onclients = "clients";
var _onpeers = "onpeers";
var _onother = "onother";
var _onecho = "onecho";
// Initialize arrays
function init_ar(ar) {
ar[_onclients] = [];
ar[_onpeers] = [];
ar[_onother] = [];
ar[_onecho] = [];
}
// check if an element exists in array using a comparer function
// comparer : function(currentElement)
Array.prototype.inArray = function(comparer, exists_cb) {
var retval = false;
for(var i=0; i < this.length; i++) {
if(comparer(this[i])) {
if(typeof exists_cb === 'function') exists_cb(this[i]);
retval = true;
}
}
return retval;
};
// adds an element to the array if it does not already exist using a comparer
// function
Array.prototype.pushIfNotExist = function(element, comparer, done_cb, exists_cb) {
//console.log("pushIfNotExist", element);
if(!this.inArray(comparer, exists_cb)) {
this.push(element);
if(typeof done_cb === 'function')
done_cb(element);
}
};
Array.prototype.doIfNotExist = function(element, comparer, do_cb) {
if(!this.inArray(comparer)) {
if(typeof do_cb === 'function')
do_cb(element);
}
};
String.prototype.startsWith = function(needle)
{
return(this.indexOf(needle) === 0);
};
return {
onclients_save: undefined,
onpeers_save: undefined,
onother_save: undefined,
onecho_save: undefined,
onother_cb_ar: Object.create(null),
onother_ar: Object.create(null),
_onclients: _onclients,
_onpeers: _onpeers,
_onother: _onother,
_onecho: _onecho,
/**
* Initialize onotherTeocli factory
*
* @returns {undefined}
*/
init: function() {
console.log("onotherTeocli.init");
init_ar(this.onother_cb_ar);
init_ar(this.onother_ar);
self = this;
// On teocli init
$rootScope.$on('teocli-init', function() {
console.log("onotherTeocli.init.'teocli-init'");
// onclients
self.onclients_save = teocli.onclients;
teocli.onclients = self.process_onclients_func;
// onpeers
self.onpeers_save = teocli.onpeers;
teocli.onpeers = self.process_onpeers_func;
// onother
self.onother_save = teocli.onother;
teocli.onother = self.process_onother_func;
// onecho
self.onecho_save = teocli.onecho;
teocli.onecho = self.process_onecho_func;
});
// On teocli close(disconnect) event
$rootScope.$on('teocli-close', this.destroy);
},
/**
* Destroy onotherTeocli factory
*
* @returns {undefined}
*/
destroy: function() {
console.log("onotherTeocli.destroy");
if(teocli !== undefined) {
teocli.onclients = this.onclients_save;
teocli.onpeers = this.onpeers_save;
teocli.onother = this.onother_save;
teocli.onecho = this.onecho_save;
}
//if(this.onother_ar !== undefined) {
this.onother_cb_ar = Object.create(null);
this.onother_ar = Object.create(null);
//}
},
/**
* Register callback function
*
* @param {type} type_func Type of function: onclients, onpeers, onother, onecho
* @param {type} func Callback function to register
* @param {type} cb Users callback, calls when processed
* @returns {undefined}
*/
register: function(type_func, func, cb) {
console.log("onotherTeocli.register", type_func);
var index = this.onother_ar[type_func].indexOf(func);
if (!(index > -1)) {
console.log("onotherTeocli.register.function", type_func);
this.onother_ar[type_func].push(func);
this.onother_cb_ar[type_func].push(cb);
}
},
/**
* Unregister callback function
*
* @param {type} type_func
* @param {type} func
* @returns {undefined}
*/
unregister: function(type_func, func) {
console.log("onotherTeocli.unregister", type_func);
var index = this.onother_ar[type_func].indexOf(func);
if (index > -1) {
console.log("onotherTeocli.unregister.function", type_func);
this.onother_ar[type_func].splice(index, 1);
this.onother_cb_ar[type_func].splice(index, 1);
}
},
/**
* Process _onpeers event: find and call calback functions
*
* @param {type} err
* @param {type} data
* @returns {undefined}
*/
process_onpeers_func: function(err, data) {
//console.log("onotherTeocli.process_onpeers_func");
var processed = 0, len = self.onother_ar[_onpeers].length;
for(var i = 0; i < len &&
(!(typeof self.onother_ar[_onpeers][i] === 'function') ||
!(processed = self.onother_ar[_onpeers][i](err, data, self.onother_cb_ar[_onpeers][i])));
i++);
//console.log("onotherTeocli.process_onpeers_func.i:", i, ", processed:" , processed);
if(!processed) {
if(self.onpeers_save !== undefined) {
//console.log("onotherTeocli.process_onpeers_func.execute.onpeers_save");
if(typeof self.onpeers_save === 'function' )
self.onpeers_save(err, data);
}
}
},
/**
* Process _onclients event: find and call calback functions
*
* @param {type} err
* @param {type} data
* @returns {undefined}
*/
process_onclients_func: function(err, data) {
//console.log("onotherTeocli.process_onclients_func");
var processed = 0, len = self.onother_ar[_onclients].length;
for(var i = 0; i < len &&
!(processed = self.onother_ar[_onclients][i](err, data, self.onother_cb_ar[_onclients][i])); i++);
//console.log("onotherTeocli.process_onclients_func.i:", i, ", processed:" , processed);
if(!processed) {
if(self.onclients_save !== undefined) {
//console.log("onotherTeocli.process_onclients_func.execute.onclients_save");
if(typeof self.onclients_save === 'function' )
self.onclients_save(err, data);
}
}
},
/**
* Process _onother event: find and call calback functions
*
* @param {type} err
* @param {type} data
* @returns {undefined}
*/
process_onother_func: function(err, data) {
//console.log("onotherTeocli.process_onother_func");
var processed = 0, len = self.onother_ar[_onother].length;
for(var i = 0; i < len &&
!(processed = self.onother_ar[_onother][i](err, data, self.onother_cb_ar[_onother][i])); i++);
//console.log("onotherTeocli.process_onother_func.i:", i, ", processed:" , processed);
if(!processed) {
if(self.onother_save !== undefined) {
//console.log("onotherTeocli.process_onother_func.execute.onother_save");
if(typeof self.onother_save === 'function' )
self.onother_save(err, data);
}
}
},
/**
* Process _onecho event: find and call calback functions
*
* @param {type} err
* @param {type} data
* @returns {undefined}
*/
process_onecho_func: function(err, data) {
//console.log("onotherTeocli.process_onecho_func");
var processed = 0, len = self.onother_ar[_onecho].length;
for(var i = 0; i < len &&
!(processed = self.onother_ar[_onecho][i](err, data, self.onother_cb_ar[_onecho][i])); i++);
//console.log("onotherTeocli.process_onecho_func.i:", i, ", processed:" , processed);
if(!processed) {
if(self.onecho_save !== undefined) {
//console.log("onotherTeocli.process_onecho_func.execute.onecho_save");
if(typeof self.onecho_save === 'function' )
self.onecho_save(err, data);
}
}
}
};
})
.factory('dbTeocli', function(dataTeocli) {
return {
id: 0,
/**
* Send CMD_D_SET command
*
* @param cmd Command SET = 0, GET = 1 or GET_LIST = 2
* @param key Key
* @param data Value
* @param db Database peer name
*
* @returns {undefined}
*/
send_db_request: function (cmd, key, data, db) {
// DB request object
var request = {
cmd: cmd + 129,
to: db === undefined ? dataTeocli.db : db,
data: "JSON: " + JSON.stringify({
key: key,
id: ++this.id,
//data: data !== undefined ? JSON.stringify(data) : undefined
data: data
})
};
// Stringify request
var request_str = JSON.stringify(request);
console.log("send_db_request: " + request_str);
// Send db request to teonet
if(teocli !== undefined)
teocli.send(request_str);
return this.id;
},
/**
* Send CMD_D_SET command
*
* @param db Database name
* @param cmd Command SET = 0, GET = 1 or GET_LIST = 2
* @param key Key
* @param data Value
*
* @returns {undefined}
*/
send_db_request_multy: function (db, cmd, key, data) {
return this.send_db_request(cmd, key, data, db);
}
// /**
// * Send CMD_D_SET command
// *
// * @param db Database name
// * @param cmd Command SET = 0, GET = 1 or GET_LIST = 2
// * @param key Key
// * @param data Value
// *
// * @returns {undefined}
// */
// send_db_request_multy: function (db, cmd, key, data) {
//
// // DB request object
// var request = {
//
// cmd: cmd + 129,
// to: db,
// data: "JSON: " + JSON.stringify({
// key: key,
// id: ++this.id,
// //data: data !== undefined ? JSON.stringify(data) : undefined
// data: data
// })
// };
//
// // Stringify request
// var request_str = JSON.stringify(request);
// console.log("send_db_request: " + request_str);
//
// // Send db request to teonet
// if(teocli !== undefined)
// teocli.send(request_str);
//
// return this.id;
// }
};
})
// Check peers present and show it in menu service
.service('guiServiceTeocli', function ($rootScope, dataTeocli, initTeocli,
onotherTeocli) {
var db_gl_peersItems = [];
var mm_peersItems = [];
var statLr_peersItems = [];
var rproxy_peersItems = [];
console.log('guiServiceTeocli');
//$rootScope.$on('teocli-init', init); // On teonet open(connect) event
$rootScope.$on('teocli-close', destroy); // On teonet close(disconnect) event
$rootScope.$on('$destroy', destroy); // On service distroy
items_clear();
this.login = init;
/**
* Menu items variables clear
* @returns {undefined}
*/
function items_clear() {
// db
$rootScope.db_peersNumber = 0;
$rootScope.db_peersItems = [];
// db_gl
$rootScope.db_gl_peersNumber = 0;
db_gl_peersItems = [];
// room
$rootScope.room_peersNumber = 0;
$rootScope.room_peersItems = [];
// mm
$rootScope.mm_peersNumber = 0;
mm_peersItems = [];
// StatLR
$rootScope.statLr_peersNumber = 0;
statLr_peersItems = [];
// r-proxy
$rootScope.rproxy_peersNumber = 0;
rproxy_peersItems = [];
}
/**
* Set teocli callbacks
* @returns {undefined}
*/
function init() {
console.log('guiServiceTeocli.init');
if(teocli !== undefined && initTeocli.teocli_login) {
onotherTeocli.register(onotherTeocli._onother, db_peers_check);
onotherTeocli.register(onotherTeocli._onpeers, db_peers_check);
send_peers_subscribe();
}
items_clear();
}
/**
* Free teocli callbacks
*
* @returns {undefined}
*/
function destroy() {
console.log('guiServiceTeocli.destroy');
if(teocli !== undefined) {
send_peers_unsubscribe();
}
onotherTeocli.unregister(onotherTeocli._onother, db_peers_check);
onotherTeocli.unregister(onotherTeocli._onpeers, db_peers_check);
items_clear();
}
/**
* Check DB peers callback function
*
* @param {type} err
* @param {type} data
* @returns {undefined}
*/
function db_peers_check (err, data) {
var processed = 0;
//console.log('guiServiceTeocli.db_func', data);
// Process peers info answer
if(data.cmd === 73) {
onotherTeocli.unregister(onotherTeocli._onpeers, db_peers_check);
//data.data.arp_data_ar;
console.log("guiServiceTeocli.db_peers_check.peers ", data);
for(var i = 0; i < data.data.arp_data_ar.length; i++) {
console.log("guiServiceTeocli.db_peers_check.peers ",
data.data.arp_data_ar[i].name);
send_peer_info_request(data.data.arp_data_ar[i].name);
}
}
// Process command #83 CMD_SUBSCRIBE_ANSWER
else if(data.cmd === teocli.CMD.CMD_SUBSCRIBE_ANSWER) {
// Peer Connected event EV_K_CONNECTED #3
if(data.data.ev === 3) {
var peer_name = atob(data.data.data);
console.log("guiServiceTeocli.db_peers_check.connected ",
$rootScope.db_peersNumber, data, peer_name);
send_peer_info_request(peer_name);
//processed = 1;
}
// Peer disconnected EV_K_DISCONNECTED #4
else if(data.data.ev === 4) {
var peer_name = atob(data.data.data);
console.log("guiServiceTeocli.db_peers_check.disconnected ",
$rootScope.db_peersNumber, data, peer_name);
send_peer_info_request(peer_name);
//processed = 1;
}
}
// Process command #91 CMD_HOST_INFO_ANSWER
else if(data.cmd === teocli.CMD.HOST_INFO_ANSWER) {
console.log(
"guiServiceTeocli.db_peers_check.host_info_answer, data:",
data);
for(var i = 0; i < data.data.type.length; i++) {
console.log(
"guiServiceTeocli.db_peers_check.host_info_answer, type:",
data.data.type[i]);
// Check DB peers
if(data.data.type[i] === dataTeocli.db) {
var d = data.from;
$rootScope.db_peersItems.pushIfNotExist(d, function(e) {
return e === d;
}, function(e) {
$rootScope.db_peersNumber += 1;
$rootScope.$apply();
});
// Check DB_GL peers
if(data.data.name === dataTeocli.db_gl) {
var d = data.from;
db_gl_peersItems.pushIfNotExist(d, function(e) {
return e === d;
}, function(e) {
$rootScope.db_gl_peersNumber += 1;
$rootScope.$apply();
});
}
}
// Check ROOM peers
else if(data.data.type[i] === dataTeocli.room) {
var d = data.from;
$rootScope.room_peersItems.pushIfNotExist(d, function(e) {
return e === d;
}, function(e) {
$rootScope.room_peersNumber += 1;
$rootScope.room_peersItems.sort(function(a, b) {
return a === b ? 0 : (a < b ? -1 : 1);
});
$rootScope.$apply();
});
}
// Check MM peers
else if(data.data.type[i] === dataTeocli.mm) {
var d = data.from;
mm_peersItems.pushIfNotExist(d, function(e) {
return e === d;
}, function(e) {
$rootScope.mm_peersNumber += 1;
$rootScope.$apply();
});
}
// Check StatLR peers
else if(data.data.type[i] === dataTeocli.stat_lr) {
var d = data.from;
statLr_peersItems.pushIfNotExist(d, function(e) {
return e === d;
}, function(e) {
$rootScope.statLr_peersNumber += 1;
$rootScope.$apply();
});
}
// Check RProxy peers
else if(data.data.type[i] === dataTeocli.rproxy) {
var d = data.from;
rproxy_peersItems.pushIfNotExist(d, function(e) {
return e === d;
}, function(e) {
$rootScope.rproxy_peersNumber += 1;
$rootScope.$apply();
});
}
}
}
return processed;
}
/**
* Send subscribe request
*
* @returns {undefined}
*/
function send_peers_subscribe() {
console.log("guiServiceTeocli.send_peers_subscribe to ", dataTeocli.l0);
// Request peers info
teocli.peers(dataTeocli.l0);
// Send CMD_SUBSCRIBE Subscribe to Peers Connected / Disconnected
teocli.send('{ "cmd": 81, "to": "' + dataTeocli.l0 + '", "data": "TEXT:3" }');
teocli.send('{ "cmd": 81, "to": "' + dataTeocli.l0 + '", "data": "TEXT:4" }');
}
/**
* Send unsubscribe request
*
* @returns {undefined}
*/
function send_peers_unsubscribe() {
console.log("guiServiceTeocli.send_peers_unsubscribe from ", dataTeocli.l0);
// Send CMD_SUBSCRIBE UnSubscribe to Peers Connected / Disconnected
teocli.send('{ "cmd": 82, "to": "' + dataTeocli.l0 + '", "data": "TEXT:3" }');
teocli.send('{ "cmd": 82, "to": "' + dataTeocli.l0 + '", "data": "TEXT:4" }');
}
/**
* Send host info request
*
* @param name Peers name
*
* @returns {undefined}
*/
function send_peer_info_request(name) {
console.log("PeersController.peers_func.send_peer_info_request:", name);
//if(teocli !== undefined)
teocli.send('{ "cmd": 90, "to": "' + name + '", "data": "JSON" }');
};
})
.factory('Base64', function () {
/* jshint ignore:start */
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
return {
encode: function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
},
decode: function (input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(input)) {
window.alert("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
"Expect errors in decoding.");
}
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 !== 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 !== 64) {
output = output + String.fromCharCode(chr3);
}
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);
return output;
}
};
/* jshint ignore:end */
})
;