11package com .bookmap .plugins .layer0 .bitmex .adapter ;
22
3+ import java .io .IOException ;
34import java .lang .reflect .Type ;
45import java .util .ArrayList ;
56import 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