-
Notifications
You must be signed in to change notification settings - Fork 9
/
AdminFeed.java
505 lines (456 loc) · 22.6 KB
/
AdminFeed.java
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
package net.jacobpeterson.iqfeed4j.feed.streaming.admin;
import net.jacobpeterson.iqfeed4j.feed.AbstractFeed;
import net.jacobpeterson.iqfeed4j.feed.message.SingleMessageFuture;
import net.jacobpeterson.iqfeed4j.feed.streaming.StreamingCSVMappers;
import net.jacobpeterson.iqfeed4j.model.feed.common.enums.FeedCommand;
import net.jacobpeterson.iqfeed4j.model.feed.common.enums.FeedMessageType;
import net.jacobpeterson.iqfeed4j.model.feed.streaming.admin.ClientStatistics;
import net.jacobpeterson.iqfeed4j.model.feed.streaming.admin.ClientStatistics.Type;
import net.jacobpeterson.iqfeed4j.model.feed.streaming.admin.enums.AdminSystemCommand;
import net.jacobpeterson.iqfeed4j.model.feed.streaming.admin.enums.AdminSystemMessageType;
import net.jacobpeterson.iqfeed4j.model.feed.streaming.admin.enums.OnOffOption;
import net.jacobpeterson.iqfeed4j.model.feed.streaming.common.FeedStatistics;
import net.jacobpeterson.iqfeed4j.model.feed.streaming.common.FeedStatistics.Status;
import net.jacobpeterson.iqfeed4j.util.csv.mapper.index.IndexCSVMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull;
import static net.jacobpeterson.iqfeed4j.util.csv.CSVUtil.valueEquals;
import static net.jacobpeterson.iqfeed4j.util.csv.CSVUtil.valueExists;
import static net.jacobpeterson.iqfeed4j.util.csv.CSVUtil.valueNotWhitespace;
import static net.jacobpeterson.iqfeed4j.util.csv.mapper.AbstractCSVMapper.DateTimeConverters.DATE_SPACE_TIME;
import static net.jacobpeterson.iqfeed4j.util.csv.mapper.AbstractCSVMapper.PrimitiveConvertors.DOUBLE;
import static net.jacobpeterson.iqfeed4j.util.csv.mapper.AbstractCSVMapper.PrimitiveConvertors.INTEGER;
import static net.jacobpeterson.iqfeed4j.util.csv.mapper.AbstractCSVMapper.PrimitiveConvertors.STRING;
/**
* {@link AdminFeed} represents the Admin {@link AbstractFeed}.
*/
public class AdminFeed extends AbstractFeed {
private static final Logger LOGGER = LoggerFactory.getLogger(AdminFeed.class);
protected static final String FEED_NAME_SUFFIX = " Admin Feed";
protected static final IndexCSVMapper<ClientStatistics> CLIENT_STATISTICS_CSV_MAPPER;
static {
// Add mappings with CSV indices analogous to line of execution
CLIENT_STATISTICS_CSV_MAPPER = new IndexCSVMapper<>(ClientStatistics::new);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setType, Type::fromValue);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setClientID, INTEGER);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setClientName, STRING);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setStartTime, DATE_SPACE_TIME);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setSymbols, INTEGER);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setRegionalSymbols, INTEGER);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setKiloBytesReceived, DOUBLE);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setKiloBytesSent, DOUBLE);
CLIENT_STATISTICS_CSV_MAPPER.addMapping(ClientStatistics::setKiloBytesQueued, DOUBLE);
}
protected final Object messageReceivedLock;
protected final HashMap<Integer, ClientStatistics> clientStatisticsOfClientIDs;
protected final HashMap<AdminSystemMessageType, SingleMessageFuture<Void>> voidFutureOfAdminSystemMessageTypes;
protected final HashMap<AdminSystemMessageType, SingleMessageFuture<String>> stringFutureOfAdminSystemMessageTypes;
protected SingleMessageFuture<FeedStatistics> feedStatisticsFuture;
protected SingleMessageFuture<ClientStatistics> clientStatisticsFuture;
protected FeedStatistics latestFeedStatistics;
/**
* Instantiates a new {@link AdminFeed}.
*
* @param adminFeedName the {@link AdminFeed} name
* @param hostname the host name
* @param port the port
*/
public AdminFeed(String adminFeedName, String hostname, int port) {
super(LOGGER, adminFeedName + FEED_NAME_SUFFIX, hostname, port, COMMA_DELIMITED_SPLITTER, true, true);
messageReceivedLock = new Object();
clientStatisticsOfClientIDs = new HashMap<>();
voidFutureOfAdminSystemMessageTypes = new HashMap<>();
stringFutureOfAdminSystemMessageTypes = new HashMap<>();
}
@Override
protected void onMessageReceived(String[] csv) {
if (valueEquals(csv, 0, FeedMessageType.ERROR.value())) {
LOGGER.error("Received error message! {}", (Object) csv);
return;
}
// Confirm message format
if (!valueEquals(csv, 0, FeedMessageType.SYSTEM.value()) || !valueNotWhitespace(csv, 1)) {
LOGGER.error("Received unknown message format: {}", (Object) csv);
return;
}
synchronized (messageReceivedLock) {
try {
AdminSystemMessageType adminSystemMessageType = AdminSystemMessageType.fromValue(csv[1]);
switch (adminSystemMessageType) {
// Complete Void Futures
case REGISTER_CLIENT_APP_COMPLETED:
case REMOVE_CLIENT_APP_COMPLETED:
case LOGIN_INFO_SAVED:
case LOGIN_INFO_NOT_SAVED:
case AUTOCONNECT_ON:
case AUTOCONNECT_OFF:
handleVoidFutureMessage(adminSystemMessageType);
break;
// Complete String Futures
case CURRENT_LOGINID:
case CURRENT_PASSWORD:
handleStringFutureMessage(adminSystemMessageType, csv);
break;
case STATS:
handleStatsMessage(csv);
break;
case CLIENTSTATS:
handleClientStatsMessage(csv);
break;
default:
LOGGER.error("Unhandled message type: {}", adminSystemMessageType);
}
} catch (IllegalArgumentException illegalArgumentException) {
LOGGER.error("Received unknown message type: {}", csv[1], illegalArgumentException);
}
}
}
private void handleVoidFutureMessage(AdminSystemMessageType adminSystemMessageType) {
SingleMessageFuture<Void> voidFuture = voidFutureOfAdminSystemMessageTypes.get(adminSystemMessageType);
if (voidFuture != null) {
voidFuture.complete(null);
voidFutureOfAdminSystemMessageTypes.put(adminSystemMessageType, null);
} else {
LOGGER.error("Could not complete {} future!", adminSystemMessageType);
}
}
private void handleStringFutureMessage(AdminSystemMessageType adminSystemMessageType, String[] csv) {
SingleMessageFuture<String> stringFuture =
stringFutureOfAdminSystemMessageTypes.get(adminSystemMessageType);
if (stringFuture != null) {
if (valueExists(csv, 2)) {
stringFuture.complete(csv[2]);
} else {
stringFuture.completeExceptionally(new RuntimeException("CSV response value missing!"));
}
stringFutureOfAdminSystemMessageTypes.put(adminSystemMessageType, null);
} else {
LOGGER.error("Could not complete {} future!", adminSystemMessageType);
}
}
private void handleStatsMessage(String[] csv) {
try {
FeedStatistics feedStatistics = StreamingCSVMappers.FEED_STATISTICS_CSV_MAPPER.map(csv, 2);
latestFeedStatistics = feedStatistics;
if (feedStatisticsFuture != null) {
feedStatisticsFuture.complete(feedStatistics);
feedStatisticsFuture = null;
}
} catch (Exception exception) {
if (feedStatisticsFuture != null) {
feedStatisticsFuture.completeExceptionally(exception);
feedStatisticsFuture = null;
}
}
}
private void handleClientStatsMessage(String[] csv) {
try {
ClientStatistics clientStatistics = CLIENT_STATISTICS_CSV_MAPPER.map(csv, 2);
clientStatisticsOfClientIDs.put(clientStatistics.getClientID(), clientStatistics);
if (clientStatisticsFuture != null) {
clientStatisticsFuture.complete(clientStatistics);
clientStatisticsFuture = null;
}
} catch (Exception exception) {
if (clientStatisticsFuture != null) {
clientStatisticsFuture.completeExceptionally(exception);
clientStatisticsFuture = null;
}
}
}
@Override
protected void onFeedSocketException(Exception exception) {
voidFutureOfAdminSystemMessageTypes.values().forEach(future -> future.completeExceptionally(exception));
stringFutureOfAdminSystemMessageTypes.values().forEach(future -> future.completeExceptionally(exception));
if (feedStatisticsFuture != null) {
feedStatisticsFuture.completeExceptionally(exception);
}
if (clientStatisticsFuture != null) {
clientStatisticsFuture.completeExceptionally(exception);
}
}
@Override
protected void onFeedSocketClose() {
onFeedSocketException(new RuntimeException("Feed socket closed normally while a request was active!"));
}
//
// START Feed commands
//
/**
* Sends a {@link FeedCommand#SYSTEM} {@link AdminSystemCommand}.
*
* @param adminSystemCommand the {@link AdminSystemCommand}
* @param arguments the arguments. <code>null</code> for no arguments.
*
* @throws IOException thrown for {@link IOException}s
*/
private void sendAdminSystemCommand(AdminSystemCommand adminSystemCommand, String... arguments) throws IOException {
super.sendSystemCommand(adminSystemCommand.value(), arguments);
}
/**
* Gets the {@link AdminSystemCommand} {@link SingleMessageFuture} or calls
* {@link #sendAdminSystemCommand(AdminSystemCommand, String...)} and creates/puts a new
* {@link SingleMessageFuture}. This method is synchronized with {@link #messageReceivedLock}.
*
* @param <T> the {@link SingleMessageFuture} type
* @param futureOfAdminSystemMessageTypes the {@link SingleMessageFuture}s of {@link AdminSystemMessageType}s
* @param adminSystemMessageType the {@link AdminSystemMessageType}
* @param adminSystemCommand the {@link AdminSystemCommand} for the {@link AdminSystemMessageType}
* @param arguments the arguments
*
* @return a {@link SingleMessageFuture}
*
* @throws IOException thrown for {@link IOException}s
*/
private <T> SingleMessageFuture<T> getOrSendAdminSystemCommandFuture(
Map<AdminSystemMessageType, SingleMessageFuture<T>> futureOfAdminSystemMessageTypes,
AdminSystemMessageType adminSystemMessageType,
AdminSystemCommand adminSystemCommand, String... arguments) throws IOException {
synchronized (messageReceivedLock) {
SingleMessageFuture<T> messageFuture = futureOfAdminSystemMessageTypes.get(adminSystemMessageType);
if (messageFuture != null) {
return messageFuture;
}
messageFuture = new SingleMessageFuture<>();
futureOfAdminSystemMessageTypes.put(adminSystemMessageType, messageFuture);
sendAdminSystemCommand(adminSystemCommand, arguments);
return messageFuture;
}
}
/**
* Registers your application with the feed. Users will not be able to login to the feed until an application is
* registered. This sends a {@link AdminSystemCommand#REGISTER_CLIENT_APP} request.
*
* @param productID the Registered Product ID that you were assigned when you created your developer API
* account.
* @param productVersion the version of YOUR application.
*
* @return a {@link SingleMessageFuture} completed upon command response
*
* @throws IOException thrown for {@link IOException}s
*/
public SingleMessageFuture<Void> registerClientApp(String productID, String productVersion) throws IOException {
checkNotNull(productID);
checkNotNull(productVersion);
return getOrSendAdminSystemCommandFuture(voidFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.REGISTER_CLIENT_APP_COMPLETED,
AdminSystemCommand.REGISTER_CLIENT_APP, productID, productVersion);
}
/**
* Removes the registration of your application with the feed. This sends a
* {@link AdminSystemCommand#REMOVE_CLIENT_APP} request.
*
* @param productID the Registered Product ID that you were assigned when you created your developer API
* account.
* @param productVersion the version of YOUR application.
*
* @return a {@link SingleMessageFuture} completed upon command response
*
* @throws IOException thrown for {@link IOException}s
*/
public SingleMessageFuture<Void> removeClientApp(String productID, String productVersion) throws IOException {
checkNotNull(productID);
checkNotNull(productVersion);
return getOrSendAdminSystemCommandFuture(voidFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.REMOVE_CLIENT_APP_COMPLETED,
AdminSystemCommand.REMOVE_CLIENT_APP, productID, productVersion);
}
/**
* Sets the user loginID for IQFeed. This sends a {@link AdminSystemCommand#SET_LOGINID} request.
*
* @param loginID the loginID that was assigned to the user when they created their datafeed account.
*
* @return a {@link SingleMessageFuture} completed upon command response
*
* @throws IOException thrown for {@link IOException}s
*/
public SingleMessageFuture<String> setLoginID(String loginID) throws IOException {
checkNotNull(loginID);
return getOrSendAdminSystemCommandFuture(stringFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.CURRENT_LOGINID,
AdminSystemCommand.SET_LOGINID, loginID);
}
/**
* Sets the user password for IQFeed. This sends a {@link AdminSystemCommand#SET_PASSWORD} request.
*
* @param password the password that was assigned to the user when they created their datafeed account.
*
* @return a {@link SingleMessageFuture} completed upon command response
*
* @throws IOException thrown for {@link IOException}s
*/
public SingleMessageFuture<String> setPassword(String password) throws IOException {
checkNotNull(password);
return getOrSendAdminSystemCommandFuture(stringFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.CURRENT_PASSWORD,
AdminSystemCommand.SET_PASSWORD, password);
}
/**
* Sets the save login info (loginID/password) flag for IQFeed. This will be ignored at the time of connection if
* either the loginID, password are not set. This sends a {@link AdminSystemCommand#SET_SAVE_LOGIN_INFO} request.
*
* @param onOffOption {@link OnOffOption#ON} if you want IQConnect to save the user's loginID and password or
* {@link OnOffOption#OFF} if you do not want IQConnect to save the user's loginID and password.
*
* @return a {@link SingleMessageFuture} completed upon command response
*
* @throws IOException thrown for {@link IOException}s
*/
public SingleMessageFuture<Void> setSaveLoginInfo(OnOffOption onOffOption) throws IOException {
checkNotNull(onOffOption);
switch (onOffOption) {
case ON:
return getOrSendAdminSystemCommandFuture(voidFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.LOGIN_INFO_SAVED,
AdminSystemCommand.SET_SAVE_LOGIN_INFO, onOffOption.value());
case OFF:
return getOrSendAdminSystemCommandFuture(voidFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.LOGIN_INFO_NOT_SAVED,
AdminSystemCommand.SET_SAVE_LOGIN_INFO, onOffOption.value());
default:
throw new UnsupportedOperationException();
}
}
/**
* Sets the auto connect flag for IQFeed. This will be ignored at the time of connection if either the loginID,
* password are not set. This sends a {@link AdminSystemCommand#SET_AUTOCONNECT} request.
*
* @param onOffOption {@link OnOffOption#ON} if you want IQConnect to automatically connect to the servers or
* {@link OnOffOption#OFF} if you do not want IQConnect to automatically connect.
*
* @return a {@link SingleMessageFuture} completed upon command response
*
* @throws IOException thrown for {@link IOException}s
*/
public SingleMessageFuture<Void> setAutoconnect(OnOffOption onOffOption) throws IOException {
checkNotNull(onOffOption);
switch (onOffOption) {
case ON:
return getOrSendAdminSystemCommandFuture(voidFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.AUTOCONNECT_ON,
AdminSystemCommand.SET_AUTOCONNECT, onOffOption.value());
case OFF:
return getOrSendAdminSystemCommandFuture(voidFutureOfAdminSystemMessageTypes,
AdminSystemMessageType.AUTOCONNECT_OFF,
AdminSystemCommand.SET_AUTOCONNECT, onOffOption.value());
default:
throw new UnsupportedOperationException();
}
}
/**
* Tells IQConnect.exe to initiate a connection to the servers. This happens automatically upon launching the feed
* unless the ProductID and/or Product version have not been set. This message is ignored if the feed is already
* connected. This sends a {@link AdminSystemCommand#CONNECT} request.
* <br>
* There is no set message associated with this command but you should notice the connection status in the
* {@link #getLatestFeedStatistics()} message change from {@link Status#NOT_CONNECTED} to {@link Status#CONNECTED}
* if the connection was successful.
*
* @throws IOException thrown for {@link IOException}s
*/
public void connect() throws IOException {
sendAdminSystemCommand(AdminSystemCommand.CONNECT);
}
/**
* Tells IQConnect.exe to disconnect from the IQFeed servers. This happens automatically as soon as the last client
* connection to IQConnect is terminated and the ClientsConnected value in the {@link ClientStatistics} message
* returns to zero (after having incremented above zero). This message is ignored if the feed is already
* disconnected. This sends a {@link AdminSystemCommand#DISCONNECT} request.
* <br>
* There is no set message associated with this command but you should notice the connection status in the
* {@link #getLatestFeedStatistics()} message change from {@link Status#CONNECTED} to {@link Status#NOT_CONNECTED}
* if the disconnection was successful.
*
* @throws IOException thrown for {@link IOException}s
*/
public void disconnect() throws IOException {
sendAdminSystemCommand(AdminSystemCommand.DISCONNECT);
}
/**
* Tells IQConnect.exe to start streaming client stats to your connection. This sends a
* {@link AdminSystemCommand#CLIENTSTATS_ON} request.
* <br>
* There is no set message associated with this command but you should start receiving {@link ClientStatistics}
* messages (detailed on the Admin System Messages page). You will receive 1 stats message per second per client
* currently connected to IQFeed.
*
* @throws IOException thrown for {@link IOException}s
* @see #getClientStatisticsOfClientIDs()
*/
public void setClientStatsOn() throws IOException {
sendAdminSystemCommand(AdminSystemCommand.CLIENTSTATS_ON);
}
/**
* Tells IQConnect.exe to stop streaming client stats to your connection. This sends a
* {@link AdminSystemCommand#CLIENTSTATS_OFF} request.
* <br>
* There is no set message associated with this command but you should stop receiving {@link ClientStatistics}
* messages.
*
* @throws IOException thrown for {@link IOException}s
* @see #getClientStatisticsOfClientIDs()
*/
public void setClientStatsOff() throws IOException {
sendAdminSystemCommand(AdminSystemCommand.CLIENTSTATS_OFF);
synchronized (messageReceivedLock) {
clientStatisticsOfClientIDs.clear();
}
}
//
// END Feed commands
//
/**
* Gets a {@link SingleMessageFuture} for the next {@link FeedStatistics} message. This method is synchronized with
* {@link #messageReceivedLock}.
*
* @return a {@link SingleMessageFuture} of {@link FeedStatistics}
*/
public SingleMessageFuture<FeedStatistics> getNextFeedStatistics() {
synchronized (messageReceivedLock) {
if (feedStatisticsFuture != null) {
return feedStatisticsFuture;
}
feedStatisticsFuture = new SingleMessageFuture<>();
return feedStatisticsFuture;
}
}
/**
* Gets a {@link SingleMessageFuture} for the next {@link ClientStatistics} message. This method is synchronized
* with {@link #messageReceivedLock}.
*
* @return a {@link SingleMessageFuture} of {@link ClientStatistics}
*/
public SingleMessageFuture<ClientStatistics> getNextClientStatistics() {
synchronized (messageReceivedLock) {
if (clientStatisticsFuture != null) {
return clientStatisticsFuture;
}
clientStatisticsFuture = new SingleMessageFuture<>();
return clientStatisticsFuture;
}
}
/**
* Gets {@link #clientStatisticsOfClientIDs}. This method is synchronized with {@link #messageReceivedLock}.
*
* @return a {@link HashMap}
*/
public HashMap<Integer, ClientStatistics> getClientStatisticsOfClientIDs() {
synchronized (messageReceivedLock) {
return clientStatisticsOfClientIDs;
}
}
/**
* Gets {@link #latestFeedStatistics}. This method is synchronized with {@link #messageReceivedLock}.
*
* @return the last {@link FeedStatistics}
*/
public FeedStatistics getLatestFeedStatistics() {
synchronized (messageReceivedLock) {
return latestFeedStatistics;
}
}
}