From 3a44c2b3aee11f1d4081588a7db3f779d16836c1 Mon Sep 17 00:00:00 2001 From: Johan Maasing Date: Wed, 27 Nov 2024 10:55:10 +0100 Subject: [PATCH] Initial commit --- .gitignore | 81 +++++++++++++++++++ JavaUDSClient/pom.xml | 17 ++++ .../java/nu/zoom/checked/client/Main.java | 40 +++++++++ JavaUDSServer/pom.xml | 17 ++++ .../java/nu/zoom/checked/server/Main.java | 49 +++++++++++ 5 files changed, 204 insertions(+) create mode 100644 .gitignore create mode 100644 JavaUDSClient/pom.xml create mode 100644 JavaUDSClient/src/main/java/nu/zoom/checked/client/Main.java create mode 100644 JavaUDSServer/pom.xml create mode 100644 JavaUDSServer/src/main/java/nu/zoom/checked/server/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bbb039d --- /dev/null +++ b/.gitignore @@ -0,0 +1,81 @@ +## File-based project format: +*.ipr +*.iws + +**/.idea + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties + +# ---> Java +*.class + +*/target/* + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# ---> SublimeText +# cache files for sublime text +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache + +# workspace files are user-specific +*.sublime-workspace + +# project files should be checked into the repository, unless a significant +# proportion of contributors will probably not be using SublimeText +# *.sublime-project + +# sftp configuration file +sftp-config.json + +# ---> macOS +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + diff --git a/JavaUDSClient/pom.xml b/JavaUDSClient/pom.xml new file mode 100644 index 0000000..dc76e2e --- /dev/null +++ b/JavaUDSClient/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + se.rutdev.proxmig + JavaUDSClient + 1.0-SNAPSHOT + + + 23 + 23 + UTF-8 + + + \ No newline at end of file diff --git a/JavaUDSClient/src/main/java/nu/zoom/checked/client/Main.java b/JavaUDSClient/src/main/java/nu/zoom/checked/client/Main.java new file mode 100644 index 0000000..deab910 --- /dev/null +++ b/JavaUDSClient/src/main/java/nu/zoom/checked/client/Main.java @@ -0,0 +1,40 @@ +package nu.zoom.checked.client; + +import java.io.IOException; +import java.net.StandardProtocolFamily; +import java.net.UnixDomainSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.SocketChannel; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.UUID; + +public class Main { + public static void main(String[] args) throws IOException { + new Main().run(); + } + + private void run() throws IOException { + try (var clientChannel = SocketChannel.open(StandardProtocolFamily.UNIX)) { + var serverPath = Path.of("/tmp").resolve("udsserver.sock") ; + var serverAddress = UnixDomainSocketAddress.of(serverPath); + clientChannel.connect(serverAddress); + + var clientMessage = "quit från - " + UUID.randomUUID(); + + sendMessage(clientMessage, clientChannel); + } + } + + private static void sendMessage(String clientMessage, SocketChannel clientChannel) throws IOException { + var messageBytes = clientMessage.getBytes(StandardCharsets.UTF_8); + var messageBuffer = ByteBuffer.allocate(500); + messageBuffer.put((byte) messageBytes.length); + messageBuffer.put(messageBytes); + messageBuffer.flip(); + clientChannel.write(messageBuffer); + } + + static class MessageToLongException extends Exception {} +} \ No newline at end of file diff --git a/JavaUDSServer/pom.xml b/JavaUDSServer/pom.xml new file mode 100644 index 0000000..d3bb6ec --- /dev/null +++ b/JavaUDSServer/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + se.rutdev.proxmig + JavaUDSServer + 1.0-SNAPSHOT + + + 23 + 23 + UTF-8 + + + \ No newline at end of file diff --git a/JavaUDSServer/src/main/java/nu/zoom/checked/server/Main.java b/JavaUDSServer/src/main/java/nu/zoom/checked/server/Main.java new file mode 100644 index 0000000..7094e46 --- /dev/null +++ b/JavaUDSServer/src/main/java/nu/zoom/checked/server/Main.java @@ -0,0 +1,49 @@ +package nu.zoom.checked.server; + +import java.io.IOException; +import java.net.StandardProtocolFamily; +import java.net.UnixDomainSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.ServerSocketChannel; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; + +public class Main { + public static void main(String[] args) throws IOException { + new Main().run(); + } + + private void run() throws IOException { + var path = Path.of("/tmp").resolve("udsserver.sock") ; + Files.deleteIfExists(path); + var socketAddress = UnixDomainSocketAddress.of(path); + ServerSocketChannel serverChannel = ServerSocketChannel + .open(StandardProtocolFamily.UNIX); + serverChannel.bind(socketAddress); + + boolean keepRunning = true; + while (keepRunning) { + try (var clientChannel = serverChannel.accept()) { + if (clientChannel != null) { + var buffer = ByteBuffer.allocate(500); + clientChannel.read(buffer); + buffer.flip(); + var length = Byte.toUnsignedInt(buffer.get()); + if (length < 400 && length > 0) { + var rawMessage = new byte[length]; + buffer.get(rawMessage); + var message = new String(rawMessage, StandardCharsets.UTF_8); + System.out.println(message); + if (message.startsWith("q")) { + keepRunning = false; + } + } + } + } + } + + serverChannel.close(); + Files.deleteIfExists(path); + } +} \ No newline at end of file