-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketSubscriber.ts
More file actions
335 lines (313 loc) · 13.7 KB
/
WebSocketSubscriber.ts
File metadata and controls
335 lines (313 loc) · 13.7 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
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
import { Observable } from 'rxjs';
import { map, first, filter } from 'rxjs/operators';
import { Commands, Kinds } from '../configuration/xcWebSocketBridgeConfiguration';
import { ApiConfiguration, SubscriberEventType } from '../configuration/apiConfiguration';
import 'rxjs/add/operator/toPromise';
import { WebSocketPublisher } from './WebSocketPublisher';
import {
DeserializedData,
Event,
Data,
getHeaderWithIncomingType,
Serializer,
Deserializer,
fatalErrorState,
JsonMessage,
} from './xcomponentMessages';
import { PrivateTopics } from '../interfaces/PrivateTopics';
import { StateMachineInstance } from '../interfaces/StateMachineInstance';
import { StateMachineRef } from '../interfaces/StateMachineRef';
import { StateMachineUpdateListener } from '../interfaces/StateMachineUpdateListener';
import { generateUUID } from '../utils/uuid';
// import { Logger } from '../utils/Logger';
import { WebSocketWrapper } from './WebSocketWrapper';
export class WebSocketSubscriber {
// private logger = Logger.getLogger('WebSocketSubscriber');
private stateMachineRefSendPublisher: WebSocketPublisher;
private subscribedStateMachines: { [componentName: string]: Array<String> };
private updates$: Observable<DeserializedData>;
private deserializer: Deserializer;
private serializer: Serializer;
private timeout: string;
constructor(private webSocketWrapper: WebSocketWrapper, private configuration: ApiConfiguration) {
this.subscribedStateMachines = {};
this.deserializer = new Deserializer();
this.serializer = new Serializer();
this.timeout = '00:00:10';
const thisSubscriber = this;
const webSocketUpdates$ = this.webSocketWrapper.getObservable();
if (webSocketUpdates$ !== undefined && webSocketUpdates$ !== null) {
this.updates$ = webSocketUpdates$.pipe(
map((rawMessage: MessageEvent) =>
thisSubscriber.deserializer.deserialize(rawMessage.data || rawMessage)
)
);
}
}
public setStateMachineRefSendPublisher(stateMachineRefSendPublisher: WebSocketPublisher) {
this.stateMachineRefSendPublisher = stateMachineRefSendPublisher;
}
public getSnapshot(
componentName: string,
stateMachineName: string,
privateTopics: PrivateTopics
): Promise<Array<StateMachineInstance>> {
const replyTopic = generateUUID();
const thisSubscriber = this;
const promise = this.updates$
.pipe(
filter(
(data: DeserializedData) =>
data.command === Commands[Commands.snapshot] && data.topic === replyTopic
),
first(),
map((data: DeserializedData) => {
thisSubscriber.sendUnsubscribeRequestToTopic(replyTopic, Kinds.Snapshot);
return thisSubscriber.getJsonDataFromSnapshot(data.stringData, data.topic);
})
)
.toPromise();
this.sendSubscribeRequestToTopic(replyTopic, Kinds.Snapshot);
const dataToSendSnapshot = this.getDataToSendSnapshot(
componentName,
stateMachineName,
replyTopic,
privateTopics
);
this.webSocketWrapper.send(thisSubscriber.serializer.convertToWebsocketInputFormat(dataToSendSnapshot));
return promise;
}
private getDataToSendSnapshot(
componentName: string,
stateMachineName: string,
replyTopic: string,
privateTopics: PrivateTopics
): Data {
const componentCode = this.configuration.getComponentCode(componentName);
const stateMachineCode = this.configuration.getStateMachineCode(componentName, stateMachineName);
let topic = this.configuration.getSnapshotTopic(componentCode);
let jsonMessage = {
Timeout: this.timeout,
CallerPrivateTopic: privateTopics.getSubscriberTopics(),
ReplyTopic: replyTopic,
};
let header = getHeaderWithIncomingType();
header.ComponentCode = componentCode;
header.StateMachineCode = stateMachineCode;
let dataToSendSnapshot = {
RoutingKey: topic,
ComponentCode: componentCode,
Event: {
Header: header,
JsonMessage: JSON.stringify(jsonMessage),
},
};
return dataToSendSnapshot;
}
private prepareStateMachineUpdates(
componentName: string,
stateMachineName: string
): Observable<StateMachineInstance> {
const componentCode = this.configuration.getComponentCode(componentName);
const stateMachineCode = this.configuration.getStateMachineCode(componentName, stateMachineName);
let thisSubscriber = this;
let filteredObservable = this.updates$.pipe(
filter((data: DeserializedData) => data.command === Commands[Commands.update]),
map((data: DeserializedData) => thisSubscriber.getJsonDataFromEvent(data.stringData, data.topic)),
filter(
(jsonData: StateMachineInstance) =>
thisSubscriber.isSameComponent(jsonData, componentCode) &&
thisSubscriber.isSameStateMachine(jsonData, stateMachineCode)
)
);
return filteredObservable;
}
private isSameComponent(jsonData: StateMachineInstance, componentCode: number): boolean {
let sameComponent = jsonData.stateMachineRef.ComponentCode === componentCode;
return sameComponent;
}
private isSameStateMachine(jsonData: StateMachineInstance, stateMachineCode: number): boolean {
let sameStateMachine = jsonData.stateMachineRef.StateMachineCode === stateMachineCode;
return sameStateMachine;
}
getStateMachineUpdates(componentName: string, stateMachineName: string): Observable<StateMachineInstance> {
let filteredObservable = this.prepareStateMachineUpdates(componentName, stateMachineName);
this.sendSubscribeRequest(componentName, stateMachineName);
return filteredObservable;
}
canSubscribe(componentName: string, stateMachineName: string): boolean {
const componentCode = this.configuration.getComponentCode(componentName);
const stateMachineCode = this.configuration.getStateMachineCode(componentName, stateMachineName);
return this.configuration.containsSubscriber(componentCode, stateMachineCode, SubscriberEventType.Update);
}
subscribe(
componentName: string,
stateMachineName: string,
stateMachineUpdateListener: StateMachineUpdateListener
): void {
this.prepareStateMachineUpdates(componentName, stateMachineName).subscribe(
(stateMachineInstance: StateMachineInstance) => {
stateMachineUpdateListener.onStateMachineUpdate(stateMachineInstance);
}
);
this.sendSubscribeRequest(componentName, stateMachineName);
}
private sendSubscribeRequest(componentName: string, stateMachineName: string): void {
if (!this.isSubscribed(this.subscribedStateMachines, componentName, stateMachineName)) {
const componentCode = this.configuration.getComponentCode(componentName);
const stateMachineCode = this.configuration.getStateMachineCode(componentName, stateMachineName);
let topic = this.configuration.getSubscriberTopic(
componentCode,
stateMachineCode,
SubscriberEventType.Update
);
let kind = Kinds.Public;
this.sendSubscribeRequestToTopic(topic, kind);
this.addSubscribedStateMachines(componentName, stateMachineName);
}
}
sendSubscribeRequestToTopic(topic: string, kind: number): void {
let data = this.getDataToSend(topic, kind);
let commandData = {
Command: Commands[Commands.subscribe],
Data: data,
};
let input = this.serializer.convertCommandDataToWebsocketInputFormat(commandData);
this.webSocketWrapper.send(input);
}
sendUnsubscribeRequestToTopic(topic: string, kind: number): void {
let data = this.getDataToSend(topic, kind);
let commandData = {
Command: Commands[Commands.unsubscribe],
Data: data,
};
let input = this.serializer.convertCommandDataToWebsocketInputFormat(commandData);
this.webSocketWrapper.send(input);
}
private getDataToSend(topic: string, kind: number): Event {
return {
Header: getHeaderWithIncomingType(),
JsonMessage: JSON.stringify({
Topic: {
Key: topic,
kind: kind,
},
}),
};
}
unsubscribe(componentName: string, stateMachineName: string): void {
if (this.isSubscribed(this.subscribedStateMachines, componentName, stateMachineName)) {
const componentCode = this.configuration.getComponentCode(componentName);
const stateMachineCode = this.configuration.getStateMachineCode(componentName, stateMachineName);
let topic = this.configuration.getSubscriberTopic(
componentCode,
stateMachineCode,
SubscriberEventType.Update
);
let kind = Kinds.Public;
let data = this.getDataToSend(topic, kind);
let commandData = {
Command: Commands[Commands.unsubscribe],
Data: data,
};
this.webSocketWrapper.send(this.serializer.convertCommandDataToWebsocketInputFormat(commandData));
this.removeSubscribedStateMachines(componentName, stateMachineName);
}
}
private isSubscribed(
subscribedStateMachines: { [componentName: string]: Array<String> },
componentName: string,
stateMachineName: string
): boolean {
let isSubscribed =
subscribedStateMachines[componentName] !== undefined &&
subscribedStateMachines[componentName].indexOf(stateMachineName) > -1;
return isSubscribed;
}
private addSubscribedStateMachines(componentName: string, stateMachineName: string): void {
if (this.subscribedStateMachines[componentName] === undefined) {
this.subscribedStateMachines[componentName] = [stateMachineName];
} else {
this.subscribedStateMachines[componentName].push(stateMachineName);
}
}
private removeSubscribedStateMachines(componentName: string, stateMachineName: string): void {
let index = this.subscribedStateMachines[componentName].indexOf(stateMachineName);
this.subscribedStateMachines[componentName].splice(index, 1);
}
public getJsonDataFromSnapshot(data: string, topic?: string): Array<StateMachineInstance> {
let jsonData = this.deserializer.getJsonData(data);
let b64Data = JSON.parse(jsonData.JsonMessage).Items;
let items;
try {
const raw = JSON.parse(this.deserializer.decodeServerMessage(b64Data)!);
if (raw.$values) {
items = raw.$values;
} else {
items = raw;
}
} catch (e) {
items = b64Data;
}
let snapshotItems = new Array<StateMachineInstance>();
for (let i = 0; i < items.length; i++) {
let stateMachineRef = this.getStateMachineRef(
items[i].StateMachineId,
parseInt(items[i].WorkerId, undefined),
parseInt(items[i].ComponentCode, undefined),
parseInt(items[i].StateMachineCode, undefined),
parseInt(items[i].StateCode, undefined)
);
snapshotItems.push(new StateMachineInstance(stateMachineRef, items[i].PublicMember));
}
return snapshotItems;
}
public getJsonDataFromEvent(data: string, topic?: string): StateMachineInstance {
// this.logger.debug('JsonData received from event: ', { data: data, topic: topic }, 2);
let jsonData = this.deserializer.getJsonData(data);
let stateMachineRef = this.getStateMachineRef(
jsonData.Header.StateMachineId,
jsonData.Header.WorkerId,
jsonData.Header.ComponentCode,
jsonData.Header.StateMachineCode,
jsonData.Header.StateCode,
jsonData.Header.ErrorMessage
);
return new StateMachineInstance(stateMachineRef, JSON.parse(jsonData.JsonMessage));
}
private getStateMachineRef(
StateMachineId: string,
workerId: number,
componentCode: number,
stateMachineCode: number,
stateCode: number,
errorMessage?: string
): StateMachineRef {
let thisSubscriber = this;
let stateMachineRef = {
StateMachineId: StateMachineId,
WorkerId: workerId,
ComponentCode: componentCode,
StateMachineCode: stateMachineCode,
StateName: errorMessage
? fatalErrorState
: thisSubscriber.configuration.getStateName(componentCode, stateMachineCode, stateCode),
send: (
messageType: string,
jsonMessage: JsonMessage,
visibilityPrivate?: boolean,
specifiedPrivateTopic?: string
) => {
thisSubscriber.stateMachineRefSendPublisher.sendWithStateMachineRef(
stateMachineRef,
messageType,
jsonMessage,
visibilityPrivate,
specifiedPrivateTopic
);
},
ErrorMessage: errorMessage,
};
return stateMachineRef;
}
}