-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditorCommunicator.java
More file actions
77 lines (68 loc) · 1.92 KB
/
Copy pathEditorCommunicator.java
File metadata and controls
77 lines (68 loc) · 1.92 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
import java.io.*;
import java.net.Socket;
/**
* Handles communication to/from the server for the editor
*
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Fall 2012
* @author Chris Bailey-Kellogg; overall structure substantially revised Winter 2014
* @author Travis Peters, Dartmouth CS 10, Winter 2015; remove EditorCommunicatorStandalone (use echo server for testing)
* @author Tim Pierson Dartmouth CS 10, provided for Winter 2024
* author Godwin Kangor
* @author Ahmed Elmi
*/
public class EditorCommunicator extends Thread {
private PrintWriter out; // to server
private BufferedReader in; // from server
protected Editor editor;
Message messages = new Message();// handling communication for
/**
* Establishes connection and in/out pair
*/
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
*/
public void send(String msg) {
out.println(msg);
}
/**
* Keeps listening for and handling (your code) messages from the server
*/
public void run() {
try {
// Handle messages
// TODO: YOUR CODE HERE
System.out.println("Start drawing!");
String line;
while((line = in.readLine()) != null) {
//System.out.println("Received: " + line);
messages.update(line, editor.getSketch());
editor.repaint();
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
System.out.println("server connections closed");
}
}
// Send editor requests to the server
// TODO: YOUR CODE HERE
public void sendMessage(String s) {
send(s);
}
}