-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathmessaging.ts
More file actions
257 lines (214 loc) · 8.19 KB
/
messaging.ts
File metadata and controls
257 lines (214 loc) · 8.19 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
/// <reference path="../../typings/main.d.ts" />
/// <reference path="models.ts" />
import Models = require("./models");
module Prefixes {
export var SUBSCRIBE = "u";
export var SNAPSHOT = "n";
export var MESSAGE = "m";
}
export interface IPublish<T> {
publish : (msg : T) => void;
registerSnapshot : (generator : () => T[]) => IPublish<T>;
}
export class Publisher<T> implements IPublish<T> {
private _snapshot : () => T[] = null;
constructor(private topic : string,
private _io : SocketIO.Server,
snapshot : () => T[],
private _log : (...args: any[]) => void) {
this.registerSnapshot(snapshot || null);
var onConnection = s => {
this._log("socket", s.id, "connected for Publisher", topic);
s.on("disconnect", () => {
this._log("socket", s.id, "disconnected for Publisher", topic);
});
s.on(Prefixes.SUBSCRIBE + "-" + topic, () => {
if (this._snapshot !== null) {
var snapshot = this._snapshot();
this._log("socket", s.id, "asking for snapshot on topic", topic);
s.emit(Prefixes.SNAPSHOT + "-" + topic, snapshot);
}
});
};
this._io.on("connection", onConnection);
Object.keys(this._io.sockets.connected).forEach(s => {
onConnection(this._io.sockets.connected[s]);
});
}
public publish = (msg : T) => this._io.emit(Prefixes.MESSAGE + "-" + this.topic, msg);
public registerSnapshot = (generator : () => T[]) => {
if (this._snapshot === null) {
this._snapshot = generator;
}
else {
throw new Error("already registered snapshot generator for topic " + this.topic);
}
return this;
}
}
export class NullPublisher<T> implements IPublish<T> {
public publish = (msg : T) => {};
public registerSnapshot = (generator : () => T[]) => this;
}
export interface ISubscribe<T> {
registerSubscriber : (incrementalHandler : (msg : T) => void, snapshotHandler : (msgs : T[]) => void) => ISubscribe<T>;
registerDisconnectedHandler : (handler : () => void) => ISubscribe<T>;
registerConnectHandler : (handler : () => void) => ISubscribe<T>;
connected: boolean;
disconnect : () => void;
}
export class Subscriber<T> implements ISubscribe<T> {
private _incrementalHandler : (msg : T) => void = null;
private _snapshotHandler : (msgs : T[]) => void = null;
private _disconnectHandler : () => void = null;
private _connectHandler : () => void = null;
private _socket : SocketIOClient.Socket;
constructor(private topic : string,
io : SocketIOClient.Socket,
private _log : (...args: any[]) => void) {
this._socket = io;
this._log("creating subscriber to", this.topic, "; connected?", this.connected);
if (this.connected)
this.onConnect();
this._socket.on("connect", this.onConnect)
.on("disconnect", this.onDisconnect)
.on(Prefixes.MESSAGE + "-" + topic, this.onIncremental)
.on(Prefixes.SNAPSHOT + "-" + topic, this.onSnapshot);
}
public get connected() : boolean {
return this._socket.connected;
}
private onConnect = () => {
this._log("connect to", this.topic);
if (this._connectHandler !== null) {
this._connectHandler();
}
this._socket.emit(Prefixes.SUBSCRIBE + "-" + this.topic);
};
private onDisconnect = () => {
this._log("disconnected from", this.topic);
if (this._disconnectHandler !== null)
this._disconnectHandler();
};
private onIncremental = (m : T) => {
if (this._incrementalHandler !== null)
this._incrementalHandler(m);
};
private onSnapshot = (msgs : T[]) => {
this._log("handling snapshot for", this.topic, "nMsgs:", msgs.length);
if (this._snapshotHandler !== null)
this._snapshotHandler(msgs);
};
public disconnect = () => {
this._log("forcing disconnection from ", this.topic);
this._socket.off("connect", this.onConnect);
this._socket.off("disconnect", this.onDisconnect);
this._socket.off(Prefixes.MESSAGE + "-" + this.topic, this.onIncremental);
this._socket.off(Prefixes.SNAPSHOT + "-" + this.topic, this.onSnapshot);
};
public registerSubscriber = (incrementalHandler : (msg : T) => void, snapshotHandler : (msgs : T[]) => void) => {
if (this._incrementalHandler === null) {
this._incrementalHandler = incrementalHandler;
}
else {
throw new Error("already registered incremental handler for topic " + this.topic);
}
if (this._snapshotHandler === null) {
this._snapshotHandler = snapshotHandler;
}
else {
throw new Error("already registered snapshot handler for topic " + this.topic);
}
return this;
};
public registerDisconnectedHandler = (handler : () => void) => {
if (this._disconnectHandler === null) {
this._disconnectHandler = handler;
}
else {
throw new Error("already registered disconnect handler for topic " + this.topic);
}
return this;
};
public registerConnectHandler = (handler : () => void) => {
if (this._connectHandler === null) {
this._connectHandler = handler;
}
else {
throw new Error("already registered connect handler for topic " + this.topic);
}
return this;
};
}
export interface IFire<T> {
fire(msg : T) : void;
}
export class Fire<T> implements IFire<T> {
private _socket : SocketIOClient.Socket;
constructor(private topic : string, io : SocketIOClient.Socket, _log : (...args: any[]) => void) {
this._socket = io;
this._socket.on("connect", () => _log("Fire connected to", this.topic))
.on("disconnect", () => _log("Fire disconnected to", this.topic));
}
public fire = (msg : T) : void => {
this._socket.emit(Prefixes.MESSAGE + "-" + this.topic, msg);
};
}
export interface IReceive<T> {
registerReceiver(handler : (msg : T) => void) : void;
}
export class NullReceiver<T> implements IReceive<T> {
registerReceiver = (handler : (msg : T) => void) => {};
}
export class Receiver<T> implements IReceive<T> {
private _handler : (msg : T) => void = null;
constructor(private topic : string, io : SocketIO.Server,
private _log : (...args: any[]) => void) {
var onConnection = (s : SocketIO.Socket) => {
this._log("socket", s.id, "connected for Receiver", topic);
s.on(Prefixes.MESSAGE + "-" + this.topic, msg => {
if (this._handler !== null)
this._handler(msg);
});
s.on("error", e => {
_log("error in Receiver", e.stack, e.message);
});
};
io.on("connection", onConnection);
Object.keys(io.sockets.connected).forEach(s => {
onConnection(io.sockets.connected[s]);
});
}
registerReceiver = (handler : (msg : T) => void) => {
if (this._handler === null) {
this._handler = handler;
}
else {
throw new Error("already registered receive handler for topic " + this.topic);
}
};
}
export class Topics {
static FairValue = "fv";
static Quote = "q";
static ActiveSubscription = "a";
static ActiveChange = "ac";
static MarketData = "md";
static QuotingParametersChange = "qp-sub";
static SafetySettings = "ss";
static Product = "p";
static OrderStatusReports = "osr";
static ProductAdvertisement = "pa";
static Position = "pos";
static ExchangeConnectivity = "ec";
static SubmitNewOrder = "sno";
static CancelOrder = "cxl";
static MarketTrade = "mt";
static Trades = "t";
static Message = "msg";
static ExternalValuation = "ev";
static QuoteStatus = "qs";
static TargetBasePosition = "tbp";
static TradeSafetyValue = "tsv";
static CancelAllOrders = "cao";
}