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

com.upstox.feeder.MarketDataFeeder Maven / Gradle / Ivy

package com.upstox.feeder;

import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import com.google.gson.Gson;
import com.upstox.ApiClient;
import com.upstox.ApiException;
import com.upstox.api.WebsocketAuthRedirectResponse;
import com.upstox.feeder.constants.Method;
import com.upstox.feeder.constants.Mode;
import com.upstox.feeder.exception.StreamerException;
import com.upstox.feeder.listener.OnCloseListener;
import com.upstox.feeder.listener.OnErrorListener;
import com.upstox.feeder.listener.OnMessageListener;
import com.upstox.feeder.listener.OnOpenListener;

import io.swagger.client.api.WebsocketApi;

public class MarketDataFeeder extends Feeder {

    private static final String SOCKET_NOT_OPEN_ERROR = "WebSocket is not open.";

    protected MarketDataFeeder(ApiClient apiClient, OnOpenListener onOpenListener, OnMessageListener onMessageListener,
            OnErrorListener onErrorListener, OnCloseListener onCloseListener) {
        super(apiClient);
        this.onOpenListener = onOpenListener;
        this.onMessageListener = onMessageListener;
        this.onErrorListener = onErrorListener;
        this.onCloseListener = onCloseListener;
    }

    @Override
    protected void connect() {

        if (this.webSocket != null) {
            return;
        }

        // Get authorized WebSocket URI for market data feed
        URI serverUri;

        try {
            serverUri = getAuthorizedWebSocketUri(apiClient);
        } catch (ApiException e) {

            if (onErrorListener != null) {
                onErrorListener.onError(e);
            }
            return;
        }

        this.webSocket = new WebSocketClient(serverUri) {

            @Override
            public void onOpen(ServerHandshake handshakedata) {

                if (onOpenListener != null) {
                    onOpenListener.onOpen();
                }
            }

            @Override
            public void onMessage(String message) {

                System.out.println("On Message as string: " + message);
            }

            @Override
            public void onMessage(ByteBuffer bytes) {

                if (onMessageListener != null) {
                    onMessageListener.onMessageAsBytes(bytes);
                }
            }

            @Override
            public void onClose(int code, String reason, boolean remote) {

                if (onCloseListener != null) {
                    onCloseListener.onClose(code, reason);
                }
            }

            @Override
            public void onError(Exception error) {

                if (onErrorListener != null) {
                    onErrorListener.onError(error);
                }
            }
        };

        this.webSocket.connect();
    }

    protected void subscribe(Set instrumentKeys, Mode mode) {

        if (webSocket != null && webSocket.isOpen()) {
            String request = buildRequest(instrumentKeys, Method.SUBSCRIBE, mode);
            byte[] binaryData = request.getBytes(StandardCharsets.UTF_8);
            webSocket.send(binaryData);
        } else {
            throw new StreamerException(SOCKET_NOT_OPEN_ERROR);
        }
    }

    protected void unsubscribe(Set instrumentKeys) {

        if (webSocket != null && webSocket.isOpen()) {
            String request = buildRequest(instrumentKeys, Method.UNSUBSCRIBE, null);
            byte[] binaryData = request.getBytes(StandardCharsets.UTF_8);
            webSocket.send(binaryData);
        } else {
            throw new StreamerException(SOCKET_NOT_OPEN_ERROR);
        }
    }

    protected void changeMode(Set instrumentKeys, Mode newMode) {

        if (webSocket != null && webSocket.isOpen()) {
            String request = buildRequest(instrumentKeys, Method.CHANGE_METHOD, newMode);
            byte[] binaryData = request.getBytes(StandardCharsets.UTF_8);
            webSocket.send(binaryData);
        } else {
            throw new StreamerException(SOCKET_NOT_OPEN_ERROR);
        }
    }

    private String buildRequest(Set instrumentKeys, Method method, Mode mode) {
        Map requestObj = new HashMap<>();
        requestObj.put("guid", UUID.randomUUID()
                .toString());
        requestObj.put("method", method.getValue());
        Map data = new HashMap<>();
        data.put("instrumentKeys", instrumentKeys);

        if (mode != null) {
            data.put("mode", mode.name()
                    .toLowerCase());
        }
        requestObj.put("data", data);
        return new Gson().toJson(requestObj);
    }

    private static URI getAuthorizedWebSocketUri(ApiClient authenticatedClient) throws ApiException {
        WebsocketApi websocketApi = new WebsocketApi(authenticatedClient);
        WebsocketAuthRedirectResponse response = websocketApi.getMarketDataFeedAuthorize("2.0");

        return URI.create(response.getData()
                .getAuthorizedRedirectUri());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy