Class SyncClientImpl

java.lang.Object
io.objectbox.sync.SyncClientImpl
All Implemented Interfaces:
SyncClient, Closeable, AutoCloseable

@Internal public final class SyncClientImpl extends Object implements SyncClient
Internal sync client implementation. Use SyncClient to access functionality, this class may change without notice.
  • Field Details

  • Constructor Details

  • Method Details

    • getHandle

      private long getHandle()
    • getServerUrl

      public String getServerUrl()
      Description copied from interface: SyncClient
      Gets the sync server URL this client is connected to.
      Specified by:
      getServerUrl in interface SyncClient
    • getLastLoginCode

      public long getLastLoginCode()
      Description copied from interface: SyncClient
      Response code of last login attempt. One of SyncLoginCodes.
      Specified by:
      getLastLoginCode in interface SyncClient
    • isLoggedIn

      public boolean isLoggedIn()
      Description copied from interface: SyncClient
      Flag indicating if the sync client was started. Logged in clients can sync with the sync destination to exchange data.
      Specified by:
      isLoggedIn in interface SyncClient
    • getServerTimeNanos

      public long getServerTimeNanos()
      Description copied from interface: SyncClient
      Estimates the current server timestamp in nanoseconds based on the last known server time.
      Specified by:
      getServerTimeNanos in interface SyncClient
      Returns:
      unix timestamp in nanoseconds (since epoch); or 0 if there has not been a server contact yet and thus the server's time is unknown
    • getServerTimeDiffNanos

      public long getServerTimeDiffNanos()
      Description copied from interface: SyncClient
      Returns the estimated difference in nanoseconds between the server time and the local timestamp. urns the difference in nanoseconds between the current local time of this client Equivalent to calculating SyncClient.getServerTimeNanos() - "current time" (nanos since epoch), except for when the server time is unknown, then the result is zero.
      Specified by:
      getServerTimeDiffNanos in interface SyncClient
      Returns:
      time difference in nanoseconds; e.g. positive if server time is ahead of local time; or 0 if there has not been a server contact yet and thus the server's time is unknown
    • getRoundtripTimeNanos

      public long getRoundtripTimeNanos()
      Description copied from interface: SyncClient
      Returns the estimated roundtrip time in nanoseconds to the server and back. This is measured during login.
      Specified by:
      getRoundtripTimeNanos in interface SyncClient
      Returns:
      roundtrip time in nanoseconds; or 0 if there has not been a server contact yet and thus the roundtrip time could not be estimated
    • getSyncState

      public SyncState getSyncState()
      Gets the current state of this sync client. Throws if close() was called.
    • setSyncLoginListener

      public void setSyncLoginListener(@Nullable SyncLoginListener listener)
      Description copied from interface: SyncClient
      Sets a listener to observe login events. Replaces a previously set listener. Set to null to remove the listener.
      Specified by:
      setSyncLoginListener in interface SyncClient
    • setSyncCompletedListener

      public void setSyncCompletedListener(@Nullable SyncCompletedListener listener)
      Description copied from interface: SyncClient
      Sets a listener to observe Sync completed events. Replaces a previously set listener. Set to null to remove the listener.
      Specified by:
      setSyncCompletedListener in interface SyncClient
    • setSyncChangeListener

      public void setSyncChangeListener(@Nullable SyncChangeListener changesListener)
      Description copied from interface: SyncClient
      Sets a SyncChangeListener. Replaces a previously set listener. Set to null to remove the listener.
      Specified by:
      setSyncChangeListener in interface SyncClient
    • setSyncTimeListener

      public void setSyncTimeListener(@Nullable SyncTimeListener timeListener)
      Description copied from interface: SyncClient
      Sets a SyncTimeListener. Replaces a previously set listener. Set to null to remove the listener.
      Specified by:
      setSyncTimeListener in interface SyncClient
    • setSyncConnectionListener

      public void setSyncConnectionListener(@Nullable SyncConnectionListener listener)
      Description copied from interface: SyncClient
      Sets a listener to observe Sync connection events. Replaces a previously set listener. Set to null to remove the listener.
      Specified by:
      setSyncConnectionListener in interface SyncClient
    • setSyncListener

      public void setSyncListener(@Nullable SyncListener listener)
      Description copied from interface: SyncClient
      Sets a listener to observe all Sync events. Replaces all other previously set listeners, except a SyncChangeListener. Set to null to remove the listener.
      Specified by:
      setSyncListener in interface SyncClient
    • setLoginCredentials

      public void setLoginCredentials(SyncCredentials credentials)
      Description copied from interface: SyncClient
      Updates the login credentials. This should not be required during regular use. The original credentials were passed when building sync client.
      Specified by:
      setLoginCredentials in interface SyncClient
    • setLoginCredentials

      public void setLoginCredentials(SyncCredentials[] multipleCredentials)
      Description copied from interface: SyncClient
      Updates the login credentials. This should not be required during regular use. It allows passing login credentials that the client can use to authenticate with the server.
      Specified by:
      setLoginCredentials in interface SyncClient
    • awaitFirstLogin

      public boolean awaitFirstLogin(long millisToWait)
      Description copied from interface: SyncClient
      Waits until the sync client receives a response to its first (connection and) login attempt or until the given time has expired. Use SyncClient.isLoggedIn() or SyncClient.getLastLoginCode() afterwards to determine if login was successful. Starts the sync if it is not already.
      Specified by:
      awaitFirstLogin in interface SyncClient
      Returns:
      true if a response was received in the given time window.
    • start

      public void start()
      Description copied from interface: SyncClient
      Starts the client. It will connect to the server, log in (authenticate) and start syncing.
      Specified by:
      start in interface SyncClient
    • isStarted

      public boolean isStarted()
      Description copied from interface: SyncClient
      Flag indicating if the sync client was started. Started clients try to connect, login, and sync with the sync destination.
      Specified by:
      isStarted in interface SyncClient
    • stop

      public void stop()
      Description copied from interface: SyncClient
      Stops the client. Does nothing if the sync client is already stopped.
      Specified by:
      stop in interface SyncClient
    • close

      public void close()
      Description copied from interface: SyncClient
      Closes and cleans up all resources used by this sync client. It can no longer be used afterwards, build a new sync client instead. Does nothing if this sync client has already been closed.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface SyncClient
    • finalize

      protected void finalize() throws Throwable
      Users of this class should explicitly call close() instead to avoid expensive finalization.
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • requestFullSync

      @Experimental public boolean requestFullSync()
      Temporary only, try not to use it.
      Specified by:
      requestFullSync in interface SyncClient
      Returns:
      'true' if the request was likely sent (e.g. the sync client is in "logged in" state) or 'false' if the request was not sent (and will not be sent in the future).
    • requestFullSyncAndUpdates

      @Experimental public boolean requestFullSyncAndUpdates()
      Temporary only, try not to use it.
    • requestUpdates

      public boolean requestUpdates()
      Description copied from interface: SyncClient
      Asks the sync server to resume sync updates. This is useful if sync updates were turned off with requestUpdatesMode(MANUAL).
      Specified by:
      requestUpdates in interface SyncClient
      Returns:
      'true' if the request was likely sent (e.g. the sync client is in "logged in" state) or 'false' if the request was not sent (and will not be sent in the future)
      See Also:
    • requestUpdatesOnce

      public boolean requestUpdatesOnce()
      Description copied from interface: SyncClient
      Asks the server to send sync updates until this sync client is up-to-date, then pauses sync updates again. This is useful if sync updates were turned off with requestUpdatesMode(MANUAL).
      Specified by:
      requestUpdatesOnce in interface SyncClient
      Returns:
      'true' if the request was likely sent (e.g. the sync client is in "logged in" state) or 'false' if the request was not sent (and will not be sent in the future)
    • cancelUpdates

      public boolean cancelUpdates()
      Description copied from interface: SyncClient
      Asks the server to pause sync updates.
      Specified by:
      cancelUpdates in interface SyncClient
      Returns:
      'true' if the request was likely sent (e.g. the sync client is in "logged in" state) or 'false' if the request was not sent (and will not be sent in the future)
      See Also:
    • notifyConnectionAvailable

      public void notifyConnectionAvailable()
      Description copied from interface: SyncClient
      Lets the sync client know that a working network connection is available.

      This can help speed up reconnecting to the sync server.

      Specified by:
      notifyConnectionAvailable in interface SyncClient
    • startObjectsMessage

      public ObjectsMessageBuilder startObjectsMessage(long flags, @Nullable String topic)
      Description copied from interface: SyncClient
      Experimental. This API might change or be removed in the future.

      Start building a message of Objects with optional flags (set to 0) and topic (set to null).

      Use like

       syncClient.startObjectsMessage(0, "some-topic")
           .addString(1, "Hello!")
           .addBytes(2, "Hello!".getBytes(StandardCharsets.UTF_8), false)
           .send();
       
      Specified by:
      startObjectsMessage in interface SyncClient
    • nativeCreate

      private static long nativeCreate(long storeHandle, String uri, @Nullable String[] certificateDirsOrPaths)
      Creates a native sync client for the given store handle ready to connect to the server at the given URI. Uses certificate authorities trusted by the host if no trusted certificate paths are passed.
    • nativeDelete

      private void nativeDelete(long handle)
    • nativeStart

      private void nativeStart(long handle)
    • nativeStop

      private void nativeStop(long handle)
    • nativeSetLoginInfo

      private void nativeSetLoginInfo(long handle, long credentialsType, @Nullable byte[] credentials)
    • nativeSetLoginInfoUserPassword

      private void nativeSetLoginInfoUserPassword(long handle, long credentialsType, String username, String password)
    • nativeAddLoginCredentials

      private void nativeAddLoginCredentials(long handle, long credentialsType, @Nullable byte[] credentials, boolean complete)
    • nativeAddLoginCredentialsUserPassword

      private void nativeAddLoginCredentialsUserPassword(long handle, long credentialsType, String username, String password, boolean complete)
    • nativeSetListener

      private void nativeSetListener(long handle, @Nullable SyncClientImpl.InternalSyncClientListener listener)
    • nativeSetSyncChangesListener

      private void nativeSetSyncChangesListener(long handle, @Nullable SyncChangeListener advancedListener)
    • nativeSetRequestUpdatesMode

      private void nativeSetRequestUpdatesMode(long handle, boolean autoRequestUpdates, boolean subscribeForPushes)
      Parameters:
      subscribeForPushes - Pass true to automatically receive updates for future changes.
    • nativeSetUncommittedAcks

      private void nativeSetUncommittedAcks(long handle, boolean uncommittedAcks)
      Parameters:
      uncommittedAcks - Default is false.
    • nativeGetState

      private int nativeGetState(long handle)
      Returns the current SyncState value.
    • nativeRequestUpdates

      private boolean nativeRequestUpdates(long handle, boolean subscribeForPushes)
      Parameters:
      subscribeForPushes - Pass true to automatically receive updates for future changes.
    • nativeRequestFullSync

      private boolean nativeRequestFullSync(long handle, boolean subscribeForPushes)
      Parameters:
      subscribeForPushes - Pass true to automatically receive updates for future changes.
    • nativeCancelUpdates

      private boolean nativeCancelUpdates(long handle)
      (Optional) Pause sync updates.
    • nativeTriggerReconnect

      private boolean nativeTriggerReconnect(long handle)
      Hints to the native client that an active network connection is available. Returns true if the native client was disconnected (and will try to re-connect).
    • nativeServerTime

      private long nativeServerTime(long handle)
      The current server timestamp approximation based on the last server time we've received and local steady clock.
      Returns:
      unix timestamp in nanoseconds
    • nativeServerTimeDiff

      private long nativeServerTimeDiff(long handle)
      Returns the difference between the current local timestamp and the current server timestamp approximation as given by nativeServerTime(). Equivalent to calculating: nanosSinceEpoch - nativeServerTime().
      Returns:
      unix timestamp difference in nanoseconds
    • nativeRoundtripTime

      private long nativeRoundtripTime(long handle)
    • nativeObjectsMessageStart

      private long nativeObjectsMessageStart(long flags, @Nullable String topic)
      Returns a handle to the message builder.
      See Also:
    • nativeObjectsMessageAddString

      private void nativeObjectsMessageAddString(long builderHandle, long optionalId, String string)
      See Also:
    • nativeObjectsMessageAddBytes

      private void nativeObjectsMessageAddBytes(long builderHandle, long optionalId, byte[] bytes, boolean isFlatBuffer)
      See Also:
    • nativeObjectsMessageSend

      private boolean nativeObjectsMessageSend(long syncClientHandle, long builderHandle)
      Do not use builderHandle afterwards.