-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventTarget.py
96 lines (85 loc) · 3.1 KB
/
EventTarget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2018/6/26 18:52
# @Author : hedengfeng
# @Site :
# @File : EventTarget.py 所有对象的父类
# @Software: LT_controller
import threading
import traceback
from Mlogging import MLog
from EventBus import EventBus
class EventTarget(object):
"""
EventBus的Target对象,由该对象的子类处理event事件和发送事件
"""
_my_client = None
# 改构造必须要再子类构造之前构造
def __init__(self, child_class):
"""
在eventBus上找到本身线程的事件处理者
:rtype: object
"""
self._child_class = child_class
self._my_client = EventBus.find_client(threading.get_ident())
if self._my_client is None:
MLog.mlogger.warn('not find client: %i' % threading.get_ident())
traceback.print_stack()
def event_handle(self, event, event_content):
"""
事件处理函数,该函数可以理解为纯虚函数,EventTarget子类必须要实现
事件客户端接收到事件总线上的数据之后,会调用该函数
:param event_content: bytes
:param event: str
"""
if self._my_client is not None:
self._child_class.event_handle(event, event_content)
else:
MLog.mlogger.warn('_my_client is not None')
def publish_event(self, event, event_content):
"""
通过事件客户端发布一条信息到eventBus上
:param event_content: bytes
:param event: str
"""
if self._my_client is not None:
self._my_client.publish_event(event, event_content)
else:
MLog.mlogger.warn('_my_client is not None')
traceback.print_stack()
def publish_loc_event(self, event, event_content):
"""
发布一个事件到本地客户端,不到EventBus上,在本客户端的订阅者将会直接调用
:param event_content: bytes
:param event: str
"""
if self._my_client is not None:
self._my_client.publish_loc_event(event, event_content)
else:
MLog.mlogger.warn('_my_client is not None')
# traceback.print_stack()
def subscribe(self, event_id, target_object):
"""
订阅一条事件。
:param event_id: 事件id
:param target_object:子类对象
"""
if self._my_client is not None:
self._my_client.register_observer(event_id, target_object)
else:
MLog.mlogger.warn('_my_client is None')
# traceback.print_stack()
def unsubscribe(self, event_id, target_object):
"""
撤销对一条事件的订阅。
:param event_id: 事件id
:param target_object:子类对象
"""
if self._my_client is not None:
self._my_client.unregister_observer(event_id, target_object)
else:
MLog.mlogger.warn('_my_client is not None')
def __del__(self):
""""""
# print('EventTarget __del__', self._child_class)
# super(EventTarget, self).__del__()