Sewy - is lightweight library for protocol-based communications over network.
Go to file
bvn13 f8727f1032 test github 2022-02-21 02:11:50 +03:00
gradle/wrapper initial commit 2022-01-28 12:49:54 +03:00
src many fixes 2022-02-21 02:07:35 +03:00
.gitignore Initial commit 2022-01-28 12:43:34 +03:00
LICENSE Initial commit 2022-01-28 12:43:34 +03:00
README.md implemented CommandServer 2022-01-29 02:42:11 +03:00
build.gradle test github 2022-02-21 02:11:50 +03:00
gradlew initial commit 2022-01-28 12:49:54 +03:00
gradlew.bat initial commit 2022-01-28 12:49:54 +03:00
settings.gradle initial commit 2022-01-28 12:49:54 +03:00

README.md

Sew'y [səʊi]

What is it?

The lightweight network library providing client-server communications that could be described with commands.

Why another network library?

  1. other protocol-based network libraries are old (in e. kryonet)
  2. or they are heavy and very overloaded

How to use?

Simple ECHO server

  1. Describe ECHOed client listener

380dd5f8b0/src/test/java/me/bvn13/sewy/EchoClientListener.java (L24-L40)

  1. Create Server
new Server("192.168.0.153", port, EchoClientListener.class);

  1. Create Client to communicate with server
Client<SimpleClientListener> client = new Client<>("192.168.0.153", port, SimpleClientListener.class);
  1. Send raw data and read the response
client.writeLine("hello");
String response1 = client.readLine();
Assertions.assertEquals("hello", response1);

Command-based client listener

  1. Implement commands inheriting from AbstractCommand
  2. Register all commands as white listed
Sewy.register(PingCommand.class);
Sewy.register(PongCommand.class);
  1. Start server with CommandClientListener implementing response creation logic
CommandServer server = new CommandServer("localhost", port, (socket) -> new CommandClientListener(socket) {
    @Override
    public AbstractCommand onCommand(AbstractCommand command) {
        if (command instanceof PingCommand) {
            return new PongCommand((PingCommand) command);
        }
        throw new IllegalArgumentException(command.toString());
    }
});
  1. Start client to send commands to server
AtomicLong latency = new AtomicLong(0);
CommandClient client = new CommandClient("localhost", port, (socket) -> new CommandClientListener(socket) {
    @Override
    public AbstractCommand onCommand(AbstractCommand command) {
        if (command instanceof PongCommand) {
            latency.set(((PongCommand)command).getLatency());
            return null;
        } else {
            throw new IllegalArgumentException(command.toString());
        }
    }
});
client.send(new PingCommand());
Thread.sleep(1000);
Assertions.assertTrue(latency.get() > 0);