package com.binance.connector.client.utils;

import com.binance.connector.client.exceptions.BinanceConnectorException;
import com.binance.connector.client.utils.websocketcallback.WebSocketClosedCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketClosingCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketFailureCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketMessageCallback;
import com.binance.connector.client.utils.websocketcallback.WebSocketOpenCallback;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: binance-connector-java-3.0.0rc3.jar:com/binance/connector/client/utils/WebSocketConnection.class */
public class WebSocketConnection extends WebSocketListener {
    private static final int NORMAL_CLOSURE_STATUS = 1000;
    private static OkHttpClient client;
    private final Request request;
    private final String streamName;
    private final WebSocketOpenCallback onOpenCallback;
    private final WebSocketMessageCallback onMessageCallback;
    private final WebSocketClosingCallback onClosingCallback;
    private final WebSocketClosedCallback onClosedCallback;
    private final WebSocketFailureCallback onFailureCallback;
    private static final AtomicInteger connectionCounter = new AtomicInteger(0);
    private static final Logger logger = LoggerFactory.getLogger(WebSocketConnection.class);
    private final int connectionId = connectionCounter.incrementAndGet();
    private WebSocket webSocket = null;
    private final Object mutex = new Object();

    public WebSocketConnection(WebSocketOpenCallback onOpenCallback, WebSocketMessageCallback onMessageCallback, WebSocketClosingCallback onClosingCallback, WebSocketClosedCallback onClosedCallback, WebSocketFailureCallback onFailureCallback, Request request, OkHttpClient client2) {
        this.onOpenCallback = onOpenCallback;
        this.onMessageCallback = onMessageCallback;
        this.onClosingCallback = onClosingCallback;
        this.onClosedCallback = onClosedCallback;
        this.onFailureCallback = onFailureCallback;
        this.request = request;
        this.streamName = request.url().host() + request.url().encodedPath();
        client = client2;
    }

    public void connect() {
        synchronized (this.mutex) {
            if (null == this.webSocket) {
                logger.info("[Connection {}] Connecting to {}", Integer.valueOf(this.connectionId), this.streamName);
                this.webSocket = client.newWebSocket(this.request, this);
            } else {
                logger.info("[Connection {}] is already connected to {}", Integer.valueOf(this.connectionId), this.streamName);
            }
        }
    }

    public int getConnectionId() {
        return this.connectionId;
    }

    public void send(String message) {
        if (null == this.webSocket) {
            throw new BinanceConnectorException("No WebSocket connection. Please connect first!");
        }
        this.webSocket.send(message);
    }

    public void close() {
        if (null != this.webSocket) {
            logger.info("[Connection {}] Closing connection to {}", Integer.valueOf(this.connectionId), this.streamName);
            this.webSocket.close((int) NORMAL_CLOSURE_STATUS, (String) null);
        }
    }

    public void onOpen(WebSocket ws, Response response) {
        logger.info("[Connection {}] Connected to Server", Integer.valueOf(this.connectionId));
        this.onOpenCallback.onOpen(response);
    }

    public void onClosing(WebSocket ws, int code, String reason) {
        WebSocketConnection.super.onClosing(ws, code, reason);
        this.onClosingCallback.onClosing(code, reason);
    }

    public void onClosed(WebSocket ws, int code, String reason) {
        WebSocketConnection.super.onClosed(ws, code, reason);
        this.onClosedCallback.onClosed(code, reason);
    }

    public void onMessage(WebSocket ws, String text) {
        this.onMessageCallback.onMessage(text);
    }

    public void onFailure(WebSocket ws, Throwable t, Response response) {
        logger.error("[Connection {}] Failure", Integer.valueOf(this.connectionId), t);
        this.onFailureCallback.onFailure(t, response);
    }
}
