summaryrefslogtreecommitdiff
path: root/java
diff options
context:
space:
mode:
authordweller <dweller@cabin.digital>2024-03-09 00:55:36 +0200
committerdweller <dweller@cabin.digital>2024-03-09 00:55:36 +0200
commit86d3f93ee338b28ab7d40aa83c129cf6b97ef4b7 (patch)
tree507a8d66932e6dea9b121dfcbf980f7925575c9f /java
Initial commit, 2 years later
Diffstat (limited to '')
-rw-r--r--java/build.xml56
-rw-r--r--java/sources/digital/cabin/ezipc/EzIPC.java106
-rw-r--r--java/sources/digital/cabin/ezipc/EzIPCClient.java22
-rw-r--r--java/sources/digital/cabin/ezipc/EzIPCServer.java33
-rw-r--r--java/sources/server.java82
5 files changed, 299 insertions, 0 deletions
diff --git a/java/build.xml b/java/build.xml
new file mode 100644
index 0000000..3d0953f
--- /dev/null
+++ b/java/build.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0"?>
+<project name="EZIPC Server Test" basedir="." default="jar">
+ <property name="version">0.1</property>
+ <property name="dir.src">sources</property>
+ <property name="dir.lib">libraries</property>
+ <property name="dir.build">build</property>
+ <property name="dir.build.classes">${dir.build}/classes</property>
+ <property name="dir.build.javadoc">${dir.build}/javadoc</property>
+ <property name="file.jar">${dir.build}/server-${version}.jar</property>
+
+ <path id="external.classpath">
+ <fileset dir="${dir.lib}">
+ <include name="**/*.jar"/>
+ </fileset>
+ </path>
+
+ <target name="clean">
+ <delete dir="${dir.build}"/>
+ </target>
+
+ <target name="init">
+ <mkdir dir="${dir.build}"/>
+ <mkdir dir="${dir.build.classes}"/>
+ </target>
+
+ <target name="compile" depends="init">
+ <echo>Compiling Java source</echo>
+
+ <javac classpathref="external.classpath"
+ srcdir="${dir.src}"
+ destdir="${dir.build.classes}"/>
+ </target>
+
+ <target name="jar" depends="compile">
+ <echo>Making JAR file</echo>
+
+ <manifest file="build/MANIFEST.MF">
+ <attribute name="Main-Class" value="EzIPCServerTest"/>
+ </manifest>
+
+ <jar manifest="build/MANIFEST.MF"
+ basedir="${dir.build.classes}"
+ file="${file.jar}">
+ <zipgroupfileset dir="${dir.lib}">
+ <include name="**/*.jar"/>
+ </zipgroupfileset>
+ </jar>
+ </target>
+
+ <target name="javadoc">
+ <echo>Making JavaDoc from source</echo>
+
+ <javadoc sourcepath="${dir.src}" destdir="${dir.build.javadoc}">
+ </javadoc>
+ </target>
+</project>
diff --git a/java/sources/digital/cabin/ezipc/EzIPC.java b/java/sources/digital/cabin/ezipc/EzIPC.java
new file mode 100644
index 0000000..7c9943d
--- /dev/null
+++ b/java/sources/digital/cabin/ezipc/EzIPC.java
@@ -0,0 +1,106 @@
+package digital.cabin.ezipc;
+
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import com.sun.jna.Library;
+import com.sun.jna.Native;
+import com.sun.jna.platform.linux.ErrNo;
+
+
+public abstract class EzIPC
+{
+ protected interface LibraryNamedFIFO extends Library
+ {
+ static LibraryNamedFIFO INSTANCE =
+ Native.load("c", LibraryNamedFIFO.class);
+
+ public int mkfifo(String pathname, int mode);
+ }
+
+
+ protected LibraryNamedFIFO lib;
+
+ protected String connPath;
+
+ protected FileInputStream in_pipe;
+ protected FileOutputStream out_pipe;
+
+
+ public EzIPC(String connPath) throws IOException
+ {
+ this.lib = LibraryNamedFIFO.INSTANCE;
+ this.connPath = connPath;
+
+ if(lib.mkfifo(connPath+"_c2s", 0660) != 0)
+ if(Native.getLastError() != ErrNo.EEXIST)
+ throw new IOException("Could not create client->server pipe");
+
+ if(lib.mkfifo(connPath+"_s2c", 0660) != 0)
+ if(Native.getLastError() != ErrNo.EEXIST)
+ throw new IOException("Could not create server->client pipe");
+
+ this.in_pipe = null;
+ this.out_pipe = null;
+ }
+
+ public abstract void open() throws IOException;
+
+ public void close() throws IOException
+ {
+ if(in_pipe != null) in_pipe.close();
+ if(out_pipe != null) out_pipe.close();
+ }
+
+
+ // FIXME: check write() return val
+ public boolean send(byte[] data) throws IOException
+ {
+ if(out_pipe == null) return false;
+ if(data.length == 0) return true;
+
+ // FIXME: JAVA DOES NOT HAVE FUCKING UNSIGNED BRO
+ ByteBuffer bb = ByteBuffer.allocate(8);
+ bb.order(ByteOrder.nativeOrder()); // Imagine not being native by def.
+ bb.putLong(data.length);
+
+ out_pipe.write(bb.array());
+ out_pipe.write(data);
+
+ return true;
+ }
+
+ // FIXME: check write() return val
+ public boolean receive(byte[] data) throws IOException
+ {
+ if(in_pipe == null) return false;
+ if(data.length == 0) return true;
+
+ byte[] bytes = new byte[8];
+
+ // FIXME: yikes
+ while(in_pipe.available() < 8);
+ in_pipe.read(bytes);
+
+ ByteBuffer bb = ByteBuffer.wrap(bytes);
+ bb.order(ByteOrder.nativeOrder()); // Imagine not being native by def.
+
+ // FIXME: JAVA DOES NOT HAVE FUCKING UNSIGNED BRO
+ long in_size = bb.getLong();
+ long read_size = in_size > data.length ? data.length : in_size;
+
+ // FIXME: yikes
+ while(in_pipe.available() < read_size);
+ long read = in_pipe.read(data);
+
+ // truncate
+ for(long i = 0; i < (in_size - read_size); i++) in_pipe.read();
+
+ return true;
+ }
+}
diff --git a/java/sources/digital/cabin/ezipc/EzIPCClient.java b/java/sources/digital/cabin/ezipc/EzIPCClient.java
new file mode 100644
index 0000000..03bc5db
--- /dev/null
+++ b/java/sources/digital/cabin/ezipc/EzIPCClient.java
@@ -0,0 +1,22 @@
+package digital.cabin.ezipc;
+
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+
+public class EzIPCClient extends EzIPC
+{
+ public EzIPCClient(String connPath) throws IOException
+ {
+ super(connPath);
+ }
+
+ public void open() throws IOException
+ {
+ super.in_pipe = new FileInputStream(super.connPath + "_s2c");
+ super.out_pipe = new FileOutputStream(super.connPath + "_c2s");
+ }
+}
+
diff --git a/java/sources/digital/cabin/ezipc/EzIPCServer.java b/java/sources/digital/cabin/ezipc/EzIPCServer.java
new file mode 100644
index 0000000..f490770
--- /dev/null
+++ b/java/sources/digital/cabin/ezipc/EzIPCServer.java
@@ -0,0 +1,33 @@
+package digital.cabin.ezipc;
+
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+
+public class EzIPCServer extends EzIPC
+{
+ public EzIPCServer(String connPath) throws IOException
+ {
+ super(connPath);
+ }
+
+ public void open() throws IOException
+ {
+ super.in_pipe = new FileInputStream(super.connPath + "_c2s");
+ super.out_pipe = new FileOutputStream(super.connPath + "_s2c");
+ }
+
+ public void close() throws IOException
+ {
+ super.close();
+
+ File fc2s = new File(super.connPath + "_c2s");
+ fc2s.delete();
+
+ File fs2c = new File(super.connPath + "_s2c");
+ fc2s.delete();
+ }
+}
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...");
+ }
+}
+