def init_servers(): # A container for all thread instances threads = list() # Inter-thread communication via FIFO Queues # DHT ---> kademlia: queue for DHT to send requests to kademlia request_q = Queue() # kademlia ---> DHT: queue for kademlia to send responses to DHT response_q = Queue() # If an exception/error occurs in any of the threads it is not detectable. # Therefore we use an additional queue that feeds the mail thread with # potential exceptions/errors that occured in any of the children threads. err_q = Queue() # Create a thread for kademlia server address = ('', config.KADEM_PORT) kademlia_server = KademliaProtocolServer(request_q, response_q, err_q, address) t = Thread(target=kademlia_server.serve_forever) threads.append(t) # Create a thread for DHT server address = ('', config.PORT) dht_server = DHTAPIServer(request_q, response_q, err_q, address) t = Thread(target=dht_server.serve_forever) threads.append(t) for t in threads: t.start() # As long as we haven't received an EXIT signal, read the queue for # potential exceptions/errors that occured in any of the children threads. # The reading of the queue is with a blocking 'get', so no CPU cycles are # wasted while waiting. Also, 'get' is given a timeout, so the SHUT_DOWN # flag is always checked, even if there's nothing in the queue. while not config.SHUT_DOWN: try: error, thread_name = err_q.get(block=True, timeout=0.05) # An error/exception occured in a child thread. Update global # variable to induce all children threads to exit and raise it config.SHUT_DOWN = 1 wait_threads_exit(threads) raise error, 'Exception in thread: %s' % thread_name except Empty: continue logger.info('some signal for exit was received') # shutting down has already been initiated by the signal handler wait_threads_exit(threads) exit_gracefully('successful shut down')
def determine_peer_id(): from hashlib import sha256 try: public_key = open(config.PUBLIC_KEY_PATH, 'rb').read() except IOError: message = 'Public key path is set to: %s\n' % config.PUBLIC_KEY_PATH message += 'If incorrect, please update file: %s' % config.CONFIG_PATH exit_gracefully(message) config.PEER_ID = int(sha256(public_key).hexdigest(), 16)
def check_args(): for i in range(len(sys.argv)): # configuration file flag if sys.argv[i] == '-c': try: config.CONFIG_PATH = sys.argv[i + 1] except IndexError: message = "missing configuration file operand after '-c'\n" message += "Try '%s --help' for more information." % sys.argv[0] exit_gracefully(message)
def check_args(): for i in range(len(sys.argv)): # configuration file flag if sys.argv[i] == '-c': try: config.CONFIG_PATH = sys.argv[i+1] except IndexError: message = "missing configuration file operand after '-c'\n" message += "Try '%s --help' for more information." % sys.argv[0] exit_gracefully(message)
def read_conf(): if not os.path.isfile(config.CONFIG_PATH): exit_gracefully("cannot read '%s': No such file or directory" % config.CONFIG_PATH) config_parser = ConfigParser.ConfigParser() config_parser.read(config.CONFIG_PATH) logger.debug('detected sections (%s): %s' % (config.CONFIG_PATH, config_parser.sections())) dht_properties = config_section_map(config_parser, 'COMMON') config.PUBLIC_KEY_PATH = dht_properties['public_key_path'] dht_properties = config_section_map(config_parser, 'KX') config.KX_PORT = int(dht_properties['port']) config.KX_HOSTNAME = dht_properties['hostname'] dht_properties = config_section_map(config_parser, 'DHT') config.PORT = int(dht_properties['port']) config.KADEM_PORT = int(dht_properties['kadem_port']) config.HOSTNAME = dht_properties['hostname']