All Downloads are FREE. Search and download functionalities are using the official Maven repository.

estonlabs.cxtl.common.AbstractExchangeFactory Maven / Gradle / Ivy

package estonlabs.cxtl.common;

import estonlabs.cxtl.common.codec.Codec;
import estonlabs.cxtl.common.codec.JacksonCodec;
import estonlabs.cxtl.common.http.JsonRestClient;
import estonlabs.cxtl.common.http.LogFileMetricsLogger;
import estonlabs.cxtl.common.http.MetricsLogger;
import estonlabs.cxtl.exchanges.a.specification.domain.Exchange;
import estonlabs.cxtl.exchanges.a.specification.domain.Ticker;
import estonlabs.cxtl.exchanges.a.specification.lib.Cex;
import estonlabs.cxtl.exchanges.a.specification.lib.EdiFactory;
import estonlabs.cxtl.exchanges.a.specification.lib.ExchangeDataInterface;
import estonlabs.cxtl.exchanges.a.specification.lib.ExchangeFactory;
import lombok.Getter;
import lombok.NonNull;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.util.List;
import java.util.Map;

@Getter
public abstract class AbstractExchangeFactory<  ORDER_REQ,
                                                CANCEL_REQ,
                                                ORDER_QUERY,
                                                CANDLE_REQ,
                                                TICKER extends Ticker,
                                                CEX extends Cex & ExchangeDataInterface,
                                                CONCRETE extends ExchangeFactory & EdiFactory
                                              > implements ExchangeFactory, EdiFactory {
    private final Exchange exchange;
    private final Map> hosts;
    protected MetricsLogger metricsLogger = new LogFileMetricsLogger();
    protected Proxy httpProxy;
    protected Long receiveWindow;
    protected String brokerId;

    protected Codec codec = new JacksonCodec();


    public AbstractExchangeFactory(Exchange exchange, Map> hosts) {
        this.exchange = exchange;
        this.hosts = hosts;
    }

    @NonNull
    protected JsonRestClient createJsonRestClient(URI uri) {
        return createJsonRestClient(uri, httpProxy);
    }
    @NonNull
    protected JsonRestClient createJsonRestClient(URI uri, Proxy httpProxy) {
        boolean isValidUri = hosts.values().stream().flatMap(List::stream).anyMatch(uri::equals);
        if (!isValidUri) {
            throw new IllegalArgumentException("Invalid URI for "+exchange.name()+": " + uri);
        }
        return new JsonRestClient(uri, codec, httpProxy);
    }
    @Override
    public Exchange getExchange() {
        return exchange;
    }

    @Override
    public CONCRETE codec(Codec codec) {
        this.codec= codec;
        return me();
    }

    @Override
    public CONCRETE metricsLogger(MetricsLogger logger) {
        this.metricsLogger= logger;
        return me();
    }

    @Override
    public CONCRETE httpProxy(Proxy httpProxy) {
        this.httpProxy = httpProxy;
        return me();
    }

    @Override
    public CONCRETE httpProxy(URI httpProxy) {
        return httpProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpProxy.getHost(), httpProxy.getPort())));
    }

    @Override
    public CONCRETE receiveWindow(long receiveWindow) {
        this.receiveWindow = receiveWindow;
        return me();
    }

    @Override
    public CONCRETE brokerId(String brokerId) {
        this.brokerId = brokerId;
        return me();
    }

    protected abstract CONCRETE me();

}