-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderBookUpdater.java
119 lines (106 loc) · 4.28 KB
/
OrderBookUpdater.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
package service.orders.$tools;
import service.orders.$tools.holders.OrderBookHolder;
import service.orders.$tools.rest_client.OrderBookRestApiClient;
import service.orders.$tools.web_socket.DiffOrdersEndpoint;
import service.orders.$tools.web_socket.DiffOrdersMessageHandler;
import service.orders.$tools.web_socket.DiffOrdersWebSocketClient;
import javax.websocket.DeploymentException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import static java.lang.Thread.sleep;
/**
* It first connects to the Web Socket to retrieve and queue the current diff orders.
* Then retrieves the current Order Book from the Rest Api Service.
* Then start the process to apply diff-orders to the Order Book every second.
*/
public class OrderBookUpdater {
private static OrderBookUpdater orderBookUpdater;
private final OrderBookHolder orderBookHolder;
private final DiffOrdersWebSocketClient diffOrdersWebSocketClient;
private final DiffOrderApplier diffOrderApplier;
private int tryCount = 10;
private OrderBookUpdater(
DiffOrdersWebSocketClient diffOrdersWebSocketClient,
OrderBookHolder orderBookHolder,
DiffOrderApplier diffOrderApplier) {
this.diffOrdersWebSocketClient = diffOrdersWebSocketClient;
this.orderBookHolder = orderBookHolder;
this.diffOrderApplier = diffOrderApplier;
}
/**
* Creates an instance of this class using the default api and web socket clients that
* connect to Bitso.
*
* @return a new or the current instance of this class.
* @throws URISyntaxException in case any uri is incorrect
*/
public static OrderBookUpdater getInstance() throws URISyntaxException {
return getInstance(
DiffOrdersWebSocketClient.getInstance(new DiffOrdersEndpoint(DiffOrdersMessageHandler.getInstance())),
OrderBookHolder.getInstance(new OrderBookRestApiClient()),
DiffOrderApplier.getInstance()
);
}
/**
* Starts the process by first get diff-orders from the Web Socket and queue them internally.
* Then retrieves the current order book when the first diff-order has been received.
* Finally, it starts the process to get diff-orders from the internal queue and apply them
* to the book order.
*
* @throws IOException if connection fails with the Rest Api.
* @throws DeploymentException if connection fails with the Web Socket.
*/
public void start() throws IOException, DeploymentException {
diffOrdersWebSocketClient.connect();
loadOrderBook();
diffOrderApplier.start();
}
/**
* Stop the service that updates the book order.
*/
public void stop() {
diffOrderApplier.stop();
}
private void loadOrderBook() {
while (tryCount-- > 0) {
try {
sleep(1000);
if (diffOrdersWebSocketClient.firstDiffOfferHasBeenReceived()) {
orderBookHolder.loadOrderBook();
return;
}
} catch (InterruptedException e) {
throw new RuntimeException("Order Book couldn't get loaded. No Diff Offers were received.");
}
}
throw new RuntimeException("Order Book couldn't get loaded. No Diff Offers were received.");
}
void setTryCountToOne() {
this.tryCount = 1;
}
static OrderBookUpdater getInstance(
DiffOrdersWebSocketClient webSocketClient,
OrderBookHolder orderBookHolder,
DiffOrderApplier diffOrderApplier) {
if (orderBookUpdater == null) {
orderBookUpdater = new OrderBookUpdater(webSocketClient, orderBookHolder, diffOrderApplier);
}
return orderBookUpdater;
}
/**
* It should only used by tests.
*/
public static OrderBookUpdater getInstance(OrderBookRestApiClient orderBookApiClient, URI uri) throws URISyntaxException {
return getInstance(
DiffOrdersWebSocketClient.getInstance(uri, new DiffOrdersEndpoint(DiffOrdersMessageHandler.getInstance())),
OrderBookHolder.getInstance(orderBookApiClient),
DiffOrderApplier.getInstance());
}
/**
* It should only used by tests.
*/
public static void clearInstance() {
orderBookUpdater = null;
}
}