/* An instance of this class represents a packet, a unit of data whose ** journey from source to destination (within the Internet) involves ** "passing through" a sequence of routers. */ public class Packet { // instance variables // ------------------ public int id; // intended to uniquely identify this packet public int size; // in bytes public int arrivalTime; public int departureTime; public int bytesRemaining; // # bytes yet to be transmitted from the // router in which it currently resides // constructor // ----------- public Packet(int id, int arrivalTime, int packetSize) { this.id = id; this.arrivalTime = arrivalTime; this.departureTime = -1; // indicates that departure has not happened this.size = packetSize; this.bytesRemaining = packetSize; } // observer // -------- @Override public String toString() { StringBuilder result = new StringBuilder(); result.append("ID:" + id); result.append("; Size:" + size); result.append("; Arrival Time:" + arrivalTime); if (departureTime != -1) { result.append("; Departure Time:" + departureTime); } else { result.append("; bytes yet to be transmitted:" + bytesRemaining); } return result.toString(); } /* Returns how many units of time this packet resided in a router. ** pre: departureTime != -1 (i.e., the packet was fully transmitted) */ public int nodeResidenceTime() { return departureTime - arrivalTime; } }