44import java .net .ServerSocket ;
55import java .net .Socket ;
66import java .util .LinkedList ;
7- import java .util .List ;
87import java .util .UUID ;
98
109class ServerSocketAcceptingThread extends Thread {
1110
1211 private final ServerSocket serverSocket ;
13- private final List <Client > clients ;
14-
15- {
16- this .clients = new LinkedList <>();
17- }
12+ private final LinkedList <Client > clients = new LinkedList <>();
1813
1914 public ServerSocketAcceptingThread (final ServerSocket serverSocket ) {
2015 this .serverSocket = serverSocket ;
@@ -23,58 +18,58 @@ public ServerSocketAcceptingThread(final ServerSocket serverSocket) {
2318 @ Override
2419 public void run () {
2520 super .run ();
26-
2721 try {
2822 while (true ) {
2923 if (this .serverSocket .isClosed ()) {
3024 this .interrupt ();
31- break ;
25+ return ;
3226 }
3327 //initialise new client socket
3428 final Socket socket = this .serverSocket .accept ();
3529 final Client client = new Client (socket );
3630 client .connect ();
3731 this .clients .add (client );
38- //update connectionUUID on clioent side
39- client .send (new UpdateUUIDPacket (client .getConnectionUUID ().get ()));
32+ //update connectionUUID on client side
33+ final UpdateUUIDPacket updateUUIDPacket = new UpdateUUIDPacket (client .getConnectionUUID ().get ());
34+ client .send (updateUUIDPacket );
4035 }
41- } catch (final IOException e ) {
42- e .printStackTrace ();
36+ } catch (final IOException exception ) {
37+ exception .printStackTrace ();
4338 }
4439 }
4540
4641 public void sendToClient (final Packet packet , final UUID uuid ) {
4742 //send to client
48- for (final Client client : this .clients ) {
49- if (!client .getConnectionUUID ().get ().equals (uuid )) {
50- continue ;
51- }
52- client .send (packet );
53- }
43+ this .clients .stream ().filter (client -> client .getConnectionUUID ().get ().equals (uuid )).forEach (client -> client .send (packet ));
5444 }
5545
5646 public void sendToAllClients (final Packet packet ) {
5747 //send to all clients
58- for (final Client client : this .clients ) {
59- client .send (packet );
60- }
48+ this .clients .forEach (client -> client .send (packet ));
6149 }
6250
63- public void disconnectClient (final UUID uuid ) throws IOException {
51+ public void disconnectClient (final UUID uuid ) {
6452 //disconnect client
65- for (final Client client : this .clients ) {
66- if (!client .getConnectionUUID ().get ().equals (uuid )) {
67- continue ;
53+ this .clients .stream ().filter (client -> client .getConnectionUUID ().get ().equals (uuid )).forEach (client -> {
54+ try {
55+ client .disconnect ();
56+ } catch (IOException exception ) {
57+ exception .printStackTrace ();
6858 }
69- client .disconnect ();
70- }
59+ });
7160 }
7261
73- public void disconnectAllClients () throws IOException {
62+ public void disconnectAllClients () {
7463 //disconnect all clients
75- for (final Client client : this .clients ) {
76- client .disconnect ();
77- }
64+ this .clients .forEach (client -> {
65+ try {
66+ if (client != null ){
67+ client .disconnect ();
68+ }
69+ } catch (IOException exception ) {
70+ exception .printStackTrace ();
71+ }
72+ });
7873 this .clients .clear ();
7974 }
8075
0 commit comments