Skip to content

Commit

Permalink
Change examples to use latest version
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
fkollmann committed Mar 10, 2024
1 parent aacb484 commit 5c6354b
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 51 deletions.
2 changes: 1 addition & 1 deletion examples/dealer_rep_client/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void {

exe.addModule("zzmq", zzmq.module("zzmq"));

exe.linkSystemLibrary("czmq");
exe.linkSystemLibrary("zmq");
exe.linkLibC();

// This declares intent for the executable to be installed into the
Expand Down
4 changes: 2 additions & 2 deletions examples/dealer_rep_client/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

.dependencies = .{
.zzmq = .{
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.1.2-zig0.11.tar.gz",
.hash = "1220b8243727a827c6d9626d8965d99f62b5d1fabf658f36d409891d616fe911146d",
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.2.1-zig0.11.tar.gz",
.hash = "12207fef0f6c6be700edec8ddb813b0e41ee843f3d354d976c3d738b3e96a52aea8f",
},
},
}
25 changes: 17 additions & 8 deletions examples/dealer_rep_client/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const sig_ign = std.os.Sigaction{
pub fn main() !void {
std.log.info("Connecting to the server...", .{});

{
const version = zzmq.ZContext.version();

std.log.info("libzmq version: {}.{}.{}", .{ version.major, version.minor, version.patch });
}

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
if (gpa.deinit() == .leak)
Expand All @@ -28,26 +34,29 @@ pub fn main() !void {

const allocator = gpa.allocator();

var socket = try zzmq.ZSocket.init(allocator, zzmq.ZSocketType.Rep);
var context = try zzmq.ZContext.init(allocator);
defer context.deinit();

var socket = try zzmq.ZSocket.init(zzmq.ZSocketType.Rep, &context);
defer socket.deinit();

try socket.setSocketOption(.{ .ReceiveTimeout = 5000 });
try socket.setSocketOption(.{ .ReceiveHighWaterMark = 2 }); // only 1 per thread + 1 reserve
try socket.setSocketOption(.{ .ReceiveBufferSize = 256 }); // keep it small
try socket.setSocketOption(.{ .SendTimeout = 500 });

try std.os.sigaction(std.os.SIG.INT, &sig_ign, null); // ZSocket.init() will re-assign interrupts
try std.os.sigaction(std.os.SIG.INT, &sig_ign, null);
try std.os.sigaction(std.os.SIG.TERM, &sig_ign, null);

try socket.connect("tcp://127.0.0.1:5555");

while (!stopRunning.load(.SeqCst)) {
// Receive the request
{
var frame = try socket.receive();
defer frame.deinit();
var msg = try socket.receive(.{});
defer msg.deinit();

const data = try frame.data();
const data = try msg.data();

std.log.info("Received request: {s}", .{data});
}
Expand All @@ -59,10 +68,10 @@ pub fn main() !void {
{
std.log.info("Sending reply...", .{});

var frame = try zzmq.ZFrame.init("World");
defer frame.deinit();
var msg = try zzmq.ZMessage.initUnmanaged("World", null);
defer msg.deinit();

try socket.send(&frame, .{});
try socket.send(&msg, .{});
}
}
}
2 changes: 1 addition & 1 deletion examples/dealer_rep_server/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void {

exe.addModule("zzmq", zzmq.module("zzmq"));

exe.linkSystemLibrary("czmq");
exe.linkSystemLibrary("zmq");
exe.linkLibC();

// This declares intent for the executable to be installed into the
Expand Down
4 changes: 2 additions & 2 deletions examples/dealer_rep_server/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

.dependencies = .{
.zzmq = .{
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.1.2-zig0.11.tar.gz",
.hash = "1220b8243727a827c6d9626d8965d99f62b5d1fabf658f36d409891d616fe911146d",
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.2.1-zig0.11.tar.gz",
.hash = "12207fef0f6c6be700edec8ddb813b0e41ee843f3d354d976c3d738b3e96a52aea8f",
},
},
}
51 changes: 33 additions & 18 deletions examples/dealer_rep_server/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ fn senderThreadMain(socket: *zzmq.ZSocket, allocator: std.mem.Allocator) !void {
// Send the header (empty)
// See https://zguide.zeromq.org/docs/chapter3/#The-DEALER-to-REP-Combination
{
var frame = try zzmq.ZFrame.initEmpty();
defer frame.deinit();

while (!stopRunning.load(.SeqCst)) { // retry until a client connects
socket.send(&frame, .{ .more = true }) catch continue;
break;
var msg = try zzmq.ZMessage.initExternalEmpty();
defer msg.deinit();

socket.send(&msg, .{ .more = true }) catch |err| {
std.log.info("Waiting for first client to connect: {}", .{err});

std.time.sleep(1 * std.time.ns_per_s);
continue;
};

break; // success, exit retry loop
}
if (stopRunning.load(.SeqCst)) return;
}
Expand All @@ -30,10 +36,10 @@ fn senderThreadMain(socket: *zzmq.ZSocket, allocator: std.mem.Allocator) !void {
var body = try std.fmt.allocPrint(allocator, "Hello: {}", .{index});
defer allocator.free(body);

var frame = try zzmq.ZFrame.init(body);
defer frame.deinit();
var msg = try zzmq.ZMessage.init(allocator, body);
defer msg.deinit();

try socket.send(&frame, .{});
try socket.send(&msg, .{});
}

// wait a moment
Expand All @@ -57,6 +63,12 @@ const sig_ign = std.os.Sigaction{
pub fn main() !void {
std.log.info("Starting the server...", .{});

{
const version = zzmq.ZContext.version();

std.log.info("libzmq version: {}.{}.{}", .{ version.major, version.minor, version.patch });
}

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
if (gpa.deinit() == .leak)
Expand All @@ -65,18 +77,21 @@ pub fn main() !void {

const allocator = gpa.allocator();

var socket = try zzmq.ZSocket.init(allocator, zzmq.ZSocketType.Dealer);
var context = try zzmq.ZContext.init(allocator);
defer context.deinit();

var socket = try zzmq.ZSocket.init(zzmq.ZSocketType.Dealer, &context);
defer socket.deinit();

try socket.setSocketOption(.{ .ReceiveTimeout = 500 });
try socket.setSocketOption(.{ .SendTimeout = 500 });
try socket.setSocketOption(.{ .SendHighWaterMark = 50 });
try socket.setSocketOption(.{ .SendBufferSize = 256 }); // keep it small

try std.os.sigaction(std.os.SIG.INT, &sig_ign, null); // ZSocket.init() will re-assign interrupts
try std.os.sigaction(std.os.SIG.INT, &sig_ign, null);
try std.os.sigaction(std.os.SIG.TERM, &sig_ign, null);

_ = try socket.bind("tcp://127.0.0.1:5555");
try socket.bind("tcp://127.0.0.1:5555");

// start sending threads
const senderThread = try std.Thread.spawn(.{}, senderThreadMain, .{ socket, allocator });
Expand All @@ -88,24 +103,24 @@ pub fn main() !void {
while (!stopRunning.load(.SeqCst)) {
// Wait for the next reply
{
var frame = socket.receive() catch continue;
defer frame.deinit();
var msg = socket.receive(.{}) catch continue;
defer msg.deinit();

if (@as(usize, 0) != try frame.size() or !try frame.hasMore()) { // ignore anything that is not a delimiter frame
if (@as(usize, 0) != try msg.size() or !msg.hasMore()) { // ignore anything that is not a delimiter frame
continue;
}
}

// read the content frames
while (true) {
var frame = try socket.receive();
defer frame.deinit();
var msg = try socket.receive(.{});
defer msg.deinit();

const data = try frame.data();
const data = try msg.data();

std.log.info("Received: {s}", .{data});

if (!try frame.hasMore()) break;
if (!msg.hasMore()) break;
}
}
}
2 changes: 1 addition & 1 deletion examples/hello_world_client/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void {

exe.addModule("zzmq", zzmq.module("zzmq"));

exe.linkSystemLibrary("czmq");
exe.linkSystemLibrary("zmq");
exe.linkLibC();

// This declares intent for the executable to be installed into the
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world_client/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

.dependencies = .{
.zzmq = .{
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.1.0-zig0.11.tar.gz",
.hash = "122080e22e9823dc0a4567c71553c4884978a33877c9b3d46f4594ca5f299d534f9b",
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.2.1-zig0.11.tar.gz",
.hash = "12207fef0f6c6be700edec8ddb813b0e41ee843f3d354d976c3d738b3e96a52aea8f",
},
},
}
23 changes: 16 additions & 7 deletions examples/hello_world_client/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const zzmq = @import("zzmq");
pub fn main() !void {
std.log.info("Connecting to the server...", .{});

{
const version = zzmq.ZContext.version();

std.log.info("libzmq version: {}.{}.{}", .{ version.major, version.minor, version.patch });
}

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
if (gpa.deinit() == .leak)
Expand All @@ -12,7 +18,10 @@ pub fn main() !void {

const allocator = gpa.allocator();

var socket = try zzmq.ZSocket.init(allocator, zzmq.ZSocketType.Req);
var context = try zzmq.ZContext.init(allocator);
defer context.deinit();

var socket = try zzmq.ZSocket.init(zzmq.ZSocketType.Req, &context);
defer socket.deinit();

try socket.connect("tcp://127.0.0.1:5555");
Expand All @@ -23,18 +32,18 @@ pub fn main() !void {
{
std.log.info("Sending request {}...", .{i});

var frame = try zzmq.ZFrame.init("Hello");
defer frame.deinit();
var msg = try zzmq.ZMessage.initUnmanaged("Hello", null);
defer msg.deinit();

try socket.send(&frame, .{});
try socket.send(&msg, .{});
}

// Receive the reply
{
var frame = try socket.receive();
defer frame.deinit();
var msg = try socket.receive(.{});
defer msg.deinit();

const data = try frame.data();
const data = try msg.data();

std.log.info("Received reply {} [ {s} ]", .{ i, data });
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world_server/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn build(b: *std.Build) void {

exe.addModule("zzmq", zzmq.module("zzmq"));

exe.linkSystemLibrary("czmq");
exe.linkSystemLibrary("zmq");
exe.linkLibC();

// This declares intent for the executable to be installed into the
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world_server/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

.dependencies = .{
.zzmq = .{
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.1.0-zig0.11.tar.gz",
.hash = "122080e22e9823dc0a4567c71553c4884978a33877c9b3d46f4594ca5f299d534f9b",
.url = "https://github.com/nine-lives-later/zzmq/archive/refs/tags/v0.2.1-zig0.11.tar.gz",
.hash = "12207fef0f6c6be700edec8ddb813b0e41ee843f3d354d976c3d738b3e96a52aea8f",
},
},
}
21 changes: 15 additions & 6 deletions examples/hello_world_server/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const zzmq = @import("zzmq");
pub fn main() !void {
std.log.info("Starting the server...", .{});

{
const version = zzmq.ZContext.version();

std.log.info("libzmq version: {}.{}.{}", .{ version.major, version.minor, version.patch });
}

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer {
if (gpa.deinit() == .leak)
Expand All @@ -12,15 +18,18 @@ pub fn main() !void {

const allocator = gpa.allocator();

var socket = try zzmq.ZSocket.init(allocator, zzmq.ZSocketType.Rep);
var context = try zzmq.ZContext.init(allocator);
defer context.deinit();

var socket = try zzmq.ZSocket.init(zzmq.ZSocketType.Rep, &context);
defer socket.deinit();

_ = try socket.bind("tcp://127.0.0.1:5555");
try socket.bind("tcp://127.0.0.1:5555");

while (true) {
// Wait for next request from client
{
var frame = try socket.receive();
var frame = try socket.receive(.{});
defer frame.deinit();

const data = try frame.data();
Expand All @@ -33,10 +42,10 @@ pub fn main() !void {

// Send reply back to client
{
var frame = try zzmq.ZFrame.init("World");
defer frame.deinit();
var msg = try zzmq.ZMessage.initUnmanaged("World", null);
defer msg.deinit();

try socket.send(&frame, .{});
try socket.send(&msg, .{});
}
}
}

0 comments on commit 5c6354b

Please sign in to comment.