-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSketchServerCommunicator.java
More file actions
185 lines (179 loc) · 7.21 KB
/
SketchServerCommunicator.java
File metadata and controls
185 lines (179 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.io.*;
import java.net.Socket;
import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
/**
* Handles communication between the SketchServer and a single client.
*
* This class is responsible for:
* - Sending the current master sketch (the shared drawing) to a newly connected client.
* - Continuously reading messages from the client, parsing them to update the master sketch,
* and broadcasting the corresponding update to all connected clients.
* - Handling messages for adding (including polyline), moving, recoloring, and deleting shapes.
* - Cleaning up resources when a client disconnects.
*
* The message formats are as follows:
* - "add <shapeType> <id> <x1> <y1> <x2> <y2> <rgb>" for ellipse, rectangle, and segment.
* - "add polyline <numPoints> <x1> <y1> ... <rgb>" for freehand/polyline shapes (the server assigns a unique id).
* - "move <id> <dx> <dy>" for moving a shape.
* - "recolor <id> <rgb>" for recoloring a shape.
* - "delete <id>" for deleting a shape.
*
* @author Triumph Kia Teh | CS10 | Winter 2025
*
*------Professors who provided the scaffold and assignment descriptions------ *
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Fall 2012; revised Winter 2014 to separate SketchServerCommunicator
* @author Tim Pierson Dartmouth CS 10, provided for Winter 2025
*/
public class SketchServerCommunicator extends Thread {
private Socket sock; // Connection to the client
private BufferedReader in; // Input stream from the client
private PrintWriter out; // Output stream to the client
private SketchServer server; // Reference to the main server instance
/**
* Constructor that sets up the communicator with the given client socket and server.
*
* @param sock the client socket
* @param server the main SketchServer instance
*/
public SketchServerCommunicator(Socket sock, SketchServer server) {
this.sock = sock;
this.server = server;
}
/**
* Sends a message to the connected client.
*
* @param msg the message to send
*/
public void send(String msg) {
out.println(msg);
}
/**
* Main method for the communicator thread.
*
* When a client connects, this method:
* - Sends the current master sketch to the client.
* - Enters a loop to read messages from the client.
* - Parses each message and updates the master sketch accordingly.
* - Broadcasts the original (or updated) message to all connected clients.
* - Cleans up and removes the communicator when the client disconnects.
*/
public void run() {
try {
System.out.println("Client connected.");
// Set up input and output streams for communication with the client.
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out = new PrintWriter(sock.getOutputStream(), true);
// Send the current master sketch to the newly connected client.
// For each shape in the master sketch, we prepend its unique ID to the message.
for (Shape s : server.getSketch().getShapes()) {
int id = server.getSketch().getIdForShape(s);
// We assume that the shape's toString() returns a message without the id,
// so we use replaceFirst to insert the id after the shape type.
out.println("add " + s.toString().replaceFirst("^(\\S+)", "$1 " + id));
}
// Continuously read messages from the client.
String msg;
while ((msg = in.readLine()) != null) {
System.out.println("Received from client: " + msg);
// Parse the message using MessageParser.
String[] tokens = MessageParser.parseMessage(msg);
if (tokens.length > 0) {
String command = tokens[0];
if (command.equals("add")) {
// Check if this is a polyline message.
if (tokens[1].equals("polyline")) {
// Expected format from client: "add polyline <numPoints> <x1> <y1> ... <rgb>"
if (tokens.length >= 3) {
int numPoints = Integer.parseInt(tokens[2]);
// Total tokens required: 3 (for "add polyline <numPoints>")
// + (numPoints * 2) for points + 1 token for the rgb value.
if (tokens.length >= 3 + numPoints * 2 + 1) {
List<Point> pts = new ArrayList<>();
int index = 3;
// Parse each point.
for (int i = 0; i < numPoints; i++) {
int px = Integer.parseInt(tokens[index++]);
int py = Integer.parseInt(tokens[index++]);
pts.add(new Point(px, py));
}
int rgb = Integer.parseInt(tokens[index]);
// Create a new polyline shape using the parsed points and color.
Shape s = new Polyline(pts, new Color(rgb));
// Add the shape to the master sketch; this assigns a unique ID.
int id = server.getSketch().addShape(s);
// Rebuild the message to include the unique ID.
StringBuilder newMsg = new StringBuilder();
newMsg.append("add polyline ").append(id).append(" ").append(numPoints);
for (Point p : pts) {
newMsg.append(" ").append(p.x).append(" ").append(p.y);
}
newMsg.append(" ").append(rgb);
// Broadcast the updated message with the unique ID.
server.broadcast(newMsg.toString());
}
}
} else {
// Handle adding ellipse, rectangle, or segment.
if (tokens.length >= 7) {
String shapeType = tokens[1];
int x1 = Integer.parseInt(tokens[2]);
int y1 = Integer.parseInt(tokens[3]);
int x2 = Integer.parseInt(tokens[4]);
int y2 = Integer.parseInt(tokens[5]);
int rgb = Integer.parseInt(tokens[6]);
Shape s = null;
if (shapeType.equals("ellipse")) {
s = new Ellipse(x1, y1, x2, y2, new Color(rgb));
} else if (shapeType.equals("rectangle")) {
s = new Rectangle(x1, y1, x2, y2, new Color(rgb));
} else if (shapeType.equals("segment")) {
s = new Segment(x1, y1, x2, y2, new Color(rgb));
}
if (s != null) {
int id = server.getSketch().addShape(s);
// Build a new message with the unique ID included.
String newMsg = "add " + shapeType + " " + id + " " + x1 + " " + y1 + " " + x2 + " " + y2 + " " + rgb;
server.broadcast(newMsg);
}
}
}
} else if (command.equals("move")) {
// Expected format: "move <id> <dx> <dy>"
if (tokens.length >= 4) {
int id = Integer.parseInt(tokens[1]);
int dx = Integer.parseInt(tokens[2]);
int dy = Integer.parseInt(tokens[3]);
server.getSketch().moveShape(id, dx, dy);
}
} else if (command.equals("recolor")) {
// Expected format: "recolor <id> <rgb>"
if (tokens.length >= 3) {
int id = Integer.parseInt(tokens[1]);
int rgb = Integer.parseInt(tokens[2]);
server.getSketch().recolorShape(id, new Color(rgb));
}
} else if (command.equals("delete")) {
// Expected format: "delete <id>"
if (tokens.length >= 2) {
int id = Integer.parseInt(tokens[1]);
server.getSketch().deleteShape(id);
}
}
}
// Broadcast the original message to all clients.
server.broadcast(msg);
}
// If we exit the loop, the client has disconnected.
server.removeCommunicator(this);
out.close();
in.close();
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}