You are tasked with designing and implementing a class (or module) called PacketReassembler
that deals with the challenge of out-of-order network packet arrivals. Each packet is represented as an object (or dictionary) with two properties:
The packets will be received in arbitrary order. Implement the following functionality:
receive(packet)
) which takes a packet as input and stores it until it and all preceding packets have been received.For example, if the PacketReassembler
receives packets in the order:
then the system should wait after receiving packet 3 (since packet 2 is missing) and once packet 2 is received, it should output the full concatenated message corresponding to packets 1, 2, and 3.
Your solution should also consider how the class might handle high throughput and a large volume of packets arriving out of order. Write code to simulate receiving packets in an arbitrary order and demonstrate that your reassembler correctly outputs completed segments of the overall message as soon as they become available.
Feel free to design any helper functions or data structures that you find appropriate for ensuring both correctness and efficiency.