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/digital/cabin/ezipc/EzIPC.java | 106 ++++++++++++++++++++++ java/sources/digital/cabin/ezipc/EzIPCClient.java | 22 +++++ java/sources/digital/cabin/ezipc/EzIPCServer.java | 33 +++++++ 3 files changed, 161 insertions(+) create mode 100644 java/sources/digital/cabin/ezipc/EzIPC.java create mode 100644 java/sources/digital/cabin/ezipc/EzIPCClient.java create mode 100644 java/sources/digital/cabin/ezipc/EzIPCServer.java (limited to 'java/sources/digital/cabin') 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(); + } +} -- cgit v1.2.3