Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. """
  2. Your awesome Distance Vector router for CS 168
  3. """
  4.  
  5. import sim.api as api
  6. import sim.basics as basics
  7.  
  8.  
  9. # We define infinity as a distance of 16.
  10. INFINITY = 16
  11.  
  12.  
  13. class DVRouter (basics.DVRouterBase):
  14. #NO_LOG = True # Set to True on an instance to disable its logging
  15. #POISON_MODE = True # Can override POISON_MODE here
  16. #DEFAULT_TIMER_INTERVAL = 5 # Can override this yourself for testing
  17.  
  18. def __init__ (self):
  19. """
  20. Called when the instance is initialized.
  21.  
  22. You probably want to do some additional initialization here.
  23. """
  24. self.start_timer() # Starts calling handle_timer() at correct rate
  25. self.DV = {}
  26. self.links = {}
  27. self.poisoned = {}
  28.  
  29. def handle_link_up (self, port, latency):
  30. """
  31. Called by the framework when a link attached to this Entity goes up.
  32.  
  33. The port attached to the link and the link latency are passed in.
  34. """
  35. self.links[port] = latency
  36. for x in self.DV.keys():
  37. helpNeighbor = basics.RoutePacket(x, self.DV[x][0])
  38. self.send(helpNeighbor, port, flood=False)
  39. #print "NEIGHBORTIME %s" % port, self
  40.  
  41.  
  42.  
  43. def handle_link_down (self, port):
  44. """
  45. Called by the framework when a link attached to this Entity does down.
  46.  
  47. The port number used by the link is passed in.
  48. """
  49. del self.links[port]
  50. if not self.POISON_MODE:
  51. for x in self.DV.keys():
  52. if self.DV[x][2] == port:
  53. del self.DV[x]
  54. else:
  55. for x in self.DV.keys():
  56. if self.DV[x][2] == port:
  57. self.poisoned[x] = self.DV[x]
  58. routePoison = basics.RoutePacket(x, INFINITY)
  59. self.send(routePoison, port=None, flood=True)
  60. del self.DV[x]
  61. #maybe dont send to next hop? idk
  62.  
  63. def handle_rx (self, packet, port):
  64. """
  65. Called by the framework when this Entity receives a packet.
  66.  
  67. packet is a Packet (or subclass).
  68. port is the port number it arrived on.
  69. You definitely want to fill this in.
  70. """
  71. #self.log("RX %s on %s (self: %s) (src: %s)", packet, port, self, packet.src)
  72. if isinstance(packet, basics.RoutePacket):
  73. if(packet.latency + self.links[port] < INFINITY):
  74. if ((self.DV.get(packet.destination) == None) or (self.DV[packet.destination][0] > packet.latency + self.links[port])):
  75. newPacket = basics.RoutePacket(packet.destination, self.links[port] + packet.latency)
  76. self.DV[packet.destination] = [newPacket.latency, api.current_time(), port]
  77. self.send(newPacket, port, flood=True)
  78. if self.POISON_MODE:
  79. poison = basics.RoutePacket(packet.destination, INFINITY)
  80. self.send(poison, port, flood=False)
  81. else:
  82. if self.DV[packet.destination][2] == port:
  83. self.DV[packet.destination][1] = api.current_time()
  84. if packet.latency + self.links[port] > self.DV[packet.destination][0]:
  85. self.DV[packet.destination][0] = packet.latency + self.links[port]
  86. newPacket = basics.RoutePacket(packet.destination, self.links[port] + packet.latency)
  87. self.send(newPacket, port, flood=True)
  88. if self.POISON_MODE:
  89. poison = basics.RoutePacket(packet.destination, INFINITY)
  90. self.send(poison, port, flood=False)
  91. if self.poisoned.get(packet.destination) != None and self.POISON_MODE:
  92. del self.poisoned[packet.destination]
  93. elif packet.latency >= INFINITY and self.POISON_MODE:
  94. for x in self.DV.keys():
  95. if (x == packet.destination) and (self.DV[x][2] == port):
  96. poison = basics.RoutePacket(packet.destination, INFINITY)
  97. self.send(poison, port, flood=True)
  98. self.poisoned[x] = self.DV[x]
  99. del self.DV[x]
  100.  
  101. elif isinstance(packet, basics.HostDiscoveryPacket):
  102. self.DV[packet.src] = [self.links[port], -1, port]
  103. route = basics.RoutePacket(packet.src, self.links[port])
  104. self.send(route, port, flood=True)
  105. else:
  106. # Totally wrong behavior for the sake of demonstration only: send
  107. # the packet back to where it came from!
  108. if (self.DV.get(packet.dst) != None and self.DV[packet.dst][2] != port):
  109. if self.DV[packet.dst][0] <= INFINITY:
  110. self.send(packet, self.DV[packet.dst][2], flood=False)
  111. #for x in self.DV.keys():
  112. #print x, self.DV[x][2], self.DV[x][0], self
  113. #print self.DV[packet.dst][2], packet.dst, packet.src, self
  114.  
  115. def handle_timer (self):
  116. """
  117. Called periodically.
  118.  
  119. When called, your router should send tables to neighbors. It also might
  120. not be a bad place to check for whether any entries have expired.
  121. """
  122. for x in self.DV.keys():
  123. #print x, self.DV[x][2], self.DV[x][0], self
  124. if (api.current_time() - self.DV[x][1] ) <= 15 or self.DV[x][1] == -1:
  125. route = basics.RoutePacket(x, self.DV[x][0])
  126. self.send(route, self.DV[x][2], flood=True)
  127. if self.POISON_MODE:
  128. routePoison = basics.RoutePacket(x, INFINITY)
  129. self.send(routePoison, self.DV[x][2], flood=False)
  130. else:
  131. if self.POISON_MODE:
  132. routePoison = basics.RoutePacket(x, INFINITY)
  133. self.send(routePoison, self.DV[x][2], flood=False)
  134. self.poisoned[x] = self.DV[x]
  135. del self.DV[x]
  136. if self.POISON_MODE:
  137. for x in self.poisoned.keys():
  138. poison = basics.RoutePacket(x, INFINITY)
  139. self.send(poison, port=None, flood=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement