Obsolete
/
libipc-old
Archived
3
0
Fork 0

Remove message type. It was useless: msg to IPCd is LOOKUP.

master
Philippe Pittoli 2023-01-05 11:49:33 +01:00
parent 4bbd5fc686
commit d99a8d13e3
6 changed files with 20 additions and 68 deletions

View File

@ -137,7 +137,7 @@ pub const Context = struct {
// content: target service name;${IPC_NETWORK}
// example: pong;pong tls://example.com:8998/pong
var m = try Message.init (ipcdfd, Message.Type.LOOKUP, allocator, lookupfbs.getWritten());
var m = try Message.init (ipcdfd, allocator, lookupfbs.getWritten());
try self.write (m);
// Read LOOKUP response
@ -310,7 +310,7 @@ pub const Context = struct {
// Wait an event.
pub fn wait_event(self: *Self) !Event {
var current_event: Event = Event.init(Event.Type.NOT_SET, 0, 0, null);
var current_event: Event = Event.init(Event.Type.ERROR, 0, 0, null);
var wait_duration: i32 = -1; // -1 == unlimited
if (self.timer) |t| { wait_duration = t; }
@ -389,9 +389,6 @@ pub const Context = struct {
};
if (maybe_message) |m| {
if (m.t == .LOOKUP) {
return Event.init(Event.Type.LOOKUP, i, fd.fd, m);
}
return Event.init(Event.Type.MESSAGE, i, fd.fd, m);
}
@ -505,7 +502,7 @@ pub const Context = struct {
// var s = Switch.init(3,8);
// var payload = "hello!!";
// // fd type payload
// var m = Message.init(0, Message.Type.DATA, payload);
// var m = Message.init(0, allocator, payload);
//
// // type index origin message
// var e = Event.init(Event.Type.CONNECTION, 5, 8, &m);
@ -610,7 +607,7 @@ const ConnectThenSendMessageThread = struct {
var message_writer = message_fbs.writer();
// 'fd' parameter is not taken into account here (no loop)
var m = try Message.init(0, Message.Type.DATA, allocator, "Hello world!");
var m = try Message.init(0, allocator, "Hello world!");
defer m.deinit();
_ = try m.write(message_writer);

View File

@ -34,14 +34,12 @@ pub const Event = struct {
// to it. This is a lookup.
pub const Type = enum {
NOT_SET, // Default. TODO: should we keep this?
ERROR, // A problem occured.
EXTERNAL, // Message received from a non IPC socket.
SWITCH, // Message to send to a corresponding fd.
CONNECTION, // New user.
DISCONNECTION, // User disconnected.
MESSAGE, // New message.
LOOKUP, // Client asking for a service through ipcd.
TIMER, // Timeout in the poll(2) function.
TX, // Message sent.
};
@ -65,7 +63,7 @@ pub const Event = struct {
}
pub fn clean(self: *Self) void {
self.t = Event.Type.NOT_SET;
self.t = Event.Type.ERROR;
self.index = @as(usize,0);
self.origin = @as(i32,0);
if (self.m) |message| {
@ -89,9 +87,9 @@ test "Event - creation and display" {
const allocator = gpa.allocator();
var s = "hello!!";
var m = try Message.init(1, Message.Type.DATA, allocator, s); // fd type payload
var m = try Message.init(1, allocator, s); // fd type payload
defer m.deinit();
var e = Event.init(Event.Type.CONNECTION, 5, 8, m); // type index origin message
try print_eq("event.Event.Type.CONNECTION, origin: 8, index 5, message: [fd: 1, message.Message.Type.DATA, payload: [hello!!]]", e);
try print_eq("event.Event.Type.CONNECTION, origin: 8, index 5, message: [fd: 1, payload: [hello!!]]", e);
}

View File

@ -103,18 +103,6 @@ fn create_service() !void {
},
.MESSAGE => {
if (some_event.m) |m| {
print("New message: {}\n", .{m});
print("Let's echo it...\n", .{});
try ctx.schedule(m);
}
else {
print("Error while receiving new message.\n", .{});
print("Ignoring...\n", .{});
}
},
.LOOKUP => {
print("Client asking for a service through ipcd.\n", .{});
if (some_event.m) |m| {
print("{}\n", .{m});
@ -162,7 +150,6 @@ fn create_service() !void {
// a message, meaning there is nothing to do. This should be
// explicitely warned about.
var response = try Message.init(some_event.origin
, Message.Type.ERROR
, allocator
, "lookup message without data");
try ctx.write(response);
@ -174,11 +161,6 @@ fn create_service() !void {
print("Message sent.\n", .{});
},
.NOT_SET => {
print("Event type not set. Something is wrong, let's suicide.\n", .{});
break;
},
.ERROR => {
print("A problem occured, event: {}, let's suicide\n", .{some_event});
break;

View File

@ -11,14 +11,6 @@ pub const Messages = std.ArrayList(Message);
pub const Message = struct {
pub const Type = enum {
SERVER_CLOSE,
ERROR,
DATA,
LOOKUP,
};
t: Message.Type, // Internal message type.
fd: i32, // File descriptor concerned about this message.
payload: []const u8,
@ -26,19 +18,10 @@ pub const Message = struct {
const Self = @This();
// TODO
//pub fn initFromConnection(fd: i32) Self {
// return Self{
// .t = Message.Type.ERROR,
// .fd = fd,
// .payload = "hello",
// };
//}
pub fn init(fd: i32, t: Message.Type
pub fn init(fd: i32
, allocator: std.mem.Allocator
, payload: []const u8) !Self {
return Message { .fd = fd, .t = t
return Message { .fd = fd
, .allocator = allocator
, .payload = try allocator.dupe(u8, payload) };
}
@ -49,34 +32,26 @@ pub const Message = struct {
pub fn read(fd: i32, buffer: []const u8, allocator: std.mem.Allocator) !Self {
// var hexbuf: [4000]u8 = undefined;
// var hexfbs = std.io.fixedBufferStream(&hexbuf);
// var hexwriter = hexfbs.writer();
// try hexdump.hexdump(hexwriter, "Message.read input buffer", buffer);
// print("{s}\n", .{hexfbs.getWritten()});
var fbs = std.io.fixedBufferStream(buffer);
var reader = fbs.reader();
const msg_type = @intToEnum(Message.Type, try reader.readByte());
const msg_len = try reader.readIntBig(u32);
if (msg_len >= buffer.len) {
if (msg_len >= buffer.len - 4) {
return error.wrongMessageLength;
}
const msg_payload = buffer[5..5+msg_len];
const msg_payload = buffer[4..4+msg_len];
return try Message.init(fd, msg_type, allocator, msg_payload);
return try Message.init(fd, allocator, msg_payload);
}
pub fn write(self: Self, writer: anytype) !usize {
try writer.writeByte(@enumToInt(self.t));
try writer.writeIntBig(u32, @truncate(u32, self.payload.len));
return 5 + try writer.write(self.payload);
return 4 + try writer.write(self.payload);
}
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
try fmt.format(out_stream, "fd: {}, {}, payload: [{s}]",
.{self.fd, self.t, self.payload} );
try fmt.format(out_stream, "fd: {}, payload: [{s}]",
.{self.fd, self.payload} );
}
};
@ -88,10 +63,10 @@ test "Message - creation and display" {
const allocator = gpa.allocator();
var s = "hello!!";
var m = try Message.init(1, Message.Type.DATA, allocator, s);
var m = try Message.init(1, allocator, s);
defer m.deinit();
try print_eq("fd: 1, message.Message.Type.DATA, payload: [hello!!]", m);
try print_eq("fd: 1, payload: [hello!!]", m);
}
test "Message - read and write" {
@ -103,7 +78,7 @@ test "Message - read and write" {
// First, create a message.
var s = "hello!!";
var first_message = try Message.init(1, Message.Type.DATA, allocator, s);
var first_message = try Message.init(1, allocator, s);
defer first_message.deinit();
// Test its content.

View File

@ -30,7 +30,7 @@ pub fn main() !u8 {
}
var pongfd = try ctx.connect_ipc(service_to_contact);
var message = try Message.init(pongfd, Message.Type.DATA, allocator, "bounce me");
var message = try Message.init(pongfd, allocator, "bounce me");
try ctx.schedule(message);
var some_event: ipc.Event = undefined;

View File

@ -60,7 +60,7 @@ fn create_service() !void {
try os.sigaction(os.SIG.HUP, &sa, null);
var some_event: ipc.Event = undefined;
ctx.timer = 10000; // 10 seconds
ctx.timer = 20000; // 2 seconds
var count: u32 = 0;
while(! S.should_quit) {
some_event = try ctx.wait_event();