def __init__(self, server_id=None, config_path="server_config.ini", on_stop=None): self.id = server_id if server_id else str(random.randint( 0, 9999)).zfill(4) self.time_started = 0 self.on_stop = on_stop # Init socket self.sel = selectors.DefaultSelector() self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) self.server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.host = socket.gethostname() self.ip = messaging.get_ip_address() # Init configs self.config_path = config_path self.config = configparser.ConfigParser() self.load_config() # Init threads self.autoconnect_thread = threading.Thread( target=self._client_processor, daemon=True, name='Client processor') self.client_processor_thread_running = threading.Event( ) # Can be used for manual thread killing self.broadcast_thread = threading.Thread(target=self._ip_broadcast, daemon=True, name='IP broadcast sender') self.broadcast_thread_running = threading.Event() self.listener_thread = threading.Thread(target=self._broadcast_listen, daemon=True, name='IP broadcast listener') self.listener_thread_running = threading.Event()
def load_config(self): self.config.read(self.config_path) self.broadcast_port = self.config.getint('SERVER', 'broadcast_port') self.server_port = self.config.getint('SERVER', 'port') self.server_host = self.config.get('SERVER', 'host') self.BUFFER_SIZE = self.config.getint('SERVER', 'buffer_size') self.USE_NTP = self.config.getboolean('NTP', 'use_ntp') self.NTP_HOST = self.config.get('NTP', 'host') self.NTP_PORT = self.config.getint('NTP', 'port') self.client_id = self.config.get('PRIVATE', 'id') if self.client_id == '/default': self.client_id = 'copter' + str(random.randrange(9999)).zfill(4) self.write_config(False, ConfigOption('PRIVATE', 'id', self.client_id)) elif self.client_id == '/hostname': self.client_id = socket.gethostname() elif self.client_id == '/ip': self.client_id = messaging.get_ip_address()
def __init__(self, server_id=None, config_path="config/server.ini"): self.id = server_id if server_id else str(random.randint( 0, 9999)).zfill(4) self.time_started = 0 # Init socket self.sel = selectors.DefaultSelector() self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) messaging.set_keepalive(self.server_socket) self.server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.host = socket.gethostname() self.ip = messaging.get_ip_address() # Init configs self.config = ConfigManager() self.config_path = config_path # Init threads self.autoconnect_thread = threading.Thread( target=self._client_processor, daemon=True, name='Client processor') self.client_processor_thread_running = threading.Event( ) # Can be used for manual thread killing self.broadcast_thread = threading.Thread(target=self._ip_broadcast, daemon=True, name='IP broadcast sender') self.broadcast_thread_running = threading.Event( ) # TODO replace by interrupt self.broadcast_thread_interrupt = threading.Event() self.listener_thread = threading.Thread(target=self._broadcast_listen, daemon=True, name='IP broadcast listener') self.listener_thread_running = threading.Event()