Obsolete
/
libipc-old
Archived
3
0
Fork 0

Pong client: code is fine.

master
Philippe Pittoli 2023-01-05 11:07:29 +01:00
parent 49b1b3bab2
commit 4bbd5fc686
1 changed files with 28 additions and 19 deletions

View File

@ -4,13 +4,9 @@ const net = std.net;
const fmt = std.fmt; const fmt = std.fmt;
const os = std.os; const os = std.os;
const ipc = @import("./main.zig");
const builtin = @import("builtin");
const native_os = builtin.target.os.tag;
const print = std.debug.print; const print = std.debug.print;
const testing = std.testing; const ipc = @import("./main.zig");
const print_eq = @import("./util.zig").print_eq; const Message = ipc.Message;
pub fn main() !u8 { pub fn main() !u8 {
const config = .{.safety = true}; const config = .{.safety = true};
@ -21,36 +17,49 @@ pub fn main() !u8 {
var ctx = try ipc.Context.init(allocator); var ctx = try ipc.Context.init(allocator);
defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful? defer ctx.deinit(); // There. Can't leak. Isn't Zig wonderful?
// TODO: currently using a bullshit path. // The service to contact, either provided with the SERVICE envvar
const path = "/tmp/.TEST_USOCK"; // or simply using "pong".
var should_free_service_to_contact: bool = true;
var service_to_contact = std.process.getEnvVarOwned(allocator, "SERVICE") catch blk: {
should_free_service_to_contact = false;
break :blk "pong";
};
defer {
if (should_free_service_to_contact)
allocator.free(service_to_contact);
}
// CLIENT SIDE: connecting to a service. var pongfd = try ctx.connect_ipc(service_to_contact);
var sfd = try ctx.connect (path); var message = try Message.init(pongfd, Message.Type.DATA, allocator, "bounce me");
try ctx.schedule(message);
var first_msg = try ipc.Message.init(sfd, ipc.Message.Type.DATA, allocator, "hello this is pong!!");
try ctx.write(first_msg);
first_msg.deinit();
var some_event: ipc.Event = undefined; var some_event: ipc.Event = undefined;
ctx.timer = 10000; // 10 seconds ctx.timer = 2000; // 2 seconds
while(true) { while(true) {
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("Timer!\n", .{});
}, },
.MESSAGE => { .MESSAGE => {
if (some_event.m) |m| { if (some_event.m) |m| {
print("Message received: {}\n", .{m}); print("message has been bounced: {}\n", .{m});
m.deinit(); m.deinit();
break;
} }
else { else {
print("A message should have been received, weird\n", .{}); print("Received empty message, ERROR.\n", .{});
break;
} }
break;
}, },
.TX => {
print("Message sent.\n", .{});
},
else => { else => {
print("Unexpected message: {s}\n", .{some_event}); print("Unexpected event: {}, let's suicide\n", .{some_event});
break; break;
}, },
} }