def buildtrees(targets): G=data.G nodes=data.nodes rnodes=data.rnodes print "Buliding shortest paths to targets" preproute(G,nodes,rnodes,targets) sol=route(G,targets) print "Saving solution to targetsol.pkl and targetnodes.pkl" pickle.dump(sol,open("targetsol.pkl","wb")) pickle.dump(nodes,open("targetnodes.pkl","wb"))
def test_server_and_client(self): master_api = MasterApi() with patch.object(master_api, 'heartbeat') as heartbeat: route('heartbeat')(heartbeat) server, client = self._setup_server_and_client( self.topic, MasterApi, master_api ) try: args = [ ['host1'], ['host1', 1], ['host2'], ['host2', 2] ] for arg in args: client.heartbeat(*arg) time.sleep(0.5) heartbeat.assert_called_with(*arg) finally: client.disconnect() server.stop()
def handle_write(self): try: if ((self.buf.find("\r\n\r\n") >= 0) or (self.buf.find("\n\n") >= 0) or (self.buf.find("\r\r") >= 0)): # whole request received logger.debug ("whole request got: %s" % self.buf) request = RtspParser (self.buf) if request.error: logger.warning ("Bad request: {}".format (self.buf)) response = MethodNotAllowed % request.get_CSeq () self.sock.send (response) self.close () else: if request.get_method () == "DESCRIBE": logger.debug ("It is a DESCRIBE request") destination = route (request.get_medianame ()) if destination is None: logger.warning ("File not found : %s" % request.get_url ()) response = FileNotFound % request.get_CSeq () self.sock.send (response) self.close () else: location = "rtsp://%s/%s" % (destination, request.get_medianame ()) response = MethodRedirect % (request.get_CSeq (), location) self.sock.send (response) accesslogger.info ("\r\nREMOTE\r\n%s\r\nREQUEST\r\n%s\r\nRESPONSE\r\n%s" % (self.sock.getpeername (), self.buf, response)) self.buf = self.buf[len(self.buf):] elif request.get_method () == "OPTIONS": logger.debug ("It is a OPTIONS request") response = MethodOptions % request.get_CSeq () self.sock.send (response) self.buf = self.buf[len(self.buf):] if not self.buf: self.reset(pyev.EV_READ) else: # should read more self.reset(pyev.EV_READ) except socket.error as err: if err.args[0] not in NONBLOCKING: self.handle_error("error writing to {0}".format(self.sock))
def evaluate_tracer_packet(self, me, tp): packet_interesting = False first = True for i in reversed(tp): if first: neigh_gw = i first = False if me.nid != i: # they aren't neighbour! # pdb.set_trace() r = route(me.nid, i, neigh_gw, tp, self) # if G.verbose: # r.print_route() # Save the route if it is interesting if me.add_route(r): packet_interesting = True return packet_interesting
def evaluate_tracer_packet(self, me, tp): packet_interesting = False first = True for i in reversed(tp): if first: neigh_gw = i first = False if (me.nid != i): #they aren't neighbour! #pdb.set_trace() r = route(me.nid, i, neigh_gw, tp, self) #if G.verbose: # r.print_route() # Save the route if it is interesting if me.add_route(r): packet_interesting = True return packet_interesting
def run(Config, Controller, Function, Id): Method = getattr(Controller, Function) if Id != None: return Method(Config,Id) else: return Method(Config) def route(Config,_module , _attribute, _id=None): module = __import__('Controllers.' + _module) instance = getattr(module, _module) return run(Config, instance, _attribute, _id) if _module == 'api' : print('Content-type: application/json\r\n\r') #result = load_json("sample") result = "{\"" \ "name\":\"Gonzalez\"}" else: result = route(Config,_module, _attribute, _id) print('Content-type: text/html\r\n\r') print(result)
def buildconn(nn): """Build a connected component starting from nn and saves this component in the file conncomp.pkl""" G=data.G sol=route(G,nn) print "Saving solution to conncomp.pkl" pickle.dump(sol[0],open("conncomp.pkl","wb"))
def exec_etp_deadnode(self, me, trcr, tmask): """Returns 1 if the ETP is interesting, 0 otherwise""" exist = 0 forwardit = 1 trash = [] gw = trcr[len(trcr) - 1] for r in self.etp.routes: contains_dead = 0 # this is the route formed by the chunks of the TP # and that of `r' # Its tpmask is r.tpmask|tmask, and its rem is # r.rem+route_of_the_tp.rem rp_tracer = [r.dst.nid] for hop in r.hops: rp_tracer.append(hop.nid) for hop in trcr: rp_tracer.append(hop) for hop in [r.dst] + r.hops: if me.is_dead(hop.nid) and hop.nid != self.etp.changed_node.nid: # We know that this hop is dead, but this # route contains it! # Since this ETP carries only information # about dead, and not about new nodes, we'll # delete it. # This event has probably happened due two # simultaneus dead-node events. # if G.verbose: # print "CONTAINS DEAD!",rp_tracer, len(r.hops), len(trcr), hop.nid contains_dead = 1 rp = route(me.nid, r.dst.nid, gw, rp_tracer, self) ## print "rp: ", rp_tracer # Delete now the rp route from the map (if present), # in other words, delete from the map all the routes passing from `gw' # and having the tpmask set to `tpm' ## print "update_map_deadnode", gw, dec2bin(rp.tpmask), me.nid if not me.update_map_deadnode(G.whole_network[gw], rp.tpmask): # no route has been deleted # If the route is dead (it passes trough the dead node), # or it contains other dead nodes (see above), # or if it isn't a better alternative to what # we already have in the map, then delete it if (rp.tpmask & (1 << self.etp.changed_node.nid)) or contains_dead or not me.add_route(rp): # this route isn't interesting, we may as # well delete it from the ETP trash.append(r) else: if G.debug: print "ETP: route added: ", rp_tracer trash = remove_duplicates(trash) for r in trash: self.etp.routes.remove(r) if self.etp.routes == []: # the etp is empty! if self.etp.interest_flag: self.etp.interest_flag = 0 # The interest_flag was set, but we've found # the packet uninteresting, thus we'll send # back the new ETP containing our routes # pack the new ETP # we need to create a new one new_pack = packet(me, G.whole_network[gw], "QSPN") link = me.rnodes[gw] new_pack.add_chunk(link) delay = link.l_rem.rtt new_pack.time = G.curtime + delay new_etp_routes = [] # copy in the new packet all the routes of me for dest, marray in me.int_map.iteritems(): new_etp_routes += me.get_all_routes_bydst(G.whole_network[dest]) new_etp_routes = remove_duplicates(new_etp_routes) # copy the etp section for the new packet new_pack.copy_etp_pkt(self.etp) # now we have to change the routes of the new pack # with the ones obtained by get_all_routes_bydst new_pack.payload.etp.routes = new_etp_routes # send the packet # if G.verbose: # print "ETP: sending back to %d the new routes"%(gw) new_pack.send_packet() else: # The ETP was considered uninteresting before, # thus it is just propagating to carry the # information of the death the node # `self.etp.changed_node'. # Let's check if we already know this info. if not me.int_map.has_key(self.etp.changed_node.nid): # Yea, we already knew it # drop the packet then! if G.debug: print "ETP: dropping it" forwardit = 0 else: if G.debug: print "good, now I know the death of ", self.etp.changed_node # delete the dead node from the map me.del_node(self.etp.changed_node) # You may as well forward the ETP return forwardit
def exec_etp_deadnode(self, me, trcr, tmask): """Returns 1 if the ETP is interesting, 0 otherwise""" exist = 0 forwardit = 1 trash = [] gw = trcr[len(trcr) - 1] for r in self.etp.routes: contains_dead = 0 # this is the route formed by the chunks of the TP # and that of `r' # Its tpmask is r.tpmask|tmask, and its rem is # r.rem+route_of_the_tp.rem rp_tracer = [r.dst.nid] for hop in r.hops: rp_tracer.append(hop.nid) for hop in trcr: rp_tracer.append(hop) for hop in [r.dst] + r.hops: if me.is_dead(hop.nid) and\ hop.nid!=self.etp.changed_node.nid: # We know that this hop is dead, but this # route contains it! # Since this ETP carries only information # about dead, and not about new nodes, we'll # delete it. # This event has probably happened due two # simultaneus dead-node events. #if G.verbose: #print "CONTAINS DEAD!",rp_tracer, len(r.hops), len(trcr), hop.nid contains_dead = 1 rp = route(me.nid, r.dst.nid, gw, rp_tracer, self) ## print "rp: ", rp_tracer # Delete now the rp route from the map (if present), # in other words, delete from the map all the routes passing from `gw' # and having the tpmask set to `tpm' ## print "update_map_deadnode", gw, dec2bin(rp.tpmask), me.nid if not me.update_map_deadnode(G.whole_network[gw], rp.tpmask): # no route has been deleted # If the route is dead (it passes trough the dead node), # or it contains other dead nodes (see above), # or if it isn't a better alternative to what # we already have in the map, then delete it if (rp.tpmask & (1<<self.etp.changed_node.nid))\ or contains_dead\ or not me.add_route(rp): \ # this route isn't interesting, we may as # well delete it from the ETP trash.append(r) else: if G.debug: print "ETP: route added: ", rp_tracer trash = remove_duplicates(trash) for r in trash: self.etp.routes.remove(r) if self.etp.routes == []: #the etp is empty! if self.etp.interest_flag: self.etp.interest_flag = 0 # The interest_flag was set, but we've found # the packet uninteresting, thus we'll send # back the new ETP containing our routes #pack the new ETP #we need to create a new one new_pack = packet(me, G.whole_network[gw], "QSPN") link = me.rnodes[gw] new_pack.add_chunk(link) delay = link.l_rem.rtt new_pack.time = G.curtime + delay new_etp_routes = [] #copy in the new packet all the routes of me for dest, marray in me.int_map.iteritems(): new_etp_routes += me.get_all_routes_bydst( G.whole_network[dest]) new_etp_routes = remove_duplicates(new_etp_routes) #copy the etp section for the new packet new_pack.copy_etp_pkt(self.etp) #now we have to change the routes of the new pack #with the ones obtained by get_all_routes_bydst new_pack.payload.etp.routes = new_etp_routes #send the packet #if G.verbose: # print "ETP: sending back to %d the new routes"%(gw) new_pack.send_packet() else: # The ETP was considered uninteresting before, # thus it is just propagating to carry the # information of the death the node # `self.etp.changed_node'. # Let's check if we already know this info. if not me.int_map.has_key(self.etp.changed_node.nid): # Yea, we already knew it # drop the packet then! if G.debug: print "ETP: dropping it" forwardit = 0 else: if G.debug: print "good, now I know the death of ", self.etp.changed_node # delete the dead node from the map me.del_node(self.etp.changed_node) # You may as well forward the ETP return forwardit