Renaming, grooming.
parent
0fddc05576
commit
1c26a69acd
|
@ -1,13 +1,14 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
const net = std.net;
|
const net = std.net;
|
||||||
|
const fmt = std.fmt;
|
||||||
|
|
||||||
// TODO: file descriptors should have a specific type (but i32 is used in std.net...).
|
// TODO: file descriptors should have a specific type (but i32 is used in std.net...).
|
||||||
|
|
||||||
// TODO: path => std.XXX.YYY, not simple [] const u8
|
// TODO: path => std.XXX.YYY, not simple [] const u8
|
||||||
|
|
||||||
// TODO: both ConnectionInfos and pollfd store file descriptors.
|
// TODO: both Connection and pollfd store file descriptors.
|
||||||
// ConnectionInfos stores either Stream (server) or Address (client).
|
// Connection stores either Stream (server) or Address (client).
|
||||||
|
|
||||||
// TODO: API should completely obfuscate the inner structures.
|
// TODO: API should completely obfuscate the inner structures.
|
||||||
// Only structures in this file should be necessary.
|
// Only structures in this file should be necessary.
|
||||||
|
@ -20,28 +21,43 @@ pub const IPC_VERSION = 4;
|
||||||
|
|
||||||
const print = std.debug.print;
|
const print = std.debug.print;
|
||||||
|
|
||||||
|
pub const Messages = std.ArrayList(Message);
|
||||||
|
pub const Switches = std.ArrayList(Switch);
|
||||||
|
pub const Connections = std.ArrayList(Connection);
|
||||||
|
pub const PollFD = std.ArrayList(i32);
|
||||||
|
|
||||||
pub const IPC_TYPE = enum {
|
pub const IPC_TYPE = enum {
|
||||||
UNIX_SOCKETS
|
UNIX_SOCKETS
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const MessageType = enum {
|
pub const Message = struct {
|
||||||
|
|
||||||
|
pub const Type = enum {
|
||||||
SERVER_CLOSE,
|
SERVER_CLOSE,
|
||||||
ERR,
|
ERROR,
|
||||||
DATA,
|
DATA,
|
||||||
NETWORK_LOOKUP,
|
NETWORK_LOOKUP,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Message = struct {
|
@"type": Message.Type, // Internal message type.
|
||||||
|
|
||||||
@"type": MessageType, // Internal message type.
|
|
||||||
user_type: u8, // User-defined message type (arbitrary).
|
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,
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
//pub fn initFromConnection(fd: usize) Self {
|
||||||
|
// return Self{
|
||||||
|
// .@"type" = Message.Type.ERROR,
|
||||||
|
// .user_type = 8,
|
||||||
|
// .fd = fd,
|
||||||
|
// .payload = "hello",
|
||||||
|
// };
|
||||||
|
//}
|
||||||
|
|
||||||
pub fn init(fd: usize,
|
pub fn init(fd: usize,
|
||||||
@"type": MessageType,
|
@"type": Message.Type,
|
||||||
user_type: u8,
|
user_type: u8,
|
||||||
payload: []const u8) Self {
|
payload: []const u8) Self {
|
||||||
return Message {
|
return Message {
|
||||||
|
@ -52,29 +68,23 @@ pub const Message = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
||||||
self: Self,
|
try fmt.format(out_stream, "fd: {}, {}, usertype {}, payload: [{s}]",
|
||||||
comptime _: []const u8, // No need.
|
|
||||||
_: std.fmt.FormatOptions, // No need.
|
|
||||||
out_stream: anytype,
|
|
||||||
) !void {
|
|
||||||
try std.fmt.format(out_stream, "fd: {}, type {}, usertype {}, payload: {s}",
|
|
||||||
.{self.fd, self.@"type", self.user_type, self.payload} );
|
.{self.fd, self.@"type", self.user_type, self.payload} );
|
||||||
}
|
}
|
||||||
// del == list.swapRemove(index)
|
|
||||||
};
|
};
|
||||||
|
|
||||||
test "Message - creation and display" {
|
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, MessageType.DATA, 3, s);
|
var m = Message.init(1, Message.Type.DATA, 3, s);
|
||||||
|
|
||||||
print("message:\t[{}]\n", .{m});
|
print("message:\t[{}]\n", .{m});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Messages = std.ArrayList(Message);
|
pub const Event = struct {
|
||||||
|
|
||||||
// Event types.
|
// Event types.
|
||||||
// In the main event loop, servers and clients can receive connections,
|
// In the main event loop, servers and clients can receive connections,
|
||||||
|
@ -101,7 +111,7 @@ pub const Messages = std.ArrayList(Message);
|
||||||
// ipc daemon (ipcd) to locate the service and establish a connection
|
// ipc daemon (ipcd) to locate the service and establish a connection
|
||||||
// to it. This is a lookup.
|
// to it. This is a lookup.
|
||||||
|
|
||||||
pub const EventType = enum {
|
pub const Type = enum {
|
||||||
NOT_SET, // Default. TODO: should we keep this?
|
NOT_SET, // Default. TODO: should we keep this?
|
||||||
ERROR, // A problem occured.
|
ERROR, // A problem occured.
|
||||||
EXTRA_SOCKET, // Message received from a non IPC socket.
|
EXTRA_SOCKET, // Message received from a non IPC socket.
|
||||||
|
@ -115,7 +125,7 @@ pub const EventType = enum {
|
||||||
};
|
};
|
||||||
|
|
||||||
// For IO callbacks (switching).
|
// For IO callbacks (switching).
|
||||||
pub const EventCallBack = enum {
|
pub const CallBack = enum {
|
||||||
NO_ERROR, // No error. A message was generated.
|
NO_ERROR, // No error. A message was generated.
|
||||||
FD_CLOSING, // The fd is closing.
|
FD_CLOSING, // The fd is closing.
|
||||||
FD_ERROR, // Generic error.
|
FD_ERROR, // Generic error.
|
||||||
|
@ -123,16 +133,14 @@ pub const EventCallBack = enum {
|
||||||
IGNORE, // The message should be ignored (protocol specific).
|
IGNORE, // The message should be ignored (protocol specific).
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const Event = struct {
|
@"type": Event.Type,
|
||||||
|
|
||||||
@"type": EventType,
|
|
||||||
index: u32,
|
index: u32,
|
||||||
origin: usize,
|
origin: usize,
|
||||||
m: ?*Message, // message pointer
|
m: ?*Message, // message pointer
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
pub fn init(@"type": EventType,
|
pub fn init(@"type": Event.Type,
|
||||||
index: u32,
|
index: u32,
|
||||||
origin: usize,
|
origin: usize,
|
||||||
m: ?*Message) Self {
|
m: ?*Message) Self {
|
||||||
|
@ -145,7 +153,7 @@ pub const Event = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set(self: *Self,
|
pub fn set(self: *Self,
|
||||||
@"type": EventType,
|
@"type": Event.Type,
|
||||||
index: u32,
|
index: u32,
|
||||||
origin: usize,
|
origin: usize,
|
||||||
m: ?*Message) void {
|
m: ?*Message) void {
|
||||||
|
@ -156,7 +164,7 @@ pub const Event = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clean(self: *Self) void {
|
pub fn clean(self: *Self) void {
|
||||||
self.@"type" = EventType.NOT_SET;
|
self.@"type" = Event.Type.NOT_SET;
|
||||||
self.index = @as(u8,0);
|
self.index = @as(u8,0);
|
||||||
self.origin = @as(usize,0);
|
self.origin = @as(usize,0);
|
||||||
if (self.m) |message| {
|
if (self.m) |message| {
|
||||||
|
@ -165,14 +173,9 @@ pub const Event = struct {
|
||||||
self.m = null;
|
self.m = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
||||||
self: Self,
|
try fmt.format(out_stream
|
||||||
comptime _: []const u8, // No need.
|
, "{}, origin: {}, index {}, message: [{}]"
|
||||||
_: std.fmt.FormatOptions, // No need.
|
|
||||||
out_stream: anytype,
|
|
||||||
) !void {
|
|
||||||
try std.fmt.format(out_stream
|
|
||||||
, "type {}, origin: {}, index {}, message: [{}]"
|
|
||||||
, .{ self.@"type", self.origin, self.index, self.m} );
|
, .{ self.@"type", self.origin, self.index, self.m} );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,23 +185,24 @@ 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, MessageType.DATA, 3, s);
|
var m = Message.init(1, Message.Type.DATA, 3, s);
|
||||||
// type index origin message
|
// type index origin message
|
||||||
var e = Event.init(EventType.CONNECTION, 5, 8, &m);
|
var e = Event.init(Event.Type.CONNECTION, 5, 8, &m);
|
||||||
|
|
||||||
print("event:\t[{}]\n", .{e});
|
print("event:\t[{}]\n", .{e});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const ConnectionType = enum {
|
pub const Connection = struct {
|
||||||
|
|
||||||
|
pub const Type = enum {
|
||||||
IPC, // Standard connection.
|
IPC, // Standard connection.
|
||||||
EXTERNAL, // Non IPC connection (TCP, UDP, etc.).
|
EXTERNAL, // Non IPC connection (TCP, UDP, etc.).
|
||||||
SERVER, // Messages received = new connections.
|
SERVER, // Messages received = new connections.
|
||||||
SWITCHED, // IO operations should go through registered callbacks.
|
SWITCHED, // IO operations should go through registered callbacks.
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const ConnectionInfos = struct {
|
@"type": Connection.Type,
|
||||||
@"type": ConnectionType,
|
|
||||||
more_to_read: bool,
|
more_to_read: bool,
|
||||||
path: ?[] const u8, // Not always needed.
|
path: ?[] const u8, // Not always needed.
|
||||||
|
|
||||||
|
@ -207,7 +211,7 @@ pub const ConnectionInfos = struct {
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
pub fn init(@"type": ConnectionType, path: ?[] const u8) Self {
|
pub fn init(@"type": Connection.Type, path: ?[] const u8) Self {
|
||||||
return Self {
|
return Self {
|
||||||
.@"type" = @"type",
|
.@"type" = @"type",
|
||||||
.more_to_read = false,
|
.more_to_read = false,
|
||||||
|
@ -219,30 +223,25 @@ pub const ConnectionInfos = struct {
|
||||||
if (self.server) |s| { s.deinit(); }
|
if (self.server) |s| { s.deinit(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
||||||
self: Self,
|
try fmt.format(out_stream
|
||||||
comptime _: []const u8, // No need.
|
, "{}, more_to_read {}, path {s}"
|
||||||
_: std.fmt.FormatOptions, // No need.
|
|
||||||
out_stream: anytype,
|
|
||||||
) !void {
|
|
||||||
try std.fmt.format(out_stream
|
|
||||||
, "connection type {}, more_to_read {}, path {s}"
|
|
||||||
, .{ self.@"type", self.more_to_read, self.path} );
|
, .{ self.@"type", self.more_to_read, self.path} );
|
||||||
if (self.server) |s| {
|
if (self.server) |s| {
|
||||||
try std.fmt.format(out_stream, "{}" , .{s});
|
try fmt.format(out_stream, "{}" , .{s});
|
||||||
}
|
}
|
||||||
if (self.client) |c| {
|
if (self.client) |c| {
|
||||||
try std.fmt.format(out_stream, "{}" , .{c});
|
try fmt.format(out_stream, "{}" , .{c});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
test "ConnectionInfos - creation and display" {
|
test "Connection - creation and display" {
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
// origin destination
|
// origin destination
|
||||||
var path = "/some/path";
|
var path = "/some/path";
|
||||||
var c1 = ConnectionInfos.init(ConnectionType.EXTERNAL, path);
|
var c1 = Connection.init(Connection.Type.EXTERNAL, path);
|
||||||
var c2 = ConnectionInfos.init(ConnectionType.IPC , null);
|
var c2 = Connection.init(Connection.Type.IPC , null);
|
||||||
print("connection 1:\t[{}]\n", .{c1});
|
print("connection 1:\t[{}]\n", .{c1});
|
||||||
print("connection 2:\t[{}]\n", .{c2});
|
print("connection 2:\t[{}]\n", .{c2});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
|
@ -266,13 +265,8 @@ pub const Switch = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
||||||
self: Self,
|
try fmt.format(out_stream
|
||||||
comptime _: []const u8, // No need.
|
|
||||||
_: std.fmt.FormatOptions, // No need.
|
|
||||||
out_stream: anytype,
|
|
||||||
) !void {
|
|
||||||
try std.fmt.format(out_stream
|
|
||||||
, "switch {} <-> {}"
|
, "switch {} <-> {}"
|
||||||
, .{ self.origin, self.destination} );
|
, .{ self.origin, self.destination} );
|
||||||
}
|
}
|
||||||
|
@ -281,14 +275,10 @@ pub const Switch = struct {
|
||||||
test "Switch - creation and display" {
|
test "Switch - creation and display" {
|
||||||
// origin destination
|
// origin destination
|
||||||
var s = Switch.init(3,8);
|
var s = Switch.init(3,8);
|
||||||
print("switch:\t[{}]\n", .{s});
|
print("\nswitch:\t[{}]\n", .{s});
|
||||||
print("\n", .{});
|
print("\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Switches = std.ArrayList(Switch);
|
|
||||||
pub const Connections = std.ArrayList(ConnectionInfos);
|
|
||||||
pub const PollFD = std.ArrayList(i32);
|
|
||||||
|
|
||||||
// Context of the whole networking state.
|
// Context of the whole networking state.
|
||||||
pub const Context = struct {
|
pub const Context = struct {
|
||||||
allocator: std.mem.Allocator, // Memory allocator.
|
allocator: std.mem.Allocator, // Memory allocator.
|
||||||
|
@ -334,11 +324,11 @@ pub const Context = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Both simple connection and the switched one share this code.
|
// Both simple connection and the switched one share this code.
|
||||||
fn connect_ (self: *Self, ctype: ConnectionType, path: []const u8) !i32 {
|
fn connect_ (self: *Self, ctype: Connection.Type, path: []const u8) !i32 {
|
||||||
var stream = try net.connectUnixSocket(path);
|
var stream = try net.connectUnixSocket(path);
|
||||||
const newfd = stream.handle;
|
const newfd = stream.handle;
|
||||||
errdefer std.os.closeSocket(newfd);
|
errdefer std.os.closeSocket(newfd);
|
||||||
var newcon = ConnectionInfos.init(ctype, path);
|
var newcon = Connection.init(ctype, path);
|
||||||
newcon.client = stream;
|
newcon.client = stream;
|
||||||
try self.connections.append(newcon);
|
try self.connections.append(newcon);
|
||||||
try self.pollfd.append(newfd);
|
try self.pollfd.append(newfd);
|
||||||
|
@ -348,7 +338,7 @@ pub const Context = struct {
|
||||||
// Return the new fd. Can be useful to the caller.
|
// Return the new fd. Can be useful to the caller.
|
||||||
pub fn connect(self: *Self, path: []const u8) !i32 {
|
pub fn connect(self: *Self, path: []const u8) !i32 {
|
||||||
print("connection to:\t{s}\n", .{path});
|
print("connection to:\t{s}\n", .{path});
|
||||||
return self.connect_ (ConnectionType.IPC, path);
|
return self.connect_ (Connection.Type.IPC, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connection to a service, but with switched with the client fd.
|
// Connection to a service, but with switched with the client fd.
|
||||||
|
@ -356,7 +346,7 @@ pub const Context = struct {
|
||||||
, path: [] const u8
|
, path: [] const u8
|
||||||
, clientfd: i32) !i32 {
|
, clientfd: i32) !i32 {
|
||||||
print("connection switched from {} to path {s}\n", .{clientfd, path});
|
print("connection switched from {} to path {s}\n", .{clientfd, path});
|
||||||
var newfd = try self.connect_ (ConnectionType.SWITCHED, path);
|
var newfd = try self.connect_ (Connection.Type.SWITCHED, path);
|
||||||
// TODO: record switch.
|
// TODO: record switch.
|
||||||
return newfd;
|
return newfd;
|
||||||
}
|
}
|
||||||
|
@ -370,7 +360,7 @@ pub const Context = struct {
|
||||||
try server.listen(socket_addr);
|
try server.listen(socket_addr);
|
||||||
|
|
||||||
const newfd = server.sockfd orelse return error.SocketLOL;
|
const newfd = server.sockfd orelse return error.SocketLOL;
|
||||||
var newcon = ConnectionInfos.init(ConnectionType.SERVER, path);
|
var newcon = Connection.init(Connection.Type.SERVER, path);
|
||||||
newcon.server = server;
|
newcon.server = server;
|
||||||
try self.connections.append(newcon);
|
try self.connections.append(newcon);
|
||||||
try self.pollfd.append(newfd);
|
try self.pollfd.append(newfd);
|
||||||
|
@ -391,7 +381,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, MessageType.DATA, 1, payload);
|
var m = Message.init(0, Message.Type.DATA, 1, payload);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -400,7 +390,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, MessageType.DATA, 1, payload);
|
var m = Message.init(0, Message.Type.DATA, 1, payload);
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,7 +404,7 @@ pub const Context = struct {
|
||||||
else { print("listening (no timer)\n", .{}); }
|
else { print("listening (no timer)\n", .{}); }
|
||||||
|
|
||||||
// TODO: listening to these file descriptors.
|
// TODO: listening to these file descriptors.
|
||||||
var event = Event.init(EventType.CONNECTION, 5, 8, null);
|
var event = Event.init(Event.Type.CONNECTION, 5, 8, null);
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -444,24 +434,19 @@ pub const Context = struct {
|
||||||
while(self.connections.items.len > 0) { try self.close(0); }
|
while(self.connections.items.len > 0) { try self.close(0); }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format(
|
pub fn format(self: Self, comptime form: []const u8, options: fmt.FormatOptions, out_stream: anytype) !void {
|
||||||
self: Self,
|
try fmt.format(out_stream
|
||||||
comptime fmt: []const u8,
|
|
||||||
options: std.fmt.FormatOptions,
|
|
||||||
out_stream: anytype,
|
|
||||||
) !void {
|
|
||||||
try std.fmt.format(out_stream
|
|
||||||
, "context ({} connections and {} messages):"
|
, "context ({} connections and {} messages):"
|
||||||
, .{self.connections.items.len, self.tx.items.len});
|
, .{self.connections.items.len, self.tx.items.len});
|
||||||
|
|
||||||
for (self.connections.items) |con| {
|
for (self.connections.items) |con| {
|
||||||
try std.fmt.format(out_stream, "\n- ", .{});
|
try fmt.format(out_stream, "\n- ", .{});
|
||||||
try con.format(fmt, options, out_stream);
|
try con.format(form, options, out_stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (self.tx.items) |tx| {
|
for (self.tx.items) |tx| {
|
||||||
try std.fmt.format(out_stream, "\n- ", .{});
|
try fmt.format(out_stream, "\n- ", .{});
|
||||||
try tx.format(fmt, options, out_stream);
|
try tx.format(form, options, out_stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -488,10 +473,10 @@ 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, MessageType.DATA, 1, payload);
|
// var m = Message.init(0, Message.Type.DATA, 1, payload);
|
||||||
//
|
//
|
||||||
// // type index origin message
|
// // type index origin message
|
||||||
// var e = Event.init(EventType.CONNECTION, 5, 8, &m);
|
// var e = Event.init(Event.Type.CONNECTION, 5, 8, &m);
|
||||||
|
|
||||||
var c = Context.init(allocator);
|
var c = Context.init(allocator);
|
||||||
defer c.deinit(); // There. Can't leak. Isn't Zig wonderful?
|
defer c.deinit(); // There. Can't leak. Isn't Zig wonderful?
|
||||||
|
|
Reference in New Issue