-
Notifications
You must be signed in to change notification settings - Fork 0
Client's Model
The Model module is the central part of the architecture, in the sense that it is the only module that every other module is directly connected to. It is mainly responsible for maintaining and updating all kinds of states and handling requests from other modules. The Model consists of three classes: TimerThread, CanvasInteraction and AdminInteraction.
As shown in the figure below the client has two threads. One is the GUI thread which is actually the implementation for phase one with little changes, where the user can click buttons to achieve function, and use the mouse to operate on canvas. The unit task for the timer thread is to get the pixel differences on canvas and send those differences to the server and then receive messages from the server and update the canvas according to the received message. The second Thread is the timer thread which is a daemon thread and executes the unit task periodically.
Inside the timer thread, two queues are maintained, which are indicated in the figure below. Every message will be sent once, in order to make sure that the server has received all the messages again. Every sent message will be recorded in the sending queue. The timer thread will acquire receiving queue from network module periodically. Once sent message was found in the receive queue which means that this message is already read by the server, then remove this message from the sending queue.
The CanvasInteraction class is responsible for all the operations related to canvases. When a user uses a drawing tool such as a pen or eraser, the Controller (mouse event listener) is triggered, and it calls a corresponding method in the CanvasInteraction to draw. Apart from the Controller, the Network module can also call drawing methods in the CanvasInteraction. It does that whenever it receives a drawing message from the server. Instead of doing actual drawing, the CanvasInteraction calls a corresponding method in the View (Draw class) to draw on the canvas.
To which layer it will be drawn is determined by the CanvasInteraction. The Controller can only update temporary layer and local layer, whereas the Network module can only update network layer. As long as all the local drawing has been confirmed by the server, the local layer will be completely cleared. In this way, all the problems related to canvas concurrency have been solved.
This class needs to be initialized with a Network module object. The “AdminInteraction” class includes the method that creates and sends the message carried with the message handlers. This class serves all kinds of Messages except the DRAW_OPERATION type. Served message types related with create a new canvas, load a canvas, save and save as, close. “AdminInteraction” is used by both button event handler and the background timer thread.