-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketBridgeCommunication.ts
More file actions
142 lines (135 loc) · 5.89 KB
/
WebSocketBridgeCommunication.ts
File metadata and controls
142 lines (135 loc) · 5.89 KB
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
import { fromEvent as observableFromEvent, Observable } from 'rxjs';
import { filter, first, map, takeWhile } from 'rxjs/operators';
import { Commands } from '../configuration/xcWebSocketBridgeConfiguration';
import { CompositionModel, DeserializedData, Serializer, Deserializer } from './xcomponentMessages';
import 'rxjs/add/observable/fromEvent';
import { Logger } from '../utils/Logger';
import { DefaultApiConfigurationParser } from '../configuration/apiConfigurationParser';
import { ApiConfiguration } from '../configuration/apiConfiguration';
export class WebSocketBridgeCommunication {
private logger = Logger.getLogger('HeartbeatManager');
private updates$: Observable<DeserializedData>;
private deserializer: Deserializer;
private serializer: Serializer;
private heartbeatTimer: NodeJS.Timer;
private runnning: boolean = true;
constructor(private webSocket: WebSocket) {
this.deserializer = new Deserializer();
this.serializer = new Serializer();
let thisWebSocketBridgeCommunication = this;
this.updates$ = observableFromEvent(this.webSocket, 'message').pipe(
takeWhile((rawMessage: MessageEvent) => this.runnning),
map((rawMessage: MessageEvent) =>
thisWebSocketBridgeCommunication.deserializer.deserializeWithoutTopic(rawMessage.data || rawMessage)
)
);
}
public startHeartbeat(heartbeatIntervalSeconds: number): void {
let thisWebSocketBridgeCommunication = this;
let command = Commands[Commands.hb];
this.updates$
.pipe(filter((data: DeserializedData) => data.command === command))
.subscribe((data: DeserializedData) => {
this.logger.debug('Heartbeat received successfully');
});
let commandData = {
Command: command,
Data: {},
};
let input = thisWebSocketBridgeCommunication.serializer.convertCommandDataToWebsocketInputFormat(commandData);
this.heartbeatTimer = setInterval(() => {
thisWebSocketBridgeCommunication.webSocket.send(input);
this.logger.debug('Heartbeat sent');
}, heartbeatIntervalSeconds * 1000);
}
public getCompositionModel(apiName: string): Promise<CompositionModel> {
const thisWebSocketBridgeCommunication = this;
const command = Commands[Commands.getModel];
const promise = this.updates$
.pipe(
filter((data: DeserializedData) => data.command === command),
first(),
map((data: DeserializedData) => {
return thisWebSocketBridgeCommunication.deserializer.getJsonDataFromGetModelRequest(
data.stringData
);
})
)
.toPromise()
.then((compositionModel: CompositionModel | undefined) => {
if (!compositionModel) {
const errorMessage = 'Model ' + apiName + ' not found';
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
this.logger.info('Model ' + apiName + ' received successfully');
return compositionModel;
});
const commandData = {
Command: command,
Data: { Name: apiName },
};
const input = thisWebSocketBridgeCommunication.serializer.convertCommandDataToWebsocketInputFormat(commandData);
this.webSocket.send(input);
return promise;
}
public getXcApiList(): Promise<Array<string>> {
const thisWebSocketBridgeCommunication = this;
const command = Commands[Commands.getXcApiList];
const promise = this.updates$
.pipe(
filter((data: DeserializedData) => data.command === command),
first(),
map((data: DeserializedData) => {
this.logger.info('ApiList received successfully : ' + JSON.stringify(data));
return thisWebSocketBridgeCommunication.deserializer.getJsonDataFromGetXcApiListRequest(
data.stringData
);
})
)
.toPromise();
const commandData = {
Command: command,
Data: {},
};
this.webSocket.send(
thisWebSocketBridgeCommunication.serializer.convertCommandDataToWebsocketInputFormat(commandData)
);
return promise;
}
public getXcApiConfiguration(apiName: string): Promise<ApiConfiguration> {
const thisWebSocketBridgeCommunication = this;
const command = Commands[Commands.getXcApi];
const promise = this.updates$
.pipe(
filter((data: DeserializedData) => data.command === command),
first(),
map((data: DeserializedData) => {
this.logger.info(apiName + ' ' + 'received successfully');
return thisWebSocketBridgeCommunication.deserializer.getJsonDataFromXcApiRequest(data.stringData);
})
)
.toPromise()
.then((xcApi: string | undefined) => {
if (!xcApi) {
const errorMessage = `Unknown Api: ${apiName}`;
this.logger.error(errorMessage);
throw new Error(errorMessage);
}
const parser = new DefaultApiConfigurationParser();
return parser.parse(xcApi);
});
const commandData = {
Command: command,
Data: { Name: apiName },
};
this.webSocket.send(
thisWebSocketBridgeCommunication.serializer.convertCommandDataToWebsocketInputFormat(commandData)
);
return promise;
}
public dispose(): void {
clearInterval(this.heartbeatTimer);
this.runnning = false;
}
}