-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
executable file
·119 lines (103 loc) · 2.81 KB
/
Client.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
package myServer2;
/*
* AUTHOR : Min Gao
* Project1-Multi-Server Chat System
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class Client {
private String clientid;
private String room;
private String ownedRoom;
private String serverid;
private Socket clientSocket;
private BufferedWriter writer;
private BufferedReader reader;
private boolean isQuitRequestSend;
public Client(String clientid, String room, String serverid, Socket clientSocket) {
this.clientid = clientid;
this.room = room;
this.ownedRoom = null;
this.serverid = serverid;
this.clientSocket = clientSocket;
isQuitRequestSend = false;
try {
reader = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream(), "UTF-8"));
writer = new BufferedWriter(new OutputStreamWriter(
clientSocket.getOutputStream(), "UTF-8"));
}
catch (IOException e) {
e.printStackTrace();
}
}
public Client(String clientid, String room, String ownedRoom, String serverid, Socket clientSocket) {
this.clientid = clientid;
this.room = room;
this.ownedRoom = ownedRoom;
this.serverid = serverid;
this.clientSocket = clientSocket;
isQuitRequestSend = false;
try {
reader = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream(), "UTF-8"));
writer = new BufferedWriter(new OutputStreamWriter(
clientSocket.getOutputStream(), "UTF-8"));
}
catch (IOException e) {
e.printStackTrace();
}
}
public String getClientid() {
return clientid;
}
public String getRoom() {
return room;
}
public String getOwnedRoom() {
return ownedRoom;
}
public String getServerid() {
return serverid;
}
public Socket getClientSocket() {
return clientSocket;
}
public synchronized String read() throws IOException {
if (reader.ready()) {
// if (reader.readLine() != null) {
return reader.readLine();
}
return null;
}
public void write(String msg) {
try {
writer.write(msg);
writer.newLine();
writer.flush();
}
catch (IOException e) {
ClientMessage quitrequest = new ClientMessage(ServerMessage.quit().toJSONString(), clientid);
if (!isQuitRequestSend) {
System.out.println("A client is disconnected abnormally! Clientid: " + clientid);
MessageQueue.getInstance().add(quitrequest);
isQuitRequestSend = true;
}
//e.printStackTrace();
}
}
public synchronized void createRoom(String roomid) {
this.room = roomid;
this.ownedRoom = roomid;
}
public synchronized void changeRoom(String roomid) {
this.room = roomid;
}
public synchronized void deleteRoom() {
this.ownedRoom = null;
}
}