Class CatnipShardImpl

java.lang.Object
com.mewna.catnip.shard.CatnipShardImpl
All Implemented Interfaces:
CatnipShard, WebSocket.Listener

public class CatnipShardImpl extends Object implements CatnipShard, WebSocket.Listener
A catnip shard encapsulates a single websocket connection to Discord's real-time gateway. Shards should be deployed/undeployed by a ShardManager; see DefaultShardManager and AbstractShardManager for more.

Shards are controlled by calling the methods exposed on the CatnipShard; it is NOT recommended that you store a reference to a shard object; and instead poll the ShardManager for the shard when needed.

Since:
8/31/18.
Author:
amy
  • Field Details

  • Constructor Details

    • CatnipShardImpl

      public CatnipShardImpl(@Nonnull Catnip catnip, @Nonnegative int id, @Nonnegative int limit, @Nullable Presence presence)
  • Method Details

    • basePayload

      public static com.grack.nanojson.JsonObject basePayload(@Nonnull GatewayOp op)
    • basePayload

      public static com.grack.nanojson.JsonObject basePayload(@Nonnull GatewayOp op, @Nullable com.grack.nanojson.JsonObject payload)
    • basePayload

      public static com.grack.nanojson.JsonObject basePayload(@Nonnull GatewayOp op, @Nonnull @Nonnegative Integer payload)
    • onOpen

      public void onOpen(WebSocket webSocket)
      Specified by:
      onOpen in interface WebSocket.Listener
    • onText

      public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last)
      Specified by:
      onText in interface WebSocket.Listener
    • onBinary

      public CompletionStage<?> onBinary(WebSocket webSocket, ByteBuffer data, boolean last)
      Specified by:
      onBinary in interface WebSocket.Listener
    • onError

      public void onError(WebSocket webSocket, Throwable error)
      Specified by:
      onError in interface WebSocket.Listener
    • onClose

      public CompletionStage<?> onClose(WebSocket webSocket, int closeCode, String reason)
      Specified by:
      onClose in interface WebSocket.Listener
    • isConnected

      public boolean isConnected()
      Description copied from interface: CatnipShard
      Checks whether or not the current shard is currently connected to the websocket gateway. This is done as a boolean because - at least for now - there's only 2 meaningful states: connected, and queued to be connected.
      Specified by:
      isConnected in interface CatnipShard
      Returns:
      Whether or not the shard with the given id is currently connected to the websocket gateway.
    • lastHeartbeatLatency

      public long lastHeartbeatLatency()
      Description copied from interface: CatnipShard
      Return the shard's computed gateway latency, ie. the time it takes for the shard to send a heartbeat to Discord and get a response.
      Specified by:
      lastHeartbeatLatency in interface CatnipShard
      Returns:
      The shard's computed gateway latency.
    • connect

      @Nonnull public io.reactivex.rxjava3.core.Single<ShardConnectState> connect()
      Specified by:
      connect in interface CatnipShard
    • disconnect

      public void disconnect()
      Specified by:
      disconnect in interface CatnipShard
    • queueSendToSocket

      public void queueSendToSocket(@Nonnull com.grack.nanojson.JsonObject payload)
      Specified by:
      queueSendToSocket in interface CatnipShard
    • sendToSocket

      public void sendToSocket(@Nonnull com.grack.nanojson.JsonObject payload)
      Specified by:
      sendToSocket in interface CatnipShard
    • updatePresence

      public void updatePresence(@Nonnull PresenceImpl presence)
      Specified by:
      updatePresence in interface CatnipShard
    • presence

      @Nonnull public Presence presence()
      Description copied from interface: CatnipShard
      Return the shard's current presence.
      Specified by:
      presence in interface CatnipShard
      Returns:
      The shard's current presence.
    • queueVoiceStateUpdate

      public void queueVoiceStateUpdate(@Nonnull com.grack.nanojson.JsonObject json)
      Specified by:
      queueVoiceStateUpdate in interface CatnipShard
    • shardInfo

      public ShardInfo shardInfo()
    • trace

      public List<String> trace()
      Description copied from interface: CatnipShard
      Fetches the trace from the shard.
      Specified by:
      trace in interface CatnipShard
      Returns:
      The shard's trace.
    • lifecycleState

      public LifecycleState lifecycleState()
      Description copied from interface: CatnipShard
      Get the lifecycle state for the current shard. This provides more meaningful info than CatnipShard.isConnected(), because it provides more granular info about the shard's state. However, it does not differentiate between "connected" or not per se; it is up to the end-user to determine whether or not the lifecycle state is actually a state of being "connected."
      Specified by:
      lifecycleState in interface CatnipShard
      Returns:
      The lifecycle state of the shard.