Class AbstractRemoteClient<M extends com.google.protobuf.Message>

java.lang.Object
org.openbase.jul.communication.controller.AbstractRemoteClient<M>
Type Parameters:
M -
All Implemented Interfaces:
RPCRemote<M>, org.openbase.jul.extension.type.iface.TransactionIdProvider, org.openbase.jul.iface.Activatable, org.openbase.jul.iface.Lockable, org.openbase.jul.iface.provider.PingProvider, org.openbase.jul.iface.Shutdownable, org.openbase.jul.pattern.controller.Remote<M>, org.openbase.jul.pattern.provider.DataProvider<M>
Direct Known Subclasses:
AbstractIdentifiableRemote

public abstract class AbstractRemoteClient<M extends com.google.protobuf.Message> extends Object implements RPCRemote<M>, org.openbase.jul.extension.type.iface.TransactionIdProvider
  • Field Details

    • REQUEST_TIMEOUT

      public static final long REQUEST_TIMEOUT
    • PING_TIMEOUT

      public static final long PING_TIMEOUT
    • PING_TEST_TIMEOUT

      public static final long PING_TEST_TIMEOUT
    • CONNECTION_TIMEOUT

      public static final long CONNECTION_TIMEOUT
    • RECONNECT_AFTER_CONNECTION_LOST_DELAY_OFFSET

      public static final long RECONNECT_AFTER_CONNECTION_LOST_DELAY_OFFSET
      See Also:
    • RECONNECT_AFTER_CONNECTION_LOST_DELAY_SEED

      public static final long RECONNECT_AFTER_CONNECTION_LOST_DELAY_SEED
      See Also:
    • METHOD_CALL_START_TIMEOUT

      public static final long METHOD_CALL_START_TIMEOUT
      See Also:
    • METHOD_CALL_TIMEOUT_MULTIPLIER

      public static final double METHOD_CALL_TIMEOUT_MULTIPLIER
      See Also:
    • METHOD_CALL_MAX_TIMEOUT

      public static final long METHOD_CALL_MAX_TIMEOUT
    • logger

      protected final org.slf4j.Logger logger
    • maintainer

      protected Object maintainer
    • scope

      protected org.openbase.type.communication.ScopeType.Scope scope
  • Constructor Details

    • AbstractRemoteClient

      public AbstractRemoteClient(Class<M> dataClass)
  • Method Details

    • setMessageProcessor

      protected void setMessageProcessor(org.openbase.jul.extension.protobuf.processing.MessageProcessor<com.google.protobuf.Message,M> messageProcessor)
    • init

      public void init(org.openbase.type.communication.ScopeType.Scope scope) throws org.openbase.jul.exception.InitializationException, InterruptedException
      Initialize the remote on the given scope.
      Specified by:
      init in interface RPCRemote<M extends com.google.protobuf.Message>
      Parameters:
      scope - the scope where the remote communicates
      Throws:
      org.openbase.jul.exception.InitializationException - if the initialization fails
      InterruptedException - if the initialization is interrupted
    • init

      public void init(String scope) throws org.openbase.jul.exception.InitializationException, InterruptedException
      Initialize the remote on a scope.
      Specified by:
      init in interface RPCRemote<M extends com.google.protobuf.Message>
      Parameters:
      scope - the scope where the remote communicates
      Throws:
      org.openbase.jul.exception.InitializationException - if the initialization fails
      InterruptedException - if the initialization is interrupted
    • postInit

      protected void postInit() throws org.openbase.jul.exception.InitializationException, InterruptedException
      Method is called after communication initialization. You can overwrite this method to trigger any component specific initialization.
      Throws:
      org.openbase.jul.exception.InitializationException
      InterruptedException
    • init

      public void init(org.openbase.type.communication.ScopeType.Scope scope, org.openbase.jul.communication.config.CommunicatorConfig communicatorConfig) throws org.openbase.jul.exception.InitializationException, InterruptedException
      Initialize the remote on the given scope.
      Specified by:
      init in interface RPCRemote<M extends com.google.protobuf.Message>
      Parameters:
      scope - the scope where the remote communicates
      communicatorConfig - the rsb communicator config which should be used for the connection.
      Throws:
      org.openbase.jul.exception.InitializationException - if the initialization fails
      InterruptedException - if the initialization is interrupted
    • verifyMaintainability

      public void verifyMaintainability() throws org.openbase.jul.exception.VerificationFailedException
      Specified by:
      verifyMaintainability in interface org.openbase.jul.iface.Lockable
    • isLocked

      public boolean isLocked()
      Specified by:
      isLocked in interface org.openbase.jul.iface.Lockable
      Returns:
    • lock

      public void lock(Object maintainer) throws org.openbase.jul.exception.CouldNotPerformException
      Specified by:
      lock in interface org.openbase.jul.iface.Lockable
    • unlock

      public void unlock(Object maintainer) throws org.openbase.jul.exception.CouldNotPerformException
      Method unlocks this instance.
      Specified by:
      unlock in interface org.openbase.jul.iface.Lockable
      Parameters:
      maintainer - the instance which currently holds the lock.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException - is thrown if the instance could not be unlocked.
    • getDataClass

      public Class<M> getDataClass()
      Specified by:
      getDataClass in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
      Specified by:
      getDataClass in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
      Returns:
    • addHandler

      public void addHandler(kotlin.jvm.functions.Function2<org.openbase.type.communication.EventType.Event,Map<String,String>,kotlin.Unit> handler, boolean wait) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Method adds an handler to the internal subscriber.
      Parameters:
      handler -
      wait -
      Throws:
      InterruptedException
      org.openbase.jul.exception.CouldNotPerformException
    • generateHandler

      protected kotlin.jvm.functions.Function2<org.openbase.type.communication.EventType.Event,Map<String,String>,kotlin.Unit> generateHandler()
    • activate

      public void activate() throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Specified by:
      activate in interface org.openbase.jul.iface.Activatable
    • activate

      public void activate(boolean waitForData) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Specified by:
      activate in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
    • activate

      public void activate(Object maintainer) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Specified by:
      activate in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
    • deactivate

      public void deactivate(Object maintainer) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException, org.openbase.jul.exception.VerificationFailedException
      Atomic deactivate which makes sure that the maintainer stays the same.
      Parameters:
      maintainer - the current maintainer of this remote
      Throws:
      InterruptedException - if deactivation is interrupted
      org.openbase.jul.exception.CouldNotPerformException - if deactivation fails
      org.openbase.jul.exception.VerificationFailedException - is thrown if the given maintainer does not match the current one
    • deactivate

      public void deactivate() throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Specified by:
      deactivate in interface org.openbase.jul.iface.Activatable
    • reset

      public void reset() throws org.openbase.jul.exception.CouldNotPerformException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • reinit

      protected void reinit() throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Method reinitialize this remote. If the remote was previously active the activation state will be recovered. This method can be used in case of a broken connection or if the communicator config has been changed.

      Note: After reinit the data remains the same but a new sync task is created. So to make sure to have new data it is necessary to call requestData.get().

      Throws:
      InterruptedException - is thrown if the current thread was externally interrupted.
      org.openbase.jul.exception.CouldNotPerformException - is throws if the reinit has been failed.
    • reinit

      protected void reinit(org.openbase.type.communication.ScopeType.Scope scope) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Method reinitialize this remote. If the remote was previously active the activation state will be recovered. This method can be used in case of a broken connection or if the communicator config has been changed.

      Note: After reinit the data remains the same but a new sync task is created. So to make sure to have new data it is necessary to call requestData.get().

      Parameters:
      scope - the new scope to configure.
      Throws:
      InterruptedException - is thrown if the current thread was externally interrupted.
      org.openbase.jul.exception.CouldNotPerformException - is throws if the reinit has been failed.
    • reinit

      public void reinit(Object maintainer) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException, org.openbase.jul.exception.VerificationFailedException
      Method reinitialize this remote. If the remote was previously active the activation state will be recovered. This method can be used in case of a broken connection or if the communicator config has been changed.

      Note: After reinit the data remains the same but a new sync task is created. So to make sure to have new data it is necessary to call requestData.get().

      Parameters:
      maintainer - the current maintainer of this remote
      Throws:
      InterruptedException - is thrown if the current thread was externally interrupted.
      org.openbase.jul.exception.CouldNotPerformException - is throws if the reinit has been failed.
      org.openbase.jul.exception.VerificationFailedException - is thrown if the given maintainerLock does not match the current maintainer
    • reinit

      public void reinit(org.openbase.type.communication.ScopeType.Scope scope, Object maintainer) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException, org.openbase.jul.exception.VerificationFailedException
      Method reinitialize this remote. If the remote was previously active the activation state will be recovered. This method can be used in case of a broken connection or if the communicator config has been changed.

      Note: After reinit the data remains the same but a new sync task is created. So to make sure to have new data it is necessary to call requestData.get().

      Parameters:
      scope - the new scope to configure.
      maintainer - the current maintainer of this remote
      Throws:
      InterruptedException - is thrown if the current thread was externally interrupted.
      org.openbase.jul.exception.CouldNotPerformException - is throws if the reinit has been failed.
      org.openbase.jul.exception.VerificationFailedException - is thrown if the given maintainerLock does not match the current maintainer
    • isConnected

      public boolean isConnected()
      Specified by:
      isConnected in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
    • getConnectionState

      public org.openbase.type.domotic.state.ConnectionStateType.ConnectionState.State getConnectionState()
      Specified by:
      getConnectionState in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
    • isActive

      public boolean isActive()
      Specified by:
      isActive in interface org.openbase.jul.iface.Activatable
    • callMethodAsync

      public <R> Future<org.openbase.jul.communication.data.RPCResponse<R>> callMethodAsync(String methodName, Class<R> returnClazz)
      Method asynchronously calls the given method without any arguments on the main controller.
      Specified by:
      callMethodAsync in interface RPCRemote<M extends com.google.protobuf.Message>
      Type Parameters:
      R - the return type of the method declaration.
      Parameters:
      methodName - the method name.
      Returns:
      a future instance which gives feedback about the asynchronously method call and when the result is available.
    • callMethod

      public <R> R callMethod(String methodName, Class<R> returnClazz) throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Method synchronously calls the given method without any arguments on the main controller.

      The method call will block until the call is successfully processed. Even if the main controller instance is currently not reachable, successively retry will be triggered. The only way to cancel the call is an externally interruption of the invoking thread.

      Specified by:
      callMethod in interface RPCRemote<M extends com.google.protobuf.Message>
      Type Parameters:
      R - the return type of the method declaration.
      Parameters:
      methodName - the method name.
      Returns:
      the return value of the remote method.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException - is thrown in case any error occurred during processing.
      InterruptedException - is thrown in case the thread was externally interrupted.
    • callMethod

      public <R, T> R callMethod(String methodName, Class<R> returnClazz, T argument) throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Method synchronously calls the given method on the main controller.

      The method call will block until the call is successfully processed. Even if the main controller instance is currently not reachable, successively retry will be triggered. The only way to cancel the call is an externally interruption of the invoking thread.

      Specified by:
      callMethod in interface RPCRemote<M extends com.google.protobuf.Message>
      Type Parameters:
      R - the return type of the method declaration.
      T - the argument type of the method.
      Parameters:
      methodName - the method name.
      argument - the method argument.
      Returns:
      the return value of the remote method.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException - is thrown in case any error occurred during processing.
      InterruptedException - is thrown in case the thread was externally interrupted.
    • callMethod

      public <R> R callMethod(String methodName, Class<R> returnClazz, long timeout) throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Method synchronously calls the given method on the main controller.

      The method call will block until the call is successfully processed or the given timeout is expired. Even if the main controller instance is currently not reachable, successively retry will be triggered.

      Specified by:
      callMethod in interface RPCRemote<M extends com.google.protobuf.Message>
      Type Parameters:
      R - the return type of the method declaration.
      Parameters:
      methodName - the method name.
      timeout - the RPC call timeout in milliseconds.
      Returns:
      the return value of the remote method.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException - is thrown in case any error occurred during processing.
      InterruptedException - is thrown in case the thread was externally interrupted.
    • callMethod

      public <R, T> R callMethod(String methodName, Class<R> returnClazz, T argument, long timeout) throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Method synchronously calls the given method on the main controller.

      The method call will block until the call is successfully processed or the given timeout is expired. Even if the main controller instance is currently not reachable, successively retry will be triggered.

      Specified by:
      callMethod in interface RPCRemote<M extends com.google.protobuf.Message>
      Type Parameters:
      R - the return type of the method declaration.
      T - the argument type of the method.
      Parameters:
      methodName - the method name.
      argument - the method argument.
      timeout - the RPC call timeout in milliseconds.
      Returns:
      the return value of the remote method.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException - is thrown in case any error occurred during processing.
      InterruptedException - is thrown in case the thread was externally interrupted.
    • callMethodAsync

      public <R, T> Future<org.openbase.jul.communication.data.RPCResponse<R>> callMethodAsync(String methodName, Class<R> returnClazz, T argument)
      Method asynchronously calls the given method on the main controller.
      Specified by:
      callMethodAsync in interface RPCRemote<M extends com.google.protobuf.Message>
      Type Parameters:
      R - the return type of the method declaration.
      T - the argument type of the method.
      Parameters:
      methodName - the method name.
      argument - the method argument.
      Returns:
      a future instance which gives feedback about the asynchronously method call and when the result is available.
    • requestData

      public Future<M> requestData()
      Specified by:
      requestData in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
      Returns:
    • getRpcClient

      protected org.openbase.jul.communication.iface.RPCClient getRpcClient()
    • internalRequestStatus

      protected Future<org.openbase.jul.communication.data.RPCResponse<M>> internalRequestStatus()
    • applyEventUpdate

      protected M applyEventUpdate(org.openbase.type.communication.EventType.Event event, Map<String,String> userProperties) throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
      InterruptedException
    • validateAndUpdateEventTimestamp

      public boolean validateAndUpdateEventTimestamp(Map<String,String> userProperties) throws org.openbase.jul.exception.CouldNotPerformException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • shutdown

      public void shutdown()
      This method deactivates the remote and cleans all resources.
      Specified by:
      shutdown in interface org.openbase.jul.iface.Shutdownable
    • getData

      public M getData() throws org.openbase.jul.exception.NotAvailableException
      Specified by:
      getData in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
      Specified by:
      getData in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
      Returns:
    • setData

      protected void setData(M data)
    • isDataAvailable

      public boolean isDataAvailable()
      Specified by:
      isDataAvailable in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
      Returns:
    • waitForData

      public void waitForData() throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Specified by:
      waitForData in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
      Specified by:
      waitForData in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
    • waitForData

      public void waitForData(long timeout, TimeUnit timeUnit) throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Specified by:
      waitForData in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
      Specified by:
      waitForData in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
      Parameters:
      timeout -
      timeUnit -
    • getDataField

      protected final Object getDataField(String name) throws org.openbase.jul.exception.CouldNotPerformException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • hasDataField

      protected final boolean hasDataField(String name) throws org.openbase.jul.exception.CouldNotPerformException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • validateInitialization

      public void validateInitialization() throws org.openbase.jul.exception.InvalidStateException
      Throws:
      org.openbase.jul.exception.InvalidStateException
    • validateActivation

      public void validateActivation() throws org.openbase.jul.exception.InvalidStateException
      Throws:
      org.openbase.jul.exception.InvalidStateException
    • validateMiddleware

      public void validateMiddleware() throws org.openbase.jul.exception.InvalidStateException
      Throws:
      org.openbase.jul.exception.InvalidStateException
    • validateData

      public void validateData() throws org.openbase.jul.exception.InvalidStateException
      Specified by:
      validateData in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
      Throws:
      org.openbase.jul.exception.InvalidStateException
    • waitForMiddleware

      public void waitForMiddleware() throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
      InterruptedException
    • waitForMiddleware

      public void waitForMiddleware(long timeout, TimeUnit timeUnit) throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
      InterruptedException
    • waitForConnectionState

      public void waitForConnectionState(org.openbase.type.domotic.state.ConnectionStateType.ConnectionState.State connectionState, long timeout) throws InterruptedException, org.openbase.jul.exception.TimeoutException, org.openbase.jul.exception.CouldNotPerformException
      Method blocks until the remote reaches the desired connection state. In case the timeout is expired an TimeoutException will be thrown.
      Specified by:
      waitForConnectionState in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
      Parameters:
      connectionState - the desired connection state
      timeout - the timeout in milliseconds until the method throw a TimeoutException in case the connection state was not reached.
      Throws:
      InterruptedException - is thrown in case the thread is externally interrupted.
      org.openbase.jul.exception.TimeoutException - is thrown in case the timeout is expired without reaching the connection state.
      org.openbase.jul.exception.CouldNotPerformException - is thrown in case the connection state does not match and the shutdown of this remote has been initialized
    • waitForConnectionState

      public void waitForConnectionState(org.openbase.type.domotic.state.ConnectionStateType.ConnectionState.State connectionState) throws InterruptedException, org.openbase.jul.exception.CouldNotPerformException
      Method blocks until the remote reaches the desired connection state.
      Parameters:
      connectionState - the desired connection state
      Throws:
      InterruptedException - is thrown in case the thread is externally interrupted.
      org.openbase.jul.exception.CouldNotPerformException - is thrown in case the the remote is not active and the waiting condition is based on ConnectionState.State CONNECTED or CONNECTING.
    • getScope

      public org.openbase.type.communication.ScopeType.Scope getScope() throws org.openbase.jul.exception.NotAvailableException
      Method returns the scope of this remote connection.
      Specified by:
      getScope in interface RPCRemote<M extends com.google.protobuf.Message>
      Returns:
      the remote controller scope.
      Throws:
      org.openbase.jul.exception.NotAvailableException
    • getInternalPrioritizedDataObservable

      protected org.openbase.jul.pattern.Observable<org.openbase.jul.pattern.provider.DataProvider<M>,M> getInternalPrioritizedDataObservable()
      This observable notifies sequentially and prior to the normal dataObserver. It should be used by remote services which need to do some things before external objects are notified.
      Returns:
      the internal prioritized data observable.
    • addDataObserver

      public void addDataObserver(org.openbase.jul.pattern.Observer<org.openbase.jul.pattern.provider.DataProvider<M>,M> observer)
      Specified by:
      addDataObserver in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
    • removeDataObserver

      public void removeDataObserver(org.openbase.jul.pattern.Observer<org.openbase.jul.pattern.provider.DataProvider<M>,M> observer)
      Specified by:
      removeDataObserver in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.Message>
    • addConnectionStateObserver

      public void addConnectionStateObserver(org.openbase.jul.pattern.Observer<org.openbase.jul.pattern.controller.Remote<?>,org.openbase.type.domotic.state.ConnectionStateType.ConnectionState.State> observer)
      Specified by:
      addConnectionStateObserver in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
    • removeConnectionStateObserver

      public void removeConnectionStateObserver(org.openbase.jul.pattern.Observer<org.openbase.jul.pattern.controller.Remote<?>,org.openbase.type.domotic.state.ConnectionStateType.ConnectionState.State> observer)
      Specified by:
      removeConnectionStateObserver in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
    • ping

      public Future<Long> ping()
      Specified by:
      ping in interface org.openbase.jul.iface.provider.PingProvider
      Specified by:
      ping in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
      Returns:
    • getPing

      public Long getPing()
      Method returns the result of the latest connection ping between this remote and its main controller.
      Specified by:
      getPing in interface org.openbase.jul.iface.provider.PingProvider
      Specified by:
      getPing in interface org.openbase.jul.pattern.controller.Remote<M extends com.google.protobuf.Message>
      Returns:
      the latest connection delay in milliseconds.
    • toString

      public String toString()
      Method prints a class instance representation.
      Overrides:
      toString in class Object
      Returns:
      the class string representation.
    • getTransactionId

      public long getTransactionId() throws org.openbase.jul.exception.NotAvailableException
      Get the latest transaction id. It will be updated every time after prioritized observers are notified.
      Specified by:
      getTransactionId in interface org.openbase.jul.extension.type.iface.TransactionIdProvider
      Returns:
      the latest transaction id.
      Throws:
      org.openbase.jul.exception.NotAvailableException - if no data has been received yet
    • isSyncRunning

      public boolean isSyncRunning()
    • restartSyncTask

      protected void restartSyncTask() throws org.openbase.jul.exception.CouldNotPerformException
      Throws:
      org.openbase.jul.exception.CouldNotPerformException