CSCI 136 |
You will be created a socket client/server program that implements a shared drawing canvas. Clients will connect to a multi-threaded socket server that keeps track of the lines drawn by all the clients. The clients will see (after a short time delay anyway) all the lines drawn by all other clients. Any client can also clear all the lines. |
Lines. The Lines class main job is to store a collection of Line objects. This class is also important as it controls access to the shared data being assessed by the server's worker threads. Hint: think carefully about what methods need to be synchronized in the API. In your multi-threaded server, a single Lines object should be used by all the worker threads. Here is the API:public class Line ---------------------------------------------------------------------------------------- Line(double x0, double y0, double x1, double y1, int r, int g, int b) // Create a line (x0,y0)-(x1,y1) with RGB(r,g,b) String toString() // String representation of the line, format "x0 y0 x1 y1 r g b" void draw() // Draw the line using StdDraw
Communication protocol. Before discussing the other classes you'll implement, it is important to understand the communication protocol that will be used between the client and server. The client and server communicate using a simple text-based protocol. The client always initiates a communication by sending a single line of text to the server. Normally the line is only a command, but in the case of the ADD command, additional parameters specifying the location and color of a line follow the command word. After receiving a command from the client, the server does the appropriate action and sends a single line of text in response.public class Lines ----------------------------------------------------------------------------------------- void add(Line line) // Add a new line to the collection String toString() // Return a single line containing the data for all the lines int size() // Find out how many lines are currently in the collection void clear() // Remove all lines from the collection
Command | Client parameters | Server response |
GET | None |
A single line of text containing fields separated by spaces.
The first field is an integer giving the number of current Line objects in the Lines collection.
The second field is an integer giving the number of currently connected clients.
Following this are 0 or more 7-tuples giving the details of the lines in the collection.
Each 7-tuple consists of 4 floating-point values for the endpoints of the line (x0 y0 x1 y1).
After this come 3 integer values specifying the RGB color of the line.
The RGB color is specified by three integers in [0, 255].
For example, the response for a canvas with 2 lines and 3 connected clients: "2 3 0.65 0.23 0.88 0.5 200 134 89 0.1 0.23 0.33 0.428 12 29 109" |
CLEAR | None | "OK" |
ADD |
A 7-tuple of the form: "x0 y0 x1 y1 r g b" where (x0, y0) and (x1, y1) are the endpoints of the line and (r, g, b) are integer values in [0, 255] specifying the line's color.
For example, to add a green line from the lower-left corner to the upper-right corner: "ADD 0.0 0.0 1.0 1.0 0 0 255 " |
"OK" |
QUIT | None | "OK" |
Extra credit.
Add additional functionality to the shared drawing program. Some ideas:
Submission. Submit your programs Line.java, Lines.java, LineClient.java, LineServer.java and LineServerWorker.java, using Moodle. Be sure each submitted source file has the required header with your name, username, and a description of the program. For extra-credit, please submit a single zip file containing all your programs/graphics/sounds via the special Assignment #6 extra-credit drop box. Page last updated: May 15, 2013 |