def __init__(self, options): """ """ self.network = None self.lease_mgr = None self.mqtt_resolv = None self.mqtt_client = None self.resolv_timer = None self.heartbeat_timer = None self.section = "dhcp" JNTDBServer.__init__(self, options) JNTControllerManager.__init__(self)
def start(self): """Start the DHCP Server """ logger.info("Start the server") self.lease_mgr = LeaseManager(self.options) #~ self.uuid = self.options.get_option(self.section, 'uuid') #~ if self.uuid == None: #~ self.uuid = muuid.uuid1() #~ self.options.set_option(self.section, 'uuid', '%s'%self.uuid) self.loop_sleep = 0.25 loop_sleep = self.options.get_option('system','loop_sleep') if loop_sleep is not None: try: self.loop_sleep = int(loop_sleep) except Exception: logger.exception("[%s] - Exception when retrieving value of loop_sleep. Use default value instead", self.__class__.__name__) self.network = DhcpNetwork(self._stopevent, self.options, is_primary=True, is_secondary=False, do_heartbeat_dispatch=True) JNTDBServer.start(self) JNTControllerManager.start_controller(self, self.section, self.options, cmd_classes=[COMMAND_DHCPD], hadd=None, name="DHCP Server", product_name="DHCP Server", product_type="DHCP Server") self.mqtt_resolv = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) self.mqtt_resolv.connect() self.mqtt_resolv.start() #~ print "self.network.resolv_timeout", self.network.resolv_timeout self.resolv_timer = threading.Timer(self.network.resolv_timeout, self.resolv_heartbeat) self.resolv_timer.start() self.network.boot({0:self.get_controller_hadd()}, loop_sleep=self.loop_sleep) self.mqtt_client = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) self.mqtt_client.add_topic(topic='/dhcp/lease/new', callback=self.mqtt_on_lease_new) self.mqtt_client.add_topic(topic='/dhcp/lease/repair', callback=self.mqtt_on_lease_repair) self.mqtt_client.add_topic(topic='/dhcp/lease/lock', callback=self.mqtt_on_lease_lock) self.mqtt_client.add_topic(topic='/dhcp/lease/remove', callback=self.mqtt_on_lease_remove) self.mqtt_client.add_topic(topic='/dhcp/lease/release', callback=self.mqtt_on_lease_release) self.mqtt_client.add_topic(topic='/dhcp/heartbeat#', callback=self.mqtt_on_heartbeat) self.mqtt_client.add_topic(topic='/dhcp/resolv/hadd', callback=self.mqtt_on_resolv_hadd) self.mqtt_client.add_topic(topic='/dhcp/resolv/name', callback=self.mqtt_on_resolv_name) self.mqtt_client.add_topic(topic='/dhcp/resolv/cmd_classes', callback=self.mqtt_on_resolv_cmd_classes) self.mqtt_client.connect() self.mqtt_client.subscribe(topic='/dhcp/#', callback=self.mqtt_on_message) self.mqtt_client.start() self.heartbeat_timer = threading.Timer(self.lease_mgr.heartbeat_timeout, self.check_heartbeat) self.heartbeat_timer.start() #ProgrammingError: (pysqlite2.dbapi2.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. #The object was created in thread id 139632282289984 and this is thread id 139632153548544 #[SQL: u'SELECT dhcpd_lease.add_ctrl AS dhcpd_lease_add_ctrl, dhcpd_lease.add_node AS dhcpd_lease_add_node, dhcpd_lease.name AS dhcpd_lease_name, dhcpd_lease.location AS dhcpd_lease_location, dhcpd_lease.cmd_classes AS dhcpd_lease_cmd_classes, dhcpd_lease.state AS dhcpd_lease_state, dhcpd_lease.last_seen AS dhcpd_lease_last_seen \nFROM dhcpd_lease'] [parameters: [immutabledict({})]] #self.lease_mgr.start(self.dbsession) #Use a new session for the lease self.lease_mgr.start(self.create_session(), self.network.heartbeat_cache) JNTControllerManager.start_controller_timer(self)
def stop(self): """Stop the DHCP Server """ logger.info("Stop the server") if self.heartbeat_timer is not None: #The manager is started self.heartbeat_timer.cancel() self.heartbeat_timer = None if self.resolv_timer is not None: #The manager is started self.resolv_timer.cancel() self.resolv_timer = None JNTControllerManager.stop_controller_timer(self) if self.network is not None: self.network.stop() if self.lease_mgr is not None: self.lease_mgr.stop() if self.mqtt_resolv is not None: self.mqtt_resolv.stop() self.mqtt_resolv = None JNTControllerManager.stop_controller(self) maxi = 1 while maxi<10 and not self.network.is_stopped: self._stopevent.wait(self.loop_sleep*10) maxi += self.loop_sleep*10 if self.mqtt_client is not None: self.mqtt_client.unsubscribe(topic='/dhcp/#') self.mqtt_client.remove_topic(topic='/dhcp/lease/new') self.mqtt_client.remove_topic(topic='/dhcp/lease/repair') self.mqtt_client.remove_topic(topic='/dhcp/lease/lock') self.mqtt_client.remove_topic(topic='/dhcp/lease/remove') self.mqtt_client.remove_topic(topic='/dhcp/lease/release') self.mqtt_client.remove_topic(topic='/dhcp/heartbeat#') self.mqtt_client.remove_topic(topic='/dhcp/resolv/hadd') self.mqtt_client.remove_topic(topic='/dhcp/resolv/name') self.mqtt_client.remove_topic(topic='/dhcp/resolv/cmd_classes') self.mqtt_client.stop() self.mqtt_client = None JNTDBServer.stop(self) self.network = None self.lease_mgr = None logger.info("Server stopped")