def add_route(self, route): for path in route.get_paths().values(): sections = path.get_sections() n = len(sections) for (i, section) in enumerate(sections): start_id = self.from_node_id(route_path=path._data, route_section=section._data, index_in_path=i) end_id = self.to_node_id(route_path=path._data, route_section=section._data, index_in_path=i) if start_id not in self.nodes: self.nodes[start_id] = Node(label=start_id) if end_id not in self.nodes: self.nodes[end_id] = Node(label=end_id) start_node = self.nodes[start_id] end_node = self.nodes[end_id] section.start_node = start_node section.end_node = end_node end_node.in_links.append(section) start_node.out_links.append(section) self.sections[section.get_number()] = section for node_id in self.nodes: node = self.nodes[node_id] if len(node.in_links) == 0: del self.nodes[node_id] node.label = "start" self.nodes["start"] = node if len(node.out_links) == 0: del self.nodes[node_id] node.label = "end" self.nodes["end"] = node start_node = Node(label="depot") end_node = self.nodes["start"] _section = list(end_node.out_links)[-1] _data = copy.deepcopy(_section._data) _data["sequence_number"] = -1 _data["minimum_running_time"] = "PT1S" section = Section(data=_data, path=_section.path) section.start_node = start_node section.requirement = None section.occupations = [] section.marker = "depot" section.end_node = end_node start_node.out_links.append(section) self.nodes["depot"] = start_node self.sections[section.get_number()] = section
def __init__(self): self.node = Node("192.168.1.82", True) self.nodes = [] self.blockchain = None # Thread management self._running = True self.t1 = threading.Thread(target=self.receiv) self.t1.start() self._broadcast_ping()
def test_checkMsgsFromNeighbors(self): node = Node(5) node.memory['r'] = 3.14 msg = Message(0, 5, {'r':6.28}) alg = FastMIS_v2(None) result = alg.checkMsgsFromNeighbors(node, [msg]) self.failUnless(result == False)
def test_checkMsgsFromNeighbors(self): # 1 - wezel nie moze przystapic, bo jakis jego sasiad o wyzszym stopniu sie zaznaczyl # 2 - wezel moze przystapic, bo co ma sasiadow o wyzszym stopniu, ale nie zaznaczonych # 3 - wezel nie moze przystapic, bo ma sasiada o tym samym stopniu z wyzszym ID ktory sie zaznaczyl # 4 - wezel moze przystapic, bo ma sasiada o tym samym stopniu, ale nie zaznaczonego # 1: node = Node(5) node.memory['neighbors_cp'] = [0] msg = Message(0, 5, {'is_marked':'marked', 'degree':2}) alg = FastMIS(None) result = alg.checkMsgsFromNeighbors(node, [msg]) self.failUnless(result == False)
def decode_payload_node(self): services, host, port = self.decode_payload_content("Q16sH") if host[0:12] == '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF': host = socket.inet_ntop(socket.AF_INET, str(host[12:16])) elif host[0:6] == '\xfd\x87\xd8\x7e\xeb\x43': # Onion, based on BMD/bitcoind host = base64.b32encode(host[6:]).lower() + ".onion" else: host = socket.inet_ntop(socket.AF_INET6, str(host)) if host == "": # This can happen on Windows systems which are not 64-bit compatible # so let us drop the IPv6 address. host = socket.inet_ntop(socket.AF_INET, str(host[12:16])) return Node(services, host, port)
def test_version(self): """check encoding/decoding of the version message""" # with single stream msg = protocol.assembleVersionMessage('127.0.0.1', 8444, [1]) decoded = self._decode_msg(msg, "IQQiiQlsLv") peer, _, ua, streams = self._decode_msg(msg, "IQQiiQlsLv")[4:] self.assertEqual(peer, Node(3, '127.0.0.1', 8444)) self.assertEqual(ua, '/PyBitmessage:' + softwareVersion + '/') self.assertEqual(streams, [1]) # with multiple streams msg = protocol.assembleVersionMessage('127.0.0.1', 8444, [1, 2, 3]) decoded = self._decode_msg(msg, "IQQiiQlslv") peer, _, ua = decoded[4:7] streams = decoded[7:] self.assertEqual(streams, [1, 2, 3])
class Network: def __init__(self): self.node = Node("192.168.1.82", True) self.nodes = [] self.blockchain = None # Thread management self._running = True self.t1 = threading.Thread(target=self.receiv) self.t1.start() self._broadcast_ping() def stop(self): self._running = False def set_blockchain(self, blockchain_): # Done self.blockchain = blockchain_ def broadcast_transaction(self, str_transaction_): # Done for nodeList in self.nodes: self.node.send("-t ", nodeList, str_transaction_) def broadcast_block(self, str_block_): # Done for nodeList in self.nodes: self.node.send("-b ", nodeList, str_block_) def broadcast_ask_chain(self): # Done for nodeList in self.nodes: self.node.send("-c ", nodeList, "") def _broadcast_ping(self): #Not use nodeBroadcast = Node("192.168.1.62") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend) def receiv(self): print("ready to receiv") while self._running is True: data, addr = self.node.my_socket.recvfrom(4096) cureNode = addr myData = data.decode() if myData[:3] == "-c ": # Done # Retourne le JSON de la chaine self.node.send("-ac", cureNode, self.blockchain.chain_for_network) elif myData[:3] == "-n ": # Done print("I received a Node") # Parcourir la liste de noeud et les envois a l'emmeteur for nodeList in self.nodes: nodeToSend = jsonpickle.encode(nodeList) self.node.send("-an", cureNode, nodeToSend) elif myData[:3] == "-t ": # Done # Reception d'une transaction self.blockchain.submit_transaction(myData[3:len(myData)]) elif myData[:3] == "-b ": # Done # Reception d'un block self.blockchain.submit_block(myData[3:len(myData)]) elif myData[:3] == "-p ": # Done print('Ping received') # Repond present nodeToSend = jsonpickle.encode(self.node) self.node.send("-ap", cureNode, nodeToSend) nodeReceiv = jsonpickle.decode(myData[3:len(myData)]) print("Try to add node : ", nodeReceiv.host) notFind = True for nodeList in self.nodes: if nodeList.host == nodeReceiv.host: notFind = False if notFind: print("Connecting to a new Node: ", nodeReceiv.host) self.nodes.append(nodeReceiv) notFind = False elif myData[:3] == "-ac": # En suspend some = None elif myData[:3] == "-ap": # Done nodeReceiv = jsonpickle.decode(myData[3:len(myData)]) print("Try to add node : ", nodeReceiv.host) notFind = True for nodeList in self.nodes: if nodeList.host == nodeReceiv.host: notFind = False if notFind: print("Connecting to a new Node: ", nodeReceiv.host) self.nodes.append(nodeReceiv) notFind = False
def _broadcast_ping(self): #Not use nodeBroadcast = Node("192.168.1.62") myNodeToSend = jsonpickle.encode(self.node) self.node.send("-p ", nodeBroadcast, myNodeToSend)