Skip to content

Commit 003bdbd

Browse files
fix: parser prints a string together with an exception that string has caused
1 parent 429740f commit 003bdbd

3 files changed

Lines changed: 81 additions & 63 deletions

File tree

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/BmConnector.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class BmConnector implements Runnable {
5454
private Provider provider;
5555
private TradeConnector tradeConnector;
5656

57-
ScheduledExecutorService executionsResetTimer;
57+
private ScheduledExecutorService executionsResetTimer;
5858
private int executionDay = 0;
5959
private boolean isExecutionReset;
6060

@@ -279,14 +279,15 @@ private void launchSnapshotTimer(BmInstrument instr) {
279279
TimerTask task = new TimerTask() {
280280
@Override
281281
public void run() {
282-
if (!instr.isOrderBookSnapshotParsed() == true) {
282+
if (!instr.isOrderBookSnapshotParsed()) {
283283
Log.info("[bitmex] BmConnector launchSnapshotTimer(): resubscribe " + now);
284284
unSubscribe(instr);
285285
subscribe(instr);
286286
}
287287
Log.info("[bitmex] BmConnector launchSnapshotTimer(): end " + now);
288288
}
289289
};
290+
290291
Timer timer = new Timer();
291292
instr.setSnapshotTimer(timer);
292293
Log.info("[bitmex] BmConnector launchSnapshotTimer(): " + now);
@@ -340,13 +341,15 @@ public void subscribe(BmInstrument instr) {
340341
}
341342

342343
public void unSubscribe(BmInstrument instr) {
344+
sendWebsocketMessage(instr.getUnSubscribeReq());
345+
343346
Timer timer = instr.getSnapshotTimer();
344347
if(timer != null){
345348
timer.cancel();
346349
Log.info("[bitmex] BmConnector unSubscribe: timer gets cancelled");
347350
}
348351
instr.setSubscribed(false);
349-
sendWebsocketMessage(instr.getUnSubscribeReq());
352+
350353
}
351354

352355
private int countExecutionsVolume(String symbol) {

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/ClientSocket.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Thread newThread(Runnable r) {
6666
long maxDelay = 5000;
6767
ScheduledExecutorService snapshotTimer = Executors.newSingleThreadScheduledExecutor(new CustomThreadFactory());
6868
this.snapshotTimer = snapshotTimer;
69-
69+
7070
snapshotTimer.scheduleWithFixedDelay(new Runnable() {
7171
@Override
7272
public void run() {
@@ -76,22 +76,25 @@ public void run() {
7676
Log.info("[bitmex] ClientSocket launchPingTimer: last message UTC=" + getLastMessageTime());
7777
// and if this happened before
7878
if (isConnectionPossiblyLost()) {
79-
Log.info("[bitmex] ClientSocket launchPingTimer: connection lost UTC=" + System.currentTimeMillis() );
79+
Log.info("[bitmex] ClientSocket launchPingTimer: connection lost UTC="
80+
+ System.currentTimeMillis());
8081
close();
8182
snapshotTimer.shutdown();
8283
} else {// but this did not happen before
8384
sendPing();
8485
setConnectionPossiblyLost(true);
85-
Log.info("[bitmex] ClientSocket launchPingTimer: connection possibly lost UTC=" + System.currentTimeMillis() );
86+
Log.info("[bitmex] ClientSocket launchPingTimer: connection possibly lost UTC="
87+
+ System.currentTimeMillis());
8688
}
8789
} else {
8890
// the last message was <5 seconds ago, everything is OK
89-
// Log.info("[bitmex] ClientSocket launchPingTimer: connection alive UTC=" + System.currentTimeMillis() );
91+
// Log.info("[bitmex] ClientSocket launchPingTimer:
92+
// connection alive UTC=" + System.currentTimeMillis() );
9093
setConnectionPossiblyLost(false);
9194
}
9295
}
9396
// sleep maxDelay=5 seconds
94-
}, 0, maxDelay, TimeUnit.MILLISECONDS);
97+
}, 0, maxDelay, TimeUnit.MILLISECONDS);
9598
}
9699

97100
public boolean isConnectionPossiblyLost() {
@@ -105,7 +108,11 @@ public void setConnectionPossiblyLost(boolean isConnectionPossiblyLost) {
105108
public void sendMessage(String str) {
106109
Log.info("[bitmex] ClientSocket sendMessage: " + str);
107110
try {
108-
session.getRemote().sendString(str);
111+
if (session != null) {
112+
session.getRemote().sendString(str);
113+
} else {
114+
Log.info("[bitmex] ClientSocket sendMessage: session is null");
115+
}
109116
} catch (WebSocketException | IOException e) {
110117
e.printStackTrace();
111118
}
@@ -132,7 +139,7 @@ public void onError(Session session, Throwable error) throws Exception {
132139

133140
public void close() {
134141
snapshotTimer.shutdownNow();
135-
142+
136143
if (session != null) {
137144
try {
138145
session.disconnect();
@@ -154,7 +161,7 @@ public void sendPing() {
154161
String data = "ping";
155162
ByteBuffer payload = ByteBuffer.wrap(data.getBytes());
156163
remote.sendPing(payload);
157-
// Log.info("[bitmex] ClientSocket sendPing: PING");
164+
// Log.info("[bitmex] ClientSocket sendPing: PING");
158165
} catch (WebSocketException e) {
159166
// e.printStackTrace(System.err);
160167
// Log.debug("RemoteEndpoint unavailable");
@@ -169,7 +176,7 @@ public void sendPing() {
169176
public void onFrame(Frame frame) {
170177
if (frame.getType() == Type.PONG) {
171178
lastMessageTime = System.currentTimeMillis();
172-
// Log.info("[bitmex] ClientSocket onFrame: PONG");
179+
// Log.info("[bitmex] ClientSocket onFrame: PONG");
173180
}
174181
}
175182

bitmex-adapter/src/com/bookmap/plugins/layer0/bitmex/adapter/JsonParser.java

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bookmap.plugins.layer0.bitmex.adapter;
22

3+
import java.io.IOException;
34
import java.lang.reflect.Type;
45
import java.util.ArrayList;
56
import java.util.Collections;
@@ -41,7 +42,7 @@ public static <T> ArrayList<T> getGenericFromMessage(String input, Class<T> cls)
4142
public void setProvider(Provider provider) {
4243
this.provider = provider;
4344
}
44-
45+
4546
public void setNonInstrumentPartialsParsed(Set<String> nonInstrumentPartialsParsed) {
4647
this.nonInstrumentPartialsParsed = nonInstrumentPartialsParsed;
4748
}
@@ -51,65 +52,71 @@ public void setActiveInstrumentsMap(Map<String, BmInstrument> activeInstrumentsM
5152
}
5253

5354
public void parse(String str) {
54-
// first let's find out what kind of object we have here
55-
ResponseByWebSocket responseWs = (ResponseByWebSocket) gson.fromJson(str, ResponseByWebSocket.class);
56-
if (responseWs.getTable() == null) {
57-
if (responseWs.getInfo() != null) {
58-
return;
59-
}
55+
try {
56+
// first let's find out what kind of object we have here
57+
ResponseByWebSocket responseWs = (ResponseByWebSocket) gson.fromJson(str, ResponseByWebSocket.class);
58+
if (responseWs.getTable() == null) {
59+
if (responseWs.getInfo() != null) {
60+
return;
61+
}
6062

61-
if (responseWs.getStatus() != null && responseWs.getStatus() != 200) {
62-
Log.info("[bitmex] JsonParser parser: websocket response status = " + responseWs.getError());
63-
provider.reportWrongCredentials(responseWs.getError());
64-
return;
65-
}
63+
if (responseWs.getStatus() != null && responseWs.getStatus() != 200) {
64+
Log.info("[bitmex] JsonParser parser: websocket response status = " + responseWs.getError());
65+
provider.reportWrongCredentials(responseWs.getError());
66+
return;
67+
}
6668

67-
if (responseWs.getSuccess() == true && responseWs.getRequest().getOp().equals("authKey")) {
68-
provider.getConnector().getWebSocketAuthLatch().countDown();
69-
}
70-
71-
if (responseWs.getSuccess() == true && responseWs.getRequest().getOp().equals("unsubscribe")) {
72-
String symbol = responseWs.getUnsubscribeSymbol();
73-
if (symbol != null){
74-
Log.info("[bitmex] JsonParser parser: getting unsbscribed from orderBookL2, symbol = " + symbol);
75-
BmInstrument instr = activeInstrumentsMap.get(symbol);
76-
instr.clearOrderBook();
69+
if (responseWs.getSuccess() == true && responseWs.getRequest().getOp().equals("authKey")) {
70+
provider.getConnector().getWebSocketAuthLatch().countDown();
7771
}
78-
}
7972

80-
if (responseWs.getSuccess() == null && responseWs.getError() == null && responseWs.getTable() == null
81-
&& responseWs.getInfo() == null) {
82-
Log.info("[bitmex] JsonParser parser: parser fails to parse " + str);
83-
throw new RuntimeException();
84-
}
73+
if (responseWs.getSuccess() == true && responseWs.getRequest().getOp().equals("unsubscribe")) {
74+
String symbol = responseWs.getUnsubscribeSymbol();
75+
if (symbol != null) {
76+
Log.info(
77+
"[bitmex] JsonParser parser: getting unsbscribed from orderBookL2, symbol = " + symbol);
78+
BmInstrument instr = activeInstrumentsMap.get(symbol);
79+
instr.clearOrderBook();
80+
}
81+
}
8582

86-
if (responseWs.getSuccess() != null || responseWs.getInfo() != null) {
87-
Log.info("[bitmex] JsonParser parser: service message " + str);
88-
return;
89-
}
83+
if (responseWs.getSuccess() == null && responseWs.getError() == null && responseWs.getTable() == null
84+
&& responseWs.getInfo() == null) {
85+
Log.info("[bitmex] JsonParser parser: parser fails to parse " + str);
86+
throw new RuntimeException();
87+
}
9088

91-
if (responseWs.getError() != null) {
92-
Log.info("[bitmex] JsonParser parser: errro message " + str);
93-
BmErrorMessage error = new Gson().fromJson(str, BmErrorMessage.class);
94-
Log.info(error.getMessage());
89+
if (responseWs.getSuccess() != null || responseWs.getInfo() != null) {
90+
Log.info("[bitmex] JsonParser parser: service message " + str);
91+
return;
92+
}
93+
94+
if (responseWs.getError() != null) {
95+
Log.info("[bitmex] JsonParser parser: errro message " + str);
96+
BmErrorMessage error = new Gson().fromJson(str, BmErrorMessage.class);
97+
Log.info(error.getMessage());
98+
return;
99+
}
95100
return;
96101
}
97-
return;
98-
}
99102

100-
// Options 'No object', 'success' and 'error' are already excluded
101-
// so only 'message' object (that contains 'data', an array of objects)
102-
// stays
103-
Message msg = (Message) gson.fromJson(str, Message.class);
103+
// Options 'No object', 'success' and 'error' are already excluded
104+
// so only 'message' object (that contains 'data', an array of
105+
// objects)
106+
// stays
107+
Message msg = (Message) gson.fromJson(str, Message.class);
104108

105-
// skip a messages if it contains empty data
106-
if (msg.getData() == null) {
107-
Log.info("[bitmex] JsonParser parser: data == null =>" + str);
108-
}
109+
// skip a messages if it contains empty data
110+
if (msg.getData() == null) {
111+
Log.info("[bitmex] JsonParser parser: data == null =>" + str);
112+
}
109113

110-
if (ConnectorUtils.stringToTopic.keySet().contains(msg.getTable())) {
111-
Topic Topic = ConnectorUtils.stringToTopic.get(msg.getTable());
112-
preprocessMessage(str, Topic);
114+
if (ConnectorUtils.stringToTopic.keySet().contains(msg.getTable())) {
115+
Topic Topic = ConnectorUtils.stringToTopic.get(msg.getTable());
116+
preprocessMessage(str, Topic);
117+
}
118+
} catch (Exception e) {
119+
throw new RuntimeException("[bitmex] Exception thrown to parser. String is: " + str, e);
113120
}
114121
}
115122

@@ -248,7 +255,8 @@ private <T> void preprocessMessage(String str, Topic topic) {
248255
Log.info("[bitmex] JsonParser preprocessMessage: partial acquired for " + container.name);
249256

250257
if (topic.equals(Topic.ORDERBOOKL2)) {
251-
BmInstrument instr = activeInstrumentsMap.get(((MessageGeneric<UnitData>)msg0).getData().get(0).getSymbol());
258+
BmInstrument instr = activeInstrumentsMap
259+
.get(((MessageGeneric<UnitData>) msg0).getData().get(0).getSymbol());
252260
instr.setOrderBookSnapshotParsed(true);
253261
Log.info("[bitmex] setOrderBookSnapshotParsed set true for " + instr.getSymbol());
254262
performOrderBookL2SpecificOpSetOne((MessageGeneric<UnitData>) msg0);
@@ -265,7 +273,7 @@ private <T> void preprocessMessage(String str, Topic topic) {
265273
Log.info("[bitmex] JsonParser preprocessMessage: skips data == [] => " + str);
266274
return;
267275
}
268-
276+
269277
ArrayList<T> units = (ArrayList<T>) msg0.getData();
270278

271279
if (topic.equals(Topic.ORDERBOOKL2) && !units.isEmpty()) {

0 commit comments

Comments
 (0)