Systems/Collaborative Editor
48 / 67

03/2021Systems

Collaborative Editor

A shared drawing canvas in Java where multiple clients edit in real time, kept consistent by a server that serializes every change.

╌╌╌╌

A collaborative drawing editor over a shared canvas. Multiple clients connect at once, and each sees the others' edits in real time.

The server is the single source of truth. SketchServer listens on port 4242 and holds the authoritative Sketch, a TreeMap<Integer, Shape> keyed by shape id. A client's Editor never mutates shared state directly: it sends a one-line message — draw <type> x1 y1 x2 y2 color, move id dx dy, recolor id color, or delete id — over its EditorCommunicator. The server stamps each new shape with an incrementing id, applies the change through MessageParser, then broadcasts it to every client, the originator included, so all views converge. A client that joins mid-session first receives the entire current sketch replayed as draw messages, then the stream of later edits.

Each Editor sends an edit over its EditorCommunicator to the SketchServer, which owns the authoritative Sketch. The server assigns the shape an id, applies the edit through MessageParser, and broadcasts it back to every connected client so all canvases converge.

Concurrency stays safe because the server dedicates one SketchServerCommunicator thread to each client, so edits can arrive at the same instant. The methods that touch shared state — addCommunicator, removeCommunicator, and broadcast — are synchronized, so one thread finishes applying an edit and broadcasting it before the next begins. Serializing writes this way prevents the data races that concurrent updates to a single sketch would otherwise cause, and routing every change through the one server sidesteps the deadlock that competing locks on shared state could invite.

References

  1. Project repository

╌╌ END ╌╌