def __init__(self, **kwargs): if "log_level" in kwargs and kwargs["log_level"]: log.basicConfig( level=kwargs["log_level"], format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%d-%m %H:%M", filename=".log", filemode="w") if "console_logging" in kwargs and kwargs["console_logging"]: console = log.StreamHandler() console.setLevel(kwargs["log_level"]) formatter = log.Formatter( "%(asctime)s %(name)-12s: %(levelname)-8s %(message)s") console.setFormatter(formatter) log.getLogger("init").addHandler(console) log.getLogger("networkio").addHandler(console) log.getLogger("cachedb").addHandler(console) log.getLogger("utility").addHandler(console) self.packet = None self.bootstrap = None self.uuid = random_id() cache = DB() cache["uuid"] = self.uuid cache["values"] = dict() if "boot_addr" and "boot_port" in kwargs: self.bootstrap = (kwargs["boot_addr"], kwargs["boot_port"]) logging.info("TaskScheduler initializing, id: {0}".format(self.uuid)) self.s = Scheduler() self.s.new(ReceiverTask(kwargs["addr"], kwargs["port"], self.bootstrap)) #self.s.new(RefresherTask()) # TODO #self.connect() self.query_result = list()
class DHT(object): """ DHT API: Arguments which should always be provided are: - addr = "" (equal to 0.0.0.0) - port = 0 (in this case OS will chose random port) Optional arguments: - boot_addr = "" - boot_port = 0 - log_level = logging.INFO - console_logging = True/False """ def __init__(self, **kwargs): if "log_level" in kwargs and kwargs["log_level"]: log.basicConfig( level=kwargs["log_level"], format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%d-%m %H:%M", filename=".log", filemode="w") if "console_logging" in kwargs and kwargs["console_logging"]: console = log.StreamHandler() console.setLevel(kwargs["log_level"]) formatter = log.Formatter( "%(asctime)s %(name)-12s: %(levelname)-8s %(message)s") console.setFormatter(formatter) log.getLogger("init").addHandler(console) log.getLogger("networkio").addHandler(console) log.getLogger("cachedb").addHandler(console) log.getLogger("utility").addHandler(console) self.packet = None self.bootstrap = None self.uuid = random_id() cache = DB() cache["uuid"] = self.uuid cache["values"] = dict() if "boot_addr" and "boot_port" in kwargs: self.bootstrap = (kwargs["boot_addr"], kwargs["boot_port"]) logging.info("TaskScheduler initializing, id: {0}".format(self.uuid)) self.s = Scheduler() self.s.new(ReceiverTask(kwargs["addr"], kwargs["port"], self.bootstrap)) #self.s.new(RefresherTask()) # TODO #self.connect() self.query_result = list() def send_query(self, send_to, query_type, query_data): return { "query_data": query_data, "query_type": query_type, # ping, store, find_node, find_value "send_to": send_to, "uuid": self.uuid } def connect(self): self.s.start() def __getitem__(self, key): #register to observable # TODO: define callback # TODO: call IterativeFindValue pass def __setitem__(self, key, value): #result = yield IterativeStore(key, value) # TODO: define callback logging.debug("DHT setting item. {}:{}".format(key, value)) # TODO: call IterativeStore yield IterativeStore(key, value, self.get_result()) #self.s.new(IterativeStore(key, value, self.get_result())) def __call__(self, packet): self.packet = packet def get_result(self, resp): logging.debug("DHT OBJECT DATA: {}".format(resp)) self.query_result.append(resp)
class DHT(object): """ DHT API: Arguments which should always be provided are: - addr = "" (equal to 0.0.0.0) - port = 0 (in this case OS will chose random port) Optional arguments: - boot_addr = "" - boot_port = 0 - log_level = logging.INFO - console_logging = True/False """ def __init__(self, **kwargs): if "log_level" in kwargs and kwargs["log_level"]: log.basicConfig( level=kwargs["log_level"], format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s", datefmt="%d-%m %H:%M", filename=".log", filemode="w") if "console_logging" in kwargs and kwargs["console_logging"]: console = log.StreamHandler() console.setLevel(kwargs["log_level"]) formatter = log.Formatter( "%(asctime)s %(name)-12s: %(levelname)-8s %(message)s") console.setFormatter(formatter) log.getLogger("init").addHandler(console) log.getLogger("networkio").addHandler(console) log.getLogger("cachedb").addHandler(console) log.getLogger("utility").addHandler(console) self.packet = None self.bootstrap = None self.uuid = random_id() cache = DB() cache["uuid"] = self.uuid cache["values"] = dict() if "boot_addr" and "boot_port" in kwargs: self.bootstrap = (kwargs["boot_addr"], kwargs["boot_port"]) logging.info("TaskScheduler initializing, id: {0}".format(self.uuid)) self.s = Scheduler() self.s.new(ReceiverTask(kwargs["addr"], kwargs["port"], self.bootstrap)) #self.s.new(RefresherTask()) # TODO #self.connect() self.query_result = list() def send_query(self, send_to, query_type, query_data): return {"query_data": query_data, "query_type": query_type, # ping, store, find_node, find_value "send_to": send_to, "uuid": self.uuid } def connect(self): self.s.start() def __getitem__(self, key): #register to observable # TODO: define callback # TODO: call IterativeFindValue pass def __setitem__(self, key, value): #result = yield IterativeStore(key, value) # TODO: define callback logging.debug("DHT setting item. {}:{}".format(key, value)) # TODO: call IterativeStore yield IterativeStore(key, value, self.get_result()) #self.s.new(IterativeStore(key, value, self.get_result())) def __call__(self, packet): self.packet = packet def get_result(self, resp): logging.debug("DHT OBJECT DATA: {}".format(resp)) self.query_result.append(resp)