def __init__(self, node_id, network, adapt_functions=None, **kwargs): """ :param node_id: any unique identifier :param network: a data structure holding every node """ Thread.__init__(self, **kwargs) # temp if adapt_functions is None: adapt_functions = [ AdaptationFunction(random.choice(("a", "b", "c")), random.choice(("a", "b", "c")), CV) for i in range(2) ] # --------------- communication related initialisation --------------- # # self.daemon = True self.id = node_id self.network = network self.wake_buffer = Queue() self.adapt_functions = adapt_functions self.routing_table = RoutingTable() for function in self.adapt_functions: self.routing_table.add_route(self.id, list(function._from), self.id, function, 0) # -------------------- counting sent and received -------------------- # self.conf_received = 0 self.conf_sent = 0
def run(self): server = (self.ip, self.port) inputQueue = queue.Queue() outputQueue = queue.Queue() ##Creates the routingtable routingTable = RoutingTable(self.routingTableDir) #Starts the UDP server sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind(server) print("Listening on " + self.ip + ":" + str(self.port)) ##Creates the Threads t = threading.Thread(target=inputThread, args=(inputQueue,sock,self.nodeID )) #t.start() t2 = threading.Thread(target=outputThread, args=(outputQueue,sock,routingTable )) t2.start() t3 = threading.Thread(target=logicalThread, args=(inputQueue,outputQueue,sock,self.blueGraphDir,self.nodeID )) t3.start() #Testing while True: test = int(input()) if test == 1: neighborList = [] testPack = obPackage(1,2,'e',0,"0.0.0.1",2,neighborList) ByteTestPack = testPack.serialize() inputQueue.put(ByteTestPack)
def cmdloop(self, routerID, routerDeadInterval, RouterPriority, hellointerval, inttransDelay): self.HelloInterval = hellointerval self.RouterID = routerID self.RouterDeadInterval = routerDeadInterval self.RouterPriority = RouterPriority self.IntTransDelay = inttransDelay self.listInterfaces = [] self.dataforUnicastDB = {} self.StartInterfacesList() self.LSDB = {} self.OpaqueID = 0 self.fakeRoutingTable = RoutingTable(self) self.realRoutingTable = ABRRoutingTable(self) self.thread = threading.Thread(target=self.multicastReceiver, args=()) self.thread.daemon = True self.thread.start() self.threadInc = threading.Thread(target=self.incrementLSATimer, args=()) self.threadInc.daemon = True self.threadInc.start() self.threadABR = False self.ABR = False self.threadUnicast = {} return cmd.Cmd.cmdloop(self)
def test_routing_table_add_valid(): table = RoutingTable() nodes = [i for i in range(10)] for node in nodes: H = random_stack(protocols, 20, 20) function = AdaptationFunction(H[-1], H[-1], CV) assert table.add_route(node, H, node, function, 1) row = table.get(node, H) assert row.cost == 1 and row.next_hop == node and row.function == function
def random_routing_table(n): table = RoutingTable() nodes = [i for i in range(n)] rd_prot = lambda: rd.choice(protocols) functions = [ AdaptationFunction(rd_prot(), rd_prot(), CV) for i in range(n) ] for i in range(3 * n): table.add_route(rd.choice(nodes), random_stack(protocols, 1, 10), rd.choice(nodes), rd.choice(functions), rd.randrange(100)) return table
def __init__(self, linkData, leafSwitchLids, spineSwitchLids): # the structure of the fabric self.linkData = linkData self.leafSwitchLids = set(leafSwitchLids) self.spineSwitchLids = set(spineSwitchLids) # generate routing table objects self.routingTables = {} for lid in leafSwitchLids + spineSwitchLids: self.routingTables[lid] = RoutingTable(linkData, lid)
def init_host(config_file): global host_ip global host_port global timeout global routing_table global watchDog global timer_dict global proxy_server_list fp = open(config_file) count = 1 for line in fp: if count == 1: para = line.strip().split() host_ip = socket.gethostbyname(socket.gethostname()) host_port = int(para[0]) timeout = float(para[1]) routing_table = RoutingTable(host_ip, host_port) else: routing_table.lock.acquire() para = line.strip().split() # splitting the ip and port t_para = para[0].split(':') # initiate the weight routing_table.set_neighbor(t_para[0], int(t_para[1]), float(para[1])) routing_table.set_edge_weight(host_ip, host_port, t_para[0], int(t_para[1]), float(para[1])) routing_table.set_edge_through(host_ip, host_port, t_para[0], int(t_para[1]), t_para[0], int(t_para[1])) routing_table.lock.release() count += 1 fp.close() routing_table.lock.acquire() routing_table.set_edge_weight(host_ip, host_port, host_ip, host_port, 0.0) routing_table.set_edge_through(host_ip, host_port, host_ip, host_port, host_ip, host_port) routing_table.lock.release() ''' This is just added for part three having a proxy list ''' for each in routing_table.get_all_neighbors(): proxy_server_list[each] = None watchDog = WatchDog(timeout, [], send_update) watchDog.start() for key in routing_table.neighbor.keys(): timer = WatchDog(3 * timeout, [key], time_up) timer.start() timer_dict[key] = timer
def __init__(self, routingTableDir, dirGrafoAzul, timeout, ID): self.rand = random self.ip = '0.0.0.0' self.routingTable = RoutingTable(routingTableDir) self.colaEntrada = queue.Queue() self.colaSalida = queue.Queue() self.diccionariosACKs = { } # { SNRN_1:{1:'', 2:'', 3:'', 4:'', 5:''}, SNRN_2:{1:'', 2:'', 3:'', 4:'', 5:''}, SNRN_N:{1:'', 2:'', 3:'', 4:'', 5:''}} self.SNRN = self.rand.randrange(65536) self.secure_UDP = 0 self.port = 0000 self.nodeID = ID self.tablaNodosAzules = TablaNodosAzules(dirGrafoAzul) self.TIMEOUT = timeout self.semaphore = threading.Semaphore(0) self.timeStamp = time.time() self.blueNodesAsignedByMe = {}
def read(self, lines): assert(isinstance(lines,list)) m = re.match("\s*getNodeId.*:[^0-9a-f]*([0-9a-f]+).*", lines[0]) if m: self.nodeId = m[1] lines = lines[1:] while len(lines) > 0: routingTable = RoutingTable() try: assert(isinstance(lines, list)) lines = routingTable.read(lines) assert(isinstance(lines, list)) self.routingTables.append(routingTable) except TableHeaderException as e: return lines except TableEntryException as e: return lines
def _generate_private_nodes(self, n): default_port = 5555 for i in range(0, n): self.users.append(User(f"private{i}", "127.0.0.1", default_port + i)) self.locks.append(threading.Lock()) self.tables.append(RoutingTable(self.users[i].node, self.bootstrap_node, self.bucket_limit, f"nodes{i}.txt", self.locks[i])) self.output_queues.append(deque()) self.server_threads.append( Server(self.users[i].node, self.tables[i], self.output_queues[i], self.k, self.connections_count)) self.server_threads[i].start() self.command_queues.append(deque()) self.client_threads.append( Client(self.users[i].node, self.tables[i], self.command_queues[i], self.k, self.alpha)) self.client_threads[i].start()
def main(self): self.login = "******" self.port = 4444 self.ip = "127.0.0.1" self.bucket_limit = 20 self.k = 10 self.connections_count = 10 self.user = User(self.login, self.ip, self.port) self.lock = threading.Lock() self.routing_table = RoutingTable(self.user.node, self.bootstrap_node, self.bucket_limit, "nodes.txt", self.lock) self.messages_output_queue = deque() self.server_thread = Server(self.user.node, self.routing_table, self.messages_output_queue, self.k, self.connections_count) self.server_thread.start() self.command_input_queue = deque() self.flask_thread = FlaskThread(self.command_input_queue, self.messages_output_queue) self.flask_thread.start() self.client_thread = Client(self.user.node, self.routing_table, self.command_input_queue, self.k, self.alpha) self.client_thread.start() while True: self.command_queues[0].append( f"{self.user.node.id} STORE priv2priv") time.sleep(1) self.command_queues[1].append( f"{self.users[self.private_nodes_count].node.id} STORE priv2pub1" ) time.sleep(1) self.command_queues[2].append( f"{self.users[self.private_nodes_count].node.id} STORE priv2pub2" ) time.sleep(1) self.command_queues[3].append( f"{self.users[self.private_nodes_count].node.id} STORE priv2pub3" ) time.sleep(1)
def run(self): inputQueue = queue.Queue() outputQueue = queue.Queue() # Create the routingTable routingTable = RoutingTable(self.routingTableDir) maxOrangeNodes = len(routingTable.table) # Creates the orange graph and the blueNodeTable table = blueNodeTable(self.blueGraphDir) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) self.ip = s.getsockname()[0] s.close() # Starts the UDP server sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((self.ip,self.port)) print("Listening Oranges on ip: %s port %d Im orange: %d" % (sock.getsockname()[0], self.port, self.nodeID)) # Starts the UDP Safe SecureUdpBlue = SecureUdp(100,1,True) #ventana de 100 con timeout de 1s print("Listening Blues on ip: %s port %d Im orange: %d" % (SecureUdpBlue.sock.getsockname()[0], SecureUdpBlue.sock.getsockname()[1], self.nodeID)) # Creates the Threads t = threading.Thread(target=inputThread, args=( inputQueue, outputQueue, sock, self.nodeID, self.debug, )) t.start() t2 = threading.Thread(target=outputThread, args=( outputQueue, sock, routingTable, self.debug, )) t2.start() t3 = threading.Thread(target=logicalThread, args=( inputQueue, outputQueue, sock, table, self.nodeID, maxOrangeNodes,self.debug,SecureUdpBlue)) t3.start() t4 = threading.Thread(target=inputThreadBlue, args=(SecureUdpBlue,inputQueue,)) t4.start()
def init(my_id): rt = RoutingTable(my_id) return rt
def test_routing_table_replace_route(): table = RoutingTable() function = AdaptationFunction("a", "a", CV) assert table.add_route(0, ["a"], 1, function, 100) for i in range(1, 101): assert table.add_route(0, ["a"], 1, function, 100 - i) # == True
def test_routing_table_add_invalid(): table = RoutingTable() function = AdaptationFunction("a", "a", CV) assert table.add_route(0, ["a"], 1, function, 12) for i in range(30): assert not table.add_route(0, ["a"], 1, function, rd.randint(12, 20))