View Javadoc
1   package org.oxerr.okcoin.rest.service;
2   
3   import static org.oxerr.okcoin.rest.OKCoinExchange.CONNECTION_REQUEST_TIMEOUT_PARAMETER;
4   import static org.oxerr.okcoin.rest.OKCoinExchange.CONNECT_TIMEOUT_PARAMETER;
5   import static org.oxerr.okcoin.rest.OKCoinExchange.LOGIN_MAX_RETRY_TIMES_PARAMETER;
6   import static org.oxerr.okcoin.rest.OKCoinExchange.SOCKET_TIMEOUT_PARAMETER;
7   import static org.oxerr.okcoin.rest.OKCoinExchange.TRADE_PASSWORD_PARAMETER;
8   
9   import java.io.IOException;
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  import org.knowm.xchange.Exchange;
14  import org.knowm.xchange.ExchangeSpecification;
15  import org.knowm.xchange.dto.meta.RateLimit;
16  import org.oxerr.okcoin.rest.OKCoin;
17  import org.oxerr.okcoin.rest.OKCoinDigest;
18  import org.oxerr.okcoin.rest.service.web.OKCoinClient;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  
22  import si.mazi.rescu.RestProxyFactory;
23  
24  /**
25   * Base trade service.
26   */
27  public class OKCoinBaseTradeService extends OKCoinBaseService {
28  
29  	private final long interval;
30  
31  	private final Logger log = LoggerFactory.getLogger(OKCoinBaseTradeService.class);
32  
33  	protected final OKCoin okCoin;
34  
35  	protected final OKCoinDigest sign;
36  
37  	protected final String apiKey;
38  
39  	private Map<String, Long> lasts = new HashMap<>();
40  
41  	protected final OKCoinClient okCoinClient;
42  	protected final int loginMaxRetryTimes;
43  
44  	protected OKCoinBaseTradeService(Exchange exchange) {
45  		super(exchange);
46  
47  		final RateLimit[]  rateLimits = exchange.getExchangeMetaData().getPrivateRateLimits();
48  		Integer maxPrivatePollRatePerSecond = 40;
49  		for (RateLimit rateLimit : rateLimits) {
50  			maxPrivatePollRatePerSecond = rateLimit.calls;
51  		}
52  		if (maxPrivatePollRatePerSecond == null || maxPrivatePollRatePerSecond.intValue() == 0) {
53  			interval = 0;
54  		} else {
55  			interval = (long) ((float) 1_000 / (float) maxPrivatePollRatePerSecond);
56  		}
57  		log.debug("interval: {}", interval);
58  
59  		ExchangeSpecification spec = exchange.getExchangeSpecification();
60  
61  		if (spec.getApiKey() != null && spec.getSecretKey() != null) {
62  			okCoin = RestProxyFactory.createProxy(OKCoin.class, spec.getSslUri());
63  			this.apiKey = spec.getApiKey();
64  			sign = new OKCoinDigest(spec.getSecretKey());
65  		} else {
66  			okCoin = null;
67  			this.apiKey = null;
68  			sign = null;
69  		}
70  
71  		Number loginMaxRetryTimes = (Number) spec.getExchangeSpecificParametersItem(LOGIN_MAX_RETRY_TIMES_PARAMETER);
72  		this.loginMaxRetryTimes = loginMaxRetryTimes == null ? 1 : loginMaxRetryTimes.intValue();
73  
74  		if (spec.getUserName() != null && spec.getPassword() != null) {
75  			String tradePassword = (String) spec.getExchangeSpecificParametersItem(TRADE_PASSWORD_PARAMETER);
76  
77  			Number socketTimeout = (Number) spec.getExchangeSpecificParametersItem(SOCKET_TIMEOUT_PARAMETER);
78  			Number connectTimeout = (Number) spec.getExchangeSpecificParametersItem(CONNECT_TIMEOUT_PARAMETER);
79  			Number connectionRequestTimeout = (Number) spec.getExchangeSpecificParametersItem(CONNECTION_REQUEST_TIMEOUT_PARAMETER);
80  
81  			okCoinClient = new OKCoinClient(
82  				spec.getUserName(), spec.getPassword(), tradePassword,
83  				socketTimeout == null ? 0 : socketTimeout.intValue(),
84  				connectTimeout == null ? 0 : connectTimeout.intValue(),
85  				connectionRequestTimeout == null ? 0 : connectionRequestTimeout.intValue());
86  
87  			try {
88  				okCoinClient.login();
89  			} catch (IOException e) {
90  				log.warn(e.getMessage(), e);
91  			}
92  
93  		} else {
94  			okCoinClient = null;
95  		}
96  	}
97  
98  	private long getLast(String method) {
99  		Long last = lasts.get(method);
100 		if (last == null) {
101 			return 0;
102 		} else {
103 			return last.longValue();
104 		}
105 	}
106 
107 	protected void updateLast(String method) {
108 		lasts.put(method, System.currentTimeMillis());
109 	}
110 
111 	protected void sleep(String method) {
112 		if (System.currentTimeMillis() - getLast(method) < interval) {
113 			sleep();
114 		}
115 	}
116 
117 	private void sleep() {
118 		try {
119 			log.trace("Sleeping for {} ms.", interval);
120 			Thread.sleep(interval);
121 		} catch (InterruptedException e) {
122 			Thread.currentThread().interrupt();
123 		}
124 	}
125 
126 }