T
- The data type data that this implementation can handle.S
- The type of Edit
s that this implementation can handle.public class ClientSyncEngine<T,S extends Edit<? extends Diff>> extends Observable
During construction the engine gets injected with an instance of ClientSynchronizer
which takes care of diff/patching operations, and an instance of ClientDataStore
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 ClientInMemoryDataStore<JsonNode, JsonPatchEdit> dataStore = new ClientInMemoryDataStore<JsonNode, JsonPatchEdit>();
final ClientSyncEngine<JsonNode, JsonPatchEdit> syncEngine = new ClientSyncEngine<JsonNode, JsonPatchEdit>(synchronizer, dataStore);
Constructor and Description |
---|
ClientSyncEngine(ClientSynchronizer<T,S> clientSynchronizer,
ClientDataStore<T,S> dataStore) |
Modifier and Type | Method and Description |
---|---|
void |
addDocument(ClientDocument<T> document)
Adds a new document to this sync engine.
|
PatchMessage<S> |
createPatchMessage(String documentId,
String clientId,
Queue<S> edits)
Creates a new
PatchMessage with the with the type of Edit that this
synchronizer can handle. |
PatchMessage<S> |
diff(ClientDocument<T> document)
Returns an
PatchMessage which contains a diff against the engine's stored
shadow document and the passed-in document. |
String |
documentToJson(ClientDocument<T> document)
Converts the
ClientDocument into a JSON String representation. |
void |
patch(PatchMessage<S> patchMessage)
Patches the client side shadow with updates (
PatchMessage ) from the server. |
PatchMessage<S> |
patchMessageFromJson(String json)
Creates a {link PatchMessage} by parsing the passed-in json.
|
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
public ClientSyncEngine(ClientSynchronizer<T,S> clientSynchronizer, ClientDataStore<T,S> dataStore)
public void addDocument(ClientDocument<T> document)
document
- the document to add.public PatchMessage<S> diff(ClientDocument<T> document)
PatchMessage
which contains a diff against the engine's stored
shadow document and the passed-in document.
There might be pending edits that represent edits that have not made it to the server
for some reason (for example packet drop). If a pending edit exits the contents (the diffs)
of the pending edit will be included in the returned Edits from this method.
The returned PatchMessage
instance is indended to be sent to the server engine
for processing.document
- the updated document.PatchMessage
containing the edits for the changes in the document.public void patch(PatchMessage<S> patchMessage)
PatchMessage
) from the server.
When updates happen on the server, the server will create an PatchMessage
instance
by calling the server engines diff method. This PatchMessage
instance will then be
sent to the client for processing which is done by this method.patchMessage
- the updates from the server.public PatchMessage<S> patchMessageFromJson(String json)
json
- the json representation of a PatchMessage
PatchMessage
the created {code PatchMessage}public String documentToJson(ClientDocument<T> document)
ClientDocument
into a JSON String
representation.document
- the ClientDocument
to convertString
the JSON String representation of the document.public PatchMessage<S> createPatchMessage(String documentId, String clientId, Queue<S> edits)
PatchMessage
with the with the type of Edit
that this
synchronizer can handle.documentId
- the document identifier for the PatchMessage
clientId
- the client identifier for the PatchMessage
edits
- the Edit
s for the PatchMessage
PatchMessage
the created {code PatchMessage}Copyright © 2015 JBoss by Red Hat. All Rights Reserved.