From 86d3f93ee338b28ab7d40aa83c129cf6b97ef4b7 Mon Sep 17 00:00:00 2001 From: dweller Date: Sat, 9 Mar 2024 00:55:36 +0200 Subject: Initial commit, 2 years later --- java/sources/server.java | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 java/sources/server.java (limited to 'java/sources/server.java') 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..."); + } +} + -- cgit v1.2.3