Advertisement
Guest User

Untitled

a guest
Nov 20th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. import random
  2. import numpy
  3. # Feel free to add any imports you might need, and note that you might
  4. # not need to use either of the ones we've provided.
  5.  
  6. from PS7_wsim import *
  7.  
  8. class CSMACWNode(WirelessNode):
  9. def __init__(self,location,network,retry):
  10. WirelessNode.__init__(self,location,network,retry)
  11. self.cw = self.network.cwmin # Initialize to cwmin
  12. self.cwmin = self.network.cwmin
  13. self.cwmax = self.network.cwmax
  14. self.curr_decision = -1
  15. self.calculatedYet = False
  16. # Set any additional state or variables here if you need them.
  17.  
  18. def channel_access(self, current_time, packet_size):
  19. # TODO: Your code here. Return true if the node should send a
  20. # packet in this timeslot (false otherwise). You can tell if
  21. # the channel is busy by using the self.network.channel_busy()
  22. # function.
  23.  
  24. if self.calculatedYet == False:
  25. self.curr_decision = random.randint(1, self.cw) + current_time
  26. self.calculatedYet = True
  27.  
  28. if (current_time == self.curr_decision) and not self.network.channel_busy():
  29. self.calculatedYet = False
  30. return True
  31. elif (current_time == self.curr_decision) and self.network.channel_busy():
  32. self.curr_decision = self.curr_decision + 1
  33. return False
  34. else:
  35. return False
  36.  
  37. def on_collision(self, packet):
  38. self.cw = min(self.cw*2.0, self.cwmax)
  39. pass
  40.  
  41. def on_success(self, packet):
  42. self.cw = max(self.cw*0.5, self.cwmin)
  43. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement