From c846ef17e59835556668a1a0b33107675553928d Mon Sep 17 00:00:00 2001 From: Christian Meusel Date: Wed, 13 Sep 2023 20:06:08 +0200 Subject: [PATCH 1/2] Bump jSerialComm for fixed POSIX signal handling (#138) jSerialComm 2.8.5 overeagerly blocks SIGINT and SIGTERM which prevents normally terminating the Java application/VM. For example, stopping with systemd takes way longer than necessary (when the process gets SIGKILL-ed after a timeout) and shutdown hooks will not be run. SIGTERM and SIGINT are no longer blocked with jSerialComm 2.9.1 and this commit bumps the dependency to the most recent release 2.9.3. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b78f14b..e87666f 100644 --- a/pom.xml +++ b/pom.xml @@ -77,7 +77,7 @@ com.fazecast jSerialComm - 2.8.5 + 2.9.3 From 7be005ff22a7479e690c403f3eec4a9925e3dba7 Mon Sep 17 00:00:00 2001 From: Muhammed Kurt Date: Wed, 13 Sep 2023 21:11:37 +0300 Subject: [PATCH 2/2] modbus slave factory getSlave fix (#128) Co-authored-by: Muhammed Kurt --- .../modbus/slave/ModbusSlaveFactory.java | 37 ++++++++++--------- .../j2mod/modbus/slave/ModbusSlaveType.java | 13 +++++++ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java b/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java index 68c72e2..2c7d648 100644 --- a/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java +++ b/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveFactory.java @@ -35,7 +35,7 @@ */ public class ModbusSlaveFactory { - private static final Map slaves = new HashMap(); + private static final Map slaves = new HashMap<>(); /** * Prevent instantiation @@ -143,7 +143,7 @@ public static synchronized ModbusSlave createUDPSlave(InetAddress address, int p * @throws ModbusException If a problem occurs e.g. port already in use */ public static synchronized ModbusSlave createSerialSlave(SerialParameters serialParams) throws ModbusException { - ModbusSlave slave = null; + ModbusSlave slave; if (serialParams == null) { throw new ModbusException("Serial parameters are null"); } @@ -152,9 +152,8 @@ else if (ModbusUtil.isBlank(serialParams.getPortName())) { } // If we have a slave already assigned to this port - if (slaves.containsKey(serialParams.getPortName())) { - slave = slaves.get(serialParams.getPortName()); - + slave = getSlave(ModbusSlaveType.SERIAL, serialParams.getPortName()); + if (slave != null) { // Check if any of the parameters have changed if (!serialParams.toString().equals(slave.getSerialParams().toString())) { close(slave); @@ -165,8 +164,7 @@ else if (ModbusUtil.isBlank(serialParams.getPortName())) { // If we don;t have a slave, create one if (slave == null) { slave = new ModbusSlave(serialParams); - slaves.put(serialParams.getPortName(), slave); - return slave; + slaves.put(ModbusSlaveType.SERIAL.getKey(serialParams.getPortName()), slave); } return slave; } @@ -176,40 +174,44 @@ else if (ModbusUtil.isBlank(serialParams.getPortName())) { * * @param slave Slave to remove */ - public static void close(ModbusSlave slave) { + public static synchronized void close(ModbusSlave slave) { if (slave != null) { slave.closeListener(); - slaves.remove(slave.getType().getKey(slave.getPort())); + if (slave.getType().is(ModbusSlaveType.SERIAL)) { + slaves.remove(slave.getType().getKey(slave.getSerialParams().getPortName())); + } else { + slaves.remove(slave.getType().getKey(slave.getPort())); + } } } /** * Closes all slaves and removes them from the running list */ - public static void close() { - for (ModbusSlave slave : new ArrayList(slaves.values())) { + public static synchronized void close() { + for (ModbusSlave slave : new ArrayList<>(slaves.values())) { slave.close(); } } /** - * Returns the running slave listening on the given IP port + * Returns the running slave listening on the given port * * @param port Port to check for running slave * @return Null or ModbusSlave */ - public static ModbusSlave getSlave(int port) { - return slaves.get(port + ""); + public static synchronized ModbusSlave getSlave(ModbusSlaveType type, int port) { + return type == null ? null : slaves.get(type.getKey(port)); } /** - * Returns the running slave listening on the given serial port + * Returns the running slave listening on the given port * * @param port Port to check for running slave * @return Null or ModbusSlave */ - public static ModbusSlave getSlave(String port) { - return ModbusUtil.isBlank(port) ? null : slaves.get(port); + public static synchronized ModbusSlave getSlave(ModbusSlaveType type, String port) { + return type == null || ModbusUtil.isBlank(port) ? null : slaves.get(type.getKey(port)); } /** @@ -226,5 +228,4 @@ public static synchronized ModbusSlave getSlave(AbstractModbusListener listener) } return null; } - } diff --git a/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveType.java b/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveType.java index c993d15..5d2f89c 100644 --- a/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveType.java +++ b/src/main/java/com/ghgande/j2mod/modbus/slave/ModbusSlaveType.java @@ -49,4 +49,17 @@ public boolean is(ModbusSlaveType... types) { public String getKey(int port) { return toString() + port; } + + /** + * Returns a unique key for this port and type + * + * @param port Port number + * @return Unique key + */ + public String getKey(String port) { + if (ModbusUtil.isBlank(port)) { + throw new IllegalArgumentException("Port must not be null or empty"); + } + return this + port; + } }