Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Sender extends NetworkHost {
- /*
- * Predefined Constant (static member variables):
- *
- * int MAXDATASIZE : the maximum size of the Message data and Packet payload
- *
- *
- * Predefined Member Methods:
- *
- * void startTimer(double increment):
- * Starts a timer, which will expire in "increment" time units, causing the interrupt handler to be called. You should only call this in the Sender class.
- * void stopTimer():
- * Stops the timer. You should only call this in the Sender class.
- * void udtSend(Packet p)
- * Sends the packet "p" into the network to arrive at other host
- * void deliverData(String dataSent)
- * Passes "dataSent" up to app layer. You should only call this in the Receiver class.
- *
- * Predefined Classes:
- *
- * NetworkSimulator: Implements the core functionality of the simulator
- *
- * double getTime()
- * Returns the current time in the simulator. Might be useful for debugging. Call it as follows: NetworkSimulator.getInstance().getTime()
- * void printEventList()
- * Prints the current event list to stdout. Might be useful for debugging. Call it as follows: NetworkSimulator.getInstance().printEventList()
- *
- * Message: Used to encapsulate a message coming from the application layer
- * Constructor:
- * Message(String inputData):
- * creates a new Message containing "inputData"
- * Methods:
- * void setData(String inputData):
- * sets an existing Message's data to "inputData"
- * String getData():
- * returns the data contained in the message
- *
- * Packet: Used to encapsulate a packet
- * Constructors:
- * Packet (Packet p):
- * creates a new Packet, which is a copy of "p"
- * Packet (int seq, int ack, int check, String newPayload):
- * creates a new Packet with a sequence field of "seq", an ack field of "ack", a checksum field of "check", and a payload of "newPayload"
- * Packet (int seq, int ack, int check)
- * chreate a new Packet with a sequence field of "seq", an ack field of "ack", a checksum field of "check", and an empty payload
- * Methods:
- * void setSeqnum(int seqnum)
- * sets the Packet's sequence field to seqnum
- * void setAcknum(int acknum)
- * sets the Packet's ack field to acknum
- * void setChecksum(int checksum)
- * sets the Packet's checksum to checksum
- * void setPayload(String payload)
- * sets the Packet's payload to payload
- * int getSeqnum()
- * returns the contents of the Packet's sequence field
- * int getAcknum()
- * returns the contents of the Packet's ack field
- * int getChecksum()
- * returns the checksum of the Packet
- * String getPayload()
- * returns the Packet's payload
- *
- */
- // Add any necessary class variables here. They can hold state information for the sender.
- // Also add any necessary methods (e.g. checksum of a String)
- public static final int firstSeqNo = 1;
- private int nextSeq = firstSeqNo;
- private int windowSize = 8;
- private int windowBase = 1; // manage start of sending window
- private int sent = 0;
- private int received = 0;
- private int corrupted = 0;
- private int retransmitted = 0;
- Packet pkt;
- boolean inTransit = false;
- // translate String 'str' into an int, and add this to seq and ack to generate a checksum
- // then return that checksum
- public int calCheckSum(int seq, int ack, String str){
- int payLoad = str.hashCode(); // generate int representation of payload
- return seq + ack + payLoad; // return seq + ack + int (payload)
- }
- // This is the constructor. Don't touch!
- public Sender(int entityName) {
- super(entityName);
- }
- // This method will be called once, before any of your other sender-side methods are called.
- // It can be used to do any required initialisation (e.g. of member variables you add to control the state of the sender).
- @Override
- public void init() {
- }
- // This method will be called whenever the app layer at the sender has a message to send.
- // The job of your protocol is to ensure that the data in such a message is delivered in-order, and correctly, to the receiving application layer.
- @Override
- public void output(Message message) {
- if (inTransit == false) {
- String msg = message.getData(); // store data from message in String variable for use in payload later
- int checksum = calCheckSum(nextSeq, -1, msg);
- Packet p = new Packet(nextSeq, -1, checksum, msg);
- pkt = new Packet(p);
- udtSend(p);
- nextSeq++;
- sent++;
- inTransit = true;
- startTimer(40);
- }
- else {
- System.out.println("Package in transit...");
- }
- }
- // This method will be called whenever a packet sent from the receiver (i.e. as a result of a udtSend() being done by a receiver procedure) arrives at the sender.
- // "packet" is the (possibly corrupted) packet sent from the receiver.
- @Override
- public void input(Packet packet) {
- received++;
- inTransit = false;
- Packet p = new Packet(packet);
- int seq = p.getSeqnum();
- int ack = p.getAcknum();
- int getCheckSum = p.getChecksum();
- int checkSum = calCheckSum(seq, ack, p.getPayload());
- if (checkSum == getCheckSum){
- windowBase = p.getAcknum()+1;
- if (windowBase == nextSeq){
- stopTimer();
- check();
- }
- else {
- corrupted++;
- startTimer(40);
- }
- }
- }
- // This method will be called when the senders's timer expires (thus generating a timer interrupt).
- // You'll probably want to use this method to control the retransmission of packets.
- // See startTimer() and stopTimer(), above, for how the timer is started and stopped.
- @Override
- public void timerInterrupt() {
- // startTimer(40);
- udtSend(pkt);
- retransmitted++;
- }
- public void check() {
- System.out.println("Sent: " + sent);
- System.out.println("Received: " + received);
- System.out.println("Amount Retransmitted: " + retransmitted);
- int lostPackets = sent - received;
- System.out.println("Packets Lost: " + lostPackets);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement