//! Basic File Share Example //! //! Demonstrates simple peer-to-peer file sharing between two P2P nodes: //! - Setting up server and client P2P instances //! - Connecting peers together //! - Sharing a file from server to client //! - Basic file transfer concepts const std = @import("std"); const p2p_lib = @import("p2p"); const core = @import("core"); const P2P = p2p_lib.P2P; const Address = core.address.Address; pub fn main() !void { const allocator = std.heap.page_allocator; std.debug.print("=== Basic File Share Example ===\n\n", .{}); // Create two P2P instances - one server, one client std.debug.print("1. Creating P2P instances...\n", .{}); var server_config = p2p_lib.P2PConfig.default(); server_config.network.listen_port = 31000; var client_config = p2p_lib.P2PConfig.default(); client_config.network.listen_port = 31001; var server = try P2P.init(allocator, server_config); defer server.deinit(); var client = try P2P.init(allocator, client_config); defer client.deinit(); std.debug.print(" Server and client initialized\n", .{}); // Start both instances std.debug.print("\n2. Starting P2P instances...\n", .{}); try server.start(); try client.start(); std.debug.print(" Both instances started\n", .{}); // Server starts listening std.debug.print("\n3. Server starting to listen...\n", .{}); const server_addr = try Address.parse("127.0.0.1:31002"); try server.startListening(server_addr); std.debug.print(" Server listening on 127.0.0.1:31002\n", .{}); // Client connects to server std.debug.print("\n4. Client connecting to server...\n", .{}); const peer_id = try client.connectToPeer(server_addr); std.debug.print(" Connected! Peer ID: {x}\n", .{peer_id.bytes[0..4].*}); // Server accepts std.debug.print("\n5. Server accepting connection...\n", .{}); try server.update(); std.debug.print(" Connection accepted\n", .{}); // Create test file std.debug.print("\n6. Creating test file...\n", .{}); const test_content = "Hello! This is a test file being shared over P2P.\n" ++ "It contains multiple lines of text.\n" ++ "The P2P library handles chunking and transfer.\n"; try createFile("./tests/test_data/share_test.txt", test_content); std.debug.print(" Created share_test.txt ({d} bytes)\n", .{test_content.len}); // Add file for sharing (if file manager is available) std.debug.print("\n7. Adding file to sharing system...\n", .{}); const session_result = server.addFile("./tests/test_data/share_test.txt"); if (session_result) |session_id| { std.debug.print(" File added with session ID: {d}\n", .{session_id}); // Start sharing server.startSharing(session_id) catch |err| { std.debug.print(" Could not start sharing: {}\n", .{err}); }; } else |err| { std.debug.print(" File sharing not available: {}\n", .{err}); } // Simulate message exchange std.debug.print("\n8. Simulating message exchange...\n", .{}); // Client sends file request message const file_request = "REQUEST:share_test.txt"; const msg_id = try client.sendMessage(peer_id, 0x10, file_request); std.debug.print(" Client sent file request (msg ID: {d})\n", .{msg_id}); // Server processes try server.update(); std.debug.print(" Server processed request\n", .{}); // Exchange a few more messages for (0..3) |i| { const chunk_msg = try std.fmt.allocPrint(allocator, "DATA_CHUNK:{d}", .{i}); defer allocator.free(chunk_msg); // In a real scenario, server would send file chunks // For this demo, we just show the concept std.debug.print(" Simulated chunk {d} transfer\n", .{i + 1}); } // Show final statistics std.debug.print("\n9. Transfer Statistics:\n", .{}); std.debug.print(" Server - Connections: {d}, Messages sent: {d}, recv: {d}\n", .{ server.stats.active_connections.load(.monotonic), server.stats.messages_sent.load(.monotonic), server.stats.messages_received.load(.monotonic), }); std.debug.print(" Client - Connections: {d}, Messages sent: {d}, recv: {d}\n", .{ client.stats.active_connections.load(.monotonic), client.stats.messages_sent.load(.monotonic), client.stats.messages_received.load(.monotonic), }); // Cleanup std.debug.print("\n10. Cleaning up...\n", .{}); try client.disconnectPeer(peer_id); try client.stop(); try server.stop(); std.fs.cwd().deleteFile("./tests/test_data/share_test.txt") catch {}; std.debug.print(" Clean up complete\n", .{}); std.debug.print("\n=== Basic File Share Complete ===\n", .{}); } fn createFile(path: []const u8, content: []const u8) !void { const file = try std.fs.cwd().createFile(path, .{}); defer file.close(); try file.writeAll(content); }