- java.lang.Object
-
- de.haumacher.msgbuf.graph.DefaultScope
-
- All Implemented Interfaces:
Scope,ScopeMixin,Listener
public class DefaultScope extends Object implements Listener, ScopeMixin
The defaultScopeof anAbstractSharedGraphNode.After adding this instance as
listenerto a node, the node is observed for changes. Changes can be externalized to apatchand transmitted over network to anotherDefaultScopecontaining a corresponding instance of the node. There, the patch an beappliedbringing both shared object graphs in sync.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface de.haumacher.msgbuf.observer.Listener
Listener.MultiplexListener
-
-
Constructor Summary
Constructors Constructor Description DefaultScope(int totalParticipants, int participantId)Creates aDefaultScope.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterRemove(Observable obj, String property, int index, Object element)Informs thisListenerthat an element was removed from a repeated property with the given name.voidapplyChanges(JsonReader json)Applies changes read from the givenJsonReader.voidbeforeAdd(Observable obj, String property, int index, Object element)Informs thisListenerthat an element will be added to the repeated property with the given name.protected voidbeforeChange()Hook called before the first change is recorded.voidbeforeSet(Observable obj, String property, Object value)Informs thisListenerthat the value of the given property is to be modified.voidcreatePatch(JsonWriter json)Exports recorded changes to the givenJsonWriter.voiddropChanges()Removes all recorded changes.voidenter(SharedGraphNode node, int id)Assigns the given ID to the given node.Set<SharedGraphNode>getDirty()The set of nodes updated after the last sync.booleanhasChanges()Checks whether there are changes to create a patch.intid(SharedGraphNode node)Looks up the ID of the given node in thisScope.Map<Object,SharedGraphNode>index()The index implementation associating object with IDs.voidinitId(SharedGraphNode node, int id)Assigns the given ID to the given node.static DefaultScopenewClientInstance()Creates aDefaultScopefor the client , where the graph is only shared between two participants, theclientand theserver.intnewId()Creates a fresh ID.static DefaultScopenewServerInstance()Creates aDefaultScopefor the server, where the graph is only shared between two participants, theclientand the server.voidreadData(SharedGraphNode node, int id, JsonReader in)Reads object data of the given node and assigns the given ID to this node.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.haumacher.msgbuf.observer.Listener
afterChanged, afterRemove, beforeAdd
-
Methods inherited from interface de.haumacher.msgbuf.graph.ScopeMixin
resolveOrFail, writeRefOrData
-
-
-
-
Constructor Detail
-
DefaultScope
public DefaultScope(int totalParticipants, int participantId)Creates aDefaultScope.- Parameters:
totalParticipants- The total number of participants operating on the shared graph.participantId- The ID of this participant.
-
-
Method Detail
-
newServerInstance
public static DefaultScope newServerInstance()
Creates aDefaultScopefor the server, where the graph is only shared between two participants, theclientand the server.
-
newClientInstance
public static DefaultScope newClientInstance()
Creates aDefaultScopefor the client , where the graph is only shared between two participants, theclientand theserver.
-
beforeSet
public void beforeSet(Observable obj, String property, Object value)
Description copied from interface:ListenerInforms thisListenerthat the value of the given property is to be modified.- Specified by:
beforeSetin interfaceListener- Parameters:
obj- TheObservableobject.property- The name of the property to be modified.value- The new value that is being set to the given property.
-
beforeAdd
public void beforeAdd(Observable obj, String property, int index, Object element)
Description copied from interface:ListenerInforms thisListenerthat an element will be added to the repeated property with the given name.- Specified by:
beforeAddin interfaceListener- Parameters:
obj- TheObservableobject.property- The name of the property to be modified.index- The index where the new element will be added if the property is ordered,-1otherwise.element- The new element that is being added to the given property.
-
afterRemove
public void afterRemove(Observable obj, String property, int index, Object element)
Description copied from interface:ListenerInforms thisListenerthat an element was removed from a repeated property with the given name.- Specified by:
afterRemovein interfaceListener- Parameters:
obj- TheObservableobject.property- The name of the property to be modified.index- The index where the element was removed if the property is ordered,-1otherwise.element- The element that was removed from the given property.
-
hasChanges
public boolean hasChanges()
Checks whether there are changes to create a patch.
-
dropChanges
public void dropChanges()
Removes all recorded changes.
-
getDirty
public Set<SharedGraphNode> getDirty()
The set of nodes updated after the last sync.
-
createPatch
public void createPatch(JsonWriter json) throws IOException
Exports recorded changes to the givenJsonWriter.The recored changes are reset when this method completes.
The patch is a list containing an entry for each command. Each entry is a list with the command configuration at position 0 followed by optional additional arguments for the command.
- Throws:
IOException- See Also:
applyChanges(JsonReader),hasChanges()
-
applyChanges
public void applyChanges(JsonReader json) throws IOException
Applies changes read from the givenJsonReader.It is expected that the patch has the format as in
createPatch(JsonWriter).- Throws:
IOException- See Also:
createPatch(JsonWriter)
-
enter
public void enter(SharedGraphNode node, int id)
Description copied from interface:ScopeMixinAssigns the given ID to the given node.- Specified by:
enterin interfaceScopeMixin
-
readData
public void readData(SharedGraphNode node, int id, JsonReader in) throws IOException
Description copied from interface:ScopeReads object data of the given node and assigns the given ID to this node.- Specified by:
readDatain interfaceScope- Specified by:
readDatain interfaceScopeMixin- Parameters:
node- The node to read data for.id- The ID to assign ot the given node.- Throws:
IOException
-
beforeChange
protected void beforeChange()
Hook called before the first change is recorded.The method is only called once during two synchronization cycles.
-
id
public int id(SharedGraphNode node)
Description copied from interface:ScopeMixinLooks up the ID of the given node in thisScope.- Specified by:
idin interfaceScopeMixin- See Also:
ScopeMixin.initId(SharedGraphNode, int)
-
initId
public void initId(SharedGraphNode node, int id)
Description copied from interface:ScopeMixinAssigns the given ID to the given node.- Specified by:
initIdin interfaceScopeMixin- See Also:
ScopeMixin.id(SharedGraphNode)
-
newId
public int newId()
Description copied from interface:ScopeMixinCreates a fresh ID.- Specified by:
newIdin interfaceScopeMixin
-
index
public Map<Object,SharedGraphNode> index()
Description copied from interface:ScopeMixinThe index implementation associating object with IDs.- Specified by:
indexin interfaceScopeMixin
-
-