Compare commits

..

7 Commits

Author SHA1 Message Date
2e06f44aa8
examples: add pub_bytes
This was the point that I realized there's no reason to have the string
variants of the publishing methods. But also there's not really much
point in porting the other getting-started examples, since we've
covered all their functionality in the existing examples
(actually, this one is redundant too, but I have already done it, so
it's getting grandfathered in).

Porting some of the more interesting examples might be a good idea, but
those have a weird argument parser that I don't really want to port
(even though it is very simple in the way that it works). For the most
part, I think writing unit tests will do a better of flexing the
bindings.
2023-08-23 22:35:27 -07:00
af788a0536
nats: export Status
There's not much of a reason this should be used externally, but I also
favor having things be exported rather than not exported. There will
probably be more opening up of the other imports here in the future.
2023-08-23 22:28:44 -07:00
65ab46f714
connection: eliminate String method variants
After contemplating this for a little bit, there's no point in exposing
these separately from the plain variants. Even nats.c internally calls
its plain variants after calling `strlen` on the input. Zig benefits
from having nicer pointer types than C, so we get "string" handling
for free from the fact that the standard variants take slices anyway.
2023-08-23 22:27:33 -07:00
1256feb7ef
examples: always build in debug mode
There's no reason I can think of for these to be optimized.
2023-08-23 22:25:36 -07:00
18205a5533
examples: add statistics to request_reply
I should probably write some tests instead.
2023-08-23 22:18:45 -07:00
3816f32101
statistics: fix a remarkably bungled wrapper
keyboards are hard.
2023-08-23 22:18:03 -07:00
b101e0acd2
message: more iterators, less problems
This seems like a pretty nice way to do this, since it lets us produce
the sliced version of each value rather than the somewhat janky many
pointer.
2023-08-23 22:03:25 -07:00
8 changed files with 81 additions and 67 deletions

View File

@ -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);

View File

@ -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
View 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);
}

View File

@ -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 },
);
}

View File

@ -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,

View File

@ -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),
};
}

View File

@ -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 {

View File

@ -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;
}
};