//----------------------------------------------------------------- // CSE 461 Project 1 // // simulate.java: This describes the interface presented to clients // of the simulator. //----------------------------------------------------------------- //----------------------------------------------------------------- // Address: an address for a node. This implementation uses // arbitrary strings as addresses, and ensures that // they are compared with string comparison. //----------------------------------------------------------------- class Address { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Constructor: takes the address in string form // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public Address (String s) { text = s; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // toString: allows printing of addresses // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public String toString () { return text; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // equals: control comparison of addresses // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public boolean equals (Address rhs) { return text.equals (rhs.text); } private String text; } //----------------------------------------------------------------- // Packet: the data carried in a packet. Feel free to add // reasonable stuff here. //----------------------------------------------------------------- class Packet { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Addresses of the sender and receiver // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public Address sourceAddr; public Address destinationAddr; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Sequence number of this packet // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public int sequenceNum; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Is this an ack or a regular packet? // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public boolean ack; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // The data carried by the packet (<= MTU of the link) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public byte[] data; } //----------------------------------------------------------------- // PacketReceiver: clients who want ot receive packets from links // need to implement this interface //----------------------------------------------------------------- interface PacketReceiver { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Callback that a packet has arrived. Implementations of this // need to run in zero simulated time. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public void PacketArrived (Packet p); } //----------------------------------------------------------------- // Link: the generic interface to a link. Clients may call these // functions on any link. //----------------------------------------------------------------- interface Link { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Send a packet, blocking until the packet has been transmitted // (but not necessarily received). Threads calling this need // to be registered with the timer. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public void TransmitPacket (Packet p); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Query the maximum data size of packets on this link // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public int MTU (); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Add and Remove receivers. Clients need to identify // themselves using AddReceiver (passing 'this' (since they // must implement PacketReceiver) and their address). // RemoveReceiver can be used to stop receiving packets. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public void AddReceiver (PacketReceiver rcvr, Address address); public void RemoveReceiver (PacketReceiver rcvr); }