Class RPCPacketListener
java.lang.Object
eu.cloudnetservice.driver.network.rpc.listener.RPCPacketListener
- All Implemented Interfaces:
PacketListener
A network packet listener designed to handle all rpc messages using an underlying handler registry to post method
call instructions to it.
- Since:
- 4.0
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRPCPacketListener(@NonNull RPCHandlerRegistry rpcHandlerRegistry) Constructs a new rpc packet listener instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate @NonNull RPCInvocationContextbuildContext(@NonNull DataBuf content, @Nullable Object workingInstance) Builds a new context for a rpc method invocation based on the given information and remaining content in the buffer.private voidexecuteRPCChainStep(int chainDepth, int currentDepth, boolean resultExpected, @NonNull DataBuf content, @NonNull Packet request, @NonNull NetworkChannel channel, @Nullable Object previousMethodReturnValue) Executes the next RPC chain step of the current RPC chain.voidhandle(@NonNull NetworkChannel channel, @NonNull Packet packet) Handles the incoming packet.postRPCRequestToHandler(@NonNull String targetClassName, @NonNull RPCInvocationContext context) Posts the given RPC invocation context to the RPC handler that is registered for the class with the given name.private voidsendResponseData(@NonNull NetworkChannel channel, @NonNull Packet request, @NonNull DataBuf response) Sends a serialized response to given request packet into the given network channel.serializeHandlingResult(@Nullable RPCInvocationResult invocationResult) Serializes the result of the RPC handling process into a data buffer.private voidwaitForInvocationCompletion(@Nullable CompletableFuture<RPCInvocationResult> invocationTask, @NonNull Consumer<RPCInvocationResult> callback) Waits for the given invocation task to finish, calling the callback if that is the case.
-
Field Details
-
rpcHandlerRegistry
-
-
Constructor Details
-
RPCPacketListener
Constructs a new rpc packet listener instance.- Parameters:
rpcHandlerRegistry- the registry to use to downstream call instructions to.- Throws:
NullPointerException- if the given rpc handler registry is null.
-
-
Method Details
-
handle
public void handle(@NonNull @NonNull NetworkChannel channel, @NonNull @NonNull Packet packet) throws Exception Handles the incoming packet. A packet handle should be release-safe and transactional to preserve the content of the packet for listeners which are following in the chain. If a packet listener is not doing that, it might lead to unexpected exceptions in other packet listener implementations for the same packet.- Specified by:
handlein interfacePacketListener- Parameters:
channel- the channel from which the original packet came.packet- the packet which was received.- Throws:
Exception- if any exception occurs during handling of the packet.
-
waitForInvocationCompletion
private void waitForInvocationCompletion(@Nullable @Nullable CompletableFuture<RPCInvocationResult> invocationTask, @NonNull @NonNull Consumer<RPCInvocationResult> callback) Waits for the given invocation task to finish, calling the callback if that is the case. The given callback receives a null value if the given task is null, the task completes exceptionally or with a value of null.- Parameters:
invocationTask- the task representing a method invocation process.callback- the callback to call when the invocation finished.- Throws:
NullPointerException- if the given callback is null.
-
executeRPCChainStep
private void executeRPCChainStep(int chainDepth, int currentDepth, boolean resultExpected, @NonNull @NonNull DataBuf content, @NonNull @NonNull Packet request, @NonNull @NonNull NetworkChannel channel, @Nullable @Nullable Object previousMethodReturnValue) Executes the next RPC chain step of the current RPC chain.- Parameters:
chainDepth- the full depth of the RPC chain.currentDepth- the current depth the chain execution is at, starting at 1.resultExpected- if the RPC invocation expects a result to be sent back.content- the data content of the RPC request.request- the request packet.channel- the network channel from which the request came.previousMethodReturnValue- the chain step invocation return value.- Throws:
NullPointerException- if one of the required non-null arguments is null.
-
serializeHandlingResult
@NonNull private @NonNull DataBuf serializeHandlingResult(@Nullable @Nullable RPCInvocationResult invocationResult) Serializes the result of the RPC handling process into a data buffer.- Parameters:
invocationResult- the handling result to serialize.- Returns:
- a buffer containing the response content for the handling result.
-
sendResponseData
private void sendResponseData(@NonNull @NonNull NetworkChannel channel, @NonNull @NonNull Packet request, @NonNull @NonNull DataBuf response) Sends a serialized response to given request packet into the given network channel.- Parameters:
channel- the channel to which the response should be sent.request- the request to which a response is being sent.response- the encoded response data to send.- Throws:
NullPointerException- if the given channel, request packet or response is null.
-
postRPCRequestToHandler
@Nullable private @Nullable CompletableFuture<RPCInvocationResult> postRPCRequestToHandler(@NonNull @NonNull String targetClassName, @NonNull @NonNull RPCInvocationContext context) Posts the given RPC invocation context to the RPC handler that is registered for the class with the given name. If no handler is registered for the class, this methods returns null instead of an invocation result.- Parameters:
targetClassName- the name of class in which the method to call is located.context- the invocation context holding the provided execution info from the remote.- Returns:
- the result of the method invocation, or null if no handler for the given class is registered.
- Throws:
NullPointerException- if either the given class name or invocation context is null.
-
buildContext
@NonNull private @NonNull RPCInvocationContext buildContext(@NonNull @NonNull DataBuf content, @Nullable @Nullable Object workingInstance) Builds a new context for a rpc method invocation based on the given information and remaining content in the buffer. The given buffer should still contain (in the given order):- the target method name.
- the target method descriptor.
- the argument information for the invocation, if any.
- Parameters:
content- the remaining buffer content, containing the data as described above.workingInstance- the instance on which the methods should be called, null to use the handler binding.- Returns:
- a generated invocation context based on the given information.
- Throws:
NullPointerException- if the given content buffer is null.
-