From d72aac1a50a8f9102f96f8ea0c9ef63877ffae84 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 4 Jan 2023 11:34:49 +0100 Subject: [PATCH] IPCd is working for aliases. No error management, WIP. --- zig-impl/src/context.zig | 14 +++++++++++ zig-impl/src/ipcd.zig | 54 +++++++++++++++++++++++++++++----------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/zig-impl/src/context.zig b/zig-impl/src/context.zig index 143574e..7fe66a8 100644 --- a/zig-impl/src/context.zig +++ b/zig-impl/src/context.zig @@ -148,6 +148,15 @@ pub const Context = struct { var reception_buffer: [1500]u8 = undefined; var reception_size: usize = 0; var newfd = try receive_fd (ipcdfd, &reception_buffer, &reception_size); + var response = reception_buffer[0..reception_size - 1]; + if (response.len > 0) { + print ("receive_fd:message received: {s} (len: {}\n)\n", .{response, response.len}); + } + const ok = "ok"; + if (! std.mem.eql(u8, response[0..1], ok[0..1])) { + print("THIS IS NOT OKAY :O\n", .{}); // DEBUG TODO FIXME + return error.IPCdFailed; + } var newcon = Connection.init(connection_type, null); try self.add_ (newcon, newfd); return newfd; @@ -169,6 +178,7 @@ pub const Context = struct { if (self.pollfd.items[i].fd == fd) { return i; } + i += 1; } return error.IndexNotFound; } @@ -461,6 +471,8 @@ pub const Context = struct { return error.IndexOutOfBounds; } + print("closing client/server at index {}\n", .{index}); + // close the connection and remove it from the two structures var con = self.connections.swapRemove(index); // Remove service's UNIX socket file. @@ -471,6 +483,8 @@ pub const Context = struct { var pollfd = self.pollfd.swapRemove(index); std.os.close(pollfd.fd); + print("closing client at index {}\n", .{index}); + // Remove all its non-sent messages. var i: usize = 0; while (true) { diff --git a/zig-impl/src/ipcd.zig b/zig-impl/src/ipcd.zig index 9d0df1d..f7e86d4 100644 --- a/zig-impl/src/ipcd.zig +++ b/zig-impl/src/ipcd.zig @@ -72,11 +72,13 @@ fn create_service() !void { var some_event: ipc.Event = undefined; ctx.timer = 10000; // 10 seconds + var count: u32 = 0; while(! S.should_quit) { some_event = try ctx.wait_event(); switch (some_event.t) { .TIMER => { - print("Timer!\n", .{}); + print("\rTimer! ({})", .{count}); + count += 1; }, .CONNECTION => { @@ -116,22 +118,46 @@ fn create_service() !void { print("Client asking for a service through ipcd.\n", .{}); if (some_event.m) |m| { print("Message: {}\n", .{m}); + // 1. split message - // TODO print("payload is: {s}\n", .{m.payload}); + var iterator = std.mem.split(u8, m.payload, ";"); + var service_to_contact = iterator.first(); + print("service to contact: {s}\n", .{service_to_contact}); + var final_destination: ?[]const u8 = null; + // 2. find relevant part of the message - // TODO - // 3. connect whether asked to - // TODO - // 4. send_fd or send an error - // TODO - var response = try Message.init(some_event.origin - , Message.Type.ERROR - , allocator - , "currently not implemented"); - try ctx.write(response); - response.deinit(); - m.deinit(); + while (iterator.next()) |next| { + print("next part: {s}\n", .{next}); + var iterator2 = std.mem.split(u8, next, " "); + var sname = iterator2.first(); + var target = iterator2.next(); + if (target) |t| { + print ("sname: {s} - target: {s}\n", .{sname, t}); + if (std.mem.eql(u8, service_to_contact, sname)) { + final_destination = t; + } + } + else { + print("ERROR: no target in: {s}\n", .{next}); + } + } + // 3. connect whether asked to and send a message + // TODO: currently only switching with other UNIX sockets ^^'. + // Should include TCP connections in a near future. + + if (final_destination) |dest| { + print("service IPCd should contact for the client: {s}, via {s}\n" + , .{service_to_contact, dest}); + + var newfd = try ctx.connect_service (dest); + send_fd (some_event.origin, "ok", newfd); + print("fd sent\n" , .{}); + try ctx.close_fd (some_event.origin); + print("FD 1 removed\n" , .{}); + try ctx.close_fd (newfd); + print("FDs removed\n" , .{}); + } } else { // There is a problem: ipcd was contacted without providing