-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.zig
More file actions
67 lines (51 loc) · 1.86 KB
/
server.zig
File metadata and controls
67 lines (51 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const std = @import("std");
const zosc = @import("zosc");
const l = std.log.scoped(.@"zosc-example-server");
pub const io_mode = .evented;
var server: zosc.Server = undefined;
const ExampleSub = struct {
osc_subscriber: zosc.Subscriber = undefined,
pub fn init(id: usize, topic: []const u8) ExampleSub {
const impl = struct {
pub fn onNext(ptr: *zosc.Subscriber, msg: *const zosc.Message) void {
const self: *ExampleSub = @fieldParentPtr("osc_subscriber", ptr);
return self.handleOscMessage(msg);
}
};
return ExampleSub{ .osc_subscriber = zosc.Subscriber{
.id = id,
.topic = topic,
.onNextFn = impl.onNext,
} };
}
pub fn subscribe(self: *ExampleSub, publisher: *zosc.Server) !void {
try publisher.subscribe(&self.osc_subscriber);
}
pub fn handleOscMessage(self: *ExampleSub, msg: *const zosc.Message) void {
l.info("\n{f}\n -> {f}", .{ self, msg });
}
pub fn format(self: ExampleSub, writer: *std.Io.Writer) std.Io.Writer.Error!void {
try writer.print("{f}", .{self.osc_subscriber});
}
};
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
try zosc.init();
defer zosc.deinit();
server = zosc.Server{
.port = 8001,
};
try server.init(allocator);
var osc_sub = ExampleSub.init(0, "/two/strings");
try osc_sub.subscribe(&server);
// var osc_sub_2 = ExampleSub.init(1, "/red");
// try osc_sub_2.subscribe(&server);
// var osc_sub_3 = ExampleSub.init(1, "/io/0/knob/2/btn");
// try osc_sub_3.subscribe(&server);
l.info("{f}", .{osc_sub});
// l.info("{any}", .{osc_sub_2});
// l.info("{any}", .{osc_sub_3});
try server.serve();
}