-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBittrexWS.java
More file actions
270 lines (232 loc) · 9.11 KB
/
Copy pathBittrexWS.java
File metadata and controls
270 lines (232 loc) · 9.11 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
package bittrex;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import microsoft.aspnet.signalr.client.ConnectionState;
import microsoft.aspnet.signalr.client.LogLevel;
import microsoft.aspnet.signalr.client.SignalRFuture;
import microsoft.aspnet.signalr.client.hubs.HubConnection;
import microsoft.aspnet.signalr.client.hubs.HubProxy;
import microsoft.aspnet.signalr.client.transport.WebsocketTransport;
public class BittrexWS {
private static final Logger LOG = LoggerFactory.getLogger(BittrexWS.class);
private static final String DEFAULT_SERVER_URL = "https://www.bittrex.com";
private static final microsoft.aspnet.signalr.client.Logger logger = (message, level) -> {
// ignore all levels but critical
if (level == LogLevel.Critical) {
LOG.warn(message);
}
};
private final AtomicBoolean reconnecting = new AtomicBoolean(false);
private HubConnection connection;
private HubProxy proxy;
private Map<String, ChannelHandler> handlers = new ConcurrentHashMap<>();
Map<CurrencyPair, Set<Consumer<Trade>>> tradeListener = new ConcurrentHashMap<>();
private Date connectionTimestamp;
private WebsocketState state = WebsocketState.DISCONNECTED;
public BittrexWS() {
reconnect0();
}
private void init() throws InterruptedException, ExecutionException {
LOG.info("BittrexWS: Going to (re)connect.");
handlers.clear();
if (connection != null) {
try {
connection.stop();
} catch (Exception ignored) {}
}
connectionTimestamp = null;
state = WebsocketState.CONNECTING;
LOG.debug("Going to connect web socket...");
connection = new HubConnection(DEFAULT_SERVER_URL, null, true, logger);
connection.error(error -> LOG.warn("There was an error communicating with the server.", error));
connection.connected(() -> {
LOG.info("Connecton started");
connectionTimestamp = new Date();
state = WebsocketState.CONNECTED;
LOG.debug("Web socket connected.");
});
connection.closed(() -> {
LOG.info("Web socket connecton closed");
connectionTimestamp = null;
state = WebsocketState.DISCONNECTED;
});
proxy = connection.createHubProxy("corehub");
proxy.subscribe(new Object() {
@SuppressWarnings("unused")
public void updateSummaryState(Object o) {
// ignore it for now
}
@SuppressWarnings("unused")
public void updateExchangeState(UpdateExchangeStateItem o) {
ChannelHandler channelHandler = handlers.get(o.marketName);
if (channelHandler != null) {
try {
channelHandler.processUpdate(o);
} catch (Throwable t) {
LOG.warn("Error processing update " + o.marketName, t);
throw t;
}
} else {
LOG.warn("Received update for unknown handler, market: " + o.marketName);
}
}
});
SignalRFuture<Void> start = connection.start(new WebsocketTransport(logger));
start.get();
}
private <T> T useHandler(CurrencyPair pair, Function<ChannelHandler, T> f) {
if (connection == null || connection.getState() != ConnectionState.Connected) {
reconnect0();
}
if (reconnecting.get()) {
throw new RuntimeException("Bittrex WebSocket is reconnecting...");
}
if (connection.getState() != ConnectionState.Connected) {
throw new RuntimeException("Bittrex WebSocket is not connected, current state: " + connection.getState());
}
try {
ChannelHandler ch;
synchronized (pair.toString().intern()) {
ch = subscribe(pair);
}
return f.apply(ch);
} catch (BittrexException e) {
if (e.isReconnect()) {
LOG.warn("Error, will reconnect.", e);
reconnect0();
} else if (e.isResubscribe()) {
LOG.warn("Error, will resubscribe " + pair, e);
handlers.remove(toBittrexMarket(pair));
}
throw e;
} catch (RuntimeException t) {
LOG.warn("Error " + pair, t);
throw t;
}
}
public OrderBook getOrderBook(CurrencyPair pair) {
return useHandler(pair, h -> {
OrderBook result = h.getOrderBook();
return result;
});
}
public List<Trade> getTrades(CurrencyPair pair) {
return useHandler(pair, h -> h.getTrades());
}
public void addTradesListener(CurrencyPair pair, Consumer<Trade> c) {
synchronized (tradeListener) {
Set<Consumer<Trade>> set = tradeListener.get(pair);
if (set == null) {
set = new HashSet<>();
tradeListener.put(pair, set);
}
set.add(c);
}
try {
useHandler(pair, h -> null);
} catch (Exception e) {
}
}
public void removeTradesListener(CurrencyPair pair, Consumer<Trade> c) {
synchronized (tradeListener) {
Set<Consumer<Trade>> set = tradeListener.get(pair);
if (set == null) {
return;
}
set.remove(c);
if (set.isEmpty()) {
tradeListener.remove(pair);
}
}
}
private void reconnect0() {
if (!reconnecting.compareAndSet(false, true)) {
return;
}
state = WebsocketState.CONNECTING;
connectionTimestamp = null;
try {
init();
state = WebsocketState.CONNECTED;
connectionTimestamp = new Date();
} catch (Exception e) {
state = WebsocketState.ERROR;
throw new RuntimeException(e);
} finally {
reconnecting.set(false);
synchronized (reconnecting) {
reconnecting.notifyAll();
}
}
}
private ChannelHandler subscribe(final CurrencyPair pair) {
final String marketName = toBittrexMarket(pair);
ChannelHandler handler = handlers.get(marketName);
if (handler != null) {
return handler;
}
synchronized (tradeListener) {
if (!tradeListener.containsKey(pair)) {
tradeListener.put(pair, new HashSet<>());
}
}
final ChannelHandler h = new ChannelHandler(marketName, pair, proxy, tradeListener.get(pair));
handlers.put(marketName, h);
SignalRFuture<Void> updates;
try {
updates = proxy.invoke("SubscribeToExchangeDeltas", marketName);
} catch (Throwable e) {
LOG.warn("Could not subscribe to " + marketName + ", going to reconnect.", e);
reconnect();
throw new RuntimeException();
}
updates.onError(err -> {
LOG.warn("Could not subscribe for exchange deltas " + marketName, err);
handlers.remove(marketName);
});
try {
updates.get(10, TimeUnit.SECONDS);
} catch (TimeoutException err) {
handlers.remove(marketName);
LOG.warn("Could not subscribe for exchange deltas " + marketName + " (timeout).", err);
throw new RuntimeException("Could not subscribe for exchange deltas " + marketName + " (timeout).", err);
} catch (InterruptedException | ExecutionException e) {
LOG.warn("Could not subscribe for exchange deltas " + marketName + ".", e);
throw new RuntimeException("Could not subscribe for exchange deltas " + marketName + ".", e);
}
h.fetchState();
LOG.info("Subscribed successfully for " + pair);
return h;
}
protected static String toBittrexMarket(CurrencyPair pair) {
return pair.counter + "-" + pair.base;
}
public Date connectionTimestamp() {
return this.connectionTimestamp;
}
public void reconnect() {
reconnect0();
}
public synchronized List<ChannelHandler> getChannels() {
return handlers.values().stream().collect(Collectors.toList());
}
public synchronized void resubscribe(String channelId) {
handlers.remove(channelId);
}
private static enum WebsocketState {
CONNECTED, CONNECTING, ERROR, DISCONNECTED;
}
}