/xchange-coinsetter/src/main/java/com/xeiam/xchange/coinsetter/service/streaming/CoinsetterSocket.java

http://github.com/timmolter/XChange · Java · 136 lines · 94 code · 38 blank · 4 comment · 7 complexity · ae0139d6e65ff3a7655e15c08043fdb3 MD5 · raw file

  1. package com.xeiam.xchange.coinsetter.service.streaming;
  2. import io.socket.IOAcknowledge;
  3. import io.socket.IOCallback;
  4. import io.socket.SocketIO;
  5. import io.socket.SocketIOException;
  6. import java.net.MalformedURLException;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Map;
  11. import javax.net.ssl.SSLContext;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import com.google.gson.JsonElement;
  15. import com.xeiam.xchange.Exchange;
  16. import com.xeiam.xchange.coinsetter.CoinsetterExchange;
  17. import com.xeiam.xchange.coinsetter.service.streaming.event.CoinsetterSocketListener;
  18. import com.xeiam.xchange.service.BaseExchangeService;
  19. /**
  20. * Coinsetter Websockets API implementation, fires socket events.
  21. */
  22. public class CoinsetterSocket extends BaseExchangeService {
  23. private final Logger log = LoggerFactory.getLogger(CoinsetterSocket.class);
  24. private final CoinsetterStreamingConfiguration coinsetterStreamingConfiguration;
  25. private final SocketIO socket;
  26. private final List<CoinsetterSocketListener> socketIOListeners = new ArrayList<CoinsetterSocketListener>();
  27. public CoinsetterSocket(Exchange exchange, CoinsetterStreamingConfiguration coinsetterStreamingConfiguration) {
  28. super(exchange);
  29. this.coinsetterStreamingConfiguration = coinsetterStreamingConfiguration;
  30. try {
  31. SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault());
  32. } catch (NoSuchAlgorithmException e) {
  33. throw new RuntimeException(e);
  34. }
  35. final String uri = (String) exchange.getExchangeSpecification().getExchangeSpecificParametersItem(CoinsetterExchange.WEBSOCKET_URI_KEY);
  36. try {
  37. socket = new SocketIO(uri);
  38. } catch (MalformedURLException e) {
  39. throw new IllegalArgumentException(e);
  40. }
  41. }
  42. public void addListener(CoinsetterSocketListener listener) {
  43. this.socketIOListeners.add(listener);
  44. }
  45. public void connect() {
  46. socket.connect(new IOCallback() {
  47. @Override
  48. public void onMessage(JsonElement json, IOAcknowledge ack) {
  49. log.trace("onMessage: {}, {}", json, ack);
  50. for (CoinsetterSocketListener listener : socketIOListeners) {
  51. listener.onMessage(json, ack);
  52. }
  53. }
  54. @Override
  55. public void onMessage(String data, IOAcknowledge ack) {
  56. log.trace("onMessage: {}, {}", data, ack);
  57. for (CoinsetterSocketListener listener : socketIOListeners) {
  58. listener.onMessage(data, ack);
  59. }
  60. }
  61. @Override
  62. public void onError(SocketIOException socketIOException) {
  63. log.trace("onError: {}", socketIOException);
  64. for (CoinsetterSocketListener listener : socketIOListeners) {
  65. listener.onError(socketIOException);
  66. }
  67. }
  68. @Override
  69. public void onDisconnect() {
  70. log.trace("onDisconnect");
  71. for (CoinsetterSocketListener listener : socketIOListeners) {
  72. listener.onDisconnect();
  73. }
  74. }
  75. @Override
  76. public void onConnect() {
  77. log.trace("onConnect");
  78. for (CoinsetterSocketListener listener : socketIOListeners) {
  79. listener.onConnect();
  80. }
  81. }
  82. @Override
  83. public void on(String event, IOAcknowledge ack, JsonElement... args) {
  84. log.trace("on: {} {} {}", event, ack, args);
  85. for (CoinsetterSocketListener listener : socketIOListeners) {
  86. listener.on(event, ack, args);
  87. }
  88. }
  89. });
  90. // This line is cached until the connection is established.
  91. for (Map.Entry<String, Object[]> event : coinsetterStreamingConfiguration.getEvents().entrySet()) {
  92. socket.emit(event.getKey(), event.getValue());
  93. }
  94. }
  95. public void disconnect() {
  96. socket.disconnect();
  97. }
  98. }