Guest User

Untitled

a guest
Jun 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. /**
  2. * Sliding window
  3. */
  4. public class ReliableSender extends SenderProtocol {
  5.  
  6. private Channel channel;
  7.  
  8. private Timer timer;
  9.  
  10. private Packet[] window;
  11.  
  12. private int windowSize;
  13.  
  14. private int base;
  15.  
  16. private double timeoutValue;
  17.  
  18. private int nextSeqNum;
  19.  
  20. ReliableSender(Channel aChannel, int aWindowSize, double aTimeout) {
  21. channel = aChannel;
  22. timer = new Timer(this);
  23. windowSize = aWindowSize;
  24. timeoutValue = aTimeout;
  25. nextSeqNum = 1;
  26. window = new Packet[windowSize];
  27. base = nextSeqNum;
  28. }
  29.  
  30. void send(Data aDataChunk) {
  31. Packet sentPacket = new Packet(nextSeqNum, aDataChunk);
  32. channel.send(sentPacket);
  33. window[nextSeqNum % windowSize] = sentPacket;
  34.  
  35. boolean windowEmpty = nextSeqNum == base;
  36. if(windowEmpty)
  37. timer.start(timeoutValue);
  38.  
  39. nextSeqNum++;
  40.  
  41. boolean windowFull = nextSeqNum % windowSize == base % windowSize;
  42. if(windowFull)
  43. blockData();
  44.  
  45. Simulator.getInstance().log("reliable sender sent " + sentPacket);
  46. }
  47.  
  48. public void receive(Packet aPacket) {
  49. Simulator.getInstance().log("reliable sender receives " + aPacket);
  50. base = aPacket.getSeqNum() + 1;
  51. acceptData();
  52. boolean windowEmpty = base == nextSeqNum;
  53. if(windowEmpty)
  54. timer.stop();
  55. else
  56. timer.start(timeoutValue);
  57. }
  58.  
  59. public void timeout(Timer aTimer) {
  60. Packet sentPacket;
  61. Simulator.getInstance().log("*** reliable sender timeouts ***");
  62. timer.start(timeoutValue);
  63. for(int i = base; i < nextSeqNum; i++) {
  64. sentPacket = window[i % windowSize];
  65. Simulator.getInstance().log("reliable sender resends " + sentPacket);
  66. channel.send(sentPacket);
  67. }
  68. }
  69. }
Add Comment
Please, Sign In to add comment