Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. public class Sender extends NetworkHost {
  2.  
  3. /*
  4. * Predefined Constant (static member variables):
  5. *
  6. * int MAXDATASIZE : the maximum size of the Message data and Packet payload
  7. *
  8. *
  9. * Predefined Member Methods:
  10. *
  11. * void startTimer(double increment):
  12. * 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.
  13. * void stopTimer():
  14. * Stops the timer. You should only call this in the Sender class.
  15. * void udtSend(Packet p)
  16. * Sends the packet "p" into the network to arrive at other host
  17. * void deliverData(String dataSent)
  18. * Passes "dataSent" up to app layer. You should only call this in the Receiver class.
  19. *
  20. * Predefined Classes:
  21. *
  22. * NetworkSimulator: Implements the core functionality of the simulator
  23. *
  24. * double getTime()
  25. * Returns the current time in the simulator. Might be useful for debugging. Call it as follows: NetworkSimulator.getInstance().getTime()
  26. * void printEventList()
  27. * Prints the current event list to stdout. Might be useful for debugging. Call it as follows: NetworkSimulator.getInstance().printEventList()
  28. *
  29. * Message: Used to encapsulate a message coming from the application layer
  30. * Constructor:
  31. * Message(String inputData):
  32. * creates a new Message containing "inputData"
  33. * Methods:
  34. * void setData(String inputData):
  35. * sets an existing Message's data to "inputData"
  36. * String getData():
  37. * returns the data contained in the message
  38. *
  39. * Packet: Used to encapsulate a packet
  40. * Constructors:
  41. * Packet (Packet p):
  42. * creates a new Packet, which is a copy of "p"
  43. * Packet (int seq, int ack, int check, String newPayload):
  44. * 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"
  45. * Packet (int seq, int ack, int check)
  46. * chreate a new Packet with a sequence field of "seq", an ack field of "ack", a checksum field of "check", and an empty payload
  47. * Methods:
  48. * void setSeqnum(int seqnum)
  49. * sets the Packet's sequence field to seqnum
  50. * void setAcknum(int acknum)
  51. * sets the Packet's ack field to acknum
  52. * void setChecksum(int checksum)
  53. * sets the Packet's checksum to checksum
  54. * void setPayload(String payload)
  55. * sets the Packet's payload to payload
  56. * int getSeqnum()
  57. * returns the contents of the Packet's sequence field
  58. * int getAcknum()
  59. * returns the contents of the Packet's ack field
  60. * int getChecksum()
  61. * returns the checksum of the Packet
  62. * String getPayload()
  63. * returns the Packet's payload
  64. *
  65. */
  66.  
  67. // Add any necessary class variables here. They can hold state information for the sender.
  68. // Also add any necessary methods (e.g. checksum of a String)
  69.  
  70. public static final int firstSeqNo = 1;
  71. private int nextSeq = firstSeqNo;
  72. private int windowSize = 8;
  73. private int windowBase = 1; // manage start of sending window
  74. private int sent = 0;
  75. private int received = 0;
  76. private int corrupted = 0;
  77. private int retransmitted = 0;
  78. Packet pkt;
  79. boolean inTransit = false;
  80.  
  81. // translate String 'str' into an int, and add this to seq and ack to generate a checksum
  82. // then return that checksum
  83. public int calCheckSum(int seq, int ack, String str){
  84. int payLoad = str.hashCode(); // generate int representation of payload
  85. return seq + ack + payLoad; // return seq + ack + int (payload)
  86. }
  87.  
  88. // This is the constructor. Don't touch!
  89. public Sender(int entityName) {
  90. super(entityName);
  91. }
  92.  
  93. // This method will be called once, before any of your other sender-side methods are called.
  94. // It can be used to do any required initialisation (e.g. of member variables you add to control the state of the sender).
  95. @Override
  96. public void init() {
  97. }
  98.  
  99. // This method will be called whenever the app layer at the sender has a message to send.
  100. // 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.
  101. @Override
  102. public void output(Message message) {
  103. if (inTransit == false) {
  104. String msg = message.getData(); // store data from message in String variable for use in payload later
  105. int checksum = calCheckSum(nextSeq, -1, msg);
  106. Packet p = new Packet(nextSeq, -1, checksum, msg);
  107. pkt = new Packet(p);
  108.  
  109. udtSend(p);
  110.  
  111. nextSeq++;
  112. sent++;
  113. inTransit = true;
  114. startTimer(40);
  115. }
  116. else {
  117. System.out.println("Package in transit...");
  118. }
  119.  
  120. }
  121.  
  122.  
  123. // 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.
  124. // "packet" is the (possibly corrupted) packet sent from the receiver.
  125. @Override
  126. public void input(Packet packet) {
  127. received++;
  128. inTransit = false;
  129.  
  130. Packet p = new Packet(packet);
  131. int seq = p.getSeqnum();
  132. int ack = p.getAcknum();
  133. int getCheckSum = p.getChecksum();
  134. int checkSum = calCheckSum(seq, ack, p.getPayload());
  135.  
  136. if (checkSum == getCheckSum){
  137. windowBase = p.getAcknum()+1;
  138.  
  139. if (windowBase == nextSeq){
  140. stopTimer();
  141. check();
  142. }
  143.  
  144. else {
  145. corrupted++;
  146. startTimer(40);
  147. }
  148. }
  149.  
  150. }
  151.  
  152.  
  153. // This method will be called when the senders's timer expires (thus generating a timer interrupt).
  154. // You'll probably want to use this method to control the retransmission of packets.
  155. // See startTimer() and stopTimer(), above, for how the timer is started and stopped.
  156. @Override
  157. public void timerInterrupt() {
  158. // startTimer(40);
  159. udtSend(pkt);
  160. retransmitted++;
  161. }
  162.  
  163.  
  164. public void check() {
  165. System.out.println("Sent: " + sent);
  166. System.out.println("Received: " + received);
  167. System.out.println("Amount Retransmitted: " + retransmitted);
  168. int lostPackets = sent - received;
  169. System.out.println("Packets Lost: " + lostPackets);
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement