Skip to content

Agent Mesh

client listen for events

Antonio Ojeaed09ecca6d02
diff --git a/cmd/sam-node/main.go b/cmd/sam-node/main.go
index eacb3178..00bd3271 100644
--- a/cmd/sam-node/main.go
+++ b/cmd/sam-node/main.go
@@ -103,6 +103,9 @@ func main() {
// Register the mandatory sovereign auth hook
node.Host.SetStreamHandler(AuthProtocol, node.HandleAuthHandshake)
+ if err := node.ListenForMeshEvents(context.Background()); err != nil {
+ log.Fatalf("Failed to listen for mesh events: %v", err)
+ }
fmt.Printf("SAM Node Online.\nPeerID: %s\nListening on: %v\n", node.Host.ID(), listenAddrs)
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index efd50c10..89b0784d 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -2,11 +2,13 @@ package main
import (
"context"
+ "encoding/json"
"fmt"
"sync"
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
+ pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
@@ -22,6 +24,7 @@ const AuthProtocol = protocol.ID("/sam/auth/1.0.0")
type SamNode struct {
Host host.Host
DHT *dht.IpfsDHT
+ PubSub *pubsub.PubSub
Store *Store
TrustedPeers map[peer.ID]bool
mu sync.RWMutex
@@ -64,6 +67,44 @@ func (n *SamNode) SecureStreamHandler(pid protocol.ID, handler network.StreamHan
})
}
+func (n *SamNode) ListenForMeshEvents(ctx context.Context) error {
+ ps, err := pubsub.NewGossipSub(ctx, n.Host)
+ if err != nil {
+ return err
+ }
+ topic, err := ps.Join("sam/mesh/events/v1")
+ if err != nil {
+ return err
+ }
+ n.PubSub = ps
+ sub, err := topic.Subscribe()
+ if err != nil {
+ return err
+ }
+
+ go func() {
+ for {
+ msg, err := sub.Next(ctx)
+ if err != nil {
+ return
+ }
+ var ev struct {
+ PeerID peer.ID `json:"peer_id"`
+ Action string `json:"action"` // "LEFT", "BANNED"
+ }
+ if err := json.Unmarshal(msg.Data, &ev); err == nil {
+ if ev.Action == "LEFT" || ev.Action == "BANNED" {
+ n.mu.Lock()
+ delete(n.TrustedPeers, ev.PeerID)
+ n.mu.Unlock()
+ fmt.Printf("[Mesh] Evicting peer %s based on Hub gossip\n", ev.PeerID)
+ }
+ }
+ }
+ }()
+ return nil
+}
+
// HandleAuthHandshake handles incoming Identity Biscuits
func (n *SamNode) HandleAuthHandshake(s network.Stream) {
defer s.Close()