Files
zigp2p/examples/network_discovery.zig
2026-02-06 21:03:59 -03:00

143 lines
4.9 KiB
Zig

//! Network Discovery Example
//!
//! Demonstrates peer discovery in the P2P network:
//! - Starting a node that listens for connections
//! - Connecting to known peers
//! - Broadcasting discovery messages
//! - Maintaining peer connections
//! - Network topology awareness
const std = @import("std");
const p2p_lib = @import("p2p");
const core = @import("core");
const network = @import("network");
const protocol = @import("protocol");
const P2P = p2p_lib.P2P;
const Address = core.address.Address;
pub fn main() !void {
const allocator = std.heap.page_allocator;
std.debug.print("=== Network Discovery Example ===\n\n", .{});
// Create node configuration
std.debug.print("1. Creating discovery node...\n", .{});
var config = p2p_lib.P2PConfig.default();
config.network.listen_port = 33000;
config.network.max_connections = 100;
var node = try P2P.init(allocator, config);
defer node.deinit();
std.debug.print(" Node initialized\n", .{});
// Start the node
std.debug.print("\n2. Starting node...\n", .{});
try node.start();
std.debug.print(" Node started\n", .{});
// Start listening for incoming connections
std.debug.print("\n3. Starting discovery listener...\n", .{});
const listen_addr = try Address.parse("127.0.0.1:33001");
try node.startListening(listen_addr);
std.debug.print(" Listening on 127.0.0.1:33001\n", .{});
// Create discovery messages
std.debug.print("\n4. Creating discovery messages...\n", .{});
// Create node ID
var node_id: protocol.NodeId = .{0} ** 32;
node_id[0] = 0xDE;
node_id[1] = 0xAD;
node_id[2] = 0xBE;
node_id[3] = 0xEF;
// Create discovery hello message
const addr = protocol.message.NetworkAddress{
.ip = .{ 127, 0, 0, 1 } ++ .{0} ** 12,
.port = 33001,
};
const hello = protocol.message.DiscoveryHello.init(
protocol.PROTOCOL_VERSION,
0x03, // Capabilities: file sharing + messaging
addr,
);
std.debug.print(" Discovery hello created:\n", .{});
std.debug.print(" Protocol version: {d}\n", .{hello.version});
std.debug.print(" Capabilities: 0x{x}\n", .{hello.capabilities});
std.debug.print(" Port: {d}\n", .{hello.address.port});
// Simulate discovery of peers
std.debug.print("\n5. Simulating peer discovery...\n", .{});
const PeerInfo = struct {
name: []const u8,
address: []const u8,
capabilities: u32,
};
const discovered_peers = [_]PeerInfo{
.{ .name = "Peer-Alpha", .address = "192.168.1.10:33001", .capabilities = 0x03 },
.{ .name = "Peer-Beta", .address = "192.168.1.11:33001", .capabilities = 0x01 },
.{ .name = "Peer-Gamma", .address = "192.168.1.12:33001", .capabilities = 0x07 },
};
for (discovered_peers, 0..) |peer, i| {
std.debug.print(" [{d}] Discovered: {s}\n", .{ i + 1, peer.name });
std.debug.print(" Address: {s}\n", .{peer.address});
std.debug.print(" Capabilities: 0x{x}\n", .{peer.capabilities});
}
// Run discovery loop
std.debug.print("\n6. Running discovery loop (5 iterations)...\n", .{});
for (0..5) |i| {
// Process network events
node.update() catch |err| {
std.debug.print(" Update error: {}\n", .{err});
};
// Check for events
while (node.getEventCount() > 0) {
if (node.getNextEvent()) |event| {
std.debug.print(" [{d}] Event: {}\n", .{ i + 1, event.event_type });
}
}
// Show current state
std.debug.print(" [{d}] Active connections: {d}\n", .{
i + 1,
node.stats.active_connections.load(.monotonic),
});
std.Thread.sleep(100 * std.time.ns_per_ms);
}
// Show discovery statistics
std.debug.print("\n7. Discovery Statistics:\n", .{});
std.debug.print(" State: {}\n", .{node.state});
std.debug.print(" Active connections: {d}\n", .{node.stats.active_connections.load(.monotonic)});
std.debug.print(" Total connections: {d}\n", .{node.stats.total_connections.load(.monotonic)});
std.debug.print(" Messages sent: {d}\n", .{node.stats.messages_sent.load(.monotonic)});
std.debug.print(" Messages received: {d}\n", .{node.stats.messages_received.load(.monotonic)});
// Demonstrate peer state tracking
std.debug.print("\n8. Peer State Tracking:\n", .{});
std.debug.print(" PeerState types available:\n", .{});
std.debug.print(" - Disconnected\n", .{});
std.debug.print(" - Connecting\n", .{});
std.debug.print(" - Connected\n", .{});
std.debug.print(" - Authenticated\n", .{});
std.debug.print(" - Active\n", .{});
// Cleanup
std.debug.print("\n9. Cleaning up...\n", .{});
try node.stop();
std.debug.print(" Node stopped\n", .{});
std.debug.print("\n=== Network Discovery Complete ===\n", .{});
}