ab ovo
This commit is contained in:
131
examples/protocol_example.zig
Normal file
131
examples/protocol_example.zig
Normal file
@@ -0,0 +1,131 @@
|
||||
//! Protocol Example
|
||||
//!
|
||||
//! Demonstrates the P2P protocol message system including:
|
||||
//! - Message creation and serialization
|
||||
//! - Protocol message types
|
||||
//! - Message header structure
|
||||
//! - Basic protocol usage
|
||||
|
||||
const std = @import("std");
|
||||
const protocol = @import("protocol");
|
||||
const core = @import("core");
|
||||
|
||||
const Message = protocol.Message;
|
||||
const MessageHeader = protocol.MessageHeader;
|
||||
const MessageSerializer = protocol.MessageSerializer;
|
||||
const MessageType = protocol.MessageType;
|
||||
const PROTOCOL_VERSION = protocol.PROTOCOL_VERSION;
|
||||
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.page_allocator;
|
||||
|
||||
std.debug.print("=== P2P Protocol System Example ===\n\n", .{});
|
||||
|
||||
// Create some node IDs
|
||||
var node1: protocol.NodeId = .{0} ** 32;
|
||||
node1[0] = 0x01;
|
||||
node1[1] = 0xAB;
|
||||
|
||||
var node2: protocol.NodeId = .{0} ** 32;
|
||||
node2[0] = 0x02;
|
||||
node2[1] = 0xCD;
|
||||
|
||||
std.debug.print("1. Node IDs created:\n", .{});
|
||||
std.debug.print(" Node 1: {x}{x}...\n", .{ node1[0], node1[1] });
|
||||
std.debug.print(" Node 2: {x}{x}...\n", .{ node2[0], node2[1] });
|
||||
|
||||
// Create message header
|
||||
std.debug.print("\n2. Creating message header...\n", .{});
|
||||
const header = MessageHeader.init(.ping, 1001, node1, node2);
|
||||
std.debug.print(" Message type: ping\n", .{});
|
||||
std.debug.print(" Message ID: {d}\n", .{header.id});
|
||||
std.debug.print(" Protocol version: {d}\n", .{header.version});
|
||||
std.debug.print(" TTL: {d} hops\n", .{header.ttl});
|
||||
|
||||
// Create ping message
|
||||
std.debug.print("\n3. Creating Ping message...\n", .{});
|
||||
const ping = protocol.message.Ping.init(1000);
|
||||
std.debug.print(" Ping sequence: {d}\n", .{ping.sequence});
|
||||
|
||||
// Create pong message
|
||||
std.debug.print("\n4. Creating Pong message...\n", .{});
|
||||
const pong = protocol.message.Pong.init(1000, 50);
|
||||
std.debug.print(" Pong sequence: {d}\n", .{pong.sequence});
|
||||
std.debug.print(" RTT: {d}ms\n", .{pong.rtt});
|
||||
|
||||
// Create discovery hello
|
||||
std.debug.print("\n5. Creating Discovery Hello message...\n", .{});
|
||||
const addr = protocol.message.NetworkAddress{
|
||||
.ip = .{ 127, 0, 0, 1 } ++ .{0} ** 12,
|
||||
.port = 8080,
|
||||
};
|
||||
const hello = protocol.message.DiscoveryHello.init(PROTOCOL_VERSION, 0x01, addr);
|
||||
std.debug.print(" Version: {d}\n", .{hello.version});
|
||||
std.debug.print(" Capabilities: 0x{x}\n", .{hello.capabilities});
|
||||
std.debug.print(" Port: {d}\n", .{hello.address.port});
|
||||
|
||||
// Create data request
|
||||
std.debug.print("\n6. Creating Data Request message...\n", .{});
|
||||
const request = protocol.message.DataRequest.init(2001, "test_file.txt", 0, 65536);
|
||||
std.debug.print(" Request ID: {d}\n", .{request.request_id});
|
||||
std.debug.print(" Data ID: {s}\n", .{request.data_id});
|
||||
std.debug.print(" Offset: {d}\n", .{request.offset});
|
||||
std.debug.print(" Size: {d}\n", .{request.size});
|
||||
|
||||
// Create app data message
|
||||
std.debug.print("\n7. Creating Application Data message...\n", .{});
|
||||
const app_data = protocol.message.AppData.init(1000, "Hello from app!");
|
||||
std.debug.print(" App ID: {d}\n", .{app_data.app_id});
|
||||
std.debug.print(" Data length: {d}\n", .{app_data.data.len});
|
||||
|
||||
// Demonstrate message serialization
|
||||
std.debug.print("\n8. Message Serialization...\n", .{});
|
||||
var serializer = MessageSerializer.init(allocator);
|
||||
|
||||
// Wrap ping in Message union
|
||||
const ping_msg = Message{ .ping = ping };
|
||||
|
||||
var buffer: [1024]u8 = undefined;
|
||||
const serialized_size = serializer.serialize(ping_msg, &buffer) catch |err| {
|
||||
std.debug.print(" Serialization error: {}\n", .{err});
|
||||
return;
|
||||
};
|
||||
std.debug.print(" Serialized ping message: {d} bytes\n", .{serialized_size});
|
||||
|
||||
// Deserialize
|
||||
std.debug.print("\n9. Message Deserialization...\n", .{});
|
||||
const base_msg = serializer.deserialize(buffer[0..serialized_size]) catch |err| {
|
||||
std.debug.print(" Deserialization error: {}\n", .{err});
|
||||
return;
|
||||
};
|
||||
std.debug.print(" Deserialized message type: {}\n", .{base_msg.header.msg_type});
|
||||
std.debug.print(" Message ID: {d}\n", .{base_msg.header.id});
|
||||
|
||||
// Protocol helpers
|
||||
std.debug.print("\n10. Protocol Helpers...\n", .{});
|
||||
std.debug.print(" Ping is valid: {}\n", .{protocol.ProtocolHelpers.isValidMessageType(.ping)});
|
||||
std.debug.print(" App message type check: {}\n", .{protocol.ProtocolHelpers.isAppMessageType(.app_data)});
|
||||
std.debug.print(" Version compatible: {}\n", .{protocol.ProtocolHelpers.isVersionCompatible(PROTOCOL_VERSION)});
|
||||
|
||||
// Message categories
|
||||
std.debug.print("\n11. Message Categories...\n", .{});
|
||||
const categories = [_]MessageType{
|
||||
.ping,
|
||||
.pong,
|
||||
.discovery_hello,
|
||||
.data_request,
|
||||
.app_data,
|
||||
};
|
||||
for (categories) |msg_type| {
|
||||
std.debug.print(" {}: {s}\n", .{ msg_type, protocol.ProtocolHelpers.getMessageCategory(msg_type) });
|
||||
}
|
||||
|
||||
// Priority levels
|
||||
std.debug.print("\n12. Message Priorities...\n", .{});
|
||||
std.debug.print(" Low priority: {d}\n", .{@intFromEnum(protocol.protocol.Priority.low)});
|
||||
std.debug.print(" Normal priority: {d}\n", .{@intFromEnum(protocol.protocol.Priority.normal)});
|
||||
std.debug.print(" High priority: {d}\n", .{@intFromEnum(protocol.protocol.Priority.high)});
|
||||
std.debug.print(" Critical priority: {d}\n", .{@intFromEnum(protocol.protocol.Priority.critical)});
|
||||
|
||||
std.debug.print("\n=== Protocol Example Complete ===\n", .{});
|
||||
}
|
||||
Reference in New Issue
Block a user