Class DefaultScope

java.lang.Object
de.haumacher.msgbuf.graph.DefaultScope
All Implemented Interfaces:
Scope, ScopeMixin, Listener

public class DefaultScope extends Object implements Listener, ScopeMixin
The default Scope of an AbstractSharedGraphNode.

After adding this instance as listener to a node, the node is observed for changes. Changes can be externalized to a patch and transmitted over network to another DefaultScope containing a corresponding instance of the node. There, the patch an be applied bringing both shared object graphs in sync.

  • Constructor Details

    • DefaultScope

      public DefaultScope(int totalParticipants, int participantId)
      Creates a DefaultScope.
      Parameters:
      totalParticipants - The total number of participants operating on the shared graph.
      participantId - The ID of this participant.
  • Method Details

    • newServerInstance

      public static DefaultScope newServerInstance()
      Creates a DefaultScope for the server, where the graph is only shared between two participants, the client and the server.
    • newClientInstance

      public static DefaultScope newClientInstance()
      Creates a DefaultScope for the client , where the graph is only shared between two participants, the client and the server.
    • beforeSet

      public void beforeSet(Observable obj, String property, Object value)
      Description copied from interface: Listener
      Informs this Listener that the value of the given property is to be modified.
      Specified by:
      beforeSet in interface Listener
      Parameters:
      obj - The Observable object.
      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: Listener
      Informs this Listener that an element will be added to the repeated property with the given name.
      Specified by:
      beforeAdd in interface Listener
      Parameters:
      obj - The Observable object.
      property - The name of the property to be modified.
      index - The index where the new element will be added if the property is ordered, -1 otherwise.
      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: Listener
      Informs this Listener that an element was removed from a repeated property with the given name.
      Specified by:
      afterRemove in interface Listener
      Parameters:
      obj - The Observable object.
      property - The name of the property to be modified.
      index - The index where the element was removed if the property is ordered, -1 otherwise.
      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.
    • createPatch

      public void createPatch(JsonWriter json) throws IOException
      Exports recorded changes to the given JsonWriter.

      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

      public void applyChanges(JsonReader json) throws IOException
      Applies changes read from the given JsonReader.

      It is expected that the patch has the format as in createPatch(JsonWriter).

      Throws:
      IOException
      See Also:
    • enter

      public void enter(SharedGraphNode node, int id)
      Description copied from interface: ScopeMixin
      Assigns the given ID to the given node.
      Specified by:
      enter in interface ScopeMixin
    • readData

      public void readData(SharedGraphNode node, int id, JsonReader in) throws IOException
      Description copied from interface: Scope
      Reads object data of the given node and assigns the given ID to this node.
      Specified by:
      readData in interface Scope
      Specified by:
      readData in interface ScopeMixin
      Parameters:
      node - The node to read data for.
      id - The ID to assign ot the given node.
      Throws:
      IOException
    • id

      public int id(SharedGraphNode node)
      Description copied from interface: ScopeMixin
      Looks up the ID of the given node in this Scope.
      Specified by:
      id in interface ScopeMixin
      See Also:
    • initId

      public void initId(SharedGraphNode node, int id)
      Description copied from interface: ScopeMixin
      Assigns the given ID to the given node.
      Specified by:
      initId in interface ScopeMixin
      See Also:
    • newId

      public int newId()
      Description copied from interface: ScopeMixin
      Creates a fresh ID.
      Specified by:
      newId in interface ScopeMixin
    • index

      public Map<Object,SharedGraphNode> index()
      Description copied from interface: ScopeMixin
      The index implementation associating object with IDs.
      Specified by:
      index in interface ScopeMixin