Implement PageReassembler Service
Description
Implement the PageReassembler class. Unlike previous data models, this is a mutable stateful class responsible for collecting incoming Packet objects and reconstructing a full Page once all fragments are received.
Technical Specifications
-
Type: Standard Java
class (not a record, as it requires mutable state).
-
Internal Storage: Use a fixed-size array
Packet[] to store fragments at their respective pagePos. This allows $O(1)$ access and clear visibility of missing slots (null).
-
Fields:
-
long pageId: The ID of the page being reassembled.
-
IpAddress srcIp: Expected source address for validation.
-
int total: Total number of packets expected.
-
int count: Current number of packets received.
-
long timeout: Absolute simulation tick when this assembler should be discarded if incomplete.
-
Key Methods:
-
boolean addPacket(Packet p): Validates metadata (ID, IP, length) and places the packet in the array. Returns true if added, false if redundant or invalid.
-
boolean isComplete(): Returns true if count == total.
-
Page package(): Produces a final Page object if complete. Throws an exception if called prematurely.
-
double getCompletionRate(): Returns the percentage of received packets.
Validation logic
- The constructor must initialize the
Packet[] array with the size of total.
addPacket must ignore packets that don't match the pageId or srcIp of the reassembler.
- Prevent duplicate packet insertion (check if
packets[pos] != null).
Tasks
Implement PageReassembler Service
Description
Implement the
PageReassemblerclass. Unlike previous data models, this is a mutable stateful class responsible for collecting incomingPacketobjects and reconstructing a fullPageonce all fragments are received.Technical Specifications
class(not a record, as it requires mutable state).Packet[]to store fragments at their respectivepagePos. This allowsnull).long pageId: The ID of the page being reassembled.IpAddress srcIp: Expected source address for validation.int total: Total number of packets expected.int count: Current number of packets received.long timeout: Absolute simulation tick when this assembler should be discarded if incomplete.boolean addPacket(Packet p): Validates metadata (ID, IP, length) and places the packet in the array. Returnstrueif added,falseif redundant or invalid.boolean isComplete(): Returnstrueifcount == total.Page package(): Produces a finalPageobject if complete. Throws an exception if called prematurely.double getCompletionRate(): Returns the percentage of received packets.Validation logic
Packet[]array with the size oftotal.addPacketmust ignore packets that don't match thepageIdorsrcIpof the reassembler.packets[pos] != null).Tasks
PageReassembler.javain theassembler(orservice) package.PageReassemblerTest.java: