1010
1111import java .io .IOException ;
1212import java .nio .file .Path ;
13- import java .util .HashMap ;
13+ import java .util .concurrent . ConcurrentHashMap ;
1414
1515public class SocketConnection {
1616
@@ -19,18 +19,18 @@ public class SocketConnection {
1919
2020 private Certificate certificate ;
2121
22- private DeviceListener deviceListener ;
22+ private volatile DeviceListener deviceListener ;
2323
2424 // serial port -> open SerialIO
25- private final HashMap <String ,SerialIO > openSerialPorts = new HashMap <>();
25+ private final ConcurrentHashMap <String ,SerialIO > openSerialPorts = new ConcurrentHashMap <>();
2626 // socket 'host:port' -> open ProtocolIO
27- private final HashMap <String ,SocketIO > openNetworkSockets = new HashMap <>();
27+ private final ConcurrentHashMap <String ,SocketIO > openNetworkSockets = new ConcurrentHashMap <>();
2828
2929 // absolute path -> open file listener
30- private final HashMap <Path ,FileIO > openFiles = new HashMap <>();
30+ private final ConcurrentHashMap <Path ,FileIO > openFiles = new ConcurrentHashMap <>();
3131
3232 // DeviceOptions -> open DeviceIO
33- private final HashMap <DeviceOptions ,DeviceIO > openDevices = new HashMap <>();
33+ private final ConcurrentHashMap <DeviceOptions ,DeviceIO > openDevices = new ConcurrentHashMap <>();
3434
3535
3636 public SocketConnection (Certificate cert ) {
@@ -46,28 +46,28 @@ public void setCertificate(Certificate newCert) {
4646 }
4747
4848
49- public void addSerialPort (String port , SerialIO io ) {
49+ public synchronized void addSerialPort (String port , SerialIO io ) {
5050 openSerialPorts .put (port , io );
5151 }
5252
53- public SerialIO getSerialPort (String port ) {
53+ public synchronized SerialIO getSerialPort (String port ) {
5454 return openSerialPorts .get (port );
5555 }
5656
57- public void removeSerialPort (String port ) {
57+ public synchronized void removeSerialPort (String port ) {
5858 openSerialPorts .remove (port );
5959 }
6060
6161
62- public void addNetworkSocket (String location , SocketIO io ) {
62+ public synchronized void addNetworkSocket (String location , SocketIO io ) {
6363 openNetworkSockets .put (location , io );
6464 }
6565
66- public SocketIO getNetworkSocket (String location ) {
66+ public synchronized SocketIO getNetworkSocket (String location ) {
6767 return openNetworkSockets .get (location );
6868 }
6969
70- public void removeNetworkSocket (String location ) {
70+ public synchronized void removeNetworkSocket (String location ) {
7171 openNetworkSockets .remove (location );
7272 }
7373
@@ -87,37 +87,38 @@ public void stopDeviceListening() {
8787 deviceListener = null ;
8888 }
8989
90- public void addFileListener (Path absolute , FileIO listener ) {
90+ public synchronized void addFileListener (Path absolute , FileIO listener ) {
9191 openFiles .put (absolute , listener );
9292 }
9393
94- public FileIO getFileListener (Path absolute ) {
94+ public synchronized FileIO getFileListener (Path absolute ) {
9595 return openFiles .get (absolute );
9696 }
9797
98- public void removeFileListener (Path absolute ) {
98+ public synchronized void removeFileListener (Path absolute ) {
9999 openFiles .remove (absolute );
100100 }
101101
102- public void removeAllFileListeners () {
103- for (Path path : openFiles .keySet ()) {
104- openFiles .get (path ).close ();
105- FileWatcher .deregisterWatch (openFiles .get (path ));
102+ public synchronized void removeAllFileListeners () {
103+ for (FileIO io : openFiles .values ()) {
104+ if (io != null ) {
105+ io .close ();
106+ FileWatcher .deregisterWatch (io );
107+ }
106108 }
107-
108109 openFiles .clear ();
109110 }
110111
111112
112- public void addDevice (DeviceOptions dOpts , DeviceIO io ) {
113+ public synchronized void addDevice (DeviceOptions dOpts , DeviceIO io ) {
113114 openDevices .put (dOpts , io );
114115 }
115116
116- public DeviceIO getDevice (DeviceOptions dOpts ) {
117+ public synchronized DeviceIO getDevice (DeviceOptions dOpts ) {
117118 return openDevices .get (dOpts );
118119 }
119120
120- public void removeDevice (DeviceOptions dOpts ) {
121+ public synchronized void removeDevice (DeviceOptions dOpts ) {
121122 openDevices .remove (dOpts );
122123 }
123124
0 commit comments