Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hitbtc] Add raw marketdata websocket service
- Loading branch information
Showing
9 changed files
with
253 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...va/com/xeiam/xchange/hitbtc/service/streaming/HitbtcStreamingMarketDataConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.xeiam.xchange.hitbtc.service.streaming; | ||
|
||
import com.xeiam.xchange.service.streaming.ExchangeStreamingConfiguration; | ||
|
||
public class HitbtcStreamingMarketDataConfiguration implements ExchangeStreamingConfiguration { | ||
|
||
private final int maxReconnectAttempts; | ||
private final int reconnectWaitTimeInMs; | ||
private final int timeoutInMs; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param maxReconnectAttempts | ||
* @param reconnectWaitTimeInMs | ||
* @param timeoutInMs | ||
*/ | ||
public HitbtcStreamingMarketDataConfiguration(int maxReconnectAttempts, int reconnectWaitTimeInMs, int timeoutInMs) { | ||
|
||
this.maxReconnectAttempts = maxReconnectAttempts; | ||
this.reconnectWaitTimeInMs = reconnectWaitTimeInMs; | ||
this.timeoutInMs = timeoutInMs; | ||
} | ||
|
||
public HitbtcStreamingMarketDataConfiguration() { | ||
|
||
maxReconnectAttempts = 30; // 67 min | ||
reconnectWaitTimeInMs = 135000; // 2:15 | ||
timeoutInMs = 120000; // 2:00 | ||
} | ||
|
||
@Override | ||
public int getMaxReconnectAttempts() { | ||
|
||
return maxReconnectAttempts; | ||
} | ||
|
||
@Override | ||
public int getReconnectWaitTimeInMs() { | ||
|
||
return reconnectWaitTimeInMs; | ||
} | ||
|
||
@Override | ||
public int getTimeoutInMs() { | ||
|
||
return timeoutInMs; | ||
} | ||
|
||
@Override | ||
public boolean isEncryptedChannel() { | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public boolean keepAlive() { | ||
return true; | ||
} | ||
} | ||
|
86 changes: 86 additions & 0 deletions
86
...com/xeiam/xchange/hitbtc/service/streaming/HitbtcStreamingMarketDataRawEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.xeiam.xchange.hitbtc.service.streaming; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.BlockingQueue; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.xeiam.xchange.exceptions.ExchangeException; | ||
import com.xeiam.xchange.hitbtc.dto.marketdata.HitbtcIncrementalRefresh; | ||
import com.xeiam.xchange.hitbtc.dto.marketdata.HitbtcSnapshotFullRefresh; | ||
import com.xeiam.xchange.service.streaming.DefaultExchangeEvent; | ||
import com.xeiam.xchange.service.streaming.ExchangeEvent; | ||
import com.xeiam.xchange.service.streaming.ExchangeEventListener; | ||
import com.xeiam.xchange.service.streaming.ExchangeEventType; | ||
|
||
public class HitbtcStreamingMarketDataRawEventListener extends ExchangeEventListener { | ||
|
||
private final BlockingQueue<ExchangeEvent> consumerEventQueue; | ||
private final ObjectMapper streamObjectMapper; | ||
|
||
/** | ||
* @param consumerEventQueue | ||
*/ | ||
public HitbtcStreamingMarketDataRawEventListener(BlockingQueue<ExchangeEvent> consumerEventQueue) { | ||
|
||
this.consumerEventQueue = consumerEventQueue; | ||
|
||
this.streamObjectMapper = new ObjectMapper(); | ||
this.streamObjectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); | ||
} | ||
|
||
@Override | ||
public void handleEvent(ExchangeEvent event) throws ExchangeException { | ||
|
||
if (event.getEventType() != ExchangeEventType.MESSAGE) { | ||
|
||
forwardEvent(event); | ||
|
||
} else { | ||
|
||
String data = event.getData(); | ||
|
||
HitbtcIncrementalRefresh incrementalRefresh; | ||
try { | ||
|
||
incrementalRefresh = streamObjectMapper.readValue(data, HitbtcIncrementalRefresh.class); | ||
|
||
} catch (JsonMappingException jme) { | ||
|
||
HitbtcSnapshotFullRefresh snapshotFullRefresh; | ||
try { | ||
|
||
snapshotFullRefresh = streamObjectMapper.readValue(data, HitbtcSnapshotFullRefresh.class); | ||
|
||
} catch (IOException e) { | ||
throw new ExchangeException("JSON parse error", e); | ||
} | ||
|
||
handleDTO(data, snapshotFullRefresh); | ||
return; | ||
|
||
} catch (IOException e) { | ||
throw new ExchangeException("JSON parse error", e); | ||
} | ||
|
||
handleDTO(data, incrementalRefresh); | ||
} | ||
} | ||
|
||
private void forwardEvent(ExchangeEvent event) { | ||
|
||
try { | ||
consumerEventQueue.put(event); | ||
} catch (InterruptedException e) { | ||
throw new ExchangeException("InterruptedException!", e); | ||
} | ||
} | ||
|
||
private void handleDTO(String data, Object dto) { | ||
|
||
ExchangeEvent event = new DefaultExchangeEvent(ExchangeEventType.EVENT, data, dto); | ||
|
||
forwardEvent(event); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../java/com/xeiam/xchange/hitbtc/service/streaming/HitbtcStreamingMarketDataServiceRaw.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.xeiam.xchange.hitbtc.service.streaming; | ||
|
||
import java.net.URI; | ||
|
||
import com.xeiam.xchange.Exchange; | ||
import com.xeiam.xchange.service.streaming.BaseWebSocketExchangeService; | ||
|
||
public class HitbtcStreamingMarketDataServiceRaw extends BaseWebSocketExchangeService { | ||
|
||
private final HitbtcStreamingMarketDataRawEventListener exchangeEventListener; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param exchange | ||
*/ | ||
public HitbtcStreamingMarketDataServiceRaw(Exchange exchange, HitbtcStreamingMarketDataConfiguration configuration) { | ||
|
||
super(exchange, configuration); | ||
|
||
this.exchangeEventListener = new HitbtcStreamingMarketDataRawEventListener(consumerEventQueue); | ||
} | ||
|
||
@Override | ||
public void connect() throws Exception { | ||
|
||
URI uri = URI.create(exchange.getExchangeSpecification().getPlainTextUriStreaming()); | ||
|
||
// Use the default internal connect | ||
internalConnect(uri, exchangeEventListener, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters