Remove singleton instances once the client is finished

This can prevent unwanted behavior to happen because of missing
initialization
This commit is contained in:
Nagy Attila Gabor 2020-03-28 19:12:37 +01:00
parent f9fce08e6c
commit a3b5c94d65
3 changed files with 23 additions and 1 deletions

View File

@ -139,6 +139,7 @@ public class TCPClient extends StatefulTransportLayerClient {
getInStream().close();
getOutStream().close();
getTcpSocketToServer().close();
releaseInstance();
Thread.currentThread().interrupt();
} catch (IOException e) {
getLogger().error("Error occurred while trying to close TCP socket to server", e);
@ -148,6 +149,10 @@ public class TCPClient extends StatefulTransportLayerClient {
}
}
private static void releaseInstance() {
uniqueTCPClientInstance = null;
}
public Socket getTcpSocketToServer() {
return tcpSocketToServer;
}

View File

@ -211,6 +211,7 @@ public class TLSClient extends StatefulTransportLayerClient {
getInStream().close();
getOutStream().close();
getTlsSocketToServer().close();
releaseInstance();
Thread.currentThread().interrupt();
} catch (IOException e) {
getLogger().error("Error occurred while trying to close TCP socket to server", e);
@ -220,6 +221,10 @@ public class TLSClient extends StatefulTransportLayerClient {
}
}
private static void releaseInstance() {
uniqueTLSClientInstance = null;
}
public SSLSocket getTlsSocketToServer() {
return tlsSocketToServer;

View File

@ -51,7 +51,7 @@ public class UDPClient {
* access the instance variable -> thread safe.
*/
private Logger logger = LogManager.getLogger(this.getClass().getSimpleName());
private static final UDPClient uniqueUDPClientInstance = new UDPClient();
private static UDPClient uniqueUDPClientInstance = new UDPClient();
private int multicastSocketPort;
private Inet6Address multicastAddress;
private MulticastSocket socketToUDPServer;
@ -106,6 +106,13 @@ public class UDPClient {
public static UDPClient getInstance() {
if (uniqueUDPClientInstance == null) {
synchronized (UDPClient.class) {
if (uniqueUDPClientInstance == null) {
uniqueUDPClientInstance = new UDPClient();
}
}
}
return uniqueUDPClientInstance;
}
@ -144,6 +151,11 @@ public class UDPClient {
public void stop() {
getSocketToUDPServer().close();
getLogger().debug("UDP client stopped");
releaseInstance();
}
private static void releaseInstance() {
uniqueUDPClientInstance = null;
}