-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlinePlayer.java
More file actions
93 lines (85 loc) · 2.57 KB
/
OnlinePlayer.java
File metadata and controls
93 lines (85 loc) · 2.57 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
// OnlinePlayer.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
/**
* Minimal online-capable player. The server calls setConnection(...) after
* accepting a socket. If no connection is set, it falls back to console I/O.
*/
public class OnlinePlayer extends Player {
// Network I/O (null when offline)
private Socket socket;
private ObjectOutputStream out;
private ObjectInputStream in;
public OnlinePlayer() {
super();
}
/**
* Used by Server to wire up a freshly accepted socket and streams.
*/
public void setConnection(Socket sock, ObjectInputStream in, ObjectOutputStream out) {
this.socket = sock;
this.in = in;
this.out = out;
}
/**
* Optional: close the connection cleanly (server may never need this).
*/
public void closeConnection() {
try {
if (socket != null)
socket.close();
} catch (Exception ignored) {
}
socket = null;
in = null;
out = null;
}
/**
* Send a message to this player. If connected, goes over the socket;
* otherwise prints to local console.
*/
@Override
public void sendMessage(Object msg) {
if (out != null) {
try {
out.writeObject(String.valueOf(msg));
out.flush();
out.reset(); // prevent memory leak
} catch (Exception e) {
System.err.println("[OnlinePlayer] send error: " + e.getMessage());
}
} else {
// fallback to console
System.out.println(msg);
}
}
/**
* Receive a line of input from this player. If connected, reads from the
* socket;
* otherwise reads from local console.
*/
@Override
public String receiveMessage() {
if (in != null && out != null) {
try {
Object o = in.readObject();
return (o == null) ? null : o.toString();
} catch (Exception e) {
System.err.println("[OnlinePlayer] receive error: " + e.getMessage());
return null;
}
} else {
// fallback to local console
System.out.print("> ");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
return br.readLine();
} catch (Exception e) {
return null;
}
}
}
}