Class DefaultQueryPacketManager
java.lang.Object
eu.cloudnetservice.driver.network.protocol.defaults.DefaultQueryPacketManager
- All Implemented Interfaces:
QueryPacketManager
The default implementation of the query manager.
- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Durationprivate final NetworkChannelprivate final Durationprivate final com.github.benmanes.caffeine.cache.Cache<UUID,eu.cloudnetservice.common.concurrent.Task<Packet>> -
Constructor Summary
ConstructorsConstructorDescriptionDefaultQueryPacketManager(@NonNull NetworkChannel networkChannel) Constructs a new query manager for the given network channel and a timeout of 30 seconds for each query.DefaultQueryPacketManager(@NonNull NetworkChannel networkChannel, @NonNull Duration queryTimeout) Constructs a new query manager for the given network with the provided query timeout. -
Method Summary
Modifier and TypeMethodDescriptionbooleanhasWaitingHandler(@NonNull UUID queryUniqueId) Checks if a waiting handler is registered (and therefore still waiting for a result) for the given unique id.Get the network channel this manager is associated with.protected @NonNull com.github.benmanes.caffeine.cache.RemovalListener<UUID,eu.cloudnetservice.common.concurrent.Task<Packet>> Constructs a new removal listener for the cache, completing the future of a query packet with a timeout exception when evicted from the cache.Get the duration before a query is marked as unanswered and the associated future gets completed with an empty packet.sendQueryPacket(@NonNull Packet packet) Sends a query packet to the associated network channel, automatically selecting a query id for the packet and setting it.sendQueryPacket(@NonNull Packet packet, @NonNull UUID queryUniqueId) Sends a query packet to the associated network channel, automatically setting the id in the packet.booleanunregisterWaitingHandler(@NonNull UUID queryUniqueId) Unregisters the waiting handler by the given unique id.waitingHandler(@NonNull UUID queryUniqueId) Gets, but does not remove the waiting handler for the given unique id.@NonNull @UnmodifiableView Map<UUID,eu.cloudnetservice.common.concurrent.Task<Packet>> Get all registered queries which are currently waiting for a response.
-
Field Details
-
DEFAULT_TIMEOUT_DURATION
-
queryTimeout
-
networkChannel
-
waitingHandlers
-
-
Constructor Details
-
DefaultQueryPacketManager
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
Get the duration before a query is marked as unanswered and the associated future gets completed with an empty packet.- Specified by:
queryTimeoutin interfaceQueryPacketManager- Returns:
- the timeout of a query.
-
networkChannel
Get the network channel this manager is associated with. Each network channel has its own query manager.- Specified by:
networkChannelin interfaceQueryPacketManager- 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:
waitingHandlersin interfaceQueryPacketManager- Returns:
- all waiting queries.
-
hasWaitingHandler
Checks if a waiting handler is registered (and therefore still waiting for a result) for the given unique id.- Specified by:
hasWaitingHandlerin interfaceQueryPacketManager- Parameters:
queryUniqueId- the unique id of the query to check for.- Returns:
- true if the handler is still registered, false otherwise.
-
unregisterWaitingHandler
Unregisters the waiting handler by the given unique id.- Specified by:
unregisterWaitingHandlerin interfaceQueryPacketManager- 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:
waitingHandlerin interfaceQueryPacketManager- 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 tomanager.sendQueryPacket(packet, UUID.randomUUID(). An existing query unique id in the packet will get overridden.- Specified by:
sendQueryPacketin interfaceQueryPacketManager- 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:
sendQueryPacketin interfaceQueryPacketManager- 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.
-