Skip to content

Zig: Cross-mod: bus, services and lanes

Declarations

lane.protocol

Zig
pub const protocol: u32 = c.PIER_LANE_PROTOCOL;

The lane protocol version this binding speaks.

lane.LaneInfo.json_keys

Zig
pub const json_keys = .{ .owner = "mod" };

Functions

busSubscribe

Zig
pub fn busSubscribe(topic: []const u8, comptime handler: fn ([]const u8, []const u8) bool) Error!Subscription

Calls handler(topic, payload) for every message on topic, from mods of any language, on the publisher's thread. It returns a veto: true refuses a vetoable publish and false has no opinion, and a plain publish ignores it. Namespace topics, as "plot:enter".

  • Parameters:
    • topic : []const u8
    • handler : comptime fn ([]const u8, []const u8) bool
  • Return type: Error!Subscription
  • Slots: bus_subscribe

busPublish

Zig
pub fn busPublish(topic: []const u8, payload: []const u8) Error!u32

Sends payload to every subscriber of topic and returns how many ran; 0 means nobody is listening, which is not an error.

  • Parameters:
    • topic : []const u8
    • payload : []const u8
  • Return type: Error!u32
  • Slots: bus_publish

busPublishVetoable

Zig
pub fn busPublishVetoable(topic: []const u8, payload: []const u8) Error!Vetoable

Sends payload and collects the subscribers' vetoes.

  • Parameters:
    • topic : []const u8
    • payload : []const u8
  • Return type: Error!Vetoable
  • Slots: bus_publish_vetoable

busSubscriberCount

Zig
pub fn busSubscriberCount(topic: []const u8) Error!u32

How many subscribers topic has now.

registerService

Zig
pub fn registerService(name: []const u8, comptime provider: fn ([]const u8, *const Reply) bool) Error!ServiceRegistration

Answers calls to name from mods of any language, and from native plugins through the bridge. provider(request, reply) runs on the caller's thread, also while this mod is disabled but loaded, since consumers resolve services in their own on_load.

  • Parameters:
    • name : []const u8
    • provider : comptime fn ([]const u8, *const Reply) bool
  • Return type: Error!ServiceRegistration
  • Slots: service_register

callService

Zig
pub fn callService(allocator: std.mem.Allocator, name: []const u8, request: []const u8) Error!CallOutcome

Calls another mod's service, whatever language it is written in.

  • Parameters:
    • allocator : std.mem.Allocator
    • name : []const u8
    • request : []const u8
  • Return type: Error!CallOutcome
  • Slots: service_call

listServicesJson

Zig
pub fn listServicesJson(allocator: std.mem.Allocator) Error![]u8

Every registered service as the host's JSON array of {"name","mod"}, owned by the caller.

  • Parameters:
    • allocator : std.mem.Allocator
  • Return type: Error![]u8
  • Slots: service_list

serviceCaller

Zig
pub fn serviceCaller(allocator: std.mem.Allocator) Error!?[]u8

Inside a provider, the mod whose call is running; null outside one and for a caller with no mod, such as a native plugin through the bridge.

  • Parameters:
    • allocator : std.mem.Allocator
  • Return type: Error!?[]u8
  • Slots: service_caller

lane.publish

Zig
pub fn publish(name: []const u8, fingerprint: u64, data: ?*anyopaque, table: *const anyopaque, retain: c.PierLaneRefFn, release: c.PierLaneRefFn) core.Error!Publication

Publishes a lane, exclusively under name. fingerprint is the value of the contract and must not be 0, which means anyone may connect and which the host refuses. table is the provider's table of function pointers, data the pointer each of them receives; retain and release count leases on data. The lane is withdrawn automatically at unload. A name that is taken is Refused, and the host log names the holder.

  • Parameters:
    • name : []const u8
    • fingerprint : u64
    • data : ?*anyopaque
    • table : *const anyopaque
    • retain : c.PierLaneRefFn
    • release : c.PierLaneRefFn
  • Return type: core.Error!Publication
  • Slots: lane_publish

lane.acquire

Zig
pub fn acquire(name: []const u8, fingerprint: u64) core.Error!Outcome

Acquires a lane. fingerprint must be the value the consumer computed itself from the contract; 0 is not valid and always yields fingerprint.

  • Parameters:
    • name : []const u8
    • fingerprint : u64
  • Return type: core.Error!Outcome
  • Slots: lane_acquire

lane.list

Zig
pub fn list(allocator: Allocator) core.Error!types.Owned(LaneInfo)

Every lane. It passes no pointer at all, so it suits looking at what exists first.

  • Parameters:
    • allocator : Allocator
  • Return type: core.Error!types.Owned(LaneInfo)
  • Slots: lane_list

