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.
This commit is contained in:
parent
81e38ce9ae
commit
73d1753935
|
|
@ -1,5 +1,6 @@
|
||||||
package com.mediamanager;
|
package com.mediamanager;
|
||||||
|
|
||||||
|
import com.mediamanager.service.ipc.IPCManager;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
|
@ -13,6 +14,7 @@ public class MediaManagerApplication {
|
||||||
private static final Logger logger = LogManager.getLogger(MediaManagerApplication.class);
|
private static final Logger logger = LogManager.getLogger(MediaManagerApplication.class);
|
||||||
private static Properties config;
|
private static Properties config;
|
||||||
private static DatabaseManager databaseManager;
|
private static DatabaseManager databaseManager;
|
||||||
|
private static IPCManager ipcManager;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
logger.info("Starting MediaManager Core Application...");
|
logger.info("Starting MediaManager Core Application...");
|
||||||
|
|
@ -23,12 +25,13 @@ public class MediaManagerApplication {
|
||||||
databaseManager = new DatabaseManager(config);
|
databaseManager = new DatabaseManager(config);
|
||||||
databaseManager.init();
|
databaseManager.init();
|
||||||
|
|
||||||
|
ipcManager = new IPCManager(config);
|
||||||
|
ipcManager.init();
|
||||||
|
|
||||||
// TODO: Initialize IPC server with named pipes
|
|
||||||
// TODO: Start application services
|
// TODO: Start application services
|
||||||
|
|
||||||
logger.info("MediaManager Core started successfully");
|
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
|
// Keep application running
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
|
@ -40,6 +43,14 @@ public class MediaManagerApplication {
|
||||||
databaseManager.close();
|
databaseManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ipcManager != null) {
|
||||||
|
try {
|
||||||
|
ipcManager.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
logger.info("MediaManager Core shutdown successfully");
|
logger.info("MediaManager Core shutdown successfully");
|
||||||
logger.info("Goodbye!");
|
logger.info("Goodbye!");
|
||||||
|
|
|
||||||
|
|
@ -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");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,3 +22,5 @@ ipc.pipe.name=mediamanager-pipe
|
||||||
ipc.pipe.path=/tmp/mediamanager
|
ipc.pipe.path=/tmp/mediamanager
|
||||||
ipc.buffer.size=8192
|
ipc.buffer.size=8192
|
||||||
|
|
||||||
|
ipc.socket.path=/tmp/mediamanager
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue