Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1.  
  2. #
  3. # Copyright 2020 University of Toronto
  4. #
  5. # Permission is hereby granted, to use this software and associated
  6. # documentation files (the "Software") in course work at the University
  7. # of Toronto, or for personal use. Other uses are prohibited, in
  8. # particular the distribution of the Software either publicly or to third
  9. # parties.
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. #
  22.  
  23. import select
  24. from senderbase import SenderBase
  25. from ece361.lab2.frame import Frame
  26.  
  27. class SlidingWindowSender(SenderBase):
  28. def __init__(self, file, destination, frame_size, timeout, maxseqnum, sender_window_size):
  29. super().__init__(file, destination, frame_size, timeout, maxseqnum)
  30. self.send_queue = []
  31. self.sender_window_size = sender_window_size
  32.  
  33. def _arqsend(self):
  34. # implementation of the Sliding Window ARQ protocol
  35. while True:
  36. # a queue is the perfect data structure for implementing sliding window
  37. while (len(self.send_queue) < self.sender_window_size):
  38. # fill up the sending window
  39. current_frame = self.get_next_frame()
  40. # add frame to queue
  41. self.send_queue.append(current_frame)
  42.  
  43. if (self.send_queue[0].data == b''):
  44. # end of file
  45. break
  46.  
  47. ''' Part 2: Implement the sliding window ARQ protocol:'''
  48.  
  49. ''' Step 1: Go through the send queue and send those frames that need to be sent/resent'''
  50. ''' Do not forget to update the frames_sent variable'''
  51. ''' Your Code'''
  52. for i in self.send_queue:
  53. i.send()
  54. self.frames_sent +=1
  55.  
  56.  
  57.  
  58. ''' Step 2: wait on all frames in the send_queue in parallel'''
  59. ''' Hint: call the wait_for_multiple_ack_nacks function'''
  60. ''' Your Code'''
  61. Frame.wait_for_multiple_ack_nacks(self.send_queue)
  62.  
  63.  
  64.  
  65.  
  66.  
  67. ''' Step 3: Go through the send queue and process ack/nack/timeout'''
  68. ''' Use the status() method to check status of the frame'''
  69. ''' Hint: In the sliding window ARQ if a frame is acked it is implied that all earlier frames are also acked even without receiving an explicit ack(why?)'''
  70. ''' Your Code'''
  71.  
  72. max_ack_nack = 0
  73. i = len(self.send_queue) -1
  74. while(i>=0):
  75. if(self.send_queue[i].status()==Frame.Status.ack_nacked):
  76. max_ack_nack = i;
  77. break
  78. i = i-1
  79.  
  80.  
  81. ''' Step 4: Remove acked frames by calling the 'pop' method on the send queue'''
  82. ''' In the sliding window ARQ there might be more than 1 acked frames in an iteration'''
  83. ''' so you might need to call 'pop' multiple times'''
  84. ''' Do not forget to update the frames_delivered variable'''
  85. ''' Your Code'''
  86. if(max_ack_nack >=0):
  87. for i in range(max_ack_nack ):
  88. self.send_queue.pop(0)
  89. self.frames_delivered = self.frames_delivered + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement