def test_node_connect_accept(self): log = stdout_logger() params1 = NodeParams(runmode=MAIN, port=8333) params2 = NodeParams(runmode=MAIN, port=8334) node1 = Node(params1, log) node2 = Node(params2, log) node1.connect_peer(SockAddr("localhost", 8334)) node2.subscribe(Node.EVT_CONNECTED, lambda event: reactor.stop()) reactor.run()
def handle_accept(self): sock, (remote_ip, remote_port) = self.accept() remote_addr = SockAddr(remote_ip, remote_port) handler = PeerConnection(remote_addr, self.message_encoder, sock, self.log) handler.subscribe(handler.EVT_DISCONNECT, self.on_peer_disconnected) handler.subscribe(PeerConnection.EVT_NEW_MESSAGE, self.__on_message) handler.subscribe(PeerConnection.EVT_ERROR, self.on_error) self.peers[remote_addr] = handler self.peer_states[handler] = Node.PEER_CONNECTED self.log.info("Peer Accepted(%s) (peers:%d)" % (remote_addr, self.connected_peer_count())) self.fire(self.EVT_CONNECTED, handler=handler, outbound=False)
def on_addr(self, event): addr_msg = event.message for taddr in addr_msg.timenetaddr_list: addr = SockAddr(taddr.netaddr.ip, taddr.netaddr.port) if ((addr not in self.new_addrs) and (addr not in self.processing_addrs) and (addr not in self.success_addrs) and (addr not in self.failed_addrs)): self.new_addrs.add(addr) #set node as processed and disconnect addr = event.handler.sockaddr if addr in self.processing_addrs: self.processing_addrs.remove(addr) self.success_addrs.add(addr) self.node.disconnect_peer(addr) self._connect_new_peers()
def test_node_send_receive_message(self): log = stdout_logger() params1 = NodeParams(runmode=MAIN, port=8335) params2 = NodeParams(runmode=MAIN, port=8336) node1 = Node(params1, log) node2 = Node(params2, log) def on_connect(event): node1.send_message(event.handler, VerackMessage()) node1.subscribe(Node.EVT_CONNECTED, on_connect) def on_message(event): assert event.message.type == MSG_VERACK reactor.stop() node2.subscribe(Node.EVT_BASIC_MESSAGE, on_message) node1.connect_peer(SockAddr("localhost", 8336)) reactor.run()
def command_line_parameters(): parser = OptionParser() parser.add_option("-d", "--data_directory", dest="data_directory", help="location of data directory", metavar="DIR") parser.add_option("-m", "--runmode", dest="runmode") parser.add_option("-p", "--port", dest="port") parser.add_option("-s", "--seeds", dest="seeds") parser.add_option("-b", "--no-bootstrap", action="store_true", dest="no_bootstrap") parser.add_option("-f", "--no-findpeers", action="store_true", dest="no_findpeers") options, args = parser.parse_args() params = ClientParams() if options.data_directory: params.set(data_directory=options.data_directory) if options.runmode: params.set(runmode={"MAIN" : MAIN, "TESTNET" : TESTNET}[options.runmode]) if options.port: params.set(port=int(options.port)) if options.seeds: params.set(seeds=[SockAddr(ip, int(port)) for ip, port in [addrstr.split(":") for addrstr in options.seeds.split(",")]]) if options.no_bootstrap: params.set(bootstrap=False) if options.no_findpeers: params.set(findpeers=False) return params
def on_addr(self, event): for timenetaddr in event.message.timenetaddr_list: peeraddr = SockAddr(timenetaddr.netaddr.ip, timenetaddr.netaddr.port) self.addr_pool.addpeer(peeraddr)
def on_addr(self, event): addr_msg = event.message for taddr in addr_msg.timenetaddr_list: addr = SockAddr(taddr.netaddr.ip, taddr.netaddr.port) if addr not in self.peers and addr not in self.bootstrap_peers: self.fire(self.EVT_FOUND_PEER, peeraddress=addr)