Imported from GitHub samson-media/barn#14 — opened by @daniel-samson on 2026-01-21 12:40:15 UTC
Implement the IPC (Inter-Process Communication) mechanism for CLI-to-service communication.
docs/config.md
Simple JSON-over-Unix-socket protocol (Windows uses named pipes).
Request format:
{ "type": "run_job", "payload": { "command": ["echo", "hello"], "tag": "test" } }
Response format:
{ "status": "ok", "payload": { "job_id": "job-9f83c" } }
Error response:
{ "status": "error", "error": { "code": "JOB_NOT_FOUND", "message": "Job not found: job-xyz" } }
public sealed interface IpcRequest { record RunJob(List<String> command, String tag, JobOptions options) implements IpcRequest {} record GetStatus(StatusFilter filter) implements IpcRequest {} record GetJob(String jobId) implements IpcRequest {} record KillJob(String jobId, boolean force) implements IpcRequest {} record CleanJobs(CleanOptions options) implements IpcRequest {} record GetServiceStatus() implements IpcRequest {} record Shutdown() implements IpcRequest {} record Reload() implements IpcRequest {} }
public sealed interface IpcResponse { record Ok<T>(T payload) implements IpcResponse {} record Error(String code, String message) implements IpcResponse {} }
com.samsonmedia.barn.ipc.IpcServer.java:
com.samsonmedia.barn.ipc.IpcServer.java
public class IpcServer { private final Path socketPath; private final RequestHandler handler; public void start(); public void stop(); // Handle incoming connections private void handleConnection(SocketChannel channel) { IpcRequest request = readRequest(channel); IpcResponse response = handler.handle(request); writeResponse(channel, response); } }
com.samsonmedia.barn.ipc.IpcClient.java:
com.samsonmedia.barn.ipc.IpcClient.java
public class IpcClient implements AutoCloseable { private final Path socketPath; public IpcClient(Path socketPath) { this.socketPath = socketPath; } public <T> T send(IpcRequest request, Class<T> responseType) throws IpcException { // 1. Connect to socket // 2. Send request JSON // 3. Read response JSON // 4. Parse and return } public void ensureConnected() throws IpcException { // Verify service is running } }
public static Path getDefaultSocketPath() { return switch (OS.current()) { case LINUX, MACOS -> Path.of("/tmp/barn/barn.sock"); case WINDOWS -> Path.of("\\\\.\\pipe\\barn"); }; }
IpcServerTest.java
IpcClientTest.java
IpcProtocolTest.java
mvn verify
Unix socket in Java:
UnixDomainSocketAddress address = UnixDomainSocketAddress.of(socketPath); ServerSocketChannel server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); server.bind(address);
Named pipe on Windows (requires JNI or library):
// Consider using a cross-platform library or // fall back to TCP localhost for Windows
For simplicity, consider TCP localhost as fallback:
@daniel-samson commented on 2026-01-21 14:29:23 UTC:
Implemented IPC mechanism with tests
No dependencies set.
The note is not visible to the blocked user.
Imported from GitHub samson-media/barn#14 — opened by @daniel-samson on 2026-01-21 12:40:15 UTC
Overview
Implement the IPC (Inter-Process Communication) mechanism for CLI-to-service communication.
Context
docs/config.md(ipc_socket setting)Requirements
1. IPC Protocol
Simple JSON-over-Unix-socket protocol (Windows uses named pipes).
Request format:
Response format:
Error response:
2. Request Types
3. Response Types
4. IPC Server (Service Side)
com.samsonmedia.barn.ipc.IpcServer.java:5. IPC Client (CLI Side)
com.samsonmedia.barn.ipc.IpcClient.java:6. Platform-Specific Socket Paths
7. Error Codes
8. Tests Required
IpcServerTest.java: Test request handling, error responsesIpcClientTest.java: Test connection, request/response cycleIpcProtocolTest.java: Test JSON serializationAcceptance Criteria
mvn verifypassesImplementation Notes
Unix socket in Java:
Named pipe on Windows (requires JNI or library):
For simplicity, consider TCP localhost as fallback:
Definition of Done
@daniel-samson commented on 2026-01-21 14:29:23 UTC:
Implemented IPC mechanism with tests