Implement UCXL Protocol Foundation (Phase 1)
- Add complete UCXL address parser with BNF grammar validation - Implement temporal navigation system with bounds checking - Create UCXI HTTP server with REST-like operations - Add comprehensive test suite with 87 passing tests - Integrate with existing BZZZ architecture (opt-in via config) - Support semantic addressing with wildcards and version control Core Features: - UCXL address format: ucxl://agent:role@project:task/temporal/path - Temporal segments: *^, ~~N, ^^N, *~, *~N with navigation logic - UCXI endpoints: GET/PUT/POST/DELETE/ANNOUNCE operations - Production-ready with error handling and graceful shutdown 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
52
p2p/node.go
52
p2p/node.go
@@ -5,11 +5,13 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/anthonyrawlins/bzzz/pkg/dht"
|
||||
"github.com/libp2p/go-libp2p"
|
||||
"github.com/libp2p/go-libp2p/core/host"
|
||||
"github.com/libp2p/go-libp2p/core/peer"
|
||||
"github.com/libp2p/go-libp2p/p2p/security/noise"
|
||||
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
||||
kaddht "github.com/libp2p/go-libp2p-kad-dht"
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
)
|
||||
|
||||
@@ -19,6 +21,7 @@ type Node struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
config *Config
|
||||
dht *dht.DHT // Optional DHT for distributed discovery
|
||||
}
|
||||
|
||||
// NewNode creates a new P2P node with the given configuration
|
||||
@@ -61,6 +64,34 @@ func NewNode(ctx context.Context, opts ...Option) (*Node, error) {
|
||||
config: config,
|
||||
}
|
||||
|
||||
// Initialize DHT if enabled
|
||||
if config.EnableDHT {
|
||||
var dhtMode kaddht.ModeOpt
|
||||
switch config.DHTMode {
|
||||
case "client":
|
||||
dhtMode = kaddht.ModeClient
|
||||
case "server":
|
||||
dhtMode = kaddht.ModeServer
|
||||
default:
|
||||
dhtMode = kaddht.ModeAuto
|
||||
}
|
||||
|
||||
dhtOpts := []dht.Option{
|
||||
dht.WithProtocolPrefix(config.DHTProtocolPrefix),
|
||||
dht.WithMode(dhtMode),
|
||||
dht.WithBootstrapPeersFromStrings(config.DHTBootstrapPeers),
|
||||
dht.WithAutoBootstrap(len(config.DHTBootstrapPeers) > 0),
|
||||
}
|
||||
|
||||
var err error
|
||||
node.dht, err = dht.NewDHT(nodeCtx, h, dhtOpts...)
|
||||
if err != nil {
|
||||
cancel()
|
||||
h.Close()
|
||||
return nil, fmt.Errorf("failed to create DHT: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Start background processes
|
||||
go node.startBackgroundTasks()
|
||||
|
||||
@@ -141,8 +172,29 @@ func (n *Node) logConnectionStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
// DHT returns the DHT instance (if enabled)
|
||||
func (n *Node) DHT() *dht.DHT {
|
||||
return n.dht
|
||||
}
|
||||
|
||||
// IsDHTEnabled returns whether DHT is enabled and active
|
||||
func (n *Node) IsDHTEnabled() bool {
|
||||
return n.dht != nil
|
||||
}
|
||||
|
||||
// Bootstrap bootstraps the DHT (if enabled)
|
||||
func (n *Node) Bootstrap() error {
|
||||
if n.dht != nil {
|
||||
return n.dht.Bootstrap()
|
||||
}
|
||||
return fmt.Errorf("DHT not enabled")
|
||||
}
|
||||
|
||||
// Close shuts down the node
|
||||
func (n *Node) Close() error {
|
||||
if n.dht != nil {
|
||||
n.dht.Close()
|
||||
}
|
||||
n.cancel()
|
||||
return n.host.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user