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 TypeFieldDescriptionprotected final NetworkChannelprotected final com.github.benmanes.caffeine.cache.Cache<UUID, CompletableFuture<Packet>> -
Constructor Summary
ConstructorsConstructorDescriptionDefaultQueryPacketManager(@NonNull NetworkChannel networkChannel) 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, CompletableFuture<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.sendQueryPacket(@NonNull Packet packet) Sends a query packet to the associated network channel, automatically selecting a query id for the packet and setting it.waitingHandler(@NonNull UUID queryUniqueId) Gets and removes the waiting handler for the given query unique id.longReturns the approximate number of query response handlers that are currently awaiting a response.
-
Field Details
-
networkChannel
-
waitingHandlers
protected final com.github.benmanes.caffeine.cache.Cache<UUID,CompletableFuture<Packet>> waitingHandlers
-
-
Constructor Details
-
DefaultQueryPacketManager
Constructs a new query manager for the given network with the provided query timeout.- Parameters:
networkChannel- the network channel associated with this manager.- Throws:
NullPointerException- if the given network channel is null.
-
-
Method Details
-
networkChannel
Get the network channel this manager is associated with. This channel is used to transmit query packets to the receiver, therefore the network channel of the received must be known in advance.- Specified by:
networkChannelin interfaceQueryPacketManager- Returns:
- the network channel to which this manager belongs.
-
waitingHandlerCount
public long waitingHandlerCount()Returns the approximate number of query response handlers that are currently awaiting a response. The returned value is an estimate, as the real value might differ due to concurrent sends of queries or receive of responses.- Specified by:
waitingHandlerCountin interfaceQueryPacketManager- Returns:
- the estimated number of handlers that are awaiting a response.
-
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.
-
waitingHandler
@Nullable public @Nullable CompletableFuture<Packet> waitingHandler(@NonNull @NonNull UUID queryUniqueId) Gets and removes the waiting handler for the given query unique id.- 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
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.
-
newRemovalListener
@NonNull protected @NonNull com.github.benmanes.caffeine.cache.RemovalListener<UUID,CompletableFuture<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.
-