main_tester/
main_tester.rs

1// dtp/examples/main_example.rs
2
3use bytes::Bytes;
4use dtp::{Frame, Header, Packet, Segment};
5
6fn main() {
7    println!("--- DTP Simple Encapsulation Example ---");
8
9    // 1. The Payload: Start with the data you want to send.
10    let my_payload = Bytes::from_static(b"Hello, DTP!");
11    println!("\n[1] Application Data: '{}'", String::from_utf8_lossy(&my_payload));
12
13    // 2. Transport Layer: Encapsulate the data in a Segment.
14    //    This layer adds source and destination port numbers.
15    let transport_segment = Segment::new(
16        Header::new(54321, 443), // Source Port (dynamic) -> Destination Port (HTTPS)
17        my_payload,
18    );
19    println!("\n[2] Encapsulated into a Transport Segment...");
20
21    // 3. Network Layer: Place the Segment into a Packet.
22    //    This layer adds source and destination IP addresses.
23    let network_packet = Packet::new(
24        Header::new(0x7F000001, 0x08080808), // 127.0.0.1 -> 8.8.8.8 (Google DNS)
25        vec![transport_segment],
26    );
27    println!("\n[3] Encapsulated into a Network Packet...");
28
29    // 4. Data Link Layer: Place the Packet into a Frame.
30    //    This is the final layer, adding MAC addresses for the local network link.
31    let datalink_frame = Frame::new(
32        Header::new(
33            [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF], // Your device's MAC Address
34            [0x11, 0x22, 0x33, 0x44, 0x55, 0x66], // The gateway/router's MAC Address
35        ),
36        vec![network_packet],
37    );
38    println!("\n[4] Encapsulated into a Data Link Frame...");
39
40    // 5. Final Result: Print the fully constructed frame.
41    //    The custom Display implementation shows the beautiful, nested structure.
42    println!("\n--- Final Frame Ready for Transmission ---\n");
43    println!("{}", datalink_frame);
44}