Class DittoPresence
java.lang.Object
com.ditto.java.DittoPresence
The entrypoint for all actions that relate to presence of other peers known by the current peer,
either directly or through other peers.
You don't create one directly but can access it via the
Ditto.getPresence() getter.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceHandler interface for controlling incoming connection requests.static interfaceListener interface for receiving presence graph updates. -
Method Summary
Modifier and TypeMethodDescription@Nullable DittoPresence.ConnectionRequestHandlerReturns the handler that controls which peers in a Ditto mesh can connect to the current peer.@NonNull DittoPresenceGraphgetGraph()Returns the latest snapshot of information about peers in the Ditto mesh network.@NonNull DittoJsonSerializable.ObjectValueReturns metadata associated with the current peer.@NonNull StringReturns metadata associated with the current peer as a JSON string.@NonNull Flow.Publisher<@NonNull DittoPresenceGraph> observe()Returns a Flow Publisher that emits presence graph updates.observe(@NonNull DittoPresence.PresenceGraphChangedListener listener) Requests information about Ditto peers in the local peer-to-peer network.voidsetConnectionRequestHandler(@Nullable DittoPresence.ConnectionRequestHandler connectionRequestHandler) Sets a handler to control which peers in a Ditto mesh can connect to the current peer.voidsetPeerMetadata(@NonNull DittoJsonSerializable.ObjectValue peerMetadata) Sets metadata to be associated with the current peer.voidsetPeerMetadataJsonString(@NonNull String peerMetadataJsonString) Sets metadata to be associated with the current peer from a JSON string.
-
Method Details
-
getGraph
Returns the latest snapshot of information about peers in the Ditto mesh network.- Returns:
- a
DittoPresenceGraphrepresenting the current state of the mesh network.
-
getPeerMetadata
Returns metadata associated with the current peer. Other peers in the same mesh can access this user-provided map of metadata via the presence graph atgetGraph()and when evaluating connection requests usinggetConnectionRequestHandler(). UsesetPeerMetadata(DittoJsonSerializable.ObjectValue)orsetPeerMetadataJsonString(String)to set this value. This is a convenience property that wrapsgetPeerMetadataJsonString().- Returns:
- a
DittoJsonSerializable.ObjectValuecontaining the peer metadata. - See Also:
-
setPeerMetadata
Sets metadata to be associated with the current peer.- Parameters:
peerMetadata- aDittoJsonSerializable.ObjectValuecontaining the metadata to set.- See Also:
-
getPeerMetadataJsonString
Returns metadata associated with the current peer as a JSON string. Other peers in the same mesh can access this user-provided map of metadata via the presence graph atgetGraph()and when evaluating connection requests usinggetConnectionRequestHandler(). UsesetPeerMetadataJsonString(String)orsetPeerMetadata(DittoJsonSerializable.ObjectValue)to set this value.- Returns:
- a JSON string containing the peer metadata.
- See Also:
-
setPeerMetadataJsonString
Sets metadata to be associated with the current peer from a JSON string.- Parameters:
peerMetadataJsonString- a JSON string containing the metadata to set.- See Also:
-
getConnectionRequestHandler
Returns the handler that controls which peers in a Ditto mesh can connect to the current peer. Each peer in a Ditto mesh will attempt to connect to other peers that it can reach. By default, the mesh will try and establish connections that optimize for the best overall connectivity between peers. However, you can set this handler to assert some control over which peers you connect to. If set, this handler is called for every incoming connection request from a remote peer and is passed the other peer'sDittoPeer.getPeerKey(),getPeerMetadata(), andDittoConnectionRequest.getIdentityServiceMetadata(). The handler can then accept or reject the request by returning aDittoConnectionRequestAuthorizationvalue. When the connection request is rejected, the remote peer may retry the connection request after a short delay. Connection request handlers must reliably respond to requests within a short time. If a handler takes too long to respond or throws an exception, the connection request will be denied. The response timeout is currently 10 seconds but may be subject to change in future releases.- Returns:
- the current
DittoPresence.ConnectionRequestHandler, or null if not set. - See Also:
- Implementation Note:
- The handler is called from a different thread.
-
setConnectionRequestHandler
public void setConnectionRequestHandler(@Nullable DittoPresence.ConnectionRequestHandler connectionRequestHandler) Sets a handler to control which peers in a Ditto mesh can connect to the current peer.- Parameters:
connectionRequestHandler- theDittoPresence.ConnectionRequestHandlerto use, or null to remove the handler.- See Also:
-
observe
Returns a Flow Publisher that emits presence graph updates.- Returns:
- a
Flow.Publisherthat emitsDittoPresenceGraphinstances when the mesh changes. - See Also:
-
observe
Requests information about Ditto peers in the local peer-to-peer network. This method returns an observer which will invoke theDittoPresence.PresenceGraphChangedListener.onGraphChanged(com.ditto.java.DittoPresenceGraph)method on the provided callback object for each update. A newly registered observer will have an update delivered to it immediately. Then it will be invoked repeatedly when new information is learned about Ditto peers or their connections in the local peer-to-peer network. When the observer is no longer required the caller must callclose()on the returnedDittoAsyncCancellable.- Parameters:
listener- an object that implements theDittoPresence.PresenceGraphChangedListenerinterface, whoseDittoPresence.PresenceGraphChangedListener.onGraphChanged(com.ditto.java.DittoPresenceGraph)method will be called when there is an update.- Returns:
- a
DittoAsyncCancellablethat can be used to stop observing.
-