def test_dispatch_event_to_listeners_without_any_registered_event_listeners( redis_client): """ GIVEN a StoreAFilesMd5MicroService instance WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db collection should be incremented by 1 """ dispatcher = EventDispatcher() dispatcher.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] dispatcher.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] dispatcher.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] dispatcher.establish_redis_connection() dispatcher.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] dispatcher.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] dispatcher.establish_mongodb_connection() common_lib = dispatcher.get_common_lib_instance() common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] common_lib.establish_redis_connection() common_lib.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] common_lib.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] common_lib.establish_mongodb_connection() result = dispatcher.dispatch_event_to_listeners( "sequence_video_clips-melt_ended_event", "20200503223628.840265", { "return_code": 0, "file_path_name": "/video_clips/output-20200503-090308.mp4", "transaction_id": "20200503174008.553417", }) assert len(result) is 0
def __init__(self, server_type: str, host: str, port: int, max_connections: int, config: MainConfig): self.rndispatcher = EventDispatcher() self.rnserver = RNServer((host, port), max_connections, b'3.25 ND1', None, self.rndispatcher) self.rndispatcher.add_listener(RNMessage.UserPacket, self.on_user_packet) self.config = config self.type = server_type self.plugins = [] self.handlers = {} self.packets = {} if self.type not in ['auth', 'chat', 'char']: self.repman = ReplicaManager(self.rndispatcher) self.register_plugins('server.core') if self.type in ['auth', 'chat']: self.register_plugins('server.' + self.type) else: self.register_plugins('server.world') if self.type != 'char': self.register_plugins('server.world.zone') log.info(f'{self.type.upper()} - Started up')
def __init__(self, window): self._event_dispatcher = EventDispatcher(self, window) self._running = False rotate_event = sdl2.SDL_Event() rotate_event.type = EventDispatcher.ROTATE_EVENT self._window = window self._rotate_event_pointer = ctypes.byref(rotate_event)
def test_dispatch_event_to_listeners_with_a_registered_event_listener_having_a_datetime_end_set_into_the_future( redis_event_listener_entry_with_a_datetime_end_set_into_the_future): """ GIVEN a StoreAFilesMd5MicroService instance WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db collection should be incremented by 1 """ return_code = 0 transaction_id = "20200503174008.553417" datetime_end = "21200503174008.553417" dispatcher = EventDispatcher() dispatcher.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] dispatcher.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] dispatcher.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] dispatcher.establish_redis_connection() dispatcher.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] dispatcher.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] dispatcher.establish_mongodb_connection() common_lib = dispatcher.get_common_lib_instance() common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] common_lib.establish_redis_connection() common_lib.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] common_lib.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] common_lib.establish_mongodb_connection() result = dispatcher.dispatch_event_to_listeners( "sequence_video_clips-melt_ended_event", "20200503223628.840265", { "return_code": return_code, "file_path_name": "/video_clips/output-20200503-090308a.mp4", "transaction_id": transaction_id, }) assert len(result) is 1 assert result[0] == ('http://some_url.com/', { "return_code": return_code, "file_path_name": "/video_clips/output-20200503-090308a.mp4", "transaction_id": transaction_id, }, { "datetime_end": datetime_end, }) result = dispatcher.dispatch_event_to_listeners( "sequence_video_clips-melt_ended_event", datetime_end, { "return_code": return_code, "file_path_name": "/video_clips/output-20200503-090308b.mp4", "transaction_id": transaction_id, }) assert len(result) is 1 assert result[0] == ('http://some_url.com/', { "return_code": return_code, "file_path_name": "/video_clips/output-20200503-090308b.mp4", "transaction_id": transaction_id, }, { "datetime_end": datetime_end, })
async def run(self): ''' shared_market_data = { "taker_fee": exchange_variables["taker_fee"], -> done "CRO_holding_backup": exchange_variables["CRO_holding_backup"], -> done "balance_USDT": 0, -> done "balance_BTC": 0, -> done "balance_CRO": 0, -> done "price_BTC_sell_to_USDT": 0, -> done "price_CRO_buy_for_BTC": 0, -> done "fee_BTC_sell_in_USDT": 0, "price_CRO_buy_for_USDT": 0, -> done "last_CRO_price_in_USDT": 0, -> done "last_CRO_price_in_BTC": 0, -> done "fee_BTC_sell_in_CRO": 0, "price_BTC_buy_for_USDT": 0, -> done "fee_BTC_buy_in_BTC": 0, "fee_BTC_buy_in_CRO": 0, } ''' event_dispatcher = EventDispatcher() event_dispatcher.register_channel_handling_method("user.balance", self.handle_channel_event_user_balance) event_dispatcher.register_response_handling_method("public/get-instruments", self.handle_response_get_instruments) async with CryptoClient( client_type=CryptoClient.USER, debug=self.debug, logger=self.logger, api_key=self.crypto_com_client.crypto_com_api_key, api_secret=self.crypto_com_client.crypto_com_secret_key, channels=[ "user.balance" ] ) as client: periodic_call_get_instruments = PeriodicAsync(5, lambda: self.get_instruments(client)) await periodic_call_get_instruments.start() try: while True: event_or_response = None try: event_or_response = await client.next_event_or_response() event_dispatcher.dispatch(event_or_response) except Exception as e: if event_or_response: message = "Exception during handling user api event or response: {}".format(repr(e)) self.logger.exception(message) self.logger.error("Event or response that failed: {}".format(event_or_response)) else: message = "Exception during parsing user api event or response: {}".format(repr(e)) self.logger.exception(message) # TODO: Send pushover notification here with message continue finally: self.logger.info("Cleanup before closing worker...") await periodic_call_get_instruments.stop()
def __init__(self): self.running = False self.event_dispatcher = EventDispatcher() self.graphics = gamegraphics.Graphics(self, self.event_dispatcher) self.map = gamemap.Map() self.player = player.Player(self, self.event_dispatcher) self.ai = ai.AI(self, self.event_dispatcher) def sigint_handler(sig, frame): self.event_dispatcher.add_event("quit", "Ctrl-C") signal.signal(signal.SIGINT, sigint_handler) self.event_dispatcher.register("quit", self.quit)
async def run(self): event_dispatcher = EventDispatcher() event_dispatcher.register_channel_handling_method( "ticker.BTC_USDT", self.handle_channel_event_ticker_BTC_USDT) event_dispatcher.register_channel_handling_method( "ticker.CRO_USDT", self.handle_channel_event_ticker_CRO_USDT) event_dispatcher.register_channel_handling_method( "ticker.CRO_BTC", self.handle_channel_event_ticker_CRO_BTC) async with CryptoClient(client_type=CryptoClient.MARKET, debug=self.debug, logger=self.logger, channels=[ "ticker.BTC_USDT", "ticker.CRO_USDT", "ticker.CRO_BTC", ]) as client: try: while True: event_or_response = None try: event_or_response = await client.next_event_or_response( ) event_dispatcher.dispatch(event_or_response) except Exception as e: if event_or_response: message = "Exception during handling market data event or response: {}".format( repr(e)) self.logger.exception(message) self.logger.error( "Event or response that failed: {}".format( event_or_response)) else: message = "Exception during parsing market data event or response: {}".format( repr(e)) self.logger.exception(message) # TODO: Send pushover notification here with message continue finally: self.logger.info("Cleanup before closing worker...")
def test_dispatch_event_to_listeners_with_a_registered_event_listener_having_several_required_data_but_1_that_does_not_exist_in_any_recent_related_events( redis_event_listener_entry_with_several_required_data_but_1_that_does_not_exist_in_any_recent_related_events, standard_db_event_entries): """ GIVEN a StoreAFilesMd5MicroService instance WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db collection should be incremented by 1 """ event_id = "sequence_video_clips-melt_ended_event" return_code = 0 file_path_name = "/video_clips/output-20200503-090308.mp4" transaction_id = "20200503174008.553417" timestamp = "20200503223628.840266" dispatcher = EventDispatcher() dispatcher.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] dispatcher.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] dispatcher.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] dispatcher.establish_redis_connection() dispatcher.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] dispatcher.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] dispatcher.establish_mongodb_connection() common_lib = dispatcher.get_common_lib_instance() common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] common_lib.establish_redis_connection() common_lib.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] common_lib.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] common_lib.establish_mongodb_connection() result = dispatcher.dispatch_event_to_listeners( event_id, timestamp, { "return_code": return_code, "file_path_name": file_path_name, "transaction_id": transaction_id, }) assert len(result) is 0
def test_dispatch_event_to_listeners_with_a_registered_event_listener_that_is_offline( mocker, standard_redis_event_listener_entry): """ GIVEN a StoreAFilesMd5MicroService instance WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db collection should be incremented by 1 """ timestamp = "20200503223628.840265" return_code = 0 file_path_name = "/video_clips/output-20200503-090308.mp4" transaction_id = "20200503174008.553417" event_id = 'sequence_video_clips-melt_ended_event' url = "http://some_url.com/" priority = 100 event_data = { "return_code": return_code, "file_path_name": file_path_name, "transaction_id": transaction_id, } expected_listener_attributes = { "url": url, "datetime_start": timestamp, "event_id": event_id, } dispatcher = EventDispatcher() dispatcher.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] dispatcher.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] dispatcher.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] redis_client = dispatcher.establish_redis_connection() dispatcher.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] dispatcher.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] dispatcher.establish_mongodb_connection() common_lib = dispatcher.get_common_lib_instance() common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] common_lib.establish_redis_connection() common_lib.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] common_lib.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] common_lib.establish_mongodb_connection() send_event_to_listener = mocker.patch.object(common_lib, 'send_event_to_listener') send_event_to_listener.return_value = False dispatcher.CONFIG['MOCKED'] = True result = dispatcher.dispatch_event_to_listeners(event_id, timestamp, event_data) dispatcher.CONFIG['MOCKED'] = False assert len(result) is 1 assert result[0] == (url, event_data, {}) send_event_to_listener.assert_called_with(url, event_data) assert redis_client.zcard(sorted_set_retro_event_listeners) == 1 assert redis_client.zrange( sorted_set_retro_event_listeners, 0, -1, withscores=True)[0] == ( url.encode('UTF-8'), # b'http://some_url.com/' priority) listener_attributes = json.loads( redis_client.hmget(hash_retro_event_listeners, url)[0].decode('UTF-8')) assert listener_attributes is not None assert listener_attributes == expected_listener_attributes
def test_dispatch_event_to_listeners_with_a_registered_event_listener_having_several_required_data_that_exists_in_recent_related_events( redis_event_listener_entry_with_several_required_data_that_exists_in_recent_related_events, standard_db_event_entries): """ GIVEN a StoreAFilesMd5MicroService instance WHEN the 'StoreAFilesMd5MicroService.get_device_id(db, device)' is invoked and given a new device as argument THEN that new device should be stored in the 'device_ids' db collection and the 'device_id_index' from 'globals' db collection should be incremented by 1 """ event_id = "sequence_video_clips-melt_ended_event" return_code = 0 file_path_name = "/video_clips/output-20200503-090308.mp4" transaction_id = "20200503174008.553417" url = 'http://some_url.com/' timestamp = "20200503223628.840266" required_data = [ "clips", "video_clips", "converted_video_clips", "sequenced_video_clips", "file_size" ] dispatcher = EventDispatcher() dispatcher.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] dispatcher.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] dispatcher.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] dispatcher.establish_redis_connection() dispatcher.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] dispatcher.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] dispatcher.establish_mongodb_connection() common_lib = dispatcher.get_common_lib_instance() common_lib.CONFIG['TESTING'] = conftest.CONFIG['TESTING'] common_lib.CONFIG['REDIS_HOST'] = conftest.CONFIG['REDIS_HOST'] common_lib.CONFIG['REDIS_DB'] = conftest.CONFIG['REDIS_DB'] common_lib.establish_redis_connection() common_lib.CONFIG['MONGODB_HOST'] = conftest.CONFIG['MONGODB_HOST'] common_lib.CONFIG['MONGODB_DB'] = conftest.CONFIG['MONGODB_DB'] common_lib.establish_mongodb_connection() result = dispatcher.dispatch_event_to_listeners( event_id, timestamp, { "return_code": return_code, "file_path_name": file_path_name, "transaction_id": transaction_id, }) assert len(result) is 1 assert result[0] == (url, { "return_code": return_code, "file_path_name": file_path_name, "transaction_id": transaction_id, 'clips': [{ 'clip_url': 'https://www.twitch.tv/ytmagma/clip/ShyFantasticPterodactylPogChamp', 'clip_title_text': 'im not gay', 'clip_player_name': 'YTMagMa', 'clip_player_url': 'https://www.twitch.tv/ytmagma', 'clip_image_url': 'https://clips-media-assets2.twitch.tv/AT-cm%7C766375085-preview-260x147.jpg', 'clip_duration': 12, 'clip_view_count': 3000, 'clip_hours_ago': '1 hour ago' }, { 'clip_url': 'https://www.twitch.tv/benjyfishy/clip/KawaiiDreamyKangarooThisIsSparta', 'clip_title_text': 'Benjy Music Timing', 'clip_player_name': 'benjyfishy', 'clip_player_url': 'https://www.twitch.tv/benjyfishy', 'clip_image_url': 'https://clips-media-assets2.twitch.tv/AT-cm%7C765635143-preview-260x147.jpg', 'clip_duration': 7, 'clip_view_count': 2400, 'clip_hours_ago': '15 hours ago' }], 'video_clips': { 'https://www.twitch.tv/benjyfishy/clip/KawaiiDreamyKangarooThisIsSparta': { 'file_path_name': '/video_clips/Benjy Music Timing.mp4', 'return_code': 0, 'std_errors': [], 'std_outputs': [] }, 'https://www.twitch.tv/ytmagma/clip/ShyFantasticPterodactylPogChamp': { 'file_path_name': '/video_clips/im not gay.mp4', 'return_code': 0, 'std_errors': [], 'std_outputs': [] } }, 'converted_video_clips': { 'https://www.twitch.tv/benjyfishy/clip/KawaiiDreamyKangarooThisIsSparta': { 'file_path_name': '/video_clips/Benjy Music Timing.Vimeo_YouTube_HQ_1080p60.mp4', 'return_code': 0, 'std_errors': [], 'std_outputs': [] }, 'https://www.twitch.tv/ytmagma/clip/ShyFantasticPterodactylPogChamp': { 'file_path_name': '/video_clips/im not gay.Vimeo_YouTube_HQ_1080p60.mp4', 'return_code': 0, 'std_errors': [], 'std_outputs': [] } }, 'sequenced_video_clips': { 'file_path_name': '/video_clips/output-20200628-084301.mp4', 'return_code': 0, 'std_errors': [], 'std_outputs': [], 'timecodes': { 'https://www.twitch.tv/benjyfishy/clip/KawaiiDreamyKangarooThisIsSparta': 12, 'https://www.twitch.tv/ytmagma/clip/ShyFantasticPterodactylPogChamp': 0 } }, "file_size": 68, }, { "required_data": required_data, })
import tornado.platform.kqueue else: import tornado.platform.select except ImportError: pass import tornado.process import tornado.ioloop import tornado.web import tornado.websocket import tornado.httpserver # Tornado server instance httpserver = None logger = logging.getLogger('livestyle') dispatcher = EventDispatcher() clients = set() # all connected clients patchers = set() # clients identified as 'patcher' editors = dict() # clients identified as 'editor' class MainHandler(tornado.web.RequestHandler): def get(self): self.render('index.html') class WebsocketHandler(tornado.websocket.WebSocketHandler): def open(self): dispatcher.emit('open', self) send(clients, message('client-connect'), self)
def main(): dispatcher = EventDispatcher() ninterface = NetworkInterface(dispatcher, -1, -1, "", 8888, "") login_app = LoginApp(dispatcher, ninterface, 1, 1) login_app.run()
def __init__(self, window): self._event_dispatcher = EventDispatcher(self, window) self._running = False