-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditorCommunicator.java
134 lines (119 loc) · 3.09 KB
/
EditorCommunicator.java
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
import java.awt.*;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/**
* Handles communication to/from the server for the editor
* This class establishes a connection to a server and sends/receives messages.
* It also contains methods to decode and handle incoming messages.
*/
public class EditorCommunicator extends Thread {
private static PrintWriter out; // to server
private BufferedReader in; // from server
protected Editor editor; // handling communication for
/**
* Establishes connection and in/out pair
* @param serverIP The IP address of the server to connect to
* @param editor The editor object associated with this communicator
*/
public EditorCommunicator(String serverIP, Editor editor) {
this.editor = editor;
System.out.println("connecting to " + serverIP + "...");
try {
Socket sock = new Socket(serverIP, 4242);
out = new PrintWriter(sock.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
System.out.println("...connected");
}
catch (IOException e) {
System.err.println("couldn't connect");
System.exit(-1);
}
}
/**
* Sends message to the server
* @param msg The message to be sent
*/
public static void send(String msg) {
out.println(msg);
}
/**
* Decodes and handles messages from the server
* @param msg The message received from the server
*/
public void decoder(String msg)
{
String[] message = msg.split(":");
// method format
// add : ellipse
if (message.length >= 2) {
if (message[0].equals("recolor")) {
ECRecolor(message);
}
if (message[0].equals("delete")) {
ECDelete(message);
}
if (message[0].equals("move")) {
ECMove(message);
}
if (message[0].equals("draw")) {
ECDraw(message);
}
}
}
/**
* Handles recoloring of shapes based on received message
* @param msg The message received from the server
*/
public void ECRecolor(String[] msg)
{
Shape currShape = editor.getSketch().idShapes.get(msg[1]);
currShape.setColor(Color.decode(msg[2]));
}
/**
* Handles deletion of shapes based on received message
* @param msg The message received from the server
*/
public void ECDelete(String[] msg)
{
Shape currShape = editor.getSketch().idShapes.get(msg[1]);
currShape = null;
}
/**
* Handles drawing of shapes based on received message
* @param msg The message received from the server
*/
public void ECDraw(String[] msg)
{
Shape currShape = editor.getSketch().idShapes.get(msg[1]);
currShape.draw(editor.getGraphics());
}
/**
* Handles movement of shapes based on received message
* @param msg The message received from the server
*/
public void ECMove(String[] msg)
{
Shape currShape = editor.getSketch().idShapes.get(msg[1]);
// TO DO:
}
/**
* Listens for and handles messages from the server
*/
public void run() {
try {
// Handle messages
String inline;
while ((inline = in.readLine()) != null)
{
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally {
System.out.println("server hung up");
}
}
}