From 27469df7a9d6916d4d913d8b39ec7d05ebf40db1 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 13 Mar 2018 17:52:53 -0400 Subject: [PATCH] completed ticker message handler, started on orderbook handler --- .../poloniex/PoloniexWSSClientRouter.java | 2 +- .../wss/model/PolonicexWSSOrderBook.java | 10 +++++++++ .../wss/handler/OrderBookMessageHandler.java | 22 ++++++++++++++++++- .../wss/handler/TickerMessageHandler.java | 18 +++++++-------- .../cf/example/PoloniexWSSClientExample.java | 5 +++-- 5 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/cf/client/poloniex/wss/model/PolonicexWSSOrderBook.java diff --git a/src/main/java/com/cf/client/poloniex/PoloniexWSSClientRouter.java b/src/main/java/com/cf/client/poloniex/PoloniexWSSClientRouter.java index be97447..0e94091 100644 --- a/src/main/java/com/cf/client/poloniex/PoloniexWSSClientRouter.java +++ b/src/main/java/com/cf/client/poloniex/PoloniexWSSClientRouter.java @@ -97,7 +97,7 @@ public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; LOG.trace("WebSocket Client received message: " + textFrame.text()); - List results = this.gson.fromJson(textFrame.text(), List.class); + List results = this.gson.fromJson(textFrame.text(), List.class); this.subscriptions.getOrDefault(results.get(0), this.defaultSubscriptionMessageHandler).handle(textFrame.text()); } else if (frame instanceof CloseWebSocketFrame) { diff --git a/src/main/java/com/cf/client/poloniex/wss/model/PolonicexWSSOrderBook.java b/src/main/java/com/cf/client/poloniex/wss/model/PolonicexWSSOrderBook.java new file mode 100644 index 0000000..69346ab --- /dev/null +++ b/src/main/java/com/cf/client/poloniex/wss/model/PolonicexWSSOrderBook.java @@ -0,0 +1,10 @@ + +package com.cf.client.poloniex.wss.model; + +/** + * + * @author David + */ +public class PolonicexWSSOrderBook { + +} diff --git a/src/main/java/com/cf/client/wss/handler/OrderBookMessageHandler.java b/src/main/java/com/cf/client/wss/handler/OrderBookMessageHandler.java index 0ab01b5..55a2f81 100644 --- a/src/main/java/com/cf/client/wss/handler/OrderBookMessageHandler.java +++ b/src/main/java/com/cf/client/wss/handler/OrderBookMessageHandler.java @@ -1,14 +1,34 @@ package com.cf.client.wss.handler; +import com.cf.client.poloniex.wss.model.PolonicexWSSOrderBook; +import com.google.gson.Gson; +import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + /** * * @author David */ public class OrderBookMessageHandler implements IMessageHandler { + private final static Logger LOG = LogManager.getLogger(); + private final static Gson GSON = new Gson(); + @Override public void handle(String message) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + LOG.trace(message); + PolonicexWSSOrderBook orderBook = this.mapMessageToPoloniexOrderBook(message); + LOG.trace(orderBook); + } + + protected PolonicexWSSOrderBook mapMessageToPoloniexOrderBook(String message) { + List results = GSON.fromJson(message, List.class); + if (results.size() < 3) { + return null; + } + + return null; } } diff --git a/src/main/java/com/cf/client/wss/handler/TickerMessageHandler.java b/src/main/java/com/cf/client/wss/handler/TickerMessageHandler.java index 18f2ebc..3acac47 100644 --- a/src/main/java/com/cf/client/wss/handler/TickerMessageHandler.java +++ b/src/main/java/com/cf/client/wss/handler/TickerMessageHandler.java @@ -14,24 +14,22 @@ public class TickerMessageHandler implements IMessageHandler { private final static Logger LOG = LogManager.getLogger(); - private final Gson gson; - - public TickerMessageHandler() { - this.gson = new Gson(); - } + private final static Gson GSON = new Gson(); @Override public void handle(String message) { LOG.trace(message); PoloniexWSSTicker ticker = this.mapMessageToPoloniexTicker(message); LOG.trace(ticker); - + } - + protected PoloniexWSSTicker mapMessageToPoloniexTicker(String message) { - List results = gson.fromJson(message, List.class); - if (results.size() < 3) return null; - + List results = GSON.fromJson(message, List.class); + if (results.size() < 3) { + return null; + } + List olhc = (List) results.get(2); return new PoloniexWSSTicker.PoloniexWSSTickerBuilder() .setCurrencyPair((Double) olhc.get(0)) diff --git a/src/main/java/com/cf/example/PoloniexWSSClientExample.java b/src/main/java/com/cf/example/PoloniexWSSClientExample.java index 9f89214..c168682 100644 --- a/src/main/java/com/cf/example/PoloniexWSSClientExample.java +++ b/src/main/java/com/cf/example/PoloniexWSSClientExample.java @@ -3,6 +3,7 @@ import com.cf.client.WSSClient; import com.cf.client.poloniex.wss.model.PoloniexWSSSubscription; import com.cf.client.wss.handler.LoggerMessageHandler; +import com.cf.client.wss.handler.OrderBookMessageHandler; import com.cf.client.wss.handler.TickerMessageHandler; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -33,9 +34,9 @@ public void run() throws Exception { try (WSSClient wssClient = new WSSClient(ENDPOINT_URL)) { - wssClient.addSubscription(PoloniexWSSSubscription.USDT_ETH, new LoggerMessageHandler()); + wssClient.addSubscription(PoloniexWSSSubscription.USDT_ETH, new OrderBookMessageHandler()); wssClient.addSubscription(PoloniexWSSSubscription.TICKER, new TickerMessageHandler()); - wssClient.run(60000); + wssClient.run(20000); } } }