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

io.contek.invoker.bybit.api.websocket.WebSocketChannel Maven / Gradle / Ivy

The newest version!
package io.contek.invoker.bybit.api.websocket;

import static io.contek.invoker.bybit.api.websocket.common.constants.WebSocketOperationKeys._subscribe;
import static io.contek.invoker.bybit.api.websocket.common.constants.WebSocketOperationKeys._unsubscribe;
import static io.contek.invoker.commons.websocket.SubscriptionState.*;

import com.google.common.collect.ImmutableList;
import io.contek.invoker.bybit.api.websocket.common.WebSocketOperationRequest;
import io.contek.invoker.bybit.api.websocket.common.WebSocketOperationResponse;
import io.contek.invoker.bybit.api.websocket.common.WebSocketTopicMessage;
import io.contek.invoker.commons.websocket.*;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public abstract class WebSocketChannel>
    extends BaseWebSocketChannel, Message, Message> {

  private final WebSocketRequestIdGenerator requestIdGenerator;

  private final AtomicReference pendingRequestHolder =
      new AtomicReference<>();

  protected WebSocketChannel(
      WebSocketChannelId id, WebSocketRequestIdGenerator requestIdGenerator) {
    super(id);
    this.requestIdGenerator = requestIdGenerator;
  }

  @Override
  protected Message getData(Message message) {
    return message;
  }

  @Override
  protected final SubscriptionState subscribe(WebSocketSession session) {
    synchronized (pendingRequestHolder) {
      if (pendingRequestHolder.get() != null) {
        throw new IllegalStateException();
      }

      WebSocketChannelId id = getId();
      WebSocketOperationRequest request = new WebSocketOperationRequest();
      request.req_id = requestIdGenerator.generateNext();
      request.op = _subscribe;
      request.args = ImmutableList.of(id.getTopic());
      session.send(request);
      pendingRequestHolder.set(request);
    }

    return SUBSCRIBING;
  }

  @Override
  protected final SubscriptionState unsubscribe(WebSocketSession session) {
    synchronized (pendingRequestHolder) {
      if (pendingRequestHolder.get() != null) {
        throw new IllegalStateException();
      }

      WebSocketChannelId id = getId();
      WebSocketOperationRequest request = new WebSocketOperationRequest();
      request.req_id = requestIdGenerator.generateNext();
      request.op = _unsubscribe;
      request.args = ImmutableList.of(id.getTopic());
      session.send(request);
      pendingRequestHolder.set(request);
    }

    return UNSUBSCRIBING;
  }

  @Nullable
  @Override
  protected final SubscriptionState getState(AnyWebSocketMessage message) {
    if (!(message instanceof WebSocketOperationResponse confirmation)) {
      return null;
    }

    synchronized (pendingRequestHolder) {
      WebSocketOperationRequest command = pendingRequestHolder.get();
      if (command == null) {
        return null;
      }

      if (confirmation.req_id == null || !confirmation.req_id.equals(command.req_id)) {
        return null;
      }

      if (!confirmation.success) {
        throw new WebSocketIllegalMessageException(confirmation.ret_msg);
      }

      reset();

      if (command.op.equals(_subscribe)) {
        return SUBSCRIBED;
      }

      if (command.op.equals(_unsubscribe)) {
        return UNSUBSCRIBED;
      }

      throw new IllegalStateException();
    }
  }

  @Override
  protected final void reset() {
    synchronized (pendingRequestHolder) {
      pendingRequestHolder.set(null);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy