Skip to content

Commit

Permalink
completed ticker message handler, started on orderbook handler
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCookieLab committed Mar 13, 2018
1 parent 29170e4 commit 27469df
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

package com.cf.client.poloniex.wss.model;

/**
*
* @author David
*/
public class PolonicexWSSOrderBook {

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
18 changes: 8 additions & 10 deletions src/main/java/com/cf/client/wss/handler/TickerMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/cf/example/PoloniexWSSClientExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 27469df

Please sign in to comment.