In this part we will see how the state is replicated and kept consistent between DVDraw applets participating in the same session.
Replication of The State What does the state consist of?
The whole state is kept inside a single object of the class ShpsState. ShpsState mostly consists of a vector containing all the shapes in the current drawing.
DistView is a toolkit for building synchronous groupware applications.
DVDraw extends the class DVApplet in which is implemented the necessary code for joining a session.
DVDraw uses a DVObject to communicate with other DVDraw in the same group. When the applet join a group, it receives all the messages broadcast in the group since it was created.Through the DVObject, the applet broadcasts messages and receives messages to/ from the group.
The Corona server is a general-purpose server. It takes care of queuing messages for stateful groups, it also takes care of group membership.
The server does not understand DVDraw messages. For that matter issues like locks or names must be handled on the client’s side in a distributed way.
The operation and the name of the objects on which it was performed are being broadcast Another approach would have been to broadcast all the attributes changed by an operation, but this would lead to larger messages in which the semantic of the operation performed would be lost.
For this to work objects need to be named in a unique way.
The simplest approach would be to pick for the ID a random number in a large space hoping that no collision will occur.
An improved version would be to have IDs of the form <applet ID, n> if the object was the nth object created by the applet, with the applet ID being chosen randomly in a large space
Objects have IDs of the form <applet ID, n>.
Applets receive an ID (never used before) every time they join a session.
There is an integer lastAppletIDAssigned part of the state that reflects the last applet id assigned.
After it has joined a group, an applet broadcasts the message "Add User - user name"
It waits until it receives its own "Add User" message back. And then picks lastAppletIDAssigned to be its ID.
When an applet receives a message "Add User", it increments the lastAppletIDAssigned variable.
An applet ends up with the ID n if the nth message of type "Add user" it saw was the one it sent. If two applets were to end up with the same ID it would mean that the nth messages they have received were different, but this can not happen since every applet in a session receives the same messages in the same order. Therefore two applets can not end up with the same ID.
One extreme approach would be to execute an operation when it comes back from the server. Since all applets in a session receives the same messages in the same order (messages are totally ordered) state would be maintained consistent all the time.
This approach could result in a possibly high response time.
The optimistic approach would consist into executing locally operations and then make necessary correction when operations are received from the server. This approach could result into operations being performed and later on undone.
These two approaches are not very user friendly.
DVDraw uses total ordering when necessary and locks otherwise.
Operations that affect the stack order are totally ordered. The state is modified when the message is received back.
Operations affecting the stack order are create, paste, group, move to front, and move to back.
One must select an object before performing any operation on it.
When one tries to select an object, an "Acquire object ID, requester ID" message is broadcast to the group.
The applet waits until it receives its own "Acquire" back. If the object’s owner is now the current applet, the object gets selected.
When an "Acquire" message is received, if the object is not owned by anyone, the requester becomes the owner. The display is updated to reflect the fact that the object is now owned.
The total ordering of the messages received from the server guarantees that an object can never be locked by two applets at the same time.