Class DefaultQueryPacketManager

java.lang.Object
eu.cloudnetservice.driver.network.protocol.defaults.DefaultQueryPacketManager
All Implemented Interfaces:
QueryPacketManager

public class DefaultQueryPacketManager extends Object implements QueryPacketManager
The default implementation of the query manager.
Since:
4.0
  • Field Details

    • DEFAULT_TIMEOUT_DURATION

      private static final Duration DEFAULT_TIMEOUT_DURATION
    • queryTimeout

      private final Duration queryTimeout
    • networkChannel

      private final NetworkChannel networkChannel
    • waitingHandlers

      private final com.github.benmanes.caffeine.cache.Cache<UUID,eu.cloudnetservice.common.concurrent.Task<Packet>> waitingHandlers
  • Constructor Details

    • DefaultQueryPacketManager

      public DefaultQueryPacketManager(@NonNull @NonNull NetworkChannel networkChannel)
      Constructs a new query manager for the given network channel and a timeout of 30 seconds for each query.
      Parameters:
      networkChannel - the network channel associated with this manager.
      Throws:
      NullPointerException - if the given network channel is null.
    • DefaultQueryPacketManager

      public DefaultQueryPacketManager(@NonNull @NonNull NetworkChannel networkChannel, @NonNull @NonNull Duration queryTimeout)
      Constructs a new query manager for the given network with the provided query timeout.
      Parameters:
      networkChannel - the network channel associated with this manager.
      queryTimeout - the time to wait for a response to each query before being completed with an empty packet.
      Throws:
      NullPointerException - if either the given network channel or query timeout is null.
  • Method Details

    • queryTimeout

      @NonNull public @NonNull Duration queryTimeout()
      Get the duration before a query is marked as unanswered and the associated future gets completed with an empty packet.
      Specified by:
      queryTimeout in interface QueryPacketManager
      Returns:
      the timeout of a query.
    • networkChannel

      @NonNull public @NonNull NetworkChannel networkChannel()
      Get the network channel this manager is associated with. Each network channel has its own query manager.
      Specified by:
      networkChannel in interface QueryPacketManager
      Returns:
      the network channel to which this manager belongs.
      See Also:
    • waitingHandlers

      @NonNull public @NonNull @UnmodifiableView Map<UUID,eu.cloudnetservice.common.concurrent.Task<Packet>> waitingHandlers()
      Get all registered queries which are currently waiting for a response.
      Specified by:
      waitingHandlers in interface QueryPacketManager
      Returns:
      all waiting queries.
    • hasWaitingHandler

      public boolean hasWaitingHandler(@NonNull @NonNull UUID queryUniqueId)
      Checks if a waiting handler is registered (and therefore still waiting for a result) for the given unique id.
      Specified by:
      hasWaitingHandler in interface QueryPacketManager
      Parameters:
      queryUniqueId - the unique id of the query to check for.
      Returns:
      true if the handler is still registered, false otherwise.
    • unregisterWaitingHandler

      public boolean unregisterWaitingHandler(@NonNull @NonNull UUID queryUniqueId)
      Unregisters the waiting handler by the given unique id.
      Specified by:
      unregisterWaitingHandler in interface QueryPacketManager
      Parameters:
      queryUniqueId - the unique id of the handler to unregister.
      Returns:
      true if the handler was unregistered, false otherwise.
    • waitingHandler

      @Nullable public @Nullable eu.cloudnetservice.common.concurrent.Task<Packet> waitingHandler(@NonNull @NonNull UUID queryUniqueId)
      Gets, but does not remove the waiting handler for the given unique id. Null if no handler with the given unique id is still registered.
      Specified by:
      waitingHandler in interface QueryPacketManager
      Parameters:
      queryUniqueId - the unique id of the handler to get.
      Returns:
      the waiting handler associated with the given unique id, null if no handler with that id is waiting.
    • sendQueryPacket

      @NonNull public @NonNull eu.cloudnetservice.common.concurrent.Task<Packet> sendQueryPacket(@NonNull @NonNull Packet packet)
      Sends a query packet to the associated network channel, automatically selecting a query id for the packet and setting it. Equivalent to manager.sendQueryPacket(packet, UUID.randomUUID(). An existing query unique id in the packet will get overridden.
      Specified by:
      sendQueryPacket in interface QueryPacketManager
      Parameters:
      packet - the packet to convert to a query packet and send to the channel.
      Returns:
      a future completed with either the response to the packet or an empty packet if the waiting time expires.
    • sendQueryPacket

      @NonNull public @NonNull eu.cloudnetservice.common.concurrent.Task<Packet> sendQueryPacket(@NonNull @NonNull Packet packet, @NonNull @NonNull UUID queryUniqueId)
      Sends a query packet to the associated network channel, automatically setting the id in the packet. An existing query unique id in the packet will get overridden.
      Specified by:
      sendQueryPacket in interface QueryPacketManager
      Parameters:
      packet - the packet to convert to a query packet and send to the channel.
      queryUniqueId - the unique id to use when sending the packet.
      Returns:
      a future completed with either the response to the packet or an empty packet if the waiting time expires.
    • newRemovalListener

      @NonNull protected @NonNull com.github.benmanes.caffeine.cache.RemovalListener<UUID,eu.cloudnetservice.common.concurrent.Task<Packet>> newRemovalListener()
      Constructs a new removal listener for the cache, completing the future of a query packet with a timeout exception when evicted from the cache.
      Returns:
      a new removal listeners for unanswered packet future completion.