def initialize(self): """Store a reference to the "external" RoomHandler instance""" self._inmessage = {} self.__clientID = None self.__user = None self.__rh = ServiceManager.get_instance().get_service(MY_ROOM_HANDLER) self.__session = ServiceManager.get_instance().get_service(MY_SESSION_HANDLER)
def start_server(self): ''' launch the true starting sequence for the server ''' self._set_logger() if self._is_server_started(): print('\nServer alrady started. If not, remove the lock (if you are running the test, this message is normal)') logging.error('Server alrady started. If not, remove the lock') return 1 else: self._lock_instance() logging.info('Starting server') cluster_adapter = ClusterAdapter.get_instance() cluster_adapter.start() service_manager = ServiceManager.get_instance() service_manager.create_all_service(True) self._external_control = ExternalControl(self) self._external_control.start() logging.info('Server started') if self._is_testing: pass else : # waiting for the stop order self._stop_event.wait() self.stop_server() return 0
def test_enable_available_service(self): print('\ntest de l\'activation d\'un service') service_manager = ServiceManager.get_instance() service_interface = ServiceInterface() self.assertEqual(service_manager.create_service('dummy', service_interface), True) self.assertEqual(service_manager.enable_service('dummy'), True) self.assertEqual(service_interface, service_manager._services_enable['dummy'])
def main(self): self._room_handler = ServiceManager.get_instance().get_service(MY_ROOM_HANDLER) self._cluster = ClusterAdapter.get_instance() self._cluster.subscribe('say_socket_send', self.say_cluster_callback) logging.info('launching SaySocket service') self._shutdown_event.wait() self._shutdown_event.clear()
def test_get_service_available(self): print('\nTest de la recuperation et du lancement d\'un service available') service_manager = ServiceManager.get_instance() service_interface = ServiceInterface() self.assertEqual(service_manager.create_service('dummy', service_interface), True) self.assertEqual(service_manager.get_service('dummy'), service_interface) self.assertEqual(service_interface, service_manager._services_enable['dummy'])
def stop_server(self): ''' stop server's sequence ''' logging.info('Halting server requested') service_manager = ServiceManager.get_instance() service_manager.stop_service_manager() self._external_control.halt_external_control() self._external_control.join() cluster_adapter = ClusterAdapter.get_instance() cluster_adapter.disable() cluster_adapter.join() self._release_instace() logging.info('Halting server done')
def on_message(self, message): self.get_user(message) self._inmessage = json.loads(message) logging.info(message) self._inmessage["usersession"]= self.__user if "task" in self._inmessage: self._inmessage["cid"]= self.__cid service_manager = ServiceManager.get_instance() routeur_service = service_manager.get_service('zephserver.service.routeur_service/RouteurService') routeur_service.route(self._inmessage) else: logging.info(message) cid = self.__rh.add_roomuser(message, self.__user) self.__cid = cid self.__rh.add_client_wsconn(self.__cid, self)
def get_current_user(self, instance, message): # get_user needs a django request object, but only looks at the session class Dummy(object): pass django_request = Dummy() try: #on essaye de se recuperer la session django_request.session = self.get_django_session(instance, message) user = django.contrib.auth.get_user(django_request) logging.info('User session %s'% user.id) except: # en cas d'echec on reset la connexion a la db logging.info('Database connection failed, trying to reset database') sm = ServiceManager.get_instance() db_service = sm.get_service('zephserver.service.db_service/DbService') db_service.reset_db() #on refait la requete django_request.session = self.get_django_session(instance, message) user = django.contrib.auth.get_user(django_request) try: if user.is_authenticated(): return user else: # try basic auth if not self.request.headers.has_key('Authorization'): return None kind, data = self.request.headers['Authorization'].split(' ') if kind != 'Basic': return None (username, _, password) = data.decode('base64').partition(':') user = django.contrib.auth.authenticate(username = username, password = password) if user is not None and user.is_authenticated(): return user return None except Exception, e: logging.warning("Authentification failed %s with exception %s" % (user, e)) return None
def tearDown(self): #apres chaque test on eteint le serveur service_manager = ServiceManager.get_instance() service_manager._services_available = {} service_manager._services_enable = {} service_manager._pending_stop = False
def test_disable_service_not_available(self): print('\ntest de la desactivation d\'un service') service_manager = ServiceManager.get_instance() self.assertEqual(service_manager.disable_service('dummy'), False)
def test_disable_service(self): print('\ntest de la desactivation d\'un service') service_manager = ServiceManager.get_instance() service_interface = ServiceInterface() self.assertEqual(service_manager.create_service('dummy', service_interface), True) self.assertEqual(service_manager.enable_service('dummy'), True)
def test_create_already_existing_service(self): print('\ntest de la creation d\'un service deja existant') service_manager = ServiceManager.get_instance() service_interface = ServiceInterface() self.assertEqual(service_manager.create_service('dummy', service_interface), True) self.assertEqual(service_manager.create_service('dummy', service_interface), False)
def test_delete_service(self): print('\ntest de la destruction d\'un service comme available') service_manager = ServiceManager.get_instance() service_interface = ServiceInterface() self.assertEqual(service_manager.create_service('dummy', service_interface), True) self.assertEqual(service_manager.delete_service('dummy'), True)
def test_create_service(self): print('\ntest de l\'enregistrement d\'un service comme available') service_manager = ServiceManager.get_instance() service_interface = ServiceInterface() self.assertEqual(service_manager.create_service('dummy', service_interface), True) self.assertEqual(service_interface, service_manager._services_available['dummy'])
def test_get_unique_instance(self): print('\ntest de l\'unicité du servicemanager') sm1 = ServiceManager.get_instance() sm2 = ServiceManager.get_instance() self.assertEqual(sm1, sm2)
def test_get_instance_simple(self): print('\ntest de la recuperation d\'une instance simple du servicemanager') self.assertIsInstance(ServiceManager.get_instance(), ServiceManager)
def test_disable_service_not_enabled(self): print('\ntest de la desactivation d\'un service qui na jamais été activé') service_manager = ServiceManager.get_instance() service_interface = ServiceInterface() self.assertEqual(service_manager.create_service('dummy', service_interface), True) self.assertEqual(service_manager.disable_service('dummy'), False)
def test_get_service_not_available(self): print('\nTest de la recuperation et du lancement d\'un service inexistant') service_manager = ServiceManager.get_instance() self.assertEqual(service_manager.get_service('dummy'), None)