Class RPCPacketListener

java.lang.Object
eu.cloudnetservice.driver.impl.network.rpc.listener.RPCPacketListener
All Implemented Interfaces:
eu.cloudnetservice.driver.network.protocol.PacketListener

@Singleton public final class RPCPacketListener extends Object implements eu.cloudnetservice.driver.network.protocol.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
    Modifier and Type
    Field
    Description
    private final eu.cloudnetservice.driver.network.rpc.handler.RPCHandlerRegistry
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    RPCPacketListener(@NonNull eu.cloudnetservice.driver.network.rpc.handler.RPCHandlerRegistry rpcHandlerRegistry)
    Constructs a new rpc packet listener instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    private @NonNull eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationContext
    buildContext(@NonNull eu.cloudnetservice.driver.network.buffer.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 void
    executeRPCChainStep(int chainDepth, int currentDepth, boolean resultExpected, @NonNull eu.cloudnetservice.driver.network.buffer.DataBuf content, @NonNull eu.cloudnetservice.driver.network.protocol.Packet request, @NonNull eu.cloudnetservice.driver.network.NetworkChannel channel, @Nullable Object previousMethodReturnValue)
    Executes the next RPC chain step of the current RPC chain.
    void
    handle(@NonNull eu.cloudnetservice.driver.network.NetworkChannel channel, @NonNull eu.cloudnetservice.driver.network.protocol.Packet packet)
    private @Nullable CompletableFuture<eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationResult>
    postRPCRequestToHandler(@NonNull String targetClassName, @NonNull eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationContext context)
    Posts the given RPC invocation context to the RPC handler that is registered for the class with the given name.
    private void
    sendResponseData(@NonNull eu.cloudnetservice.driver.network.NetworkChannel channel, @NonNull eu.cloudnetservice.driver.network.protocol.Packet request, @NonNull eu.cloudnetservice.driver.network.buffer.DataBuf response)
    Sends a serialized response to given request packet into the given network channel.
    private @NonNull eu.cloudnetservice.driver.network.buffer.DataBuf
    serializeHandlingResult(@Nullable eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationResult invocationResult)
    Serializes the result of the RPC handling process into a data buffer.
    private void
    waitForInvocationCompletion(@Nullable CompletableFuture<eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationResult> invocationTask, @NonNull Consumer<eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationResult> callback)
    Waits for the given invocation task to finish, calling the callback if that is the case.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • rpcHandlerRegistry

      private final eu.cloudnetservice.driver.network.rpc.handler.RPCHandlerRegistry rpcHandlerRegistry
  • Constructor Details

    • RPCPacketListener

      @Inject public RPCPacketListener(@NonNull @NonNull eu.cloudnetservice.driver.network.rpc.handler.RPCHandlerRegistry rpcHandlerRegistry)
      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 eu.cloudnetservice.driver.network.NetworkChannel channel, @NonNull @NonNull eu.cloudnetservice.driver.network.protocol.Packet packet) throws Exception
      Specified by:
      handle in interface eu.cloudnetservice.driver.network.protocol.PacketListener
      Throws:
      Exception
    • waitForInvocationCompletion

      private void waitForInvocationCompletion(@Nullable @Nullable CompletableFuture<eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationResult> invocationTask, @NonNull @NonNull Consumer<eu.cloudnetservice.driver.network.rpc.handler.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 eu.cloudnetservice.driver.network.buffer.DataBuf content, @NonNull @NonNull eu.cloudnetservice.driver.network.protocol.Packet request, @NonNull @NonNull eu.cloudnetservice.driver.network.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 eu.cloudnetservice.driver.network.buffer.DataBuf serializeHandlingResult(@Nullable @Nullable eu.cloudnetservice.driver.network.rpc.handler.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 eu.cloudnetservice.driver.network.NetworkChannel channel, @NonNull @NonNull eu.cloudnetservice.driver.network.protocol.Packet request, @NonNull @NonNull eu.cloudnetservice.driver.network.buffer.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<eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationResult> postRPCRequestToHandler(@NonNull @NonNull String targetClassName, @NonNull @NonNull eu.cloudnetservice.driver.network.rpc.handler.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 eu.cloudnetservice.driver.network.rpc.handler.RPCInvocationContext buildContext(@NonNull @NonNull eu.cloudnetservice.driver.network.buffer.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):
      1. the target method name.
      2. the target method descriptor.
      3. 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.