Class AbstractControllerServer<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>>

java.lang.Object
org.openbase.jul.communication.controller.AbstractControllerServer<M,MB>
Type Parameters:
M - the message type of the communication service
MB - the builder for message M
All Implemented Interfaces:
org.openbase.jul.extension.protobuf.iface.DataBuilderProvider<M,MB>, org.openbase.jul.extension.type.iface.ScopeProvider, org.openbase.jul.extension.type.iface.TransactionIdProvider, org.openbase.jul.iface.Activatable, org.openbase.jul.iface.Pingable, org.openbase.jul.iface.Readyable, org.openbase.jul.iface.Requestable<M>, org.openbase.jul.iface.Shutdownable, org.openbase.jul.pattern.ChangeListener, org.openbase.jul.pattern.controller.Controller<M>, org.openbase.jul.pattern.controller.MessageController<M,MB>, org.openbase.jul.pattern.provider.DataProvider<M>
Direct Known Subclasses:
AbstractIdentifiableController

public abstract class AbstractControllerServer<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>> extends Object implements org.openbase.jul.pattern.controller.MessageController<M,MB>, org.openbase.jul.extension.type.iface.ScopeProvider, org.openbase.jul.pattern.provider.DataProvider<M>, org.openbase.jul.iface.Readyable, org.openbase.jul.extension.type.iface.TransactionIdProvider
  • Field Details

    • SCOPE_ELEMENT_SUFFIX_CONTROL

      public static final String SCOPE_ELEMENT_SUFFIX_CONTROL
      See Also:
    • SCOPE_ELEMENT_SUFFIX_STATUS

      public static final String SCOPE_ELEMENT_SUFFIX_STATUS
      See Also:
    • SCOPE_SUFFIX_CONTROL

      public static final org.openbase.type.communication.ScopeType.Scope SCOPE_SUFFIX_CONTROL
    • SCOPE_SUFFIX_STATUS

      public static final org.openbase.type.communication.ScopeType.Scope SCOPE_SUFFIX_STATUS
    • RPC_REQUEST_STATUS

      public static final String RPC_REQUEST_STATUS
      See Also:
    • logger

      protected final kr.pe.kwonnam.slf4jlambda.LambdaLogger logger
    • publisher

      protected org.openbase.jul.communication.iface.Publisher publisher
    • server

      protected org.openbase.jul.communication.iface.RPCServer server
    • publisherWatchDog

      protected org.openbase.jul.schedule.WatchDog publisherWatchDog
    • serverWatchDog

      protected org.openbase.jul.schedule.WatchDog serverWatchDog
    • dataLock

      protected final ReentrantReadWriteLock dataLock
    • scope

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

    • AbstractControllerServer

      public AbstractControllerServer(MB builder) throws org.openbase.jul.exception.InstantiationException
      Create a communication service.
      Parameters:
      builder - the initial data builder
      Throws:
      org.openbase.jul.exception.InstantiationException - if the creation fails
  • Method Details

    • getShutdownDelay

      protected long getShutdownDelay()
      Method can be overwritten to delay the controller shutdown.
      Returns:
      the delay in milliseconds.
    • init

      public void init(org.openbase.type.communication.ScopeType.Scope scope) throws org.openbase.jul.exception.InitializationException, InterruptedException
      Parameters:
      scope -
      Throws:
      org.openbase.jul.exception.InitializationException
      InterruptedException
    • init

      public void init(String scope) throws org.openbase.jul.exception.InitializationException, InterruptedException
      Parameters:
      scope -
      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
      Parameters:
      scope -
      communicatorConfig -
      Throws:
      org.openbase.jul.exception.InitializationException
      InterruptedException
    • 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
    • getDataClass

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

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

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

      public void shutdown()
      Specified by:
      shutdown in interface org.openbase.jul.iface.Shutdownable
    • isActive

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

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

      public Future<M> getDataFuture()
      Specified by:
      getDataFuture in interface org.openbase.jul.pattern.provider.DataProvider<M extends com.google.protobuf.AbstractMessage>
      Returns:
    • waitForAvailabilityState

      public void waitForAvailabilityState(org.openbase.type.domotic.state.AvailabilityStateType.AvailabilityState.State availabilityState) throws InterruptedException
      Specified by:
      waitForAvailabilityState in interface org.openbase.jul.pattern.controller.Controller<M extends com.google.protobuf.AbstractMessage>
    • cloneDataBuilder

      public MB cloneDataBuilder()
      Specified by:
      cloneDataBuilder in interface org.openbase.jul.extension.protobuf.iface.DataBuilderProvider<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>>
      Returns:
    • getBuilderSetup

      public org.openbase.jul.extension.protobuf.BuilderSyncSetup<MB> getBuilderSetup()
      Specified by:
      getBuilderSetup in interface org.openbase.jul.extension.protobuf.iface.DataBuilderProvider<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>>
      Returns:
    • getDataBuilder

      @Deprecated public org.openbase.jul.extension.protobuf.ClosableDataBuilder<MB> getDataBuilder(Object consumer)
      Deprecated.
      Specified by:
      getDataBuilder in interface org.openbase.jul.extension.protobuf.iface.DataBuilderProvider<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>>
      Parameters:
      consumer -
      Returns:
    • getDataBuilder

      @Deprecated public org.openbase.jul.extension.protobuf.ClosableDataBuilder<MB> getDataBuilder(Object consumer, org.openbase.jul.extension.protobuf.BuilderSyncSetup.NotificationStrategy notificationStrategy)
      Deprecated.
      Specified by:
      getDataBuilder in interface org.openbase.jul.extension.protobuf.iface.DataBuilderProvider<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>>
      Parameters:
      consumer -
      notificationStrategy -
      Returns:
    • getDataBuilderInterruptible

      public org.openbase.jul.extension.protobuf.ClosableDataBuilder<MB> getDataBuilderInterruptible(Object consumer) throws InterruptedException
      Specified by:
      getDataBuilderInterruptible in interface org.openbase.jul.extension.protobuf.iface.DataBuilderProvider<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>>
      Parameters:
      consumer -
      Returns:
      Throws:
      InterruptedException
    • getDataBuilderInterruptible

      public org.openbase.jul.extension.protobuf.ClosableDataBuilder<MB> getDataBuilderInterruptible(Object consumer, org.openbase.jul.extension.protobuf.BuilderSyncSetup.NotificationStrategy notificationStrategy) throws InterruptedException
      Specified by:
      getDataBuilderInterruptible in interface org.openbase.jul.extension.protobuf.iface.DataBuilderProvider<M extends com.google.protobuf.AbstractMessage,MB extends com.google.protobuf.AbstractMessage.Builder<MB>>
      Parameters:
      consumer -
      notificationStrategy -
      Returns:
      Throws:
      InterruptedException
    • getManageWriteLock

      protected org.openbase.jul.schedule.CloseableWriteLockWrapper getManageWriteLock(Object consumer)
      This method generates a closable manager lock wrapper. Be informed that the controller and all its services are directly locked and internal builder operations are queued. Therefore please call the close method soon as possible to release the lock after you are done, otherwise the overall processing pipeline is delayed.

      Note: Be aware that your access is time limited an the lock will auto released if locked in longer term. This is a recovering feature but should never be used by design!

       Usage Example:
      
           try(final CloseableWriteLockWrapper ignored = getManageWriteLock(this)) {
               // do important stuff...
           }
       
       
      In this example the CloseableWriteLockWrapper.close method is be called in background after leaving the try brackets.
      Parameters:
      consumer - a responsible instance which consumes the lock.
      Returns:
      a new builder wrapper which already locks the manage lock.
    • getManageReadLock

      protected org.openbase.jul.schedule.CloseableReadLockWrapper getManageReadLock(Object consumer)
      This method generates a closable manager lock wrapper. Be informed that the controller and all its services are directly locked and internal builder operations are queued. Therefore please call the close method soon as possible to release the lock after you are done, otherwise the overall processing pipeline is delayed.

      Note: Be aware that your access is time limited an the lock will auto released if locked in longer term. This is a recovering feature but should never be used by design!

       Usage Example:
      
           try(final CloseableReadLockWrapper ignored = getManageReadLock(this)) {
               // do important stuff...
           }
       
       
      In this example the CloseableWriteLockWrapper.close method is be called in background after leaving the try brackets.
      Parameters:
      consumer - a responsible instance which consumes the lock.
      Returns:
      a new builder wrapper which already locks the manage lock.
    • getManageWriteLockInterruptible

      protected org.openbase.jul.schedule.CloseableWriteLockWrapper getManageWriteLockInterruptible(Object consumer) throws InterruptedException
      This method generates a closable manager lock wrapper. Be informed that the controller and all its services are directly locked and internal builder operations are queued. Therefore please call the close method soon as possible to release the lock after you are done, otherwise the overall processing pipeline is delayed.

      Note: Be aware that your access is time limited an the lock will auto released if locked in longer term. This is a recovering feature but should never be used by design!

       Usage Example:
      
           try(final CloseableInterruptibleWriteLockWrapper ignored = getManageWriteLock(this)) {
               // do important stuff...
           }
       
       
      In this example the CloseableWriteLockWrapper.close method is be called in background after leaving the try brackets.
      Parameters:
      consumer - a responsible instance which consumes the lock.
      Returns:
      a new builder wrapper which already locks the manage lock.
      Throws:
      InterruptedException - in case the thread was externally interrupted during the locking.
    • getManageReadLockInterruptible

      protected org.openbase.jul.schedule.CloseableReadLockWrapper getManageReadLockInterruptible(Object consumer) throws InterruptedException
      This method generates a closable manager lock wrapper. Be informed that the controller and all its services are directly locked and internal builder operations are queued. Therefore please call the close method soon as possible to release the lock after you are done, otherwise the overall processing pipeline is delayed.

      Note: Be aware that your access is time limited an the lock will auto released if locked in longer term. This is a recovering feature but should never be used by design!

       Usage Example:
      
           try(final CloseableInterruptibleReadLockWrapper ignored = getManageReadLock(this)) {
               // do important stuff...
           }
       
       
      In this example the CloseableWriteLockWrapper.close method is be called in background after leaving the try brackets.
      Parameters:
      consumer - a responsible instance which consumes the lock.
      Returns:
      a new builder wrapper which already locks the manage lock.
      Throws:
      InterruptedException - in case the thread was externally interrupted during the locking.
    • getManageLock

      protected org.openbase.jul.schedule.CloseableLockProvider getManageLock()
      This method generates a closable lock provider. Be informed that the controller and all its services are directly locked and internal builder operations are queued. Therefore please release the locks after usage, otherwise the overall processing pipeline is delayed.

      Note: Be aware that your access is time limited an the lock will auto released if locked in longer term. This is a recovering feature but should never be used by design!

      Returns:
      a provider to access the manage read and write lock.
    • getScope

      public org.openbase.type.communication.ScopeType.Scope getScope() throws org.openbase.jul.exception.NotAvailableException
      Specified by:
      getScope in interface org.openbase.jul.extension.type.iface.ScopeProvider
      Returns:
    • notifyChange

      public void notifyChange() throws org.openbase.jul.exception.CouldNotPerformException, InterruptedException
      Synchronize all registered remote instances about a data change.
      Specified by:
      notifyChange in interface org.openbase.jul.pattern.ChangeListener
      Specified by:
      notifyChange in interface org.openbase.jul.pattern.controller.Controller<M extends com.google.protobuf.AbstractMessage>
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
      InterruptedException
    • updateDataToPublish

      protected M updateDataToPublish(MB dataBuilder) throws org.openbase.jul.exception.CouldNotPerformException
      Called before publishing data via the publisher. Can be implemented by sub classes to update data which can be received by everyone.
      Parameters:
      dataBuilder - a clone of the current data builder.
      Returns:
      a message build from the data builder
      Throws:
      org.openbase.jul.exception.CouldNotPerformException - if the update fails
    • notifyDataUpdate

      protected void notifyDataUpdate(M data) throws org.openbase.jul.exception.CouldNotPerformException
      Overwrite this method to get informed about data updates.
      Parameters:
      data - new arrived data messages.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • setDataField

      protected final void setDataField(int fieldNumber, Object value) throws org.openbase.jul.exception.CouldNotPerformException
      Parameters:
      fieldNumber -
      value -
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • setDataField

      protected final void setDataField(String fieldName, Object value) throws org.openbase.jul.exception.CouldNotPerformException
      Parameters:
      fieldName -
      value -
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • getDataField

      protected final Object getDataField(String name) throws org.openbase.jul.exception.NotAvailableException
      Parameters:
      name -
      Returns:
      Throws:
      org.openbase.jul.exception.NotAvailableException
    • hasDataField

      protected final boolean hasDataField(String name) throws org.openbase.jul.exception.CouldNotPerformException
      Parameters:
      name -
      Returns:
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • supportsDataField

      protected final boolean supportsDataField(String name) throws org.openbase.jul.exception.CouldNotPerformException
      Parameters:
      name -
      Returns:
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • getDataFieldDescriptor

      protected final com.google.protobuf.Descriptors.FieldDescriptor getDataFieldDescriptor(int fieldId)
      Parameters:
      fieldId -
      Returns:
    • getControllerAvailabilityState

      public org.openbase.type.domotic.state.AvailabilityStateType.AvailabilityState.State getControllerAvailabilityState()
      Specified by:
      getControllerAvailabilityState in interface org.openbase.jul.pattern.controller.Controller<M extends com.google.protobuf.AbstractMessage>
      Returns:
    • validateInitialization

      public void validateInitialization() throws org.openbase.jul.exception.NotInitializedException, InterruptedException
      This method validates the controller initialisation.
      Throws:
      org.openbase.jul.exception.NotInitializedException - is thrown if the controller is not initialized.
      InterruptedException - is thrown in case the thread was externally interrupted.
    • validateActivation

      public void validateActivation() throws org.openbase.jul.exception.InvalidStateException
      This method validates the controller activation.
      Throws:
      org.openbase.jul.exception.InvalidStateException - is thrown if the controller is not active.
    • validateMiddleware

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

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

      public boolean isDataBuilderWriteLockedByCurrentThread()
      Method checks if the requesting thread holds the data builder write lock of this unit. This check can help to decide if a notification should be performed or not since releasing the write lock mostly notifies anyway depending on the selected notification strategy.
      Returns:
      true if the current thread hold the builder write lock, otherwise false.
    • ping

      public Future<Long> ping(Long timestamp)
      Specified by:
      ping in interface org.openbase.jul.iface.Pingable
      Parameters:
      timestamp -
      Returns:
    • requestStatus

      public M requestStatus() throws org.openbase.jul.exception.CouldNotPerformException
      Specified by:
      requestStatus in interface org.openbase.jul.iface.Requestable<M extends com.google.protobuf.AbstractMessage>
      Returns:
    • registerMethods

      public abstract void registerMethods(org.openbase.jul.communication.iface.RPCServer server) throws org.openbase.jul.exception.CouldNotPerformException
      Register methods for RPCs on the internal RPC server.
      Parameters:
      server - the rpc server on which the methods should be registered.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException - if registering methods fails
    • isDataAvailable

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

      public Boolean isReady()
      Method returns true if this instance was initialized, activated and is successfully connected to the middleware.
      Specified by:
      isReady in interface org.openbase.jul.iface.Readyable
      Returns:
      returns true if this instance is ready otherwise false.
    • 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.AbstractMessage>
    • 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.AbstractMessage>
      Parameters:
      timeout -
      timeUnit - .
    • 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.AbstractMessage>
      Parameters:
      observer -
    • 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.AbstractMessage>
      Parameters:
      observer -
    • getTransactionId

      public long getTransactionId()
      Specified by:
      getTransactionId in interface org.openbase.jul.extension.type.iface.TransactionIdProvider
      Returns:
    • generateTransactionId

      public long generateTransactionId()
      Method generates a new id which can be used for the next transaction (e.g. state transition).
      Returns:
      the next transaction identifier.
    • updateTransactionId

      public void updateTransactionId() throws org.openbase.jul.exception.CouldNotPerformException
      Methde sets a new generated transaction id in the global data field of this controller.
      Throws:
      org.openbase.jul.exception.CouldNotPerformException
    • 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.AbstractMessage>
      Throws:
      org.openbase.jul.exception.InvalidStateException
    • isShutdownInProgress

      public boolean isShutdownInProgress()
      Method returns true if this instance is currently shutting down.
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns: