Skip to content

Commit 769fcc1

Browse files
committed
Better locking and concurrency support for SocketConnection (serial, network, files, usb)
1 parent 38f6434 commit 769fcc1

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

src/qz/utils/FileWatcher.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
import java.io.IOException;
1212
import java.io.RandomAccessFile;
1313
import java.nio.channels.ClosedChannelException;
14-
import java.nio.charset.Charset;
14+
import java.nio.charset.StandardCharsets;
1515
import java.nio.file.*;
1616
import java.util.ArrayList;
1717
import java.util.Collections;
18-
import java.util.HashSet;
18+
import java.util.Set;
19+
import java.util.concurrent.ConcurrentHashMap;
1920

2021
public class FileWatcher {
2122

@@ -24,8 +25,7 @@ public class FileWatcher {
2425
private static Thread watchThread;
2526
private static WatchService watchService;
2627

27-
private static HashSet<FileIO> fileIOs = new HashSet<>();
28-
28+
private static final Set<FileIO> fileIOs = ConcurrentHashMap.newKeySet();
2929

3030
public synchronized static void startWatchThread() throws IOException {
3131
if (watchThread != null && watchThread.isAlive()) {
@@ -59,7 +59,7 @@ public synchronized static void startWatchThread() throws IOException {
5959
watchThread.start();
6060
}
6161

62-
public static void registerWatch(FileIO fileIO) throws IOException {
62+
public synchronized static void registerWatch(FileIO fileIO) throws IOException {
6363
fileIO.setWk(fileIO.getAbsolutePath().register(watchService,
6464
StandardWatchEventKinds.ENTRY_MODIFY,
6565
StandardWatchEventKinds.ENTRY_CREATE,
@@ -73,7 +73,7 @@ public static void deregisterWatch(FileIO fileIO) {
7373
}
7474

7575

76-
private synchronized static void fileChanged(Path path, String fileName, String type) throws ClosedChannelException {
76+
private static void fileChanged(Path path, String fileName, String type) throws ClosedChannelException {
7777
Path filePath = path.resolve(fileName);
7878
for(FileIO fio : fileIOs) {
7979
if (!fio.isMatch(fileName)) continue;
@@ -109,7 +109,7 @@ private synchronized static String getBytes(Path path, FileIO listener) throws I
109109
if (listener.isReversed()) { raf.seek(raf.length() - bytes.length); }
110110
raf.readFully(bytes);
111111

112-
return new String(bytes, Charset.forName("UTF-8"));
112+
return new String(bytes, StandardCharsets.UTF_8);
113113
}
114114
}
115115

@@ -118,7 +118,7 @@ private synchronized static String getLines(Path path, FileIO listener) throws I
118118

119119
String buffer;
120120
if (listener.isReversed()) {
121-
try(ReversedLinesFileReader reader = new ReversedLinesFileReader(path.toFile())) {
121+
try(ReversedLinesFileReader reader = ReversedLinesFileReader.builder().setPath(path).get()) {
122122
int count = 0;
123123
while((buffer = reader.readLine()) != null && count++ != listener.getLines()) {
124124
// Warning, this will strip "\r" from the data

src/qz/ws/SocketConnection.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import java.io.IOException;
1212
import java.nio.file.Path;
13-
import java.util.HashMap;
13+
import java.util.concurrent.ConcurrentHashMap;
1414

1515
public 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

Comments
 (0)