130 lines
4.3 KiB
Zig
130 lines
4.3 KiB
Zig
//! File Download Client Example
|
|
//!
|
|
//! Demonstrates a P2P file download client that:
|
|
//! - Connects to multiple peers for downloading
|
|
//! - Requests files from connected peers
|
|
//! - Shows download progress and statistics
|
|
//! - Handles download completion and cleanup
|
|
|
|
const std = @import("std");
|
|
const p2p_lib = @import("p2p");
|
|
const core = @import("core");
|
|
const files = @import("files");
|
|
|
|
const P2P = p2p_lib.P2P;
|
|
const Address = core.address.Address;
|
|
|
|
pub fn main() !void {
|
|
const allocator = std.heap.page_allocator;
|
|
|
|
std.debug.print("=== File Download Client Example ===\n\n", .{});
|
|
|
|
// Initialize download client
|
|
std.debug.print("1. Initializing download client...\n", .{});
|
|
|
|
var config = p2p_lib.P2PConfig.default();
|
|
config.network.listen_port = 32000;
|
|
config.file_sharing.max_concurrent_downloads = 5;
|
|
|
|
var client = try P2P.init(allocator, config);
|
|
defer client.deinit();
|
|
|
|
std.debug.print(" Client initialized\n", .{});
|
|
|
|
// Start client
|
|
std.debug.print("\n2. Starting client...\n", .{});
|
|
try client.start();
|
|
std.debug.print(" Client started\n", .{});
|
|
|
|
// Simulate connecting to multiple peers
|
|
std.debug.print("\n3. Simulating peer connections...\n", .{});
|
|
|
|
const peer_addrs = [_][]const u8{
|
|
"127.0.0.1:32001",
|
|
"127.0.0.1:32002",
|
|
"127.0.0.1:32003",
|
|
};
|
|
|
|
var connected_peers: usize = 0;
|
|
for (peer_addrs, 0..) |addr_str, i| {
|
|
std.debug.print(" Attempting to connect to peer {d}: {s}...\n", .{ i + 1, addr_str });
|
|
|
|
const addr = Address.parse(addr_str) catch {
|
|
std.debug.print(" Invalid address\n", .{});
|
|
continue;
|
|
};
|
|
|
|
// In a real scenario, we'd have actual peers to connect to
|
|
// This demonstrates the API usage pattern
|
|
_ = client.connectToPeer(addr) catch |err| {
|
|
std.debug.print(" Connection failed (expected without running servers): {}\n", .{err});
|
|
continue;
|
|
};
|
|
|
|
connected_peers += 1;
|
|
}
|
|
|
|
std.debug.print(" Connected to {d} peers\n", .{connected_peers});
|
|
|
|
// Demonstrate download request pattern
|
|
std.debug.print("\n4. Demonstrating download request pattern...\n", .{});
|
|
|
|
// Create a download directory
|
|
std.fs.cwd().makePath("./downloads") catch {};
|
|
|
|
// Simulate file metadata for download
|
|
const FileInfo = struct {
|
|
name: []const u8,
|
|
size: u64,
|
|
hash: [32]u8,
|
|
};
|
|
|
|
const files_to_download = [_]FileInfo{
|
|
.{ .name = "document.pdf", .size = 1024 * 1024, .hash = .{0} ** 32 },
|
|
.{ .name = "image.png", .size = 512 * 1024, .hash = .{1} ** 32 },
|
|
.{ .name = "archive.zip", .size = 2 * 1024 * 1024, .hash = .{2} ** 32 },
|
|
};
|
|
|
|
for (files_to_download, 0..) |file_info, i| {
|
|
std.debug.print(" File {d}: {s} ({d} KB)\n", .{
|
|
i + 1,
|
|
file_info.name,
|
|
file_info.size / 1024,
|
|
});
|
|
}
|
|
|
|
// Simulate download progress
|
|
std.debug.print("\n5. Simulating download progress...\n", .{});
|
|
|
|
for (files_to_download, 0..) |file_info, i| {
|
|
std.debug.print(" [{d}] Starting download: {s}\n", .{ i + 1, file_info.name });
|
|
|
|
// Simulate progress updates
|
|
var progress: u8 = 0;
|
|
while (progress < 100) : (progress += 25) {
|
|
std.debug.print(" Progress: {d}%\n", .{progress});
|
|
std.Thread.sleep(50 * std.time.ns_per_ms);
|
|
}
|
|
std.debug.print(" Progress: 100%\n", .{});
|
|
std.debug.print(" [{d}] Download complete: {s}\n", .{ i + 1, file_info.name });
|
|
}
|
|
|
|
// Show statistics
|
|
std.debug.print("\n6. Download Statistics:\n", .{});
|
|
std.debug.print(" Active downloads: {d}\n", .{client.stats.active_downloads.load(.monotonic)});
|
|
std.debug.print(" Total downloaded: {d} bytes\n", .{client.stats.total_downloaded.load(.monotonic)});
|
|
std.debug.print(" Messages sent: {d}\n", .{client.stats.messages_sent.load(.monotonic)});
|
|
std.debug.print(" Messages received: {d}\n", .{client.stats.messages_received.load(.monotonic)});
|
|
|
|
// Cleanup
|
|
std.debug.print("\n7. Cleaning up...\n", .{});
|
|
try client.stop();
|
|
|
|
// Remove downloads directory
|
|
std.fs.cwd().deleteTree("./downloads") catch {};
|
|
|
|
std.debug.print(" Client stopped\n", .{});
|
|
|
|
std.debug.print("\n=== File Download Client Complete ===\n", .{});
|
|
}
|