[enocean] Fix concurrency exception on startup (#11408)
* made listeners a ConcurrentHashMap * synchronized access to listeners and eventListeners collections of EnOceanTransceiver * reverted ConcurrentHashMap change as NULL-Key is required Fixes #11393 Also-by: Daniel Weber <uni@fruggy.de> Signed-off-by: Thomas Lauterbach <lauterbachthomas@gmail.com>
This commit is contained in:
parent
d7df7a698d
commit
9cc3f441fd
|
@ -220,6 +220,7 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener {
|
||||||
readingTask = null;
|
readingTask = null;
|
||||||
timeOut = null;
|
timeOut = null;
|
||||||
listeners.clear();
|
listeners.clear();
|
||||||
|
eventListeners.clear();
|
||||||
teachInListener = null;
|
teachInListener = null;
|
||||||
errorListener = null;
|
errorListener = null;
|
||||||
|
|
||||||
|
@ -302,11 +303,13 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
long s = Long.parseLong(HexUtils.bytesToHex(senderId), 16);
|
long s = Long.parseLong(HexUtils.bytesToHex(senderId), 16);
|
||||||
|
synchronized (this) {
|
||||||
HashSet<PacketListener> pl = listeners.get(s);
|
HashSet<PacketListener> pl = listeners.get(s);
|
||||||
if (pl != null) {
|
if (pl != null) {
|
||||||
pl.forEach(l -> l.packetReceived(msg));
|
pl.forEach(l -> l.packetReceived(msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.debug("Received unknown RORG");
|
logger.debug("Received unknown RORG");
|
||||||
}
|
}
|
||||||
|
@ -331,8 +334,10 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
eventListeners.forEach(l -> l.eventReceived(event));
|
eventListeners.forEach(l -> l.eventReceived(event));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Exception in informListeners", e);
|
logger.error("Exception in informListeners", e);
|
||||||
}
|
}
|
||||||
|
@ -374,13 +379,13 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener {
|
||||||
|
|
||||||
protected abstract byte[] serializePacket(BasePacket packet) throws EnOceanException;
|
protected abstract byte[] serializePacket(BasePacket packet) throws EnOceanException;
|
||||||
|
|
||||||
public void addPacketListener(PacketListener listener, long senderIdToListenTo) {
|
public synchronized void addPacketListener(PacketListener listener, long senderIdToListenTo) {
|
||||||
if (listeners.computeIfAbsent(senderIdToListenTo, k -> new HashSet<>()).add(listener)) {
|
if (listeners.computeIfAbsent(senderIdToListenTo, k -> new HashSet<>()).add(listener)) {
|
||||||
logger.debug("Listener added: {}", senderIdToListenTo);
|
logger.debug("Listener added: {}", senderIdToListenTo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removePacketListener(PacketListener listener, long senderIdToListenTo) {
|
public synchronized void removePacketListener(PacketListener listener, long senderIdToListenTo) {
|
||||||
HashSet<PacketListener> pl = listeners.get(senderIdToListenTo);
|
HashSet<PacketListener> pl = listeners.get(senderIdToListenTo);
|
||||||
if (pl != null) {
|
if (pl != null) {
|
||||||
pl.remove(listener);
|
pl.remove(listener);
|
||||||
|
@ -390,11 +395,11 @@ public abstract class EnOceanTransceiver implements SerialPortEventListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEventMessageListener(EventListener listener) {
|
public synchronized void addEventMessageListener(EventListener listener) {
|
||||||
eventListeners.add(listener);
|
eventListeners.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeEventMessageListener(EventListener listener) {
|
public synchronized void removeEventMessageListener(EventListener listener) {
|
||||||
eventListeners.remove(listener);
|
eventListeners.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue