From 317a1a894e3afc8be35c22a936d10bb45e3c6c26 Mon Sep 17 00:00:00 2001 From: Agustin Izaguirre Date: Tue, 9 May 2023 19:46:12 -0300 Subject: [PATCH] Changes ISOServer channels map implementation to ConcurrentHashMap. Fixes #508 --- .../src/main/java/org/jpos/iso/ISOServer.java | 40 +++++++++---------- .../test/java/org/jpos/iso/ISOServerTest.java | 18 +++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/ISOServer.java b/jpos/src/main/java/org/jpos/iso/ISOServer.java index d0521189c6..da9bfe8261 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOServer.java +++ b/jpos/src/main/java/org/jpos/iso/ISOServer.java @@ -40,6 +40,7 @@ import java.util.Observer; import java.util.Random; import java.util.Vector; +import java.util.concurrent.ConcurrentHashMap; import org.jpos.core.Configurable; import org.jpos.core.Configuration; @@ -95,7 +96,7 @@ private enum PermLogPolicy { protected Configuration cfg; private boolean shutdown = false; private ServerSocket serverSocket; - private Map channels; + private Map> channels; protected boolean ignoreISOExceptions; protected List serverListeners = null; @@ -118,7 +119,7 @@ public ISOServer(int port, ServerChannel clientSide, ThreadPool pool) { new ThreadPool (1, DEFAULT_MAX_THREADS) : pool; listeners = new Vector(); name = ""; - channels = new HashMap(); + channels = new ConcurrentHashMap<>(); cnt = new int[SIZEOF_CNT]; serverListeners = new ArrayList(); } @@ -759,26 +760,21 @@ public String getCountersAsString (String isoChannelName) { return sb.toString(); } @Override - public void dump (PrintStream p, String indent) { - p.println (indent + getCountersAsString()); - Iterator iter = channels.entrySet().iterator(); - String inner = indent + " "; - for (int i=0; iter.hasNext(); i++) { - Map.Entry entry = (Map.Entry) iter.next(); - WeakReference ref = (WeakReference) entry.getValue(); - ISOChannel c = (ISOChannel) ref.get (); - if (c != null && !LAST.equals (entry.getKey()) && c.isConnected() && c instanceof BaseChannel) { - StringBuilder sb = new StringBuilder (); - int[] cc = ((BaseChannel)c).getCounters(); - sb.append (inner); - sb.append (entry.getKey()); - sb.append (": rx="); - sb.append (Integer.toString (cc[ISOChannel.RX])); - sb.append (", tx="); - sb.append (Integer.toString (cc[ISOChannel.TX])); - sb.append (", last="); - sb.append (Long.toString(lastTxn)); - p.println (sb.toString()); + public void dump(PrintStream p, String indent) { + String counters = getCountersAsString(); + p.println (indent + counters); + + for (Map.Entry> entry : channels.entrySet()) { + String key = entry.getKey(); + //Ignore last as it is a special key with the last channel used and would result in duplication. + if (!LAST.equals(key)) { + ISOChannel c = entry.getValue().get(); + if (c != null && c.isConnected() && c instanceof BaseChannel) { + int[] cc = ((BaseChannel) c).getCounters(); + String line = String.format("%s %s: rx=%d, tx=%d, last=%d", + indent, key, cc[ISOChannel.RX], cc[ISOChannel.TX], lastTxn); + p.println(line); + } } } } diff --git a/jpos/src/test/java/org/jpos/iso/ISOServerTest.java b/jpos/src/test/java/org/jpos/iso/ISOServerTest.java index 8fa379182e..3a92ebd6a2 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOServerTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOServerTest.java @@ -25,8 +25,14 @@ import static org.junit.jupiter.api.Assertions.fail; import org.jpos.util.NameRegistrar; +import org.jpos.util.ThreadPool; import org.junit.jupiter.api.Test; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.ServerSocket; + public class ISOServerTest { @Test @@ -52,4 +58,16 @@ public void testGetServerThrowsNotFoundException() throws Throwable { assertEquals("server.testISOServerName", ex.getMessage(), "ex.getMessage()"); } } + + + @Test + public void testDump() throws Throwable { + ISOServer server = new ISOServer(80, new BaseChannel() {}, new ThreadPool()); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(os); + + server.dump(ps, ""); + + assertEquals("connected=0, rx=0, tx=0, last=0\n", os.toString("UTF8")); + } }