def IterativeFindNode(uuid, callback): logging.info("Initiating IterativeFindNode() task") routing_table = kBucket(cache["addr"], cache["port"], cache["uuid"]) used = list() # finger table result = None def find_node_callback(resp, err): if not err: if isinstance(resp["data"], tuple): result = resp["data"] while True: nodes = routing_table.get_nearest_nodes(uuid, limit=ALPHA, diff=used) if not nodes: break else: used += nodes for node in nodes: find_node = AsyncRPC(find_node_callback) socket_observable.register(find_node) find_node.send(find_node.new_packet()) # TODO: yield callback(result)
def IterativeStore(key, value, callback): logging.info("Storing: dict({}:{})".format(key, value)) routing_table = kBucket(cache["addr"], cache["port"], cache["uuid"]) hashed_key = get_hash(key) def store_callback(resp, err): if not err: logging.debug("store_callback({},{})".format(resp, err)) callback(resp) nodes = routing_table.get_nearest_nodes(hashed_key) logging.debug("Storing to nodes: {}".format(nodes)) for node in nodes: store_data = AsyncRPC(store_callback()) socket_observable.register(store_data) store_data.send( store_data.new_packet( node, "store", (key, value, hashed_key, cache["uuid"]) ) ) cache["values"].update( { hashed_key: (key, value), "author": cache["uuid"] }) logging.debug("Values stored, yielding...") yield
def recv(self): packet, addr = yield self.sock.recv() logging.debug("Received packet {0} from {1}.".format(packet, addr)) routing_table = kBucket(cache["addr"], cache["port"], cache["uuid"]) routing_table.insert(packet["recv_dest"][0], packet["recv_dest"][1], packet["uuid"]) logging.debug("Routing table updated: ({0}, {1}, {2})".format( packet["recv_dest"][0], packet["recv_dest"][1], packet["uuid"])) if packet["type"] == "query": ## unpack => (resp, addr, rpc_no, uuid) if packet["query_type"] == "shellcmd": unpack = (self.handle_shellcmd(packet), packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "ping": # ping(): pong resp = "pong" unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "store": # store(key, value h_key): True resp = True try: cache["values"].update({ packet["data"][2]: (packet["data"][0], packet["data"][1]), "author": packet["data"][3] }) logging.debug("Values stored: {}".format(cache["values"])) except Exception: resp = False unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "find_node": # find_node(hkey): k_nearest | node nearest = routing_table.get_nearest_nodes(packet["data"]) if nearest[0][2] == packet["data"]: resp = nearest[0] else: resp = nearest unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "find_value": # find_value(hkey): k_nearest | value resp = cache["values"].get(packet["data"], None) if resp is None: resp = routing_table.get_nearest_nodes(packet["data"]) unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["type"] == "err": yield packet, "err", addr, packet["rpc_no"], packet["uuid"] if packet["type"] == "resp": yield packet, addr, packet["rpc_no"], packet["uuid"]
def recv(self): packet, addr = yield self.sock.recv() logging.debug("Received packet {0} from {1}.".format(packet, addr)) routing_table = kBucket(cache["addr"], cache["port"], cache["uuid"]) routing_table.insert(packet["recv_dest"][0], packet["recv_dest"][1], packet["uuid"]) logging.debug( "Routing table updated: ({0}, {1}, {2})".format( packet["recv_dest"][0], packet["recv_dest"][1], packet["uuid"] ) ) if packet["type"] == "query": ## unpack => (resp, addr, rpc_no, uuid) if packet["query_type"] == "shellcmd": unpack = (self.handle_shellcmd(packet), packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "ping": # ping(): pong resp = "pong" unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "store": # store(key, value h_key): True resp = True try: cache["values"].update( {packet["data"][2]: (packet["data"][0], packet["data"][1]), "author": packet["data"][3]} ) logging.debug("Values stored: {}".format(cache["values"])) except Exception: resp = False unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "find_node": # find_node(hkey): k_nearest | node nearest = routing_table.get_nearest_nodes(packet["data"]) if nearest[0][2] == packet["data"]: resp = nearest[0] else: resp = nearest unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["query_type"] == "find_value": # find_value(hkey): k_nearest | value resp = cache["values"].get(packet["data"], None) if resp is None: resp = routing_table.get_nearest_nodes(packet["data"]) unpack = (resp, packet["recv_dest"], packet["rpc_no"], packet["uuid"]) yield unpack if packet["type"] == "err": yield packet, "err", addr, packet["rpc_no"], packet["uuid"] if packet["type"] == "resp": yield packet, addr, packet["rpc_no"], packet["uuid"]