2022-02-05 13:16:44 +01:00
|
|
|
const std = @import("std");
|
|
|
|
const testing = std.testing;
|
2022-02-07 18:30:06 +01:00
|
|
|
const net = std.net;
|
2022-05-07 13:13:40 +02:00
|
|
|
const fmt = std.fmt;
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-02-08 05:59:18 +01:00
|
|
|
// TODO: file descriptors should have a specific type (but i32 is used in std.net...).
|
2022-02-06 18:11:01 +01:00
|
|
|
|
|
|
|
// TODO: path => std.XXX.YYY, not simple [] const u8
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
// TODO: both Connection and pollfd store file descriptors.
|
|
|
|
// Connection stores either Stream (server) or Address (client).
|
2022-02-08 05:59:18 +01:00
|
|
|
|
|
|
|
// TODO: API should completely obfuscate the inner structures.
|
|
|
|
// Only structures in this file should be necessary.
|
2022-02-05 13:16:44 +01:00
|
|
|
|
|
|
|
const print = std.debug.print;
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub const Messages = std.ArrayList(Message);
|
|
|
|
pub const Switches = std.ArrayList(Switch);
|
|
|
|
pub const Connections = std.ArrayList(Connection);
|
|
|
|
pub const PollFD = std.ArrayList(i32);
|
|
|
|
|
2022-02-05 13:16:44 +01:00
|
|
|
pub const IPC_TYPE = enum {
|
|
|
|
UNIX_SOCKETS
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Message = struct {
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub const Type = enum {
|
|
|
|
SERVER_CLOSE,
|
|
|
|
ERROR,
|
|
|
|
DATA,
|
|
|
|
NETWORK_LOOKUP,
|
|
|
|
};
|
|
|
|
|
|
|
|
@"type": Message.Type, // Internal message type.
|
2022-02-06 00:32:16 +01:00
|
|
|
user_type: u8, // User-defined message type (arbitrary).
|
|
|
|
fd: usize, // File descriptor concerned about this message.
|
2022-02-05 13:16:44 +01:00
|
|
|
payload: []const u8,
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
// TODO
|
|
|
|
//pub fn initFromConnection(fd: usize) Self {
|
|
|
|
// return Self{
|
|
|
|
// .@"type" = Message.Type.ERROR,
|
|
|
|
// .user_type = 8,
|
|
|
|
// .fd = fd,
|
|
|
|
// .payload = "hello",
|
|
|
|
// };
|
|
|
|
//}
|
|
|
|
|
2022-02-05 13:16:44 +01:00
|
|
|
pub fn init(fd: usize,
|
2022-05-07 13:13:40 +02:00
|
|
|
@"type": Message.Type,
|
2022-02-05 13:16:44 +01:00
|
|
|
user_type: u8,
|
|
|
|
payload: []const u8) Self {
|
|
|
|
return Message {
|
|
|
|
.fd = fd,
|
|
|
|
.@"type" = @"type",
|
|
|
|
.user_type = user_type,
|
|
|
|
.payload = payload,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
|
|
|
try fmt.format(out_stream, "fd: {}, {}, usertype {}, payload: [{s}]",
|
2022-02-06 00:32:16 +01:00
|
|
|
.{self.fd, self.@"type", self.user_type, self.payload} );
|
|
|
|
}
|
2022-02-05 13:16:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
test "Message - creation and display" {
|
2022-02-07 03:24:39 +01:00
|
|
|
print("\n", .{});
|
2022-02-05 13:16:44 +01:00
|
|
|
// fd type usertype payload
|
2022-02-06 00:32:16 +01:00
|
|
|
var s = "hello!!";
|
2022-05-07 13:13:40 +02:00
|
|
|
var m = Message.init(1, Message.Type.DATA, 3, s);
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-02-06 16:57:23 +01:00
|
|
|
print("message:\t[{}]\n", .{m});
|
2022-02-07 03:24:39 +01:00
|
|
|
print("\n", .{});
|
2022-02-05 13:16:44 +01:00
|
|
|
}
|
|
|
|
|
2022-02-06 00:32:16 +01:00
|
|
|
pub const Event = struct {
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
// Event types.
|
|
|
|
// In the main event loop, servers and clients can receive connections,
|
|
|
|
// disconnections, errors or messages from their pairs. They also can
|
|
|
|
// set a timer so the loop will allow a periodic routine (sending ping
|
|
|
|
// messages for websockets, for instance).
|
|
|
|
//
|
|
|
|
// A few other events can occur.
|
|
|
|
//
|
|
|
|
// Extra socket
|
|
|
|
// The main loop waiting for an event can be used as an unique entry
|
|
|
|
// point for socket management. libipc users can register sockets via
|
|
|
|
// ipc_add_fd allowing them to trigger an event, so events unrelated
|
|
|
|
// to libipc are managed the same way.
|
|
|
|
// Switch
|
|
|
|
// libipc can be used to create protocol-related programs, such as a
|
|
|
|
// websocket proxy allowing libipc services to be accessible online.
|
|
|
|
// To help those programs (with TCP-complient sockets), two sockets
|
|
|
|
// can be bound together, each message coming from one end will be
|
|
|
|
// automatically transfered to the other socket and a Switch event
|
|
|
|
// will be triggered.
|
|
|
|
// Look Up
|
|
|
|
// When a client establishes a connection to a service, it asks the
|
|
|
|
// ipc daemon (ipcd) to locate the service and establish a connection
|
|
|
|
// to it. This is a lookup.
|
|
|
|
|
|
|
|
pub const Type = enum {
|
|
|
|
NOT_SET, // Default. TODO: should we keep this?
|
|
|
|
ERROR, // A problem occured.
|
|
|
|
EXTRA_SOCKET, // 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.
|
|
|
|
};
|
|
|
|
|
|
|
|
@"type": Event.Type,
|
2022-02-05 13:16:44 +01:00
|
|
|
index: u32,
|
|
|
|
origin: usize,
|
|
|
|
m: ?*Message, // message pointer
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub fn init(@"type": Event.Type,
|
2022-02-05 13:16:44 +01:00
|
|
|
index: u32,
|
|
|
|
origin: usize,
|
|
|
|
m: ?*Message) Self {
|
|
|
|
return Self {
|
|
|
|
.@"type" = @"type",
|
|
|
|
.index = index,
|
|
|
|
.origin = origin,
|
|
|
|
.m = m,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set(self: *Self,
|
2022-05-07 13:13:40 +02:00
|
|
|
@"type": Event.Type,
|
2022-02-05 13:16:44 +01:00
|
|
|
index: u32,
|
|
|
|
origin: usize,
|
|
|
|
m: ?*Message) void {
|
|
|
|
self.@"type" = @"type";
|
|
|
|
self.index = index;
|
|
|
|
self.origin = origin;
|
|
|
|
self.m = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clean(self: *Self) void {
|
2022-05-07 13:13:40 +02:00
|
|
|
self.@"type" = Event.Type.NOT_SET;
|
2022-02-05 13:16:44 +01:00
|
|
|
self.index = @as(u8,0);
|
|
|
|
self.origin = @as(usize,0);
|
|
|
|
if (self.m) |message| {
|
|
|
|
message.deinit();
|
|
|
|
}
|
|
|
|
self.m = null;
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
|
|
|
try fmt.format(out_stream
|
2022-12-20 07:16:55 +01:00
|
|
|
, "{}, origin: {}, index {}, message: [{?}]"
|
2022-02-06 16:57:23 +01:00
|
|
|
, .{ self.@"type", self.origin, self.index, self.m} );
|
|
|
|
}
|
|
|
|
|
2022-02-05 13:16:44 +01:00
|
|
|
};
|
|
|
|
|
2022-02-06 16:57:23 +01:00
|
|
|
test "Event - creation and display" {
|
2022-02-07 03:24:39 +01:00
|
|
|
print("\n", .{});
|
2022-02-06 16:57:23 +01:00
|
|
|
var s = "hello!!";
|
|
|
|
// fd type usertype payload
|
2022-05-07 13:13:40 +02:00
|
|
|
var m = Message.init(1, Message.Type.DATA, 3, s);
|
2022-02-06 16:57:23 +01:00
|
|
|
// type index origin message
|
2022-05-07 13:13:40 +02:00
|
|
|
var e = Event.init(Event.Type.CONNECTION, 5, 8, &m);
|
2022-02-06 16:57:23 +01:00
|
|
|
|
|
|
|
print("event:\t[{}]\n", .{e});
|
2022-02-07 03:24:39 +01:00
|
|
|
print("\n", .{});
|
2022-02-06 16:57:23 +01:00
|
|
|
}
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-05-08 02:33:02 +02:00
|
|
|
pub const CBEvent = struct {
|
|
|
|
|
|
|
|
// CallBack Event types.
|
|
|
|
// In the main event loop, servers and clients can receive connections,
|
|
|
|
// disconnections, errors or messages from their pairs. They also can
|
|
|
|
// set a timer so the loop will allow a periodic routine (sending ping
|
|
|
|
// messages for websockets, for instance).
|
|
|
|
//
|
|
|
|
// A few other events can occur.
|
|
|
|
//
|
|
|
|
// Extra socket
|
|
|
|
// The main loop waiting for an event can be used as an unique entry
|
|
|
|
// point for socket management. libipc users can register sockets via
|
|
|
|
// ipc_add_fd allowing them to trigger an event, so events unrelated
|
|
|
|
// to libipc are managed the same way.
|
|
|
|
// Switch
|
|
|
|
// libipc can be used to create protocol-related programs, such as a
|
|
|
|
// websocket proxy allowing libipc services to be accessible online.
|
|
|
|
// To help those programs (with TCP-complient sockets), two sockets
|
|
|
|
// can be bound together, each message coming from one end will be
|
|
|
|
// automatically transfered to the other socket and a Switch event
|
|
|
|
// will be triggered.
|
|
|
|
// Look Up
|
|
|
|
// When a client establishes a connection to a service, it asks the
|
|
|
|
// ipc daemon (ipcd) to locate the service and establish a connection
|
|
|
|
// to it. This is a lookup.
|
|
|
|
|
|
|
|
// For IO callbacks (switching).
|
|
|
|
pub const Type = enum {
|
|
|
|
NO_ERROR, // No error. A message was generated.
|
|
|
|
FD_CLOSING, // The fd is closing.
|
|
|
|
FD_ERROR, // Generic error.
|
|
|
|
PARSING_ERROR, // The message was read but with errors.
|
|
|
|
IGNORE, // The message should be ignored (protocol specific).
|
|
|
|
};
|
|
|
|
|
|
|
|
@"type": CBEvent.Type,
|
|
|
|
};
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub const Connection = struct {
|
2022-02-06 00:32:16 +01:00
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub const Type = enum {
|
|
|
|
IPC, // Standard connection.
|
|
|
|
EXTERNAL, // Non IPC connection (TCP, UDP, etc.).
|
|
|
|
SERVER, // Messages received = new connections.
|
|
|
|
SWITCHED, // IO operations should go through registered callbacks.
|
|
|
|
};
|
|
|
|
|
|
|
|
@"type": Connection.Type,
|
2022-02-06 16:57:23 +01:00
|
|
|
path: ?[] const u8, // Not always needed.
|
|
|
|
|
2022-05-08 02:33:02 +02:00
|
|
|
// TODO: use these connections
|
2022-02-08 05:59:18 +01:00
|
|
|
server: ?net.StreamServer = null,
|
|
|
|
client: ?net.StreamServer.Connection = null,
|
2022-02-07 18:30:06 +01:00
|
|
|
|
2022-05-08 02:33:02 +02:00
|
|
|
// more_to_read: bool, // useless for now
|
|
|
|
|
2022-02-06 16:57:23 +01:00
|
|
|
const Self = @This();
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub fn init(@"type": Connection.Type, path: ?[] const u8) Self {
|
2022-02-06 16:57:23 +01:00
|
|
|
return Self {
|
|
|
|
.@"type" = @"type",
|
|
|
|
.path = path,
|
2022-05-08 02:33:02 +02:00
|
|
|
// .more_to_read = false, // TODO: maybe useless
|
2022-02-06 16:57:23 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-08 05:59:18 +01:00
|
|
|
pub fn deinit(self: *Self) void {
|
2022-05-08 02:33:02 +02:00
|
|
|
if (self.server) |*s| { s.deinit(); }
|
|
|
|
// if (self.client) |*c| { c.deinit(); }
|
2022-02-08 05:59:18 +01:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
2022-12-20 07:16:55 +01:00
|
|
|
try fmt.format(out_stream, "{}, path {?s}", .{ self.@"type", self.path});
|
2022-05-08 02:33:02 +02:00
|
|
|
|
2022-02-08 05:59:18 +01:00
|
|
|
if (self.server) |s| {
|
2022-05-07 13:13:40 +02:00
|
|
|
try fmt.format(out_stream, "{}" , .{s});
|
2022-02-08 05:59:18 +01:00
|
|
|
}
|
|
|
|
if (self.client) |c| {
|
2022-05-07 13:13:40 +02:00
|
|
|
try fmt.format(out_stream, "{}" , .{c});
|
2022-02-08 05:59:18 +01:00
|
|
|
}
|
2022-02-06 16:57:23 +01:00
|
|
|
}
|
2022-02-05 13:16:44 +01:00
|
|
|
};
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
test "Connection - creation and display" {
|
2022-02-07 03:24:39 +01:00
|
|
|
print("\n", .{});
|
2022-02-06 16:57:23 +01:00
|
|
|
// origin destination
|
|
|
|
var path = "/some/path";
|
2022-05-07 13:13:40 +02:00
|
|
|
var c1 = Connection.init(Connection.Type.EXTERNAL, path);
|
2022-05-08 02:33:02 +02:00
|
|
|
defer c1.deinit();
|
2022-05-07 13:13:40 +02:00
|
|
|
var c2 = Connection.init(Connection.Type.IPC , null);
|
2022-05-08 02:33:02 +02:00
|
|
|
defer c2.deinit();
|
2022-02-06 16:57:23 +01:00
|
|
|
print("connection 1:\t[{}]\n", .{c1});
|
|
|
|
print("connection 2:\t[{}]\n", .{c2});
|
2022-02-07 03:24:39 +01:00
|
|
|
print("\n", .{});
|
2022-02-06 16:57:23 +01:00
|
|
|
}
|
|
|
|
|
2022-05-08 02:33:02 +02:00
|
|
|
// TODO: default callbacks, actual switching.
|
2022-02-05 13:16:44 +01:00
|
|
|
pub const Switch = struct {
|
2022-02-06 16:57:23 +01:00
|
|
|
origin : usize,
|
|
|
|
destination : usize,
|
2022-05-08 02:33:02 +02:00
|
|
|
|
|
|
|
// orig_in: ?fn (origin: usize, m: Message) CBEvent,
|
|
|
|
// orig_out: ?fn (origin: usize, m: Message) CBEvent,
|
|
|
|
// dest_in: ?fn (origin: usize, m: Message) CBEvent,
|
|
|
|
// dest_out: ?fn (origin: usize, m: Message) CBEvent,
|
2022-02-06 16:57:23 +01:00
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
pub fn init(origin: usize, destination: usize) Self {
|
|
|
|
return Self {
|
|
|
|
.origin = origin,
|
|
|
|
.destination = destination,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void {
|
|
|
|
try fmt.format(out_stream
|
2022-02-06 16:57:23 +01:00
|
|
|
, "switch {} <-> {}"
|
|
|
|
, .{ self.origin, self.destination} );
|
|
|
|
}
|
2022-02-05 13:16:44 +01:00
|
|
|
};
|
|
|
|
|
2022-02-06 16:57:23 +01:00
|
|
|
test "Switch - creation and display" {
|
|
|
|
// origin destination
|
|
|
|
var s = Switch.init(3,8);
|
2022-05-07 13:13:40 +02:00
|
|
|
print("\nswitch:\t[{}]\n", .{s});
|
2022-02-07 03:24:39 +01:00
|
|
|
print("\n", .{});
|
2022-02-06 16:57:23 +01:00
|
|
|
}
|
|
|
|
|
2022-02-05 13:16:44 +01:00
|
|
|
// Context of the whole networking state.
|
|
|
|
pub const Context = struct {
|
2022-05-08 02:33:02 +02:00
|
|
|
pub var RUNDIR = "/run/ipc/";
|
|
|
|
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_MAX_MESSAGE_SIZE = IPC_BASE_SIZE-IPC_HEADER_SIZE;
|
|
|
|
pub const IPC_VERSION = 1;
|
|
|
|
|
2022-02-06 18:11:01 +01:00
|
|
|
allocator: std.mem.Allocator, // Memory allocator.
|
|
|
|
connections: Connections, // Keep track of connections.
|
|
|
|
|
|
|
|
// TODO: List of "pollfd" structures within cinfos,
|
|
|
|
// so we can pass it to poll(2). Share indexes with 'connections'.
|
2022-02-07 18:30:06 +01:00
|
|
|
// For now, this list doesn't do anything.
|
|
|
|
// Can even be replaced in a near future.
|
2022-05-01 00:42:17 +02:00
|
|
|
pollfd: PollFD, // File descriptors.
|
2022-02-06 18:11:01 +01:00
|
|
|
|
|
|
|
tx: Messages, // Messages to send, once their fd is available.
|
|
|
|
switchdb: ?Switches, // Relations between fd.
|
|
|
|
|
2022-05-01 00:42:17 +02:00
|
|
|
timer: ?i32 = null, // No timer by default (no TIMER event).
|
2022-02-06 18:11:01 +01:00
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
2022-02-07 03:24:39 +01:00
|
|
|
// Context initialization:
|
|
|
|
// - init structures (provide the allocator)
|
2022-05-08 02:33:02 +02:00
|
|
|
pub fn init(allocator: std.mem.Allocator) !Self {
|
2022-02-07 03:24:39 +01:00
|
|
|
print("Context init\n", .{});
|
2022-05-08 02:33:02 +02:00
|
|
|
|
2022-12-20 07:16:55 +01:00
|
|
|
var rundir = std.process.getEnvVarOwned(allocator, "RUNDIR") catch |err| switch(err) {
|
|
|
|
error.EnvironmentVariableNotFound => blk: {
|
|
|
|
print("RUNTIME variable not set, using default /tmp/runtime\n", .{});
|
|
|
|
break :blk try allocator.dupeZ(u8, "/tmp/runtime");
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return err;
|
|
|
|
},
|
|
|
|
};
|
2022-05-08 02:33:02 +02:00
|
|
|
defer allocator.free(rundir);
|
|
|
|
print("rundir: {s}\n", .{rundir});
|
|
|
|
|
2022-02-06 18:11:01 +01:00
|
|
|
return Self {
|
|
|
|
.connections = Connections.init(allocator)
|
2022-02-07 03:24:39 +01:00
|
|
|
, .pollfd = PollFD.init(allocator)
|
2022-02-06 18:11:01 +01:00
|
|
|
, .tx = Messages.init(allocator)
|
|
|
|
, .switchdb = null
|
|
|
|
, .allocator = allocator
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-07 04:35:21 +01:00
|
|
|
pub fn deinit(self: *Self) void {
|
2022-05-01 00:42:17 +02:00
|
|
|
print("context deinit\n", .{});
|
2022-02-07 04:35:21 +01:00
|
|
|
self.close_all() catch |err| switch(err){
|
|
|
|
error.IndexOutOfBounds => {
|
|
|
|
print("context.deinit(): IndexOutOfBounds\n", .{});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
self.connections.deinit();
|
|
|
|
self.pollfd.deinit();
|
|
|
|
self.tx.deinit();
|
|
|
|
if (self.switchdb) |sdb| { sdb.deinit(); }
|
|
|
|
}
|
|
|
|
|
2022-05-01 00:42:17 +02:00
|
|
|
// Both simple connection and the switched one share this code.
|
2022-05-07 13:13:40 +02:00
|
|
|
fn connect_ (self: *Self, ctype: Connection.Type, path: []const u8) !i32 {
|
2022-02-07 18:30:06 +01:00
|
|
|
var stream = try net.connectUnixSocket(path);
|
|
|
|
const newfd = stream.handle;
|
|
|
|
errdefer std.os.closeSocket(newfd);
|
2022-05-07 13:13:40 +02:00
|
|
|
var newcon = Connection.init(ctype, path);
|
2022-02-08 05:59:18 +01:00
|
|
|
newcon.client = stream;
|
2022-02-06 18:11:01 +01:00
|
|
|
try self.connections.append(newcon);
|
2022-02-07 03:24:39 +01:00
|
|
|
try self.pollfd.append(newfd);
|
|
|
|
return newfd;
|
|
|
|
}
|
|
|
|
|
2022-02-07 18:30:06 +01:00
|
|
|
// Return the new fd. Can be useful to the caller.
|
|
|
|
pub fn connect(self: *Self, path: []const u8) !i32 {
|
|
|
|
print("connection to:\t{s}\n", .{path});
|
2022-05-07 13:13:40 +02:00
|
|
|
return self.connect_ (Connection.Type.IPC, path);
|
2022-02-07 18:30:06 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:35:21 +01:00
|
|
|
// Connection to a service, but with switched with the client fd.
|
|
|
|
pub fn connection_switched(self: *Self
|
2022-05-01 00:42:17 +02:00
|
|
|
, path: [] const u8
|
|
|
|
, clientfd: i32) !i32 {
|
2022-02-07 18:30:06 +01:00
|
|
|
print("connection switched from {} to path {s}\n", .{clientfd, path});
|
2022-05-07 13:13:40 +02:00
|
|
|
var newfd = try self.connect_ (Connection.Type.SWITCHED, path);
|
2022-02-07 04:35:21 +01:00
|
|
|
// TODO: record switch.
|
|
|
|
return newfd;
|
|
|
|
}
|
|
|
|
|
2022-02-07 03:24:39 +01:00
|
|
|
// Create a unix socket.
|
2022-05-01 00:42:17 +02:00
|
|
|
// Store std lib structures in the context.
|
2022-02-07 18:30:06 +01:00
|
|
|
pub fn server_init(self: *Self, path: [] const u8) !net.StreamServer {
|
2022-02-07 03:24:39 +01:00
|
|
|
print("context server init {s}\n", .{path});
|
2022-02-07 18:30:06 +01:00
|
|
|
var server = net.StreamServer.init(.{});
|
|
|
|
var socket_addr = try net.Address.initUnix(path);
|
|
|
|
try server.listen(socket_addr);
|
|
|
|
|
|
|
|
const newfd = server.sockfd orelse return error.SocketLOL;
|
2022-05-07 13:13:40 +02:00
|
|
|
var newcon = Connection.init(Connection.Type.SERVER, path);
|
2022-02-08 05:59:18 +01:00
|
|
|
newcon.server = server;
|
2022-02-07 03:24:39 +01:00
|
|
|
try self.connections.append(newcon);
|
|
|
|
try self.pollfd.append(newfd);
|
2022-02-07 18:30:06 +01:00
|
|
|
return server;
|
2022-02-07 03:24:39 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:35:21 +01:00
|
|
|
pub fn write (self: *Self, m: Message) !void {
|
|
|
|
print("write fd {}\n", .{m.fd});
|
|
|
|
self.tx.append(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read (self: *Self, index: u32) !Message {
|
|
|
|
// TODO: read the actual content.
|
|
|
|
if (index >= self.pollfd.items.len) {
|
|
|
|
return error.IndexOutOfBounds;
|
|
|
|
}
|
|
|
|
var fd = self.pollfd[index];
|
|
|
|
print("read fd {} index {}\n", .{fd, index});
|
|
|
|
var payload = "hello!!";
|
|
|
|
// fd type usertype payload
|
2022-05-07 13:13:40 +02:00
|
|
|
var m = Message.init(0, Message.Type.DATA, 1, payload);
|
2022-02-07 04:35:21 +01:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2022-02-07 18:30:06 +01:00
|
|
|
pub fn read_fd (_: *Self, fd: i32) !Message {
|
2022-02-07 04:35:21 +01:00
|
|
|
// TODO: read the actual content.
|
|
|
|
print("read fd {}\n", .{fd});
|
|
|
|
var payload = "hello!!";
|
|
|
|
// fd type usertype payload
|
2022-05-07 13:13:40 +02:00
|
|
|
var m = Message.init(0, Message.Type.DATA, 1, payload);
|
2022-02-07 04:35:21 +01:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2022-02-07 03:24:39 +01:00
|
|
|
// Wait an event.
|
2022-05-01 00:42:17 +02:00
|
|
|
pub fn wait_event(self: *Self) !Event {
|
|
|
|
// TODO: remove these debug prints.
|
|
|
|
// for (self.pollfd.items) |fd| {
|
|
|
|
// print("listening to fd {}\n", .{fd});
|
|
|
|
// }
|
|
|
|
if (self.timer) |t| { print("listening for MAXIMUM {} us\n", .{t}); }
|
|
|
|
else { print("listening (no timer)\n", .{}); }
|
|
|
|
|
2022-02-07 04:35:21 +01:00
|
|
|
// TODO: listening to these file descriptors.
|
2022-05-07 13:13:40 +02:00
|
|
|
var event = Event.init(Event.Type.CONNECTION, 5, 8, null);
|
2022-02-07 03:24:39 +01:00
|
|
|
return event;
|
2022-02-06 18:11:01 +01:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:35:21 +01:00
|
|
|
pub fn close(self: *Self, index: usize) !void {
|
2022-05-01 00:42:17 +02:00
|
|
|
// REMINDER: connections and pollfd have the same length
|
2022-02-07 04:35:21 +01:00
|
|
|
if (index >= self.pollfd.items.len) {
|
|
|
|
return error.IndexOutOfBounds;
|
|
|
|
}
|
2022-05-01 00:42:17 +02:00
|
|
|
|
2022-02-07 04:35:21 +01:00
|
|
|
// close the connection and remove it from the two structures
|
2022-05-01 00:42:17 +02:00
|
|
|
// TODO: actually close the file descriptor.
|
|
|
|
var con = self.connections.swapRemove(index);
|
|
|
|
if (con.server) |s| {
|
|
|
|
// Remove service's UNIX socket file.
|
|
|
|
var addr = s.listen_address;
|
|
|
|
var path = std.mem.sliceTo(&addr.un.path, 0);
|
|
|
|
std.fs.cwd().deleteFile(path) catch {};
|
2022-02-07 04:35:21 +01:00
|
|
|
}
|
2022-05-01 00:42:17 +02:00
|
|
|
if (con.client) |c| {
|
|
|
|
// Close the client's socket.
|
|
|
|
c.stream.close();
|
|
|
|
}
|
|
|
|
_ = self.pollfd.swapRemove(index);
|
2022-02-07 04:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close_all(self: *Self) !void {
|
|
|
|
while(self.connections.items.len > 0) { try self.close(0); }
|
2022-02-07 01:21:46 +01:00
|
|
|
}
|
|
|
|
|
2022-05-07 13:13:40 +02:00
|
|
|
pub fn format(self: Self, comptime form: []const u8, options: fmt.FormatOptions, out_stream: anytype) !void {
|
|
|
|
try fmt.format(out_stream
|
2022-02-06 18:11:01 +01:00
|
|
|
, "context ({} connections and {} messages):"
|
|
|
|
, .{self.connections.items.len, self.tx.items.len});
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-02-06 18:11:01 +01:00
|
|
|
for (self.connections.items) |con| {
|
2022-05-07 13:13:40 +02:00
|
|
|
try fmt.format(out_stream, "\n- ", .{});
|
|
|
|
try con.format(form, options, out_stream);
|
2022-02-06 18:11:01 +01:00
|
|
|
}
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-02-06 18:11:01 +01:00
|
|
|
for (self.tx.items) |tx| {
|
2022-05-07 13:13:40 +02:00
|
|
|
try fmt.format(out_stream, "\n- ", .{});
|
|
|
|
try tx.format(form, options, out_stream);
|
2022-02-06 18:11:01 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-05 13:16:44 +01:00
|
|
|
|
2022-05-01 00:42:17 +02:00
|
|
|
// PRIVATE API
|
|
|
|
|
|
|
|
fn read_ (_: *Self, client: net.StreamServer.Connection, buf: [] u8) !usize {
|
|
|
|
return try client.stream.reader().read(buf);
|
|
|
|
}
|
|
|
|
|
2022-02-05 13:16:44 +01:00
|
|
|
};
|
|
|
|
|
2022-02-06 18:11:01 +01:00
|
|
|
|
|
|
|
// TODO
|
2022-02-07 03:24:39 +01:00
|
|
|
test "Context - creation, display and memory check" {
|
|
|
|
print("\n", .{});
|
2022-02-06 18:11:01 +01:00
|
|
|
// origin destination
|
|
|
|
//var s = Switch.init(3,8);
|
|
|
|
|
|
|
|
const config = .{.safety = true};
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(config){};
|
2022-02-07 01:21:46 +01:00
|
|
|
defer _ = gpa.deinit();
|
2022-02-06 18:11:01 +01:00
|
|
|
|
|
|
|
const allocator = gpa.allocator();
|
|
|
|
|
|
|
|
// var payload = "hello!!";
|
|
|
|
// // fd type usertype payload
|
2022-05-07 13:13:40 +02:00
|
|
|
// var m = Message.init(0, Message.Type.DATA, 1, payload);
|
2022-02-06 18:11:01 +01:00
|
|
|
//
|
|
|
|
// // type index origin message
|
2022-05-07 13:13:40 +02:00
|
|
|
// var e = Event.init(Event.Type.CONNECTION, 5, 8, &m);
|
2022-02-06 18:11:01 +01:00
|
|
|
|
2022-05-08 02:33:02 +02:00
|
|
|
var c = try Context.init(allocator);
|
2022-02-07 01:21:46 +01:00
|
|
|
defer c.deinit(); // There. Can't leak. Isn't Zig wonderful?
|
2022-02-06 18:11:01 +01:00
|
|
|
|
2022-02-07 18:30:06 +01:00
|
|
|
const path = "/tmp/.TEST_USOCK";
|
2022-02-07 03:24:39 +01:00
|
|
|
|
2022-02-07 18:30:06 +01:00
|
|
|
// SERVER SIDE: creating a service.
|
|
|
|
var server = try c.server_init(path);
|
|
|
|
defer server.deinit();
|
|
|
|
defer std.fs.cwd().deleteFile(path) catch {}; // Once done, remove file.
|
2022-02-06 18:11:01 +01:00
|
|
|
|
2022-02-07 18:30:06 +01:00
|
|
|
|
|
|
|
// CLIENT SIDE: connection to a service.
|
|
|
|
//_ = try c.connect(path);
|
|
|
|
|
|
|
|
// TODO: connection to a server, but switched with clientfd "3".
|
|
|
|
// _ = try c.connection_switched(path, 3);
|
|
|
|
|
|
|
|
// print ("Context: {}\n", .{c});
|
|
|
|
// print("\n", .{});
|
|
|
|
|
|
|
|
const S = struct {
|
|
|
|
fn clientFn() !void {
|
|
|
|
const socket = try net.connectUnixSocket(path);
|
|
|
|
defer socket.close();
|
|
|
|
print("So we're a client now... path: {s}\n", .{path});
|
|
|
|
|
|
|
|
_ = try socket.writer().writeAll("Hello world!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const t = try std.Thread.spawn(.{}, S.clientFn, .{});
|
|
|
|
defer t.join();
|
|
|
|
|
2022-02-08 05:59:18 +01:00
|
|
|
// Server.accept returns a net.StreamServer.Connection.
|
2022-02-07 18:30:06 +01:00
|
|
|
var client = try server.accept();
|
|
|
|
defer client.stream.close();
|
|
|
|
var buf: [16]u8 = undefined;
|
|
|
|
const n = try client.stream.reader().read(&buf);
|
|
|
|
|
|
|
|
try testing.expectEqual(@as(usize, 12), n);
|
|
|
|
try testing.expectEqualSlices(u8, "Hello world!", buf[0..n]);
|
2022-02-06 18:11:01 +01:00
|
|
|
}
|
|
|
|
|
2022-02-08 05:59:18 +01:00
|
|
|
|
|
|
|
// FIRST
|
|
|
|
fn create_service() !void {
|
|
|
|
const config = .{.safety = true};
|
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(config){};
|
|
|
|
defer _ = gpa.deinit();
|
|
|
|
|
|
|
|
const allocator = gpa.allocator();
|
2022-05-08 02:33:02 +02:00
|
|
|
var ctx = try Context.init(allocator);
|
2022-02-08 05:59:18 +01:00
|
|
|
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
|
|
|
|
|
|
|
|
const path = "/tmp/.TEST_USOCK";
|
|
|
|
|
|
|
|
// SERVER SIDE: creating a service.
|
2022-05-01 00:42:17 +02:00
|
|
|
_ = try ctx.server_init(path);
|
|
|
|
var event = try ctx.wait_event();
|
|
|
|
switch (event.@"type") {
|
|
|
|
.CONNECTION => {
|
|
|
|
print("New connection!\n", .{});
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
print("New event: {}\n", .{event.@"type"});
|
|
|
|
},
|
|
|
|
}
|
2022-02-08 05:59:18 +01:00
|
|
|
|
|
|
|
// Server.accept returns a net.Connection (handle = fd, addr = net.Address).
|
2022-05-01 00:42:17 +02:00
|
|
|
// var client = try server.accept();
|
|
|
|
// var buf: [4096]u8 = undefined;
|
|
|
|
// const n = try ctx.read_ (client, &buf);
|
2022-02-08 05:59:18 +01:00
|
|
|
|
2022-05-01 00:42:17 +02:00
|
|
|
// print("new client: {}\n", .{client});
|
|
|
|
// print("{} bytes: {s}\n", .{n, buf});
|
|
|
|
print("End the create_service function\n", .{});
|
2022-02-08 05:59:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() !u8 {
|
|
|
|
try create_service();
|
2022-02-05 13:16:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// export fn add(a: i32, b: i32) i32 {
|
|
|
|
// return a + b;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// test "basic add functionality" {
|
|
|
|
// try testing.expect(add(3, 7) == 10);
|
|
|
|
// }
|