Class DefaultRPCFactory

java.lang.Object
eu.cloudnetservice.driver.network.rpc.defaults.DefaultRPCFactory
All Implemented Interfaces:
RPCFactory

@Singleton @Provides(RPCFactory.class) public class DefaultRPCFactory extends Object implements RPCFactory
The default factory implementation for everything related to rpc.
Since:
4.0
  • Field Details

  • Constructor Details

    • DefaultRPCFactory

      @Inject public DefaultRPCFactory(@NonNull @NonNull ObjectMapper defaultObjectMapper, @NonNull @NonNull DataBufFactory defaultDataBufFactory)
      Constructs a new default rpc provider factory instance.
      Parameters:
      defaultObjectMapper - the default object mapper to use if no object mapper is provided in factory calls.
      defaultDataBufFactory - the default data buf factory to use if no object mapper is provided in factory calls.
      Throws:
      NullPointerException - if either the given object mapper or data buf factory is null.
  • Method Details

    • defaultObjectMapper

      @NonNull public @NonNull ObjectMapper defaultObjectMapper()
      Get the default object mapper used by this factory if no other mapper is supplied to a factory method.
      Specified by:
      defaultObjectMapper in interface RPCFactory
      Returns:
      the default object mapper of this factory.
    • defaultDataBufFactory

      @NonNull public @NonNull DataBufFactory defaultDataBufFactory()
      Get the default data buf factory used by this factory if no other data buf factory is supplied to a factory method.
      Specified by:
      defaultDataBufFactory in interface RPCFactory
      Returns:
      the default data buf factory of this factory.
    • providerForClass

      @NonNull public @NonNull RPCSender providerForClass(@Nullable @Nullable NetworkComponent component, @NonNull @NonNull Class<?> clazz)
      Constructs a new rpc sender for the given class.
      Specified by:
      providerForClass in interface RPCFactory
      Parameters:
      component - the associated network component, or null if not associated.
      clazz - the class which the handler should handler.
      Returns:
      a new rpc sender targeting the given class.
    • providerForClass

      @NonNull public @NonNull RPCSender providerForClass(@Nullable @Nullable NetworkComponent component, @NonNull @NonNull Class<?> clazz, @NonNull @NonNull ObjectMapper objectMapper, @NonNull @NonNull DataBufFactory dataBufFactory)
      Constructs a new rpc sender for the given class.
      Specified by:
      providerForClass in interface RPCFactory
      Parameters:
      component - the associated network component, or null if not associated.
      clazz - the class which the handler should handler.
      objectMapper - the object mapper to use for argument (de-) serialization.
      dataBufFactory - the data buf factory to use for buffer allocation.
      Returns:
      a new rpc sender targeting the given class.
    • generateRPCBasedApi

      @NonNull public <T> @NonNull InstanceFactory<T> generateRPCBasedApi(@NonNull @NonNull Class<T> baseClass, @NonNull @NonNull GenerationContext context)
      Generates an api implementation for the given base class, invoking all of its method using rpc. This method only overrides methods which are abstract in the given class tree. In other words, if you're passing an implementation which has all methods which need to be processed locally already done, no rpc based method implementation will be generated for the class.

      The defined base class in the context must define one of the following constructors:

      1. A constructor with no arguments, or
      2. A constructor taking exactly one argument, a rpc sender instance.
      Note: the constructor taking the rpc sender instance is preferred by the generator over the no args constructor.

      This method will cache the result of the generation, calling this method twice will only result in two different instances in the same class, but only if the given generation context did not change between the calls.

      Specified by:
      generateRPCBasedApi in interface RPCFactory
      Type Parameters:
      T - the type which gets generated.
      Parameters:
      baseClass - the base class to generate the class methods based on.
      context - the context of the class generation, holding the options for it.
      Returns:
      an implementation of the given class which has all abstract methods rpc based implemented.
    • generateRPCChainBasedApi

      @NonNull public <T> @NonNull ChainInstanceFactory<T> generateRPCChainBasedApi(@NonNull @NonNull RPCSender baseSender, @NonNull @NonNull Class<T> chainBaseClass, @NonNull @NonNull GenerationContext context)
      Generates an api implementation for the given base class, invoking all of its method using a chained rpc call. The base rpc is discovered using the given rpc sender and the name of the method which called the method. The invoker of the method must be direct (1 up in the local stack trace). If the method name cannot be determined by the stack, use RPCFactory.generateRPCChainBasedApi(RPCSender, String, Class, GenerationContext) to supply the name of the method to use instead. This method only overrides methods which are abstract in the given class tree. In other words, if you're passing an implementation which has all methods which need to be processed locally already done, no rpc based method implementation will be generated for the class.

      If the base class defined in the invocation context needs arguments to be supplied, they must get passed to the created instance factory. Note: the constructor parameters are not required to match the parameters supplied to the base rpc call.

      This method will cache the result of the generation, calling this method twice will only result in two different instances in the same class, but only if the given generation context and the calling method name did not change between the calls.

      Specified by:
      generateRPCChainBasedApi in interface RPCFactory
      Type Parameters:
      T - the type which gets generated.
      Parameters:
      baseSender - the rpc sender for the base class from which the chain should start.
      chainBaseClass - the base class from which methods one step into the chain should get called.
      context - the context of the class generation, holding the options for it.
      Returns:
      a factory which is capable to create new instances of the given chain base class, for rpc chain calls.
    • generateRPCChainBasedApi

      @NonNull public <T> @NonNull ChainInstanceFactory<T> generateRPCChainBasedApi(@NonNull @NonNull RPCSender baseSender, @NonNull @NonNull String baseCallerMethod, @NonNull @NonNull Class<T> chainBaseClass, @NonNull @NonNull GenerationContext context)
      Generates an api implementation for the given base class, invoking all of its method using a chained rpc call. The base rpc is discovered using the given rpc sender and the given method name. This method only overrides methods which are abstract in the given class tree. In other words, if you're passing an implementation which has all methods which need to be processed locally already done, no rpc based method implementation will be generated for the class.

      If the base class defined in the invocation context needs arguments to be supplied, they must get passed to the created instance factory. Note: the constructor parameters are not required to match the parameters supplied to the base rpc call.

      This method will cache the result of the generation, calling this method twice will only result in two different instances in the same class, but only if the given generation context and the calling method name did not change between the calls.

      Specified by:
      generateRPCChainBasedApi in interface RPCFactory
      Type Parameters:
      T - the type which gets generated.
      Parameters:
      baseSender - the rpc sender for the base class from which the chain should start.
      baseCallerMethod - the name of the method to use to obtain the base instance to call the chain on.
      chainBaseClass - the base class from which methods one step into the chain should get called.
      context - the context of the class generation, holding the options for it.
      Returns:
      a factory which is capable to create new instances of the given chain base class, for rpc chain calls.
    • newHandler

      @NonNull public @NonNull RPCHandler newHandler(@NonNull @NonNull Class<?> clazz, @Nullable @Nullable Object binding)
      Constructs a new rpc handler for the given class.
      Specified by:
      newHandler in interface RPCFactory
      Parameters:
      clazz - the class which the handler handles.
      binding - an optional instance binding for the handler, can be null if relying on contextual instances.
      Returns:
      a new rpc handler for the given class.
    • newHandler

      @NonNull public @NonNull RPCHandler newHandler(@NonNull @NonNull Class<?> clazz, @Nullable @Nullable Object binding, @NonNull @NonNull ObjectMapper objectMapper, @NonNull @NonNull DataBufFactory dataBufFactory)
      Constructs a new rpc handler for the given class.
      Specified by:
      newHandler in interface RPCFactory
      Parameters:
      clazz - the class which the handler handles.
      binding - an optional instance binding for the handler, null if relying on contextual instances.
      objectMapper - the object mapper to use for argument (de-) serialization.
      dataBufFactory - the data buf factory to use for buffer allocation.
      Returns:
      a new rpc handler for the given class.
    • senderFromGenerationContext

      @NonNull private @NonNull RPCSender senderFromGenerationContext(@NonNull @NonNull GenerationContext context, @NonNull @NonNull Class<?> base)
      Constructs a new rpc sender for the given base class and using the options supplied by the given generation context.
      Parameters:
      context - the context to take out the needed options from.
      base - the base class in which the new sender should call the methods.
      Returns:
      a new rpc sender instance.
      Throws:
      NullPointerException - if the given context or base class is null.