IPCd is working for aliases. No error management, WIP.
parent
1b19118701
commit
d72aac1a50
|
@ -148,6 +148,15 @@ pub const Context = struct {
|
||||||
var reception_buffer: [1500]u8 = undefined;
|
var reception_buffer: [1500]u8 = undefined;
|
||||||
var reception_size: usize = 0;
|
var reception_size: usize = 0;
|
||||||
var newfd = try receive_fd (ipcdfd, &reception_buffer, &reception_size);
|
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);
|
var newcon = Connection.init(connection_type, null);
|
||||||
try self.add_ (newcon, newfd);
|
try self.add_ (newcon, newfd);
|
||||||
return newfd;
|
return newfd;
|
||||||
|
@ -169,6 +178,7 @@ pub const Context = struct {
|
||||||
if (self.pollfd.items[i].fd == fd) {
|
if (self.pollfd.items[i].fd == fd) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
i += 1;
|
||||||
}
|
}
|
||||||
return error.IndexNotFound;
|
return error.IndexNotFound;
|
||||||
}
|
}
|
||||||
|
@ -461,6 +471,8 @@ pub const Context = struct {
|
||||||
return error.IndexOutOfBounds;
|
return error.IndexOutOfBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print("closing client/server at index {}\n", .{index});
|
||||||
|
|
||||||
// close the connection and remove it from the two structures
|
// close the connection and remove it from the two structures
|
||||||
var con = self.connections.swapRemove(index);
|
var con = self.connections.swapRemove(index);
|
||||||
// Remove service's UNIX socket file.
|
// Remove service's UNIX socket file.
|
||||||
|
@ -471,6 +483,8 @@ pub const Context = struct {
|
||||||
var pollfd = self.pollfd.swapRemove(index);
|
var pollfd = self.pollfd.swapRemove(index);
|
||||||
std.os.close(pollfd.fd);
|
std.os.close(pollfd.fd);
|
||||||
|
|
||||||
|
print("closing client at index {}\n", .{index});
|
||||||
|
|
||||||
// Remove all its non-sent messages.
|
// Remove all its non-sent messages.
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
|
@ -72,11 +72,13 @@ fn create_service() !void {
|
||||||
|
|
||||||
var some_event: ipc.Event = undefined;
|
var some_event: ipc.Event = undefined;
|
||||||
ctx.timer = 10000; // 10 seconds
|
ctx.timer = 10000; // 10 seconds
|
||||||
|
var count: u32 = 0;
|
||||||
while(! S.should_quit) {
|
while(! S.should_quit) {
|
||||||
some_event = try ctx.wait_event();
|
some_event = try ctx.wait_event();
|
||||||
switch (some_event.t) {
|
switch (some_event.t) {
|
||||||
.TIMER => {
|
.TIMER => {
|
||||||
print("Timer!\n", .{});
|
print("\rTimer! ({})", .{count});
|
||||||
|
count += 1;
|
||||||
},
|
},
|
||||||
|
|
||||||
.CONNECTION => {
|
.CONNECTION => {
|
||||||
|
@ -116,22 +118,46 @@ fn create_service() !void {
|
||||||
print("Client asking for a service through ipcd.\n", .{});
|
print("Client asking for a service through ipcd.\n", .{});
|
||||||
if (some_event.m) |m| {
|
if (some_event.m) |m| {
|
||||||
print("Message: {}\n", .{m});
|
print("Message: {}\n", .{m});
|
||||||
|
|
||||||
// 1. split message
|
// 1. split message
|
||||||
// TODO
|
|
||||||
print("payload is: {s}\n", .{m.payload});
|
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
|
// 2. find relevant part of the message
|
||||||
// TODO
|
while (iterator.next()) |next| {
|
||||||
// 3. connect whether asked to
|
print("next part: {s}\n", .{next});
|
||||||
// TODO
|
var iterator2 = std.mem.split(u8, next, " ");
|
||||||
// 4. send_fd or send an error
|
var sname = iterator2.first();
|
||||||
// TODO
|
var target = iterator2.next();
|
||||||
var response = try Message.init(some_event.origin
|
if (target) |t| {
|
||||||
, Message.Type.ERROR
|
print ("sname: {s} - target: {s}\n", .{sname, t});
|
||||||
, allocator
|
if (std.mem.eql(u8, service_to_contact, sname)) {
|
||||||
, "currently not implemented");
|
final_destination = t;
|
||||||
try ctx.write(response);
|
}
|
||||||
response.deinit();
|
}
|
||||||
m.deinit();
|
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 {
|
else {
|
||||||
// There is a problem: ipcd was contacted without providing
|
// There is a problem: ipcd was contacted without providing
|
||||||
|
|
Reference in New Issue