separator is changeable

master sewy-1.2.1
bvn13 2022-01-29 03:58:07 +03:00
parent f89587c393
commit 7aa45d2067
4 changed files with 38 additions and 25 deletions

View File

@ -5,7 +5,7 @@ plugins {
} }
group 'me.bvn13' group 'me.bvn13'
version '1.2' version '1.2.1'
repositories { repositories {
mavenCentral() mavenCentral()

View File

@ -25,7 +25,7 @@ import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static me.bvn13.sewy.Sewy.SEPARATOR; import static me.bvn13.sewy.Sewy.getSeparator;
/** /**
* TCP Client listener. * TCP Client listener.
@ -66,7 +66,7 @@ public abstract class AbstractClientListener implements Runnable {
* @return the line read from socket * @return the line read from socket
*/ */
public String readLine() { public String readLine() {
final byte[] bytes = readBytes(SEPARATOR); final byte[] bytes = readBytes(getSeparator());
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
for (byte aByte : bytes) { for (byte aByte : bytes) {
sb.append((char) aByte); sb.append((char) aByte);
@ -106,19 +106,30 @@ public abstract class AbstractClientListener implements Runnable {
/** /**
* Writes line into socket ending with default separator '\n'. * Writes line into socket ending with default separator '\n'.
* Flushes after writing. * 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) { public void writeBytes(byte[] bytes, byte separator) {
if (log.isTraceEnabled()) log.trace("Sending: " + data); if (log.isTraceEnabled()) log.trace("Sending: " + new String(bytes));
try { try {
out.write(data.getBytes()); out.write(bytes);
out.write(SEPARATOR); out.write(separator);
out.flush(); out.flush();
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(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 * Stops client listener gracefully
*/ */

View File

@ -18,12 +18,11 @@ package me.bvn13.sewy;
import me.bvn13.sewy.command.AbstractCommand; import me.bvn13.sewy.command.AbstractCommand;
import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.net.Socket; import java.net.Socket;
import static java.lang.String.format; 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 * Client listener describing protocol-oriented communication
@ -42,14 +41,14 @@ public class CommandClientListener extends AbstractClientListener implements Abs
} }
while (socket.isConnected()) { while (socket.isConnected()) {
Thread.yield(); Thread.yield();
byte[] line = readBytes(SEPARATOR); byte[] line = readBytes(getSeparator());
if (line == null || line.length == 0) { if (line == null || line.length == 0) {
continue; continue;
} }
final Object command; final Object command;
try { try {
command = SerializationUtils.deserialize(line); command = SerializationUtils.deserialize(line);
} catch (Exception e) { } catch (Throwable e) {
log.warn("Deserialization exception occurred!", e); log.warn("Deserialization exception occurred!", e);
continue; continue;
} }
@ -67,12 +66,7 @@ public class CommandClientListener extends AbstractClientListener implements Abs
} }
final Serializable response = onCommand((AbstractCommand) command); final Serializable response = onCommand((AbstractCommand) command);
log.debug(format("Response for %s is: %s", command, response)); log.debug(format("Response for %s is: %s", command, response));
try { writeBytes(SerializationUtils.serialize(response), getSeparator());
out.write(SerializationUtils.serialize(response));
out.write(SEPARATOR);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
} }
@ -96,13 +90,7 @@ public class CommandClientListener extends AbstractClientListener implements Abs
*/ */
public <T extends AbstractCommand> void send(T command) { public <T extends AbstractCommand> void send(T command) {
log.debug("Start to send command: " + command); log.debug("Start to send command: " + command);
try { writeBytes(SerializationUtils.serialize(command), getSeparator());
out.write(SerializationUtils.serialize(command));
out.write(SEPARATOR);
out.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
} }

View File

@ -33,6 +33,7 @@ public final class Sewy {
private static final ReentrantLock LOCK = new ReentrantLock(); private static final ReentrantLock LOCK = new ReentrantLock();
private final List<Class<?>> registeredDataTypes = new CopyOnWriteArrayList<>(); private final List<Class<?>> registeredDataTypes = new CopyOnWriteArrayList<>();
private byte separator = SEPARATOR;
/** /**
* Registers command in white list for further communications * 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() { private Sewy() {
} }