diff options
author | dweller <dweller@cabin.digital> | 2024-03-09 00:55:36 +0200 |
---|---|---|
committer | dweller <dweller@cabin.digital> | 2024-03-09 00:55:36 +0200 |
commit | 86d3f93ee338b28ab7d40aa83c129cf6b97ef4b7 (patch) | |
tree | 507a8d66932e6dea9b121dfcbf980f7925575c9f /java/sources/server.java |
Initial commit, 2 years later
Diffstat (limited to '')
-rw-r--r-- | java/sources/server.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/java/sources/server.java b/java/sources/server.java new file mode 100644 index 0000000..0ba1127 --- /dev/null +++ b/java/sources/server.java @@ -0,0 +1,82 @@ +import digital.cabin.ezipc.EzIPCServer; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; + + +class EzIPCServerTest +{ + private static final String PIPE_PATH = "/tmp/ezi_conn_test"; + + public static void main(String[] args) + { + System.out.println("EZIPC Java Server"); + + try + { + EzIPCServer srv = new EzIPCServer("/tmp/ezi_conn_test"); + + System.out.print("Opening connection..."); + srv.open(); + System.out.println(" DONE!"); + + ByteBuffer msg_ok = ByteBuffer.allocate(132); + msg_ok.order(ByteOrder.nativeOrder()); + msg_ok.putInt(1); + + ByteBuffer msg_exit = ByteBuffer.allocate(132); + msg_exit.order(ByteOrder.nativeOrder()); + msg_exit.putInt(2); + + while(true) + { + byte[] in = new byte[132]; + srv.receive(in); + + ByteBuffer msg_in = ByteBuffer.wrap(in); + msg_in.order(ByteOrder.nativeOrder()); + + int type = msg_in.getInt(); + if(type == 0) + { + int end = 4; + for(; end < 132; end++) if(in[end] == 0) break; + + String msg = new String(in, 4, end - 4, + StandardCharsets.UTF_8); + + System.out.println("received: " + msg); + + srv.send(msg_ok.array()); + } + else if(type == 1) + { + System.out.println("received spurious OK"); + srv.send(msg_exit.array()); + } + else if(type == 2) + { + System.out.println("received EXIT"); + srv.send(msg_exit.array()); + break; + } + else + { + System.out.println("received unkown msg type:" + type); + srv.send(msg_exit.array()); + } + } + + srv.close(); + } + catch (Exception ex) + { + System.out.println(ex.getMessage()); + System.exit(1); + } + + System.out.println("bye..."); + } +} + |