-
Notifications
You must be signed in to change notification settings - Fork 0
/
teocli.clients.ts
514 lines (433 loc) · 15 KB
/
teocli.clients.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
/*
* 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 {Component, OnDestroy} from '@angular/core';
import {TeonetCli, Teonet, onotherData, onechoData} from './teocli.module';
import {IntervalObservable} from 'rxjs/observable/IntervalObservable';
export type onclientDisplArEl = {name: string, translate: string, email: string, type: string, time: number};
export type onclientDisplAr = Array<onclientDisplArEl>;
type onclientsDataAr = Array<{name: string}>;
type onclientsData = {
cmd: number,
from: string,
data: {
length: number,
client_data_ar: onclientsDataAr
}
};
export class TeonetClientsRequest {
constructor(private t: TeonetCli) {}
/**
* Send user info request
*
* @param {string} name Clients accessToken
* @returns {undefined}
*/
sendUserInfo (name: string) {
//console.debug("TeonetCli.send_user_info_request", name);
//if(teocli !== undefined)
this.t.send('{ "cmd": 132, "to": "' + Teonet.peer.auth + '", "data": ["' + name + '"] }');
}
/**
* Send clients info request
*
* @param {type} client
* @returns {undefined}
*/
sendClientInfo(client: string) {
//console.debug("ClientsController.send_client_info_request", client);
//if(teocli !== undefined)
this.t.send('{ "cmd": 134, "to": "' + Teonet.peer.auth + '", "data": ["' + client + '"] }');
}
}
@Component({
selector: 'teonet-clients',
styles: ['\n\
.border-right {\n\
border-right: solid 1px #eee;\n\
}\n\
.header {\n\
border: solid 1px #eee;\n\
}\n\
'],
template: '\n\
<div class="row item-divider item header toolbar-background-md">\n\
<div class="col border-right" col-1>№</div>\n\
<div class="col border-right">User name</div>\n\
<div class="col border-right">Client type</div>\n\
<div class="col" col-2 text-right>Ping</div>\n\
</div>\n\
\n\
<div class="content">\n\
<div class="teonet-clients-body row item {{ (i%2 ? \'toolbar-background-md\' : \'\') }}" \n\
*ngFor="let client of clients; index as i; first as isFirst">\n\
<div class="col border-right" col-1>{{i+1}}</div>\n\
<div class="col border-right"><a href="#">{{(client.translate ? client.translate : client.name) | slice:0:15}}</a></div>\n\
<div class="col border-right">{{(client.type ? client.type : "") | slice:0:15}}</div>\n\
<div class="col" col-2 text-right>{{(client.time ? client.time : "")}}</div>\n\
</div>\n\
</div>\n\
\n'
// <div class="row text-center padding" [hidden]="clients.length">\n\
// <ion-col col-1><ion-spinner icon="lines"></ion-spinner></ion-col>\n\
// <ion-col class="padding">Clients loading...</ion-col>\n\
// </div>\n\
})
/**
* Clients class
*/
export class TeonetClients implements OnDestroy {
n: number = 0;
tabIndex: number;
clients: onclientDisplAr = [];
private f: Array<any> = [];
private onclients: any;
private cli = new TeonetClientsTranslate(this.t);
private interval = IntervalObservable.create(1000).subscribe(() => { //.subscribe(n => this.n = n);
this.n++;
if (this.isComponentActive()) this.t.clients(Teonet.peer.l0);
});
constructor(private t: TeonetCli) {
//console.debug('TeonetClients::constructor');
this.f.push(t.whenInit(() => {
//console.debug('TeonetClients::whenInit');
t.clients(Teonet.peer.l0);
}));
this.f.push(t.whenClose(() => {
//console.debug('TeonetClients::whenClose');
this.clients = [];
}));
this.f.push(t.whenEvent('onclients', (data: any): number => {
let need_sort = false;
let d = <onclientsData>(data[0][0]);
if (d.from == Teonet.peer.l0) {
//console.debug('TeonetClients::onclients', data, d, this.clients);
// Remove not existing (disconnected) clients
let clients = this.clients.filter(
function (x) {
return d.data.client_data_ar.inArray(
function (e) {
return x.name === e.name;
},
function () {}
);
}
);
if (clients.length != this.clients.length) {
this.clients = clients;
//need_sort = true;
}
// Add new clients to clients array
for (let client of d.data.client_data_ar) {
let el: onclientDisplArEl = {
name: client.name,
translate: '',
email: '',
type: '',
time: 0
};
this.clients.pushIfNotExist(
// Client to add
el,
// Comparer
function (currentElement: onclientDisplArEl) {
return currentElement.name === el.name;
},
// Done
(currentElement: onclientDisplArEl) => {
//console.debug('TeonetClients::pushIfNotExist Add element:',
// currentElement);
// Send user_info & client_info requests
let name_split = currentElement.name.split(':');
if (name_split[0]) {
//this.cli.sendUserInfo(name_split[0]);
this.cli.getUserInfo(name_split[0],
(err: any, user: TeonetUserInfo) => {
if (!err) {
this.clients.inArray(
// Comparer
function (currentElement: onclientDisplArEl) {
let name_split = currentElement.name.split(':');
return name_split[0] === user.userId;
},
// Exists
function (currentElement: onclientDisplArEl) {
currentElement.translate = user.username;
currentElement.email = user.email;
}
);
}
}
);
}
if (name_split[1]) {
this.cli.getClientInfo(name_split[1],
(err: any, client: TeonetClientInfo) => {
if (!err) {
//this.cli.sendClientInfo(name_split[1]);
this.clients.inArray(
// Comparer
function (currentElement: onclientDisplArEl) {
let name_split = currentElement.name.split(':');
return name_split[1] === client.clientId;
},
// Exists
function (currentElement: onclientDisplArEl) {
currentElement.type = client.data.type;
}
);
}
}
);
}
need_sort = true;
},
// Exists
function (currentElement: onclientDisplArEl) {
//console.debug('TeonetClients::pushIfNotExist Element exists:',
// currentElement);
}
);
// Send ping to client
if (client.name != t.getClientName())
t.echo(client.name, 'Hello from contact page n = ' + this.n);
}
// Sort clients array
if (need_sort) {
this.clients.sort(function (a, b) {
return a.name == b.name ? (a.type == b.type ? 0 :
(a.type < b.type ? -1 : 1)) : (a.name < b.name ? -1 : 1);
});
}
}
if (this.onclients) this.onclients();
return 0;
}));
this.f.push(t.whenEvent('onecho', (data: any): number => {
if (!this.isComponentActive()) return 0;
var d: onechoData = data[0][0];
//console.debug('TeonetClients::onecho', data, d);
this.clients.inArray(
// Comparer
function (currentElement: onclientDisplArEl) {
return currentElement.name === d.from;
},
// Exists
function (currentElement: onclientDisplArEl) {
currentElement.time = d.data.time;
}
);
return 0;
}));
// Send clients list request to L0 server
t.clients(Teonet.peer.l0);
}
ngOnDestroy() {
//console.debug('TeonetClients::ngOnDestroy');
for (let f of this.f) this.t.unsubscribe(f);
this.interval.unsubscribe();
}
setOnclients(onclients: any) {
this.onclients = onclients;
}
private isComponentActive(): boolean {
return this.t.isTeonetClientsActive();
}
}
/**
* Number of loggedin clients
*/
export class TeonetClientsNum implements OnDestroy {
num_clients = 0;
private f: Array<any> = [];
constructor(public t: TeonetCli) {
//console.debug('TeonetClientsNum::constructor');
this.f.push(t.whenInit(() => {
//console.debug('TeonetClientsNum::whenInit');
// Send clients number request command to "teo-web" peer
this.sendClientsNum();
}));
this.f.push(t.whenEvent('onother', (data: any): number => {
let d: onotherData = data[0][0];
if(d.from == Teonet.peer.l0) {
//console.debug('TeonetClientsNum::onother', data, d);
// Process clients number responces
this.processClientsNum(d);
}
return 0;
}));
}
ngOnDestroy() {
//console.debug('TeonetClientsNum::destructor');
for (let f of this.f) this.t.unsubscribe(f);
this.sendClientsMumUnsubscribe();
this.num_clients = 0;
}
/**
* Get clients number answer callback function
*
* @param {onotherData} data
* @returns {number}
*/
private processClientsNum(data: onotherData) {
let processed = 0;
let client = "";
let client_code = "";
let clientsNumber = this.num_clients;
// Process command #85 CMD_L0_CLIENTS_N_ANSWER
if (data.cmd === 85) {
type CMD_L0_CLIENTS_N_ANSWER = {numClients: number};
//console.debug("TeonetClientsNum::processClientsNum, data:", data);
clientsNumber = (<CMD_L0_CLIENTS_N_ANSWER> data.data).numClients;
this.sendClientsNumSubscribe();
processed = 1;
}
// Process command #83 CMD_SUBSCRIBE_ANSWER
else if (data.cmd === 83) {
type CMD_SUBSCRIBE_ANSWER = {ev: number, cmd: number, data: string}
let byteCharacters = atob((<CMD_SUBSCRIBE_ANSWER> data.data).data);
for (let i = 0; i < (byteCharacters.length); i++) {
client_code += byteCharacters.charCodeAt(i) + '.';
}
client = atob((<CMD_SUBSCRIBE_ANSWER> data.data).data);
// L0 client Connected
if ((<CMD_SUBSCRIBE_ANSWER> data.data).ev === 21) {
if (byteCharacters.charCodeAt(0)) clientsNumber += 1;
processed = 1;
}
// L0 client Disconnected
else if ((<CMD_SUBSCRIBE_ANSWER> data.data).ev === 22) {
clientsNumber -= 1;
processed = 1;
}
}
if (processed) {
//console.debug("TeonetClientsNum::processClientsNum, data:",
// data, "client:", client, "client_code:", client_code);
this.num_clients = clientsNumber;
}
return processed;
}
/**
* Send clients number request
*
* @returns {undefined}
*/
private sendClientsNum() {
// Send clients number request
this.t.send('{ "cmd": 84, "to": "' + Teonet.peer.l0 + '", "data": "JSON" }');
}
/**
* Send subscribe to connect / disconnect client events request
*
* @returns {undefined}
*/
private sendClientsNumSubscribe() {
// Send CMD_SUBSCRIBE Subscribe to L0 client Connected / Disconnected
this.t.send('{ "cmd": 81, "to": "' + Teonet.peer.l0 + '", "data": "TEXT:21" }');
this.t.send('{ "cmd": 81, "to": "' + Teonet.peer.l0 + '", "data": "TEXT:22" }');
}
/**
* Request unsubscribe from Connected / Disconnected events
*
* @returns {undefined}
*/
private sendClientsMumUnsubscribe() {
// Send CMD_SUBSCRIBE UnSubscribe to L0 client Connected / Disconnected
if (this.t && this.t.isInit()) {
this.t.send('{ "cmd": 82, "to": "' + Teonet.peer.l0 + '", "data": "TEXT:21" }');
this.t.send('{ "cmd": 82, "to": "' + Teonet.peer.l0 + '", "data": "TEXT:22" }');
}
}
}
export type TeonetUserInfo = {
userId: string,
username: string,
email: string
};
export type TeonetClientInfo = {
clientId: string,
registerDate: Date,
data: {
type: string;
}
};
/**
* Class to translate user and clien from code to human name (from teonet)
*/
export class TeonetClientsTranslate implements OnDestroy {
//private f: Array<any> = [];
private cli = new TeonetClientsRequest(this.t);
private TIMEOUT = 3000;
// Register to TeonetCli onother event
constructor(private t: TeonetCli) {
//
}
ngOnDestroy() {
//
}
private setTimeout(f: any, done: any) {
return setTimeout(()=>{
done('Timeout');
this.t.unsubscribe(f);
}, this.TIMEOUT);
}
splitName(name: string): { user: string, client: string } {
let name_split = name.split(':');
return { user: name_split[0], client: name_split[1] };
}
private getInfo(id: string, type: boolean,
done: (err: any, info?: TeonetUserInfo | TeonetClientInfo) => void) {
let f = this.t.whenEvent('onother', (data: any): number => {
var d: onotherData = data[0][0];
// Process ansewers from teo-auth
if (d.from == Teonet.peer.auth) {
// Process user_info # 133
if (!type && d.cmd == 133) {
let user: TeonetUserInfo = d.data;
if (id == user.userId) _done(user);
}
// Process client_info # 135
else if (type && d.cmd == 135) {
let client: TeonetClientInfo = d.data;
if (id == client.clientId) _done(client);
}
}
return 0;
}),
timer = this.setTimeout(f, done),
_done = (arg: any) => {
this.t.unsubscribe(f);
clearTimeout(timer);
done(null, arg);
}
if(!type) this.cli.sendUserInfo(id);
else this.cli.sendClientInfo(id);
}
getUserInfo(user_id: string, done: (err: any, user?: TeonetUserInfo) => void) {
this.getInfo(user_id, false, done);
}
getClientInfo(client_id: string, done: (err: any, client?: TeonetClientInfo) => void) {
this.getInfo(client_id, true, done);
}
}