T
- The data type data that this implementation can handle.S
- The type of Edit
s that this implementation can handle.public class ServerSyncEngine<T,S extends Edit<? extends Diff>> extends Object
During construction the engine gets injected with an instance of ServerSynchronizer
which takes care of diff/patching operations, and an instance of ServerDataStore
for
storing data.
A synchronizer in AeroGear is a module that serves two purposes which are closely related. One, is to provide storage for the data type, and the second is to provide the patching algorithm to be used on that data type. The name synchronizer is because they take care of the synchronization part of the Differential Synchronization algorithm. For example, one synchronizer might support plain text while another supports JSON Objects as the content of documents being stored. But a patching algorithm used for plain text might not be appropriate for JSON Objects.
To construct a server that uses the JSON Patch you would use the following code:
final JsonPatchServerSynchronizer synchronizer = new JsonPatchServerSynchronizer();
final ServerInMemoryDataStore<JsonNode, JsonPatchEdit> dataStore = new ServerInMemoryDataStore<JsonNode, JsonPatchEdit>();
final ServerSyncEngine<JsonNode, JsonPatchEdit> syncEngine = new ServerSyncEngine<JsonNode, JsonPatchEdit>(synchronizer, dataStore);
Constructor and Description |
---|
ServerSyncEngine(ServerSynchronizer<T,S> synchronizer,
ServerDataStore<T,S> dataStore)
Sole constructor.
|
Modifier and Type | Method and Description |
---|---|
PatchMessage<S> |
addSubscriber(Subscriber<?> subscriber,
Document<T> document)
Adds a subscriber for the specified document.
|
void |
connectSubscriber(Subscriber<?> subscriber,
String documentId)
Connects a subscriber to an already existing document.
|
S |
diff(String documentId,
String clientId)
Performs the server side diff which is performed when the server document is modified.
|
Document<T> |
documentFromJson(com.fasterxml.jackson.databind.JsonNode json)
Converts the
JsonNode into a Document instance. |
PatchMessage<S> |
getPatchMessage(String documentId,
String clientId)
Returns the
PatchMessage for the specified documentId and clientId. |
Set<Subscriber<?>> |
getSubscribers(String documentId)
Returns all the subscribers for the specified document.
|
void |
notifySubscribers(PatchMessage<S> patchMessage)
Performs the server side patching for a specific client and updates
all subscribers to the patched document.
|
PatchMessage<S> |
patch(PatchMessage<S> patchMessage)
Performs the server side patching for a specific client.
|
PatchMessage<S> |
patchMessageFromJson(String json)
Creates a {link PatchMessage} by parsing the passed-in json.
|
void |
removeSubscriber(Subscriber<?> subscriber,
String documentId)
Removes the specified
Subscriber . |
public ServerSyncEngine(ServerSynchronizer<T,S> synchronizer, ServerDataStore<T,S> dataStore)
synchronizer
- an instance of ServerSynchronizer
that will take care for the diff/patchingdataStore
- an instance of ServerDataStore
to store the document/objectspublic PatchMessage<S> addSubscriber(Subscriber<?> subscriber, Document<T> document)
subscriber
- the subscriber to adddocument
- the document that the subscriber subscribes to. Will be added to the underlying
datastore if it does not already exist in the datastore.PatchMessage
for the Document
. Will either be an PatchMessage with an empty
diff, if this is the initial addition of the document, or if the document already
exists in the underlying datastore the patch message will contain a diff to bring
the document up to date.public void connectSubscriber(Subscriber<?> subscriber, String documentId)
subscriber
- the Subscriber
to adddocumentId
- the id of the document that the subscriber wants to subscribe.public void removeSubscriber(Subscriber<?> subscriber, String documentId)
Subscriber
.subscriber
- the Subscriber
to removedocumentId
- the document id that the subscriber subscribes topublic Set<Subscriber<?>> getSubscribers(String documentId)
documentId
- the id of the document for which all subscribers should be returned.Set
all the Subscriber
spublic S diff(String documentId, String clientId)
Edit
can be sent to the client for patching the client side document.documentId
- the document in question.clientId
- the clientId for whom we should perform a diff and create edits for.Edit
The server edits, or updates, that were generated by this diff .public PatchMessage<S> patch(PatchMessage<S> patchMessage)
patchMessage
- the changes made by a client.PatchMessage
to allow method chainingpublic void notifySubscribers(PatchMessage<S> patchMessage)
patchMessage
- the changes made by a client.public PatchMessage<S> patchMessageFromJson(String json)
json
- the json representation of a PatchMessage
PatchMessage
the created {code PatchMessage}public Document<T> documentFromJson(com.fasterxml.jackson.databind.JsonNode json)
JsonNode
into a Document
instance.json
- the JsonNode
to convertDocument
the document representing the contents of the JsonNode
instance.public PatchMessage<S> getPatchMessage(String documentId, String clientId)
PatchMessage
for the specified documentId and clientId.documentId
- the document identifierclientId
- the client identifierPatchMessage
for the current document/client combinationCopyright © 2015 JBoss by Red Hat. All Rights Reserved.