-
Notifications
You must be signed in to change notification settings - Fork 0
/
teocli.module.ts
670 lines (583 loc) · 17.1 KB
/
teocli.module.ts
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
/*
* The MIT License
*
* Copyright 2017 Kirill Scherba <kirill@scherba.ru>.
*
* 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.
*/
import { Injectable } from '@angular/core';
import { TeonetAuth } from './teocli.auth';
import { TeocliRTC } from './teocli.webrtc';
export interface TeonetEventType {
TEONET_INIT: string,
TEONET_LOGGEDIN: string,
TEONET_CLOSE: string
};
export type eventSubscribersFunc = (event: string, ...obj: object[]) => number;
export type onotherData = {
cmd: number,
from: string,
data: any
};
export type onechoData = {
cmd: number,
from: string,
data: {
msg: string,
time: number
}
};
export const Teonet = {
peer: {
l0: 'ps-server',
auth: 'teo-auth'
},
status: {
offline: 0,
connecting: 1,
logining: 2,
online: 3
}
}
@Injectable()
export class TeonetCli extends TeocliRTC {
status: number;
protected client_name: string;
private connect_url: string;
private inited: boolean;
private RECONNECT_TIMEOUT: number;
private INIT_TIMEOUT: number;
private isTeonetClientsActiveFunc: () => boolean;
private eventSubscribers: Array<eventSubscribersFunc>;
private login_page: any;
private signup_page: any;
private restore_page: any;
static EVENT: TeonetEventType = {
TEONET_INIT: 'teonet-init',
TEONET_CLOSE: 'teonet-close',
TEONET_LOGGEDIN: 'teonet-loggedin'
};
constructor() {
console.debug("TeonetCli::constructor");
var connect_url = 'ws://' + 'teomac.ksproject.org:80' + '/ws';
var ws = new WebSocket(connect_url);
super(ws);
this.status = Teonet.status.connecting
this.isTeonetClientsActiveFunc = () => { return true; };
this.connect_url = connect_url;
this.RECONNECT_TIMEOUT = 1000;
this.INIT_TIMEOUT = 7000;
this.inited = false;
this.eventSubscribers = [];
this.ws = ws;
this.init();
}
private init(): void {
let authserver = new TeonetAuth(this);
// When connected to websocket
this.onopen = (ev) => {
console.debug("TeonetCli::teocli.onopen");
// Prepare client name
this.status = Teonet.status.logining;
if (!this.client_name) {
// Get name from local storage
let user = authserver.storage.get();
console.debug("TeonetCli::init user", user);
if (user.remember_me && user.accessToken) {
this.setClientName(user.accessToken);
}
// Set temporary name
else {
this.setClientName('teoclits-' + Math.floor((Math.random() * 100) + 1));
}
}
// Send login command to teonet L0 server
this.login(this.client_name);
// Set reconnect timeout (reconnect if does not login during timeout
setTimeout(() => {
if (!this.isInit()) {
console.debug("TeonetCli::teocli.onopen can't login - disconnect");
this.disconnect();
}
}, this.INIT_TIMEOUT);
};
// When disconnect from web socket (reconnect)
// Send 'teonet-close' to subscribers
this.onclose = (ev) => {
console.debug("TeonetCli::teocli.onclose");
// Reconnect after timeout
setTimeout(() => {
delete this.ws;
this.status = Teonet.status.connecting;
this.ws = new WebSocket(this.connect_url);
this.ws.onopen = this.onopen;
this.ws.onclose = this.onclose;
this.ws.onerror = this.onerror;
this.ws.onmessage = (ev) => {
if (!this.process(ev.data)) {
//if (typeof this.onmessage === 'function') {
// this.onmessage(ev);
//}
}
};
}, this.RECONNECT_TIMEOUT);
this.sendEvent(TeonetCli.EVENT.TEONET_CLOSE);
this.status = Teonet.status.offline;
this.inited = false;
};
// When get websocket error
this.onerror = function(ev) { };
// When get 'onother' message fom webspcket. Check cmd 96 - teo-auth answer
// Send 'teonet-init' to subscribers
// Send 'onother' to subscribers
this.onother = (err, data: object) => {
console.debug("TeonetCli::onother", err, data);
var processed = 0;
var d = <onotherData>data;
// Check login answer
if (d && d.cmd === 96) {
console.debug("TeonetCli:: Got check login answer", d);
// Set name
this.setClientName(d.data.name);
// Get user from storage
const user = authserver.storage.get();
// Send teocli-init event
const sendEventInit = (loggedin = false) => {
this.sendEvent(TeonetCli.EVENT.TEONET_INIT);
this.status = Teonet.status.online;
this.inited = true;
if (loggedin) this.sendEvent(TeonetCli.EVENT.TEONET_LOGGEDIN);
//$rootScope.networksItems = data.data.networks;
};
// Login success
if (this.getClientName() == user.userId + ':' + user.clientId) {
sendEventInit(true);
}
else {
// Login with saved email and password if remember_me is set
if (user.remember_me) {
//authserver.refresh(); // refresh auth token
authserver.login(user.email, authserver.base64.decode(user.password),
(err: any, response: any) => {
if (err) {
// Goto Login screen
sendEventInit();
this.loginPage();
}
else {
// Reconect with new client name
this.setClientName(authserver.getUser().accessToken);
this.disconnect();
}
}
);
}
// Goto Login page
else {
sendEventInit();
this.loginPage();
}
}
processed = 1;
}
if (!processed) this.sendEvent('onother', data);
return processed;
}
// Send 'onecho' to subscribers
this.onecho = (err, data: object) => {
console.debug("TeonetCli::onecho", err, data);
this.sendEvent('onecho', data);
return 1;
}
// Send 'onclients' to subscribers
this.onclients = (err, data: object) => {
console.debug("TeonetCli::onclients", err, data);
this.sendEvent('onclients', data);
return 1;
}
// Send 'onpeers' to subscribers
this.onpeers = (err, data: object) => {
console.debug("TeonetCli::onpeers", err, data);
this.sendEvent('onpeers', data);
return 1;
}
}
/**
* Disconnect TeonetCli
*
*/
disconnect(): void {
this.ws.close();
}
getConnectUrl() {
return this.connect_url.replace('ws://', '').replace('/ws', '');
}
setConnectUrl(url: string) {
this.connect_url = 'ws://' + url + '/ws';
}
/**
* Sel login page
*/
setLoginPage(func: () => void) {
this.login_page = func;
}
/**
* Load Login page
*/
loginPage(push: boolean = undefined) {
if (this.login_page) this.login_page(push);
else {
alert('TODO: Login page is udefined!');
}
}
/**
* Sel SignUp page
*/
setSignupPage(func: () => void) {
this.signup_page = func;
}
/**
* Load SignUp page
*/
signupPage(push: boolean = undefined) {
if (this.signup_page) this.signup_page(push);
else {
alert('TODO: SignUp page is udefined!');
}
}
/**
* Sel Restore page
*/
setRestorePage(func: () => void) {
this.restore_page = func;
}
/**
* Load Restore page
*/
restorePage(push: boolean = undefined) {
if (this.restore_page) this.restore_page(push);
else {
alert('TODO: Restore page is udefined!');
}
}
/**
* Is TeonetCli connected and initialized
*
* @returns {boolean} True if connected and initialized
*/
isInit(): boolean {
return this.inited;
}
/**
* Get teonet status
*
* @return {number} Teonet status
*/
getStatus(): number {
return this.status;
}
/**
* Get client name
*
* @return {string} Teonet client name
*/
getClientName(): string {
return this.client_name;
}
/**
* Set client name
*
* @return {string} Teonet client name
*/
setClientName(name: string = '') {
this.client_name = name;
}
/**
* Subscribe to even
*
* @param {eventSubscribersFunc} func
*/
subscribe(func: eventSubscribersFunc) {
this.eventSubscribers.push(func);
return func;
}
/**
* Unsubscribe from event
*
* @param {eventSubscribersFunc} func
*/
unsubscribe(func: eventSubscribersFunc): void {
this.eventSubscribers = this.eventSubscribers.filter(f => f !== func);
}
/**
* Send event to Event Subscribers
*
* @param {string} ev Event name
* @param {object[]) ...obj Objects send to subscribers
*/
sendEvent(ev: string, ...obj: object[]): void {
for (var func of this.eventSubscribers) {
func(ev, obj);
}
super.sendEvent(ev, obj); // Send event to parrent class
}
whenEvent(event: string, func: (...obj: object[]) => number) {
return this.subscribe((ev: string, ...obj: object[]): number => {
if (ev == event) {
func(obj);
}
return 0;
});
}
whenInit(func: () => void) {
if (this.isInit()) func();
return this.whenEvent(TeonetCli.EVENT.TEONET_INIT, () => {
func();
return 0;
});
}
whenClose(func: () => void) {
return this.whenEvent(TeonetCli.EVENT.TEONET_CLOSE, () => {
func();
return 0;
});
}
isTeonetClientsActive() {
return this.isTeonetClientsActiveFunc();
}
setTeonetClientsActive(func: () => boolean) {
this.isTeonetClientsActiveFunc = func;
}
};
import { Component } from '@angular/core';
import { AfterContentInit } from '@angular/core';
import { IntervalObservable } from 'rxjs/observable/IntervalObservable';
import { TeonetClientsNum } from './teocli.clients';
@Component({
selector: 'teonet-status',
inputs: ['peer', 'label', 'show_peer', 'reconnect'],
styles: ['\n\
.teonet-status { \n\
opacity: 0.85;\n\
position: relative;\n\
float: right;\n\
right: 20px;\n\
top: 10px;\n\
/*font-size: 60%;*/\n\
}\n\
/deep/ ion-nav.menu-content-open + teonet-status {\n\
display: none;\n\
}\n\
/deep/ ion-app.platform-ios .teonet-status {\n\
top: 22px;\n\
}\n\
.teonet-status-no-badge {\n\
top: 3px;\n\
}\n\
/deep/ ion-app.platform-ios .teonet-status-no-badge {\n\
top: 35px;\n\
}\n\
/deep/ ion-app.platform-android .teonet-status-no-badge {\n\
top: 20px;\n\
}\n\
.teonet-status-badge {\n\
position: absolute;\n\
bottom: -15px;\n\
right: 0;\n\
font-size: 70%;\n\
}\n\
.teonet-badge {\n\
padding: 3px 8px;\n\
text-align: center;\n\
display: inline-block;\n\
min-width: 10px;\n\
/* font-size: 1.3rem; */\n\
font-weight: bold;\n\
line-height: 1;\n\
white-space: nowrap;\n\
vertical-align: baseline;\n\
}\n\
'],
template: '\n\
<span class="teonet-status \n\
text-md-{{ getStatusColor(t.status) }}\n\
{{ (!ifPeer(peer) ? \' teonet-status-no-badge\' : \'\') }}">\n\
{{\n\
(ifOnline() ? label + " " : "") + \n\
getStatusText(t.status) + \n\
(ifOnline() && tcli.num_clients ? ": " + tcli.num_clients : "")\n\
}}\n\
<div *ngIf="ifPeer(peer)"\n\
class="teonet-badge teonet-status-badge badge-md\n\
badge-md-{{ time >= 0 ? \'secondary\' : \'danger\'}}"\n\
>\n\
{{\n\
(peer && show_peer == "true" ? (peer | titlecase) + ": " : "") + \n\
(time >= 0 ? time + " ms" : "offline")\n\
}}\n\
</div>\n\
</span>'
})
/**
* Show teonet connection status
*/
export class TeonetStatus implements AfterContentInit {
peer: string = ''; //! Peer name Component input
label: string = 'Teo is'; //! Label of state Component input
reconnect: string = 'true'; //! Reconnect when peer don't answer
show_peer: string = 'false'; //! Show peer name Component input
tcli: TeonetClientsNum; //! TeonetClientsNum object
time: number = (this.t.status == Teonet.status.online) ? 0 : -1; //! Peer answer time in ms
private last_answere = 0.00; //! Last peer answer time
private SEND_ECHO_AFTER = 1.00; //! Send echo after timeout
private SET_LOGOFF_AFTER = 3.00;//! Set status logoff after timeout
private RECONNECT_AFTER = 8.00; //! Reconnect after timeout
/**
* Constructor connect to Teonet and load TeonetClientsNum class
*/
constructor(public t: TeonetCli) {
console.debug('TeonetStatus::constructor, peer = ' + this.peer);
this.tcli = new TeonetClientsNum(t);
}
/**
* This function calls by angular after html content init
*/
ngAfterContentInit() {
console.debug('TeonetStatus::ngAfterContentInit, peer = ' + this.peer +
', reconnect: ' + this.reconnect);
if (this.peer) {
//Send ping to peer
this.sendEcho();
IntervalObservable.create(1000).subscribe(() => {
if (this.t.isInit()) {
let date = new Date();
let current = date.valueOf() / 1000.0;
if ((current - this.last_answere) > this.SEND_ECHO_AFTER) {
this.sendEcho();
}
if (!this.last_answere) this.last_answere = current;
if (this.time >= 0 && this.last_answere &&
(current - this.last_answere) > this.SET_LOGOFF_AFTER) {
console.debug('TeonetStatus::IntervalObservable - set "peer logoff"');
this.time = -1;
}
if (this.last_answere &&
(current - this.last_answere) > this.RECONNECT_AFTER &&
this.reconnect == 'true') {
console.debug('TeonetStatus::IntervalObservable' +
'- disconnect from teonet, ' +
'(current - this.last_answere) = ',
(current - this.last_answere)
);
this.last_answere = 0;
this.t.disconnect();
}
}
else this.time = -1;
});
// Process echo answer
this.t.whenEvent('onecho', (data: any): number => {
let d: onechoData = data[0][0];
console.debug('TeonetStatus::onecho', data, d, 'this.peer:', this.peer);
if (d.from == this.peer) {
let date = new Date();
this.time = d.data.time;
this.last_answere = date.valueOf() / 1000.0;
console.debug('TeonetStatus::onecho this.last_answere:', this.last_answere);
}
return 0;
});
}
}
/**
* Send ping to peer
*/
private sendEcho() {
console.debug('TeonetStatus::sendEcho to peer: ' + this.peer);
this.t.echo(this.peer, 'TeonetStatus');
}
/**
* Return true if Teonet is online
* @return {boolean}
*/
ifOnline() {
return this.t.status == Teonet.status.online;
}
/**
* Return true if peer input is set
*
* @param {string} peer Peer name
*/
ifPeer(peer: string) {
if (peer) return true;
else return false;
}
/**
* Get Teonet status color
*
* @param {number} Teonet status
* @return Teonet status color string
*/
getStatusColor(status: number): string {
let color: string;
switch (status) {
case Teonet.status.offline:
color = 'danger';
break;
case Teonet.status.connecting:
color = 'dark';
break;
case Teonet.status.logining:
color = 'primary';
break;
case Teonet.status.online:
color = 'secondary';
break;
default:
color = 'light';
}
return color;
}
/**
* Get name of Teonet status
*
* @param {number} Teonet status
* @return Teonet status string
*/
getStatusText(status: number): string {
let text: string;
switch (status) {
case Teonet.status.offline:
text = 'Offline';
break;
case Teonet.status.connecting:
text = 'Connecting...';
break;
case Teonet.status.logining:
text = 'Logining';
break;
case Teonet.status.online:
text = 'online';
break;
default:
text = '';
}
return text;
}
}