pub struct Ditto { /* private fields */ }
Expand description

The entry point for accessing Ditto-related functionality.

This struct is generally a handle and interface to ditto-functionality which operates in background threads.

Implementations§

source§

impl Ditto

source

pub fn close(self)

Clean shutdown of the Ditto instance

source

pub fn start_sync(&self) -> Result<(), DittoError>

Start syncing on all configured transports

source

pub fn stop_sync(&self)

Stop syncing on all transports.

source

pub fn set_transport_config(&self, config: TransportConfig)

Set a new TransportConfig and begin syncing over these transports. Any change to start or stop a specific transport should proceed via providing a modified configuration to this method.

source

pub fn current_transport_config(&self) -> Result<TransportConfig, DittoError>

Returns a snapshot of the currently configured transports.

source§

impl Ditto

source

pub fn with_sdk_version<R>(ret: impl FnOnce(&str) -> R) -> R

Return the version of the SDK.

source§

impl Ditto

source

pub fn set_logging_enabled(enabled: bool)

Enable of disable logging.

source

pub fn get_logging_enabled() -> bool

Return true if logging is enabled.

source

pub fn get_emoji_log_level_headings_enabled() -> bool

Represent whether or not emojis should be used as the log level indicator in the logs.

source

pub fn set_emoji_log_level_headings_enabled(enabled: bool)

Set whether or not emojis should be used as the log level indicator in the logs.

source

pub fn get_minimum_log_level() -> LogLevel

Get the current minimum log level.

source

pub fn set_minimum_log_level(log_level: LogLevel)

Set the current minimum log level.

source§

impl Ditto

source

pub fn set_offline_only_license_token( &self, license_token: &str ) -> Result<(), DittoError>

Activate an offline Ditto instance by setting a license token.

You cannot initiate sync on an offline (OfflinePlayground, Manual, or SharedKey) Ditto instance before you have activated it.

source

pub fn set_license_from_env(&self, var_name: &str) -> Result<(), DittoError>

Look for a license token from a given environment variable.

source

pub fn set_peer_signed_info<S: Serialize>( &self, peer_info: HashMap<String, S> ) -> Result<(), DittoError>

Set a dictionary of arbitrary data about this device to be shared with peers in the mesh.

This data is gossiped in the presence collection across the mesh. This can be useful to include extra metadata like app versions,capabilities, etc., to help peers decide who to interact with.

This peer information is persisted in the SDK, and thus needn’t be set at every start of Ditto.

Security (and caveats)

This peer info will be signed by your peer key to prevent forgery of this info by other peers.

However, for compatibility, there is no attestation of the lack of peer info – that is, participants in the mesh could maliciously remove peer info. If this is a concern for your application, a workaround for this is to have your application require that peers have a signed peer info dictionary present.

Similarly, as there is no monotonic version counter or timestamp/expiration of the signed peer info, replay attacks (replacing the current info with previously, possibly outdated, signed info) are possible without counter-measures. If this is a concern for your application, you might consider including a counter or creation timestamp to prevent replays, depending on your use-case.

Performance caveats

Because this information is included in the presence data that is gossiped among peers, the size of this peer info and the frequency it is updated can drastically affect performance if it is too large.

Errors

Because of the performance implications, the serialized info dictionary is currently limited to 4KiB.

Examples
let mut peer_info = HashMap::new();
peer_info.insert("app_version".into(), "1.0.0".to_string());
ditto.set_peer_signed_info(peer_info)?;
source