lane.listJson

Zig
pub fn listJson(allocator: Allocator) core.Error![]u8

The raw JSON of the lane listing, unparsed, owned by the caller.

  • Parameters:
    • allocator : Allocator
  • Return type: core.Error![]u8
  • Slots: lane_list

lane.Lease

Zig
1
2
3
4
5
6
7
8
9
pub const Lease = struct {
    lease: u64,
    fingerprint: u64,
    data: ?*anyopaque,
    table: ?*const anyopaque,
    alive: ?*const u32,
    busy: ?*u32,
    // ...
};

A lease on a published lane, kept to return it.

lane.Lease.isAlive

Zig
pub fn isAlive(self: Lease) bool

Whether the provider is still there.

  • Return type: bool

lane.Lease.enter

Zig
pub fn enter(self: Lease) ?Guard

Enters the provider for one call. Null when the provider is gone. The caller makes its calls through the guard and then leaves, so the provider cannot unload underneath a call that is running:

Text Only
1
2
3
4
if (lease.enter()) |inside| {
    defer inside.leave();
    inside.table(MyTable).hello(inside.data);
}
  • Return type: ?Guard

lane.Lease.release

Zig
pub fn release(self: *Lease) core.Error!bool

Returns the lease. Only a lease this mod holds. False when the provider is already gone, since the loader has released it by then and releasing again would be a double free.

lane.Guard

Zig
1
2
3
4
pub const Guard = struct {
    lease: Lease,
    // ...
};

One call inside a provider. leave ends it.

lane.Guard.table

Zig
pub fn table(self: Guard, comptime Table: type) *const Table

The provider's table, which the contract names the type of.

  • Parameters:
    • Table : comptime type
  • Return type: *const Table

lane.Guard.data

Zig
pub fn data(self: Guard) ?*anyopaque

The pointer each function of the table receives.

  • Return type: ?*anyopaque

lane.Guard.leave

Zig
pub fn leave(self: Guard) void
  • Return type: void

Subscription

Zig
1
2
3
4
pub const Subscription = struct {
    id: u64,
    // ...
};

One bus subscription, kept to end it.

Subscription.unsubscribe

Zig
pub fn unsubscribe(self: *Subscription) Error!void

Ends the subscription; calling it twice is harmless.

Reply

Zig
1
2
3
4
5
pub const Reply = struct {
    ctx: ?*anyopaque,
    sink: c.PierStrSink,
    // ...
};

The way a service provider answers: send the reply and return true, or send the reason and return false, which the caller receives unchanged as the provider's message.

Reply.send

Zig
pub fn send(self: *const Reply, text: []const u8) void
  • Parameters:
    • text : []const u8
  • Return type: void

ServiceRegistration

Zig
1
2
3
4
pub const ServiceRegistration = struct {
    id: u64,
    // ...
};

One service this mod provides, kept to withdraw it.

ServiceRegistration.unregister

Zig
pub fn unregister(self: *ServiceRegistration) Error!void

Withdraws the service; calling it twice is harmless.

lane.Publication

Zig
1
2
3
4
pub const Publication = struct {
    id: u64,
    // ...
};

One publication. withdraw takes the lane back.

lane.Publication.withdraw

Zig
pub fn withdraw(self: *Publication) core.Error!void

Withdraws the lane; calling it twice is harmless. Every outstanding lease is released and a consumer's next liveness check sees the lane gone.

Vetoable

Zig
1
2
3
4
pub const Vetoable = struct {
    vetoed: bool, delivered: u32
    // ...
};

The result of one vetoable publish. There is no short circuit: after a veto the later subscribers still receive the message.

CallCode

Zig
1
2
3
pub const CallCode = enum {
    ok, not_found, provider_error, refused
};

How a service call ended, matching the PIER_SERVICE_* results.

CallOutcome

Zig
1
2
3
4
pub const CallOutcome = struct {
    code: CallCode, body: []u8
    // ...
};

One service call's outcome. body is the reply for ok and the provider's message for provider_error, owned by the caller.

lane.Outcome

Zig
1
2
3
4
5
6
7
8
9
pub const Outcome = union(enum) {
    lease: Lease,
    /// No lane by that name is published.
    not_found,
    /// The provider's fingerprint, which differs from the one asked for.
    fingerprint: u64,
    refused,
    // ...
};

How acquiring a lane ended.

lane.LaneInfo

Zig
1
2
3
4
5
6
7
8
9
pub const LaneInfo = struct {
    name: []const u8 = "",
    owner: []const u8 = "",
    fingerprint: []const u8 = "",
    protocol: u32 = 0,
    leases: u32 = 0,
    alive: bool = false,
    // ...
};

The registration record of a published lane.