-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientState.java
executable file
·77 lines (62 loc) · 1.69 KB
/
ClientState.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
package myServer2;
/*
* AUTHOR : Min Gao
* Project1-Multi-Server Chat System
*/
import java.util.ArrayList;
public class ClientState {
private static ClientState instance;
private ArrayList<Client> clientList;
private ArrayList<String> lockClientList;
private ClientState() {
clientList = new ArrayList<>();
lockClientList = new ArrayList<>();
}
public synchronized static ClientState getInstance() {
if(instance == null) {
instance = new ClientState();
}
return instance;
}
public synchronized Client getClient(String clientid) {
for (Client client : clientList) {
if (client.getClientid().equals(clientid)) {
return client;
}
}
return null;
}
public synchronized ArrayList<Client> getClientList() {
return clientList;
}
public synchronized boolean isClientidExist(String clientid) {
if (lockClientList.contains(clientid)) {
return true;
}
if (getClient(clientid) != null) {
return true;
}
return false;
}
public synchronized void addClient(Client client) {
clientList.add(client);
}
public synchronized void removeClient(Client client) {
clientList.remove(client);
}
public synchronized void addLockClient(String clientid) {
lockClientList.add(clientid);
}
public synchronized void releaseLockClient(String clientid) {
lockClientList.remove(clientid);
}
public synchronized ArrayList<String> getAllClientList() {
ArrayList<Client> allClient = new ArrayList<>();
allClient.addAll(clientList);
ArrayList<String> allClientid = new ArrayList<>();
for (Client client : allClient) {
allClientid.add(client.getClientid());
}
return allClientid;
}
}