-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerManager.java
More file actions
210 lines (179 loc) · 7.8 KB
/
ServerManager.java
File metadata and controls
210 lines (179 loc) · 7.8 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package readiefur.sockets;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import readiefur.misc.Event;
import readiefur.misc.IDisposable;
import readiefur.misc.ManualResetEvent;
import readiefur.misc.Pair;
//This is taking inspiration from my CSharpTools.Pipes project as the way Java handles networking is similar: https://github.com/ReadieFur/CSharpTools/blob/main/src/CSharpTools.Pipes
public class ServerManager extends Thread implements IDisposable
{
public static final UUID SERVER_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000");
public static final UUID INVALID_UUID = UUID.fromString("FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF");
private final Object lock = new Object();
private int port;
private ManualResetEvent startEvent = new ManualResetEvent(false);
protected Boolean isDisposed = false;
protected ServerSocket server = null;
protected ConcurrentHashMap<UUID, ServerClientHost> servers = new ConcurrentHashMap<>();
/*I can use the final keyword here to make the instance readonly,
*The only reason I wouldn't like to do this is inherited classes wouldn't be able to override this I don't believe.*/
/*You will also notice that I haven't fully capitalized these variables as while they are "constant",
*(not a primitive, known, type at compile time so they're not really), I am using it as a readonly modifier.*/
public final Event<UUID> onConnect = new Event<>();
public final Event<Pair<UUID, Object>> onMessage = new Event<>();
public final Event<UUID> onClose = new Event<>();
public final Event<Pair<UUID, Exception>> onError = new Event<>();
public ServerManager(int port)
{
this.port = port;
}
public void Dispose()
{
//Prevent race conditions.
synchronized (lock)
{
if (isDisposed)
return;
isDisposed = true;
//Disconnect all clients.
for (ServerClientHost serverClientHost : servers.values())
{
try { serverClientHost.Dispose(); }
catch (Exception ex) { onError.Invoke(new Pair<>(SERVER_UUID, ex)); }
}
servers.clear();
//Close the server.
if (server != null)
{
try { server.close(); }
catch (Exception ex) { onError.Invoke(new Pair<>(SERVER_UUID, ex)); }
server = null;
}
//Stop the thread.
if (this.isAlive())
{
try { this.interrupt(); }
catch (Exception ex) { onError.Invoke(new Pair<>(SERVER_UUID, ex)); }
}
//Clear the list of clients.
servers.clear();
onClose.Invoke(SERVER_UUID);
}
}
@Override
public void run()
{
//Try to set the thread name to the class name, not required but useful for debugging.
try { setName(getClass().getSimpleName()); }
catch (Exception e) {}
try
{
try { server = new ServerSocket(port); }
catch (Exception ex)
{
server = null;
onError.Invoke(new Pair<>(SERVER_UUID, ex));
//If the server fails to start, then cancel the startup (done in the while loop below).
}
finally
{
startEvent.Set();
}
while (server != null && !isDisposed && !server.isClosed())
{
try
{
Socket socket = server.accept();
final UUID uuid = GenerateUUID();
ServerClientHost serverClientHost = new ServerClientHost(socket, uuid);
if (servers.putIfAbsent(uuid, serverClientHost) != null)
{
socket.close();
onError.Invoke(new Pair<>(SERVER_UUID, new Exception("Failed to add client to list.")));
}
//We manually fire this event as the constructor for the ServerClientHost class starts the thread immediately and so the registration of the event could be missed.
onConnect.Invoke(uuid);
//These may need encapsulating to maintain access to instance variables.
/*A new limitation has been found, I don't think java has such encapsulation
*and so reading a scoped variable that is not readonly causes an error.
*To work around this I have made the UUID final and then created an external method that generates the UUID as required.*/
serverClientHost.onMessage.Add(obj -> OnMessage(uuid, obj));
serverClientHost.onClose.Add(nul -> OnClose(uuid));
serverClientHost.onError.Add(ex -> OnError(uuid, ex));
}
catch (Exception ex)
{
if (isDisposed || server == null || server.isClosed())
break;
onError.Invoke(new Pair<>(SERVER_UUID, ex));
}
}
}
catch (Exception ex) { onError.Invoke(new Pair<>(SERVER_UUID, ex)); }
/*In my previous tests I had this close the connection and thread as opposed to disposing of the this instance.
*The reason I don't do this anymore is because after a quick look, I found you cannot reuse threads in java.
*I could work around this by creating another wrapper instance but that can be done later if needs be.
*I will be leaving the code for closure in the deconstructor though for good practice and if I need to reimplement it again later.*/
Dispose();
}
public Boolean Start()
{
super.start();
startEvent.WaitOne();
return server != null;
}
public Boolean IsDisposed()
{
return isDisposed;
}
public ConcurrentHashMap<UUID, ServerClientHost> GetClientHosts()
{
return servers;
}
private UUID GenerateUUID()
{
UUID uuid;
//While it is EXTREMELY unlikely that duplicate UUID will ever be made, I like to always place my ID generators in a do/while loop.
do { uuid = UUID.randomUUID(); }
while (servers.containsKey(uuid) || uuid.equals(SERVER_UUID) || uuid.equals(INVALID_UUID));
return uuid;
}
private void OnMessage(UUID uuid, Object data)
{
onMessage.Invoke(new Pair<>(uuid, data));
}
private void OnClose(UUID uuid)
{
try { servers.remove(uuid); }
catch (NullPointerException ex) { /*Ignore as a possible race condition was met.*/ }
onClose.Invoke(uuid);
}
private void OnError(UUID uuid, Exception ex)
{
onError.Invoke(new Pair<>(uuid, ex));
}
//A NullPointerException can occur if the guid is not found or a race condition occurs.
public void SendMessage(UUID uuid, Object data) throws NullPointerException
{
ServerClientHost serverClientHost = servers.getOrDefault(uuid, null);
if (serverClientHost == null)
throw new NullPointerException("The client was not found.");
serverClientHost.SendMessage(data);
}
public void BroadcastMessage(Object data)
{
for (ServerClientHost serverClientHost : servers.values())
serverClientHost.SendMessage(data);
}
public void DisconnectClient(UUID uuid) throws NullPointerException
{
ServerClientHost serverClientHost = servers.getOrDefault(uuid, null);
if (serverClientHost == null)
throw new NullPointerException("The client was not found.");
serverClientHost.Dispose();
//The client will be removed from the servers dictionary in the OnClose method.
}
}