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

116 lines
3.9 KiB
Zig

//! File Sharing Server Example
//!
//! Demonstrates a P2P file server that:
//! - Accepts connections from multiple clients
//! - Serves files to connected peers
//! - Handles file requests using the P2P API
//! - Shows connection and transfer statistics
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 Sharing Server Example ===\n\n", .{});
// Configuration for the server
var config = p2p_lib.P2PConfig.default();
config.network.listen_port = 30000;
config.file_sharing.max_concurrent_uploads = 10;
config.file_sharing.max_concurrent_downloads = 5;
std.debug.print("1. Initializing P2P server...\n", .{});
var server = try P2P.init(allocator, config);
defer server.deinit();
std.debug.print(" Server initialized\n", .{});
// Start the server
std.debug.print("\n2. Starting server...\n", .{});
try server.start();
std.debug.print(" Server started\n", .{});
// Start listening for connections
std.debug.print("\n3. Starting to listen for connections...\n", .{});
const listen_addr = try Address.parse("127.0.0.1:30001");
try server.startListening(listen_addr);
std.debug.print(" Listening on 127.0.0.1:30001\n", .{});
// Create a test file to share
std.debug.print("\n4. Creating test file to share...\n", .{});
try createTestFile("./tests/test_data/test_share.txt", "This is a test file for P2P file sharing!\nLine 2 of the file.\nLine 3 with more content.\n");
std.debug.print(" Created test_share.txt\n", .{});
// Add file to sharing system
std.debug.print("\n5. Adding file to sharing system...\n", .{});
const session_id = server.addFile("./tests/test_data/test_share.txt") catch |err| {
std.debug.print(" Could not add file (file manager may not be initialized): {}\n", .{err});
// Continue anyway for demonstration
runServerLoop(&server);
return;
};
std.debug.print(" File added with session ID: {d}\n", .{session_id});
// Start sharing
std.debug.print("\n6. Starting file sharing session...\n", .{});
server.startSharing(session_id) catch |err| {
std.debug.print(" Could not start sharing: {}\n", .{err});
};
std.debug.print(" Sharing started\n", .{});
// Run server loop
runServerLoop(&server);
// Clean up
std.debug.print("\n8. Cleaning up...\n", .{});
try server.stop();
std.debug.print(" Server stopped\n", .{});
// Delete test file
std.fs.cwd().deleteFile("./tests/test_data/test_share.txt") catch {};
std.debug.print("\n=== File Server Example Complete ===\n", .{});
}
fn runServerLoop(server: *P2P) void {
std.debug.print("\n7. Running server loop (5 iterations)...\n", .{});
var i: usize = 0;
while (i < 5) : (i += 1) {
// Process incoming connections and messages
server.update() catch |err| {
std.debug.print(" Update error: {}\n", .{err});
};
// Check for events
while (server.getEventCount() > 0) {
if (server.getNextEvent()) |event| {
std.debug.print(" Event: {}\n", .{event.event_type});
}
}
// Show statistics
std.debug.print(" [{d}] Active connections: {d}, Messages: sent={d} recv={d}\n", .{
i + 1,
server.stats.active_connections.load(.monotonic),
server.stats.messages_sent.load(.monotonic),
server.stats.messages_received.load(.monotonic),
});
// Small delay
std.Thread.sleep(100 * std.time.ns_per_ms);
}
}
fn createTestFile(path: []const u8, content: []const u8) !void {
const file = try std.fs.cwd().createFile(path, .{});
defer file.close();
try file.writeAll(content);
}