separator is changeable

This commit is contained in:
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'
version '1.2'
version '1.2.1'
repositories {
mavenCentral()

View File

@ -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
*/

View File

@ -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 <T extends AbstractCommand> 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());
}
}

View File

@ -33,6 +33,7 @@ public final class Sewy {
private static final ReentrantLock LOCK = new ReentrantLock();
private final List<Class<?>> 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() {
}