Compare commits
7 Commits
ebd3e64111
...
2e06f44aa8
Author | SHA1 | Date | |
---|---|---|---|
2e06f44aa8 | |||
af788a0536 | |||
65ab46f714 | |||
1256feb7ef | |||
18205a5533 | |||
3816f32101 | |||
b101e0acd2 |
@ -54,6 +54,7 @@ const Example = struct {
|
||||
const examples = [_]Example{
|
||||
.{ .name = "request_reply", .file = "examples/request_reply.zig" },
|
||||
.{ .name = "headers", .file = "examples/headers.zig" },
|
||||
.{ .name = "pub_bytes", .file = "examples/pub_bytes.zig" },
|
||||
};
|
||||
|
||||
pub fn add_examples(b: *std.build, options: ExampleOptions) void {
|
||||
@ -64,7 +65,7 @@ pub fn add_examples(b: *std.build, options: ExampleOptions) void {
|
||||
.name = example.name,
|
||||
.root_source_file = .{ .path = example.file },
|
||||
.target = options.target,
|
||||
.optimize = options.optimize,
|
||||
.optimize = .Debug,
|
||||
});
|
||||
|
||||
ex_exe.addModule("nats", options.nats_module);
|
||||
|
@ -15,14 +15,20 @@ pub fn main() !void {
|
||||
|
||||
try message.deleteHeader("My-Key3");
|
||||
|
||||
var iter = try message.headerIterator();
|
||||
defer iter.destroy();
|
||||
{
|
||||
var iter = try message.headerIterator();
|
||||
defer iter.destroy();
|
||||
|
||||
while (try iter.next()) |pair| {
|
||||
std.debug.print(
|
||||
"Key: '{s}', Value: '{s}'\n",
|
||||
.{ pair.key, pair.value orelse "null" },
|
||||
);
|
||||
while (iter.next()) |resolv| {
|
||||
var val_iter = try resolv.getValueIterator();
|
||||
defer val_iter.destroy();
|
||||
|
||||
std.debug.print("key '{s}' got: ", .{resolv.key});
|
||||
while (val_iter.next()) |value| {
|
||||
std.debug.print("'{s}', ", .{value});
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
}
|
||||
|
||||
const subscription = try connection.subscribeSync("subject");
|
||||
@ -33,13 +39,12 @@ pub fn main() !void {
|
||||
defer received.destroy();
|
||||
|
||||
{
|
||||
const vals1 = try received.getAllHeaderValues("My-Key1");
|
||||
defer std.heap.raw_c_allocator.free(vals1);
|
||||
std.debug.print("For key 'My-Key1' got: ", .{});
|
||||
for (vals1) |value| {
|
||||
const val = std.mem.sliceTo(value, 0);
|
||||
var iter = try received.getHeaderValueIterator("My-Key1");
|
||||
defer iter.destroy();
|
||||
|
||||
std.debug.print("'{s}', ", .{val});
|
||||
std.debug.print("For key 'My-Key1' got: ", .{});
|
||||
while (iter.next()) |value| {
|
||||
std.debug.print("'{s}', ", .{value});
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
10
examples/pub_bytes.zig
Normal file
10
examples/pub_bytes.zig
Normal file
@ -0,0 +1,10 @@
|
||||
const std = @import("std");
|
||||
const nats = @import("nats");
|
||||
|
||||
pub fn main() !void {
|
||||
const connection = try nats.Connection.connectTo(nats.default_server_url);
|
||||
defer connection.destroy();
|
||||
|
||||
const data = [_]u8{ 104, 101, 108, 108, 111, 33 };
|
||||
try connection.publish("subject", &data);
|
||||
}
|
@ -15,7 +15,7 @@ fn onMessage(
|
||||
});
|
||||
|
||||
if (message.getReply()) |reply| {
|
||||
connection.publishString(reply, "salutations") catch @panic("HELP");
|
||||
connection.publish(reply, "salutations") catch @panic("HELP");
|
||||
}
|
||||
|
||||
userdata.* = true;
|
||||
@ -30,7 +30,7 @@ pub fn main() !void {
|
||||
defer subscription.destroy();
|
||||
|
||||
while (!done) {
|
||||
const reply = try connection.requestString("channel", "greetings", 1000);
|
||||
const reply = try connection.request("channel", "greetings", 1000);
|
||||
defer reply.destroy();
|
||||
|
||||
std.debug.print("Reply \"{s}\" got message: {s}\n", .{
|
||||
@ -38,4 +38,10 @@ pub fn main() !void {
|
||||
reply.getData() orelse "[null]",
|
||||
});
|
||||
}
|
||||
|
||||
const stats = try connection.getStats();
|
||||
std.debug.print(
|
||||
"Server stats => {{\n\tmessages_in: {d} ({d} B),\n\tmessages_out: {d} ({d} B),\n\treconnects: {d}\n}}\n",
|
||||
.{ stats.messages_in, stats.bytes_in, stats.messages_out, stats.bytes_out, stats.reconnects },
|
||||
);
|
||||
}
|
||||
|
@ -244,16 +244,6 @@ pub const Connection = opaque {
|
||||
).raise();
|
||||
}
|
||||
|
||||
pub fn publishString(
|
||||
self: *Connection,
|
||||
subject: [:0]const u8,
|
||||
message: [:0]const u8,
|
||||
) Error!void {
|
||||
return Status.fromInt(
|
||||
nats_c.natsConnection_PublishString(@ptrCast(self), subject.ptr, message.ptr),
|
||||
).raise();
|
||||
}
|
||||
|
||||
pub fn publishMessage(self: *Connection, message: *Message) Error!void {
|
||||
return Status.fromInt(
|
||||
nats_c.natsConnection_PublishMsg(@ptrCast(self), @ptrCast(message)),
|
||||
@ -277,22 +267,6 @@ pub const Connection = opaque {
|
||||
).raise();
|
||||
}
|
||||
|
||||
pub fn publishRequestString(
|
||||
self: *Connection,
|
||||
subject: [:0]const u8,
|
||||
reply: [:0]const u8,
|
||||
message: [:0]const u8,
|
||||
) Error!void {
|
||||
return Status.fromInt(
|
||||
nats_c.natsConnection_PublishRequestString(
|
||||
@ptrCast(self),
|
||||
subject.ptr,
|
||||
reply.ptr,
|
||||
message.ptr,
|
||||
),
|
||||
).raise();
|
||||
}
|
||||
|
||||
pub fn request(
|
||||
self: *Connection,
|
||||
subject: [:0]const u8,
|
||||
@ -313,25 +287,6 @@ pub const Connection = opaque {
|
||||
return status.toError() orelse response;
|
||||
}
|
||||
|
||||
pub fn requestString(
|
||||
self: *Connection,
|
||||
subject: [:0]const u8,
|
||||
req: [:0]const u8,
|
||||
timeout: i64,
|
||||
) Error!*Message {
|
||||
var response: *Message = undefined;
|
||||
|
||||
const status = Status.fromInt(nats_c.natsConnection_RequestString(
|
||||
@ptrCast(&response),
|
||||
@ptrCast(self),
|
||||
subject.ptr,
|
||||
req.ptr,
|
||||
timeout,
|
||||
));
|
||||
|
||||
return status.toError() orelse response;
|
||||
}
|
||||
|
||||
pub fn requestMessage(
|
||||
self: *Connection,
|
||||
req: *Message,
|
||||
|
@ -76,6 +76,10 @@ pub const Message = opaque {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn getHeaderValueIterator(self: *Message, key: [:0]const u8) Error!HeaderValueIterator {
|
||||
return .{ .values = try self.getAllHeaderValues(key) };
|
||||
}
|
||||
|
||||
pub fn getAllHeaderKeys(self: *Message) Error![][*:0]const u8 {
|
||||
var keys: [*c][*c]const u8 = undefined;
|
||||
var count: c_int = 0;
|
||||
@ -93,23 +97,56 @@ pub const Message = opaque {
|
||||
};
|
||||
}
|
||||
|
||||
pub const HeaderValueIterator = struct {
|
||||
values: [][*:0]const u8,
|
||||
index: usize = 0,
|
||||
|
||||
pub fn destroy(self: HeaderValueIterator) void {
|
||||
std.heap.raw_c_allocator.free(self.values);
|
||||
}
|
||||
|
||||
pub fn next(self: *HeaderValueIterator) ?[:0]const u8 {
|
||||
if (self.index >= self.values.len) return null;
|
||||
defer self.index += 1;
|
||||
|
||||
return std.mem.sliceTo(self.values[self.index], 0);
|
||||
}
|
||||
};
|
||||
|
||||
pub const HeaderIterator = struct {
|
||||
message: *Message,
|
||||
keys: [][*:0]const u8,
|
||||
index: usize = 0,
|
||||
|
||||
pub const ValueResolver = struct {
|
||||
message: *Message,
|
||||
key: [:0]const u8,
|
||||
|
||||
pub fn getValue(self: ValueResolver) Error![:0]const u8 {
|
||||
// TODO: if we didn't care about the lifecycle of self.message, we
|
||||
// could do catch unreachable here and make this error-free
|
||||
return try self.message.getHeaderValue(self.key);
|
||||
}
|
||||
|
||||
pub fn getValueIterator(self: ValueResolver) Error!HeaderValueIterator {
|
||||
return .{
|
||||
.values = try self.message.getAllHeaderValues(self.key),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub fn destroy(self: *HeaderIterator) void {
|
||||
std.heap.raw_c_allocator.free(self.keys);
|
||||
}
|
||||
|
||||
pub fn next(self: *HeaderIterator) Error!?struct { key: [:0]const u8, value: ?[:0]const u8 } {
|
||||
pub fn next(self: *HeaderIterator) ?ValueResolver {
|
||||
if (self.index >= self.keys.len) return null;
|
||||
defer self.index += 1;
|
||||
|
||||
const sliced = std.mem.sliceTo(self.keys[self.index], 0);
|
||||
return .{
|
||||
.message = self.message,
|
||||
.key = sliced,
|
||||
.value = try self.message.getHeaderValue(sliced),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,7 @@ pub const Message = msg_.Message;
|
||||
pub const Statistics = sta_.Statistics;
|
||||
pub const StatsCounts = sta_.StatsCounts;
|
||||
|
||||
const Status = err_.Status;
|
||||
pub const Status = err_.Status;
|
||||
pub const Error = err_.Error;
|
||||
|
||||
pub fn getVersion() [:0]const u8 {
|
||||
|
@ -29,14 +29,14 @@ pub const Statistics = opaque {
|
||||
|
||||
pub fn getCounts(self: *Statistics) Error!StatsCounts {
|
||||
var counts: StatsCounts = .{};
|
||||
const status = Status.fromInt(nats_c.natsStatistics_GetCounts)(
|
||||
self,
|
||||
const status = Status.fromInt(nats_c.natsStatistics_GetCounts(
|
||||
@ptrCast(self),
|
||||
&counts.messages_in,
|
||||
&counts.bytes_in,
|
||||
&counts.messages_out,
|
||||
&counts.bytes_out,
|
||||
&counts.reconnects,
|
||||
);
|
||||
));
|
||||
return status.toError() orelse counts;
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user