-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatMember.java
More file actions
64 lines (62 loc) · 1.91 KB
/
ChatMember.java
File metadata and controls
64 lines (62 loc) · 1.91 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
import java.io.IOException;
import java.util.Objects;
public class ChatMember implements Runnable, Comparable<ChatMember> { //знает, как связаться с участником чата
private final String name;
private final Connection connection;
private final ChatServer server;
private final int hashCode;
boolean repeat = true;
public ChatMember(String name, Connection connection, ChatServer server) {
this.name = name;
this.connection = connection;
this.server = server;
this.hashCode = Objects.hash(name);
}
public void disconnect() throws IOException {
repeat = false;
connection.close();
}
public void send(String message) { //передаём сообщение через соединение
try {
connection.sendString(message);
} catch (Exception e) {
server.removeMember(this);
e.printStackTrace();
}
}
@Override
public void run() {
while(repeat) {
try {
server.processMessage(this, connection.getString()); //ожидаем новой строки от клиента
} catch (Exception e) {
server.removeMember(this);
//e.printStackTrace();
}
}
}
public String getName() {
return name;
}
public boolean isMe(Object o) {
return this == o;
}
private boolean likeMe(Object o) {
return this.getClass() == o.getClass();
}
@Override
public boolean equals(Object o) {
if (o!=null)
if(likeMe(o))
return isMe(o) || this.name.equals(((ChatMember) o).getName());
return false;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public int compareTo(ChatMember o) {
return this.name.compareTo(o.getName());
}
}