Obsolete
/
libipc-old
Archived
3
0
Fork 0

user_type is dropped. Rundir is now handled in Context.

master
Philippe Pittoli 2022-12-20 23:56:50 +01:00
parent 29d18e8ca1
commit 51e10d7f1e
1 changed files with 14 additions and 19 deletions

View File

@ -34,7 +34,6 @@ pub const Message = struct {
}; };
@"type": Message.Type, // Internal message type. @"type": Message.Type, // Internal message type.
user_type: u8, // User-defined message type (arbitrary).
fd: usize, // File descriptor concerned about this message. fd: usize, // File descriptor concerned about this message.
payload: []const u8, payload: []const u8,
@ -44,27 +43,22 @@ pub const Message = struct {
//pub fn initFromConnection(fd: usize) Self { //pub fn initFromConnection(fd: usize) Self {
// return Self{ // return Self{
// .@"type" = Message.Type.ERROR, // .@"type" = Message.Type.ERROR,
// .user_type = 8,
// .fd = fd, // .fd = fd,
// .payload = "hello", // .payload = "hello",
// }; // };
//} //}
pub fn init(fd: usize, pub fn init(fd: usize, @"type": Message.Type, payload: []const u8) Self {
@"type": Message.Type,
user_type: u8,
payload: []const u8) Self {
return Message { return Message {
.fd = fd, .fd = fd,
.@"type" = @"type", .@"type" = @"type",
.user_type = user_type,
.payload = payload, .payload = payload,
}; };
} }
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void { pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
try fmt.format(out_stream, "fd: {}, {}, usertype {}, payload: [{s}]", try fmt.format(out_stream, "fd: {}, {}, payload: [{s}]",
.{self.fd, self.@"type", self.user_type, self.payload} ); .{self.fd, self.@"type", self.payload} );
} }
}; };
@ -72,7 +66,7 @@ test "Message - creation and display" {
print("\n", .{}); print("\n", .{});
// fd type usertype payload // fd type usertype payload
var s = "hello!!"; var s = "hello!!";
var m = Message.init(1, Message.Type.DATA, 3, s); var m = Message.init(1, Message.Type.DATA, s);
print("message:\t[{}]\n", .{m}); print("message:\t[{}]\n", .{m});
print("\n", .{}); print("\n", .{});
@ -170,7 +164,7 @@ test "Event - creation and display" {
print("\n", .{}); print("\n", .{});
var s = "hello!!"; var s = "hello!!";
// fd type usertype payload // fd type usertype payload
var m = Message.init(1, Message.Type.DATA, 3, s); var m = Message.init(1, Message.Type.DATA, s);
// type index origin message // type index origin message
var e = Event.init(Event.Type.CONNECTION, 5, 8, &m); var e = Event.init(Event.Type.CONNECTION, 5, 8, &m);
@ -310,12 +304,12 @@ test "Switch - creation and display" {
// Context of the whole networking state. // Context of the whole networking state.
pub const Context = struct { pub const Context = struct {
pub var RUNDIR = "/run/ipc/";
pub const IPC_HEADER_SIZE = 4; // Size (4 bytes) then content. pub const IPC_HEADER_SIZE = 4; // Size (4 bytes) then content.
pub const IPC_BASE_SIZE = 2000000; // 2 MB, plenty enough space for messages pub const IPC_BASE_SIZE = 2000000; // 2 MB, plenty enough space for messages
pub const IPC_MAX_MESSAGE_SIZE = IPC_BASE_SIZE-IPC_HEADER_SIZE; pub const IPC_MAX_MESSAGE_SIZE = IPC_BASE_SIZE-IPC_HEADER_SIZE;
pub const IPC_VERSION = 1; pub const IPC_VERSION = 1;
rundir: [] u8,
allocator: std.mem.Allocator, // Memory allocator. allocator: std.mem.Allocator, // Memory allocator.
connections: Connections, // Keep track of connections. connections: Connections, // Keep track of connections.
@ -339,18 +333,18 @@ pub const Context = struct {
var rundir = std.process.getEnvVarOwned(allocator, "RUNDIR") catch |err| switch(err) { var rundir = std.process.getEnvVarOwned(allocator, "RUNDIR") catch |err| switch(err) {
error.EnvironmentVariableNotFound => blk: { error.EnvironmentVariableNotFound => blk: {
print("RUNTIME variable not set, using default /tmp/runtime\n", .{}); print("RUNTIME variable not set, using default /tmp/libipc-run/\n", .{});
break :blk try allocator.dupeZ(u8, "/tmp/runtime"); break :blk try allocator.dupeZ(u8, "/tmp/libipc-run/");
}, },
else => { else => {
return err; return err;
}, },
}; };
defer allocator.free(rundir);
print("rundir: {s}\n", .{rundir}); print("rundir: {s}\n", .{rundir});
return Self { return Self {
.connections = Connections.init(allocator) .rundir = rundir
, .connections = Connections.init(allocator)
, .pollfd = PollFD.init(allocator) , .pollfd = PollFD.init(allocator)
, .tx = Messages.init(allocator) , .tx = Messages.init(allocator)
, .switchdb = null , .switchdb = null
@ -365,6 +359,7 @@ pub const Context = struct {
print("context.deinit(): IndexOutOfBounds\n", .{}); print("context.deinit(): IndexOutOfBounds\n", .{});
}, },
}; };
self.allocator.free(self.rundir);
self.connections.deinit(); self.connections.deinit();
self.pollfd.deinit(); self.pollfd.deinit();
self.tx.deinit(); self.tx.deinit();
@ -429,7 +424,7 @@ pub const Context = struct {
print("read fd {} index {}\n", .{fd, index}); print("read fd {} index {}\n", .{fd, index});
var payload = "hello!!"; var payload = "hello!!";
// fd type usertype payload // fd type usertype payload
var m = Message.init(0, Message.Type.DATA, 1, payload); var m = Message.init(0, Message.Type.DATA, payload);
return m; return m;
} }
@ -438,7 +433,7 @@ pub const Context = struct {
print("read fd {}\n", .{fd}); print("read fd {}\n", .{fd});
var payload = "hello!!"; var payload = "hello!!";
// fd type usertype payload // fd type usertype payload
var m = Message.init(0, Message.Type.DATA, 1, payload); var m = Message.init(0, Message.Type.DATA, payload);
return m; return m;
} }
@ -521,7 +516,7 @@ test "Context - creation, display and memory check" {
// var payload = "hello!!"; // var payload = "hello!!";
// // fd type usertype payload // // fd type usertype payload
// var m = Message.init(0, Message.Type.DATA, 1, payload); // var m = Message.init(0, Message.Type.DATA, payload);
// //
// // type index origin message // // type index origin message
// var e = Event.init(Event.Type.CONNECTION, 5, 8, &m); // var e = Event.init(Event.Type.CONNECTION, 5, 8, &m);