import random,sys,math from lab9_net import * """Skeleton for link-state routing lab in 6.082 """ # use our own node class derived from the node class of network10.py # so we can override routing behavior class Router(Node): HELLO_INTERVAL = 5 # time between HELLO packets ADVERT_INTERVAL = 50 # time between route advertisements def __init__(self,location,qsize,address=None): Node.__init__(self, location, address=address) # additional instance variables self.neighbors = {} # Link -> (timestamp, address, linkcost) self.routes = {} # address -> Link self.routes[self.address] = 'Self' self.spcost = {} # address -> shortest path cost to node self.spcost[self.address] = 0 if qsize == 0: self.qsize = self.INFINITY else: self.qsize = qsize self.qdrop = 0 def reset(self): Node.reset(self) self.properties = {} self.spcost[self.address] = 0 def __repr__(self): return 'Router<%s>' % str(self.address) # return the link corresponding to a given neighbor, nbhr def getlink(self, nbhr): if self.address == nbhr: return None for l in self.links: if l.end2.address == nbhr or l.end1.address == nbhr: return l return None def peer(self, link): if link.end1.address == self.address: return link.end2.address if link.end2.address == self.address: return link.end1.address # use routing table to forward packet along appropriate outgoing link def forward(self,p): link = self.routes.get(p.destination, None) if link is None: print 'No route for ',p,' at node ',self else: # drop packet if the queue is already full if link.queue_length(self) >= self.qsize: if self.network.verbose: print "time ", self.network.time, ": ", self, " queue full (dropping pkt)" self.qdrop = self.qdrop + 1 return link.send(self, p) def process(self,p,link,time): Node.process(self, p, link, time) def transmit(self, time): return def OnClick(self,which): if which == 'left': #print whatever debugging information you want to print print self print ' neighbors:',self.neighbors.values() print ' routes:' for (key,value) in self.routes.items(): print ' ',key,': ',value class CrossTrafficNode(Router): def __init__(self,location,qsize,address,xrate,dest): self.dest = dest self.xrate = xrate self.total_cross = 0 Router.__init__(self,location,qsize,address) print self.address def __repr__(self): return 'CrossTrafficNode<%s>' % str(self.address) def transmit(self, time): if random.random() <= self.xrate: # time to send! self.total_cross = self.total_cross + 1 xmit_pkt = self.network.make_packet(self.address, self.dest, 'DATA', time, timestamp=time, color='red') self.forward(xmit_pkt) # Network with link costs. By default, the cost of a link is the # Euclidean distance between the nodes at the ends of the link class RouterNetwork(Network): def __init__(self,SIMTIME,NODES,LINKS): Network.__init__(self,SIMTIME) for n,r,c in NODES: self.add_node(r,c,address=n) for a1,a2 in LINKS: n1 = self.addresses[a1] n2 = self.addresses[a2] self.add_link(n1.location[0],n1.location[1], n2.location[0],n2.location[1]) # nodes should be an instance of LSNode (defined above) def make_node(self,loc,address=None): return Router(loc,address=address) def make_link(self,n1,n2): return CostLink(n1,n2) def add_cost_link(self,x1,y1,x2,y2): n1 = self.find_node(x1,y1) n2 = self.find_node(x2,y2) if n1 is not None and n2 is not None: link = self.make_cost_link(n1,n2) link.network = self self.links.append(link)