From 73d1753935f4fbf9b8ffa0e4b51a96a17156e155 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Santos Souza de Miranda Date: Thu, 13 Nov 2025 20:45:37 -0300 Subject: [PATCH] Add `IPCManager` for UNIX domain socket communication: - Introduced `IPCManager` class to handle IPC using UNIX domain sockets. - Updated `MediaManagerApplication` to initialize and manage `IPCManager` during startup and shutdown. - Enhanced logging for IPC initialization and cleanup. - Updated `config.properties.example` with `ipc.socket.path` property. --- .../mediamanager/MediaManagerApplication.java | 17 ++- .../mediamanager/service/ipc/IPCManager.java | 101 ++++++++++++++++++ src/main/resources/config.properties.example | 2 + 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/mediamanager/service/ipc/IPCManager.java diff --git a/src/main/java/com/mediamanager/MediaManagerApplication.java b/src/main/java/com/mediamanager/MediaManagerApplication.java index f4acc3c..d483da3 100644 --- a/src/main/java/com/mediamanager/MediaManagerApplication.java +++ b/src/main/java/com/mediamanager/MediaManagerApplication.java @@ -1,5 +1,6 @@ package com.mediamanager; +import com.mediamanager.service.ipc.IPCManager; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -13,6 +14,7 @@ public class MediaManagerApplication { private static final Logger logger = LogManager.getLogger(MediaManagerApplication.class); private static Properties config; private static DatabaseManager databaseManager; + private static IPCManager ipcManager; public static void main(String[] args) { logger.info("Starting MediaManager Core Application..."); @@ -23,12 +25,13 @@ public class MediaManagerApplication { databaseManager = new DatabaseManager(config); databaseManager.init(); - - // TODO: Initialize IPC server with named pipes + ipcManager = new IPCManager(config); + ipcManager.init(); + // TODO: Start application services logger.info("MediaManager Core started successfully"); - logger.info("IPC Pipe: {}", config.getProperty("ipc.pipe.path") + "/" + config.getProperty("ipc.pipe.name")); + logger.info("IPC Socket: {}", ipcManager.getSocketPath().toAbsolutePath().toString()); // Keep application running Runtime.getRuntime().addShutdownHook(new Thread(() -> { @@ -40,6 +43,14 @@ public class MediaManagerApplication { databaseManager.close(); } + if (ipcManager != null) { + try { + ipcManager.close(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + logger.info("MediaManager Core shutdown successfully"); logger.info("Goodbye!"); diff --git a/src/main/java/com/mediamanager/service/ipc/IPCManager.java b/src/main/java/com/mediamanager/service/ipc/IPCManager.java new file mode 100644 index 0000000..e1cc0ae --- /dev/null +++ b/src/main/java/com/mediamanager/service/ipc/IPCManager.java @@ -0,0 +1,101 @@ +package com.mediamanager.service.ipc; + + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.net.StandardProtocolFamily; +import java.net.UnixDomainSocketAddress; +import java.nio.channels.ServerSocketChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +public class IPCManager { + private static Properties configuration; + private static final Logger logger = LogManager.getLogger(IPCManager.class); + private Path socketPath; + private UnixDomainSocketAddress socketAddress; + private ServerSocketChannel serverChannel; + + public IPCManager(Properties config){ + configuration = config; + logger.debug("IPCManager created with configuration:"); + + + } + public void init() throws Exception { + logger.info("Initializing IPC connection..."); + validateConfiguration(); + socketPath = Path.of(configuration.getProperty("ipc.socket.path")).resolve("mediamanager.sock"); + + if (checkUnixSocketExists()){ + logger.warn("IPC socket already exists"); + logger.info("Deleting existing socket..."); + Files.deleteIfExists(socketPath); + + + } + + try{ + socketAddress = UnixDomainSocketAddress.of(socketPath); + logger.info("IPC socket created successfully"); + } catch (Exception e){ + logger.error("Failed to create socket: {}", e.getMessage()); + throw new Exception("Failed to create socket: " + e.getMessage(), e); + } + if (!Files.isDirectory(socketPath.getParent())) { + logger.info("Creating parent directory for socket..."); + Files.createDirectories(socketPath.getParent()); + } + + serverChannel = ServerSocketChannel.open(StandardProtocolFamily.UNIX); + logger.info("IPC server channel opened successfully"); + serverChannel.bind(socketAddress); + logger.info("IPC server channel bound successfully"); + logger.info("IPC server listening on {}", socketPath.toAbsolutePath().toString()); + + + } + private void validateConfiguration() throws Exception { + String[] requiredProperties = { + "ipc.socket.path" + }; + for (String property : requiredProperties) { + if (configuration.getProperty(property) == null) { + throw new Exception("Missing required configuration property: " + property); + } + } + logger.debug("IPC configuration validated successfully"); + } + private boolean checkUnixSocketExists() { + File socketFile = new File(String.valueOf(socketPath)); + return socketFile.exists(); + } + + public Path getSocketPath(){ + return socketPath; + } + + public void close() throws Exception { + logger.info("Closing IPC connection..."); + if (serverChannel != null) { + serverChannel.close(); + } + File socketFile = new File(String.valueOf(socketPath)); + boolean delete = false; + if (socketFile.exists()) { + delete = socketFile.delete(); + } + if (!delete){ + logger.warn("Failed to delete socket file"); + }else { + logger.info("IPC socket deleted successfully"); + Files.deleteIfExists(socketPath.getParent()); + logger.info("IPC socket parent directory deleted successfully"); + + } + + } +} diff --git a/src/main/resources/config.properties.example b/src/main/resources/config.properties.example index 8840823..095d75c 100644 --- a/src/main/resources/config.properties.example +++ b/src/main/resources/config.properties.example @@ -22,3 +22,5 @@ ipc.pipe.name=mediamanager-pipe ipc.pipe.path=/tmp/mediamanager ipc.buffer.size=8192 +ipc.socket.path=/tmp/mediamanager +