From 7aa45d2067a05a18dff06cad151d196ebb95e6c6 Mon Sep 17 00:00:00 2001 From: bvn13 Date: Sat, 29 Jan 2022 03:58:07 +0300 Subject: [PATCH] separator is changeable --- build.gradle | 2 +- .../me/bvn13/sewy/AbstractClientListener.java | 25 +++++++++++++------ .../me/bvn13/sewy/CommandClientListener.java | 22 ++++------------ src/main/java/me/bvn13/sewy/Sewy.java | 14 +++++++++++ 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/build.gradle b/build.gradle index ffe86dd..43d6418 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'me.bvn13' -version '1.2' +version '1.2.1' repositories { mavenCentral() diff --git a/src/main/java/me/bvn13/sewy/AbstractClientListener.java b/src/main/java/me/bvn13/sewy/AbstractClientListener.java index ece07a0..3768f49 100644 --- a/src/main/java/me/bvn13/sewy/AbstractClientListener.java +++ b/src/main/java/me/bvn13/sewy/AbstractClientListener.java @@ -25,7 +25,7 @@ import java.net.Socket; import java.util.ArrayList; import java.util.List; -import static me.bvn13.sewy.Sewy.SEPARATOR; +import static me.bvn13.sewy.Sewy.getSeparator; /** * TCP Client listener. @@ -66,7 +66,7 @@ public abstract class AbstractClientListener implements Runnable { * @return the line read from socket */ public String readLine() { - final byte[] bytes = readBytes(SEPARATOR); + final byte[] bytes = readBytes(getSeparator()); final StringBuilder sb = new StringBuilder(); for (byte aByte : bytes) { sb.append((char) aByte); @@ -106,19 +106,30 @@ public abstract class AbstractClientListener implements Runnable { /** * Writes line into socket ending with default separator '\n'. * Flushes after writing. - * @param data data to be sent into socket + * @param bytes bytes to be sent into socket + * @param separator byte to separate data portions */ - public void writeLine(String data) { - if (log.isTraceEnabled()) log.trace("Sending: " + data); + public void writeBytes(byte[] bytes, byte separator) { + if (log.isTraceEnabled()) log.trace("Sending: " + new String(bytes)); try { - out.write(data.getBytes()); - out.write(SEPARATOR); + out.write(bytes); + out.write(separator); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } } + /** + * Writes line into socket ending with default separator '\n'. + * Flushes after writing. + * @param data data to be sent into socket + */ + public void writeLine(String data) { + if (log.isTraceEnabled()) log.trace("Sending: " + data); + writeBytes(data.getBytes(), getSeparator()); + } + /** * Stops client listener gracefully */ diff --git a/src/main/java/me/bvn13/sewy/CommandClientListener.java b/src/main/java/me/bvn13/sewy/CommandClientListener.java index 83006c9..419d025 100644 --- a/src/main/java/me/bvn13/sewy/CommandClientListener.java +++ b/src/main/java/me/bvn13/sewy/CommandClientListener.java @@ -18,12 +18,11 @@ package me.bvn13.sewy; import me.bvn13.sewy.command.AbstractCommand; import org.apache.commons.lang3.SerializationUtils; -import java.io.IOException; import java.io.Serializable; import java.net.Socket; import static java.lang.String.format; -import static me.bvn13.sewy.Sewy.SEPARATOR; +import static me.bvn13.sewy.Sewy.getSeparator; /** * Client listener describing protocol-oriented communication @@ -42,14 +41,14 @@ public class CommandClientListener extends AbstractClientListener implements Abs } while (socket.isConnected()) { Thread.yield(); - byte[] line = readBytes(SEPARATOR); + byte[] line = readBytes(getSeparator()); if (line == null || line.length == 0) { continue; } final Object command; try { command = SerializationUtils.deserialize(line); - } catch (Exception e) { + } catch (Throwable e) { log.warn("Deserialization exception occurred!", e); continue; } @@ -67,12 +66,7 @@ public class CommandClientListener extends AbstractClientListener implements Abs } final Serializable response = onCommand((AbstractCommand) command); log.debug(format("Response for %s is: %s", command, response)); - try { - out.write(SerializationUtils.serialize(response)); - out.write(SEPARATOR); - } catch (IOException e) { - throw new RuntimeException(e); - } + writeBytes(SerializationUtils.serialize(response), getSeparator()); } } @@ -96,13 +90,7 @@ public class CommandClientListener extends AbstractClientListener implements Abs */ public void send(T command) { log.debug("Start to send command: " + command); - try { - out.write(SerializationUtils.serialize(command)); - out.write(SEPARATOR); - out.flush(); - } catch (IOException e) { - throw new RuntimeException(e); - } + writeBytes(SerializationUtils.serialize(command), getSeparator()); } } diff --git a/src/main/java/me/bvn13/sewy/Sewy.java b/src/main/java/me/bvn13/sewy/Sewy.java index c351a35..0092e34 100644 --- a/src/main/java/me/bvn13/sewy/Sewy.java +++ b/src/main/java/me/bvn13/sewy/Sewy.java @@ -33,6 +33,7 @@ public final class Sewy { private static final ReentrantLock LOCK = new ReentrantLock(); private final List> registeredDataTypes = new CopyOnWriteArrayList<>(); + private byte separator = SEPARATOR; /** * Registers command in white list for further communications @@ -54,6 +55,19 @@ public final class Sewy { } } + public static byte getSeparator() { + return getInstance().separator; + } + + public static void setSeparator(byte separator) { + try { + LOCK.lock(); + getInstance().separator = separator; + } finally { + LOCK.unlock(); + } + } + private Sewy() { }