Files
Igor SirotinandClaude Opus 5 7b138bf71f feat: Messaging API (MessagingClient) (#120)
* feat(messaging): object-oriented Go mirror of the Nim MessagingClient

Adds pkg/messaging: a high-level, idiomatic Go binding for the Messaging
API, mirroring logos-delivery's Nim MessagingClient. A MessagingClient owns
a node and carries the messaging surface as methods on it — New / Start /
Stop / Close, Subscribe / Unsubscribe, Send(ctx, Envelope) (RequestID,
error) — over internal/ffi rather than exposing the raw FFI.

Events arrive on a single Events() <-chan Event with a sealed Event
interface: MessageReceivedEvent, MessageSentEvent, MessagePropagatedEvent,
MessageErrorEvent and ConnectionStatusEvent. Delivery never blocks the
library's event thread; an event is dropped when a consumer falls behind.
Config marshals to the layered configuration JSON (mode / preset /
messagingOverrides / channelsOverrides), with every field omitempty so it
can never be mistaken for the legacy flat blob.

Migrating internal/ffi to the current C ABI comes with it, because the
generated surface has moved on since the bridge was written and no longer
compiles: nim-ffi now generates the header from the {.ffi.} annotations,
argument-taking calls pass a per-call <Name>Req struct and a typed
<Name>ReplyFn, no-argument calls take a raw scalar callback, destroy is
synchronous, and the single set_event_callback has been replaced by a
per-event listener registry. The bridge now also copies every callback
string while it is still borrowed, and ignores the non-terminal
STALE_WARN progress code instead of settling the call on it.

pkg/kernel follows the same listener change, registering the three kernel
events it already consumed.

Verified against a liblogosdelivery built from logos-delivery master:
build / vet / golangci-lint / go mod tidy clean, unit tests green, and the
tagged integration test does a full create-start-subscribe-send round trip
on logos.dev, observing the message back and its propagation confirmation.

Closes #119.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CnzdnSMtHM5aLHLtDGBGR9

* review: address feedback on the MessagingClient API

- Send takes contentTopic / payload / ephemeral directly instead of an
  Envelope struct, which drops the Envelope type; ContentTopic and RequestID
  move to types.go.
- messagingEvents becomes a function returning the slice, so the set cannot
  be mutated by accident.
- Document what seals the Event interface and what that buys callers.
- Document why the received payload is decoded from a JSON integer array:
  base64 is only used on the send path and by the channel events, not by the
  messaging events.

* ci: stop golangci-lint's config verify from failing on a network timeout

golangci-lint-action runs `golangci-lint config verify` before linting, which
fetches the v2.4 JSON schema from golangci-lint.run on every run. That request
timed out on the runner and failed the gate with no lint finding behind it. An
invalid config still fails the lint run itself, so the pre-check only costs a
network dependency.

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-24 11:53:36 +01:00

102 lines
2.9 KiB
Go

// Command messaging is a runnable demonstration of the Messaging API: it
// starts a node, subscribes to a content topic, sends a message on it and
// prints every event the node reports until interrupted.
//
// Build it against a local liblogosdelivery:
//
// export LOGOS_DELIVERY_DIR=/path/to/logos-delivery
// export CGO_CFLAGS="-I$LOGOS_DELIVERY_DIR/library/"
// export CGO_LDFLAGS="-L$LOGOS_DELIVERY_DIR/build/ -Wl,-rpath,$LOGOS_DELIVERY_DIR/build/"
// go run ./examples/messaging
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/logos-messaging/logos-delivery-go-bindings/pkg/messaging"
)
const contentTopic = "/logos-delivery-go-bindings/1/example/proto"
func main() {
client, err := messaging.New(messaging.Config{
Mode: messaging.ModeCore,
Preset: messaging.PresetLogosDev,
MessagingOverrides: messaging.Overrides{
"listen-address": "0.0.0.0",
"tcp-port": 60000,
},
})
if err != nil {
log.Fatalf("create client: %v", err)
}
defer func() {
if err := client.Close(); err != nil {
log.Printf("close: %v", err)
}
}()
// Consume events for the client's whole lifetime. Events() is closed by
// Close, which ends this goroutine.
go printEvents(client.Events())
if err := client.Start(); err != nil {
log.Fatalf("start: %v", err)
}
log.Printf("node started")
if err := client.Subscribe(contentTopic); err != nil {
log.Fatalf("subscribe: %v", err)
}
log.Printf("subscribed to %s", contentTopic)
// Give the node a moment to find peers before publishing.
time.Sleep(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
requestID, err := client.Send(ctx, contentTopic, []byte("hello from logos-delivery-go-bindings"), false)
if err != nil {
log.Fatalf("send: %v", err)
}
log.Printf("sent, request id %s", requestID)
// Run until interrupted so the delivery events have time to arrive.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
<-sig
log.Printf("shutting down")
if err := client.Stop(); err != nil {
log.Printf("stop: %v", err)
}
}
// printEvents type-switches over the sealed Event interface. Keep the default
// branch: the event set grows over time.
func printEvents(events <-chan messaging.Event) {
for ev := range events {
switch e := ev.(type) {
case messaging.MessageReceivedEvent:
log.Printf("received %q on %s (hash %s)",
e.Message.Payload, e.Message.ContentTopic, e.MessageHash)
case messaging.MessageSentEvent:
log.Printf("sent %s (hash %s)", e.RequestID, e.MessageHash)
case messaging.MessagePropagatedEvent:
log.Printf("propagated %s (hash %s)", e.RequestID, e.MessageHash)
case messaging.MessageErrorEvent:
log.Printf("error %s (hash %s): %s", e.RequestID, e.MessageHash, e.Err)
case messaging.ConnectionStatusEvent:
log.Printf("connection status: %s", e.Status)
default:
log.Printf("unhandled event %T", e)
}
}
}