pub fn register_on_connecting_callback( &self, callback: impl Fn(ConnectingPeer, Box<dyn FnOnce(bool)>) + Send + Sync + 'static )

Add a callback that can decide based on the known peer information at the time whether Ditto should complete the handshake or reject the connection.

This is an asynchronous API, so your callback will receive information about the connecting peer, and you must then call the provided callback function with your decision.

Decisions

Responding with true will allow the peer to connect. Responding with false will reject the peer. Not responding will lead to a handshake timeout, effectively rejecting the peer.

Examples

ditto.register_on_connecting_callback(|peer, respond| {
    match peer.peer_info.unwrap_or_default().get("app_version") {
        // Reject peers reporting a known buggy version.
        Some(InfoValue::Text(v)) if v == "1.2.3" => respond(false),
        _ => respond(true),
    }
});
source

pub fn store(&self) -> &Store

Returns a reference to the underlying local data store.

source

pub fn sync(&self) -> &Sync

Returns a reference to the synchronization controller.

source

pub fn small_peer_info(&self) -> &SmallPeerInfo

Return a reference to the SmallPeerInfo object.

source

pub fn site_id(&self) -> u64

Returns the site ID that the instance of Ditto is using as part of its identity.

source

pub fn persistence_directory(&self) -> &Path

Returns the Ditto persistence directory path.

source

pub fn application_id(&self) -> AppId

Returns the application Id being used by this Ditto instance.

source

pub fn set_device_name(&self, name: &str)

Set a custom identifier for the current device.

When using observe_peers, each remote peer is represented by a short UTF-8 “device name”. By default this will be a truncated version of the device’s hostname. It does not need to be unique among peers. Configure the device name before calling start_sync. If it is too long it will be truncated.

source

pub fn observe_peers<H>(&self, handler: H) -> PeersObserverwhere H: Fn(V2Presence) + Send + Sync + 'static,

👎Deprecated: Use presence().observe() instead

Request information about Ditto peers in range of this device.

This method returns an observer which should be held as long as updates are required. A newly registered observer will have a peers update delivered to it immediately. Then it will be invoked repeatedly when Ditto devices come and go, or the active connections to them change.

source

pub fn presence(&self) -> &Arc<Presence>

Return a handle to Presence to monitor the peers’ activity in the Ditto mesh. It can be used to retrieve an immediate representation of known peers :

let presence_graph = ditto.presence().exec();

Or to bind a callback to the changes :

let handle = ditto.presence().observe(|graph| {
    // do something with the graph
});
// The handle must be kept to keep receiving updates on the other peers.
// To stop receiving update, drop the handle.
source

pub fn disk_usage(&self) -> &DiskUsage

Return a DiskUsage to monitor the disk usage of the Ditto instance. It can be used to retrieve an immediate representation of the Ditto file system:

let fs_tree = ditto.disk_usage().exec();

Or to bind a callback to the changes :

let handle = ditto.disk_usage().observe(|fs_tree| {
    // do something with the graph
});
// The handle must be kept to keep receiving updates on the file system.
// To stop receiving update, drop the handle.
source

pub fn root_dir(&self) -> &Path

👎Deprecated: Use persistence_directory instead

Returns the DittoRoot path.

source

pub fn data_dir(&self) -> &Path

👎Deprecated: Use persistence_directory instead

Returns the ditto persistence data directory path.

source

pub fn authenticator(&self) -> Option<DittoAuthenticator>

Returns the current DittoAuthenticator, if it exists.

source

pub fn is_activated(&self) -> bool

Has this Ditto instance been activated yet with a valid license token.

source§

impl Ditto

source

pub fn builder() -> DittoBuilder

Returns a builder for Ditto following the builder pattern.

Start constructing a new Ditto instance.

source

pub fn new(app_id: AppId) -> Ditto

Construct a Ditto instance with sensible defaults.

This instance will still need to have a license set (if necessary) and sync functionality manually started.

source§

impl Ditto

source

pub fn run_garbage_collection(&self)

Removes all sync metadata for any remote peers which aren’t currently connected. This method shouldn’t usually be called. Manually running garbage collection often will result in slower sync times. Ditto automatically runs a garbage a collection process in the background at optimal times.

Manually running garbage collection is typically only useful during testing if large amounts of data are being generated. Alternatively, if an entire data set is to be evicted and it’s clear that maintaining this metadata isn’t necessary, then garbage collection could be run after evicting the old data.

source

pub fn disable_sync_with_v3(&self) -> Result<(), DittoError>

Explicitly opt-in to disabling the ability to sync with Ditto peers running any version of the SDK in the v3 (or lower) series of releases.

Assuming this succeeds then this peer will only be able to sync with other peers using SDKs in the v4 (or higher) series of releases. Note that this disabling of sync spreads to peers that sync with a peer that has disabled, or has (transitively) had disabled, syncing with v3 SDK peers.

Trait Implementations§

source§

impl Deref for Ditto

§

type Target = DittoFields

The resulting type after dereferencing.
source§

fn deref(&self) -> &DittoFields

Dereferences the value.
source§

impl Drop for Ditto

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Ditto

§

impl Send for Ditto

§

impl Sync for Ditto

§

impl Unpin for Ditto

§

impl !UnwindSafe for Ditto

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
§

impl<T> FitForCBox for T

§

type CBoxWrapped = Box_<T>

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> ManuallyDropMut for T

§

type Ret = ManuallyDrop<T>

§

fn manually_drop_mut<'__>(&'__ mut self) -> &'__ mut ManuallyDrop<T>

§

impl<T> To for Twhere T: ?Sized,

§

fn to<T>(self) -> Twhere Self: Into<T>,

Converts to T by calling Into<T>::into.
§

fn try_to<T>(self) -> Result<T, Self::Error>where Self: TryInto<T>,

Tries to convert to T by calling TryInto<T>::try_into.
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V