def main(): if options.is_bootstrap: node = Local(Address(ip=options.address, port=options.port)) else: node = Local( Address(ip=options.address, port=options.port), Address(ip=options.bootstrap_address, port=options.bootstrap_port)) app = PlayStackTornado(node=node) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) node.start() IOLoop.current().start()
def setUpClass(cls) -> None: # create addresses cls.address_list = [] port_list = [ 9065, 9081, 9074, 9087, 9085, 9118, 9117, 9897, 9120, 9103, 9107, 9108, 9192, 9203, 9204, 9212 ] for port in port_list: cls.address_list.append(Address(ip='127.0.0.1', port=port)) # keep unique ones address_list = sorted(set(cls.address_list)) # hash the addresses cls.hash_list = [addr.__hash__() for addr in address_list] cls.hash_list.sort() # create the nodes cls.peers = [] cls.server_list = [] for i in range(0, len(address_list)): if i == 0: local = Local(address_list[i]) else: # use a first already created peer's address as the remote for bootstrapping local = Local(address_list[i], cls.peers[0].address) app = TornadoThread(node=local) app.start() cls.server_list.append(app) cls.peers.append(local)
async def get_successors(self): msg = {'cmd': CommandType.GET_SUCCESSORS} response = await self.send(msg) # if our next guy doesn't have successors, return empty list successors = [Remote(Address(node['ip'], node['port'])) for node in response['data']] return successors
async def execute_command(self, command): cmd = command['cmd'] data = command.get('data') result = dict(data=None) if cmd == CommandType.GET_SUCCESSOR: successor = await self.get_successor() result['data'] = { 'ip': successor.address.ip, 'port': successor.address.port } elif cmd == CommandType.GET_PREDECESSOR: # we can only reply if we have a predecessor if self.predecessor: result['data'] = { 'ip': self.predecessor.address.ip, 'port': self.predecessor.address.port } elif cmd == CommandType.FIND_SUCCESSOR: successor = await self.find_successor(data['identifier']) result['data'] = { 'ip': successor.address.ip, 'port': successor.address.port } elif cmd == CommandType.CLOSEST_PRECEDING_FINGER: closest = await self.get_closest_preceding_finger( data['identifier']) result['data'] = { 'ip': closest.address.ip, 'port': closest.address.port } elif cmd == CommandType.NOTIFY: await self.notify(Remote(Address(data['ip'], data['port']))) elif cmd == CommandType.GET_SUCCESSORS: result['data'] = await self.get_successors() elif cmd == CommandType.PING: result['data'] = True return result
async def get_closest_preceding_finger(self, identifier): msg = {'cmd': CommandType.CLOSEST_PRECEDING_FINGER, 'data': {'identifier': identifier}} response = await self.send(msg) return Remote(Address(response['data']['ip'], response['data']['port']))
async def find_successor(self, identifier): msg = {'cmd': CommandType.FIND_SUCCESSOR, 'data': {'identifier': identifier}} response = await self.send(msg) return Remote(Address(response['data']['ip'], response['data']['port']))
async def get_predecessor(self): msg = {'cmd': CommandType.GET_PREDECESSOR} response = await self.send(msg) return Remote(Address(response['data']['ip'], response['data']['port'])) if response['data'] else None
async def get_successor(self): msg = {'cmd': CommandType.GET_SUCCESSOR} response = await self.send(msg) return Remote(Address(response['data']['ip'], response['data']['port']))
def get_app(self): local = Local(Address(ip='127.0.0.1', port=9000)) return TornadoThread(node=local)