From 169e98469cef4ab148b6d1b2bcde8e81c17884db Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 13 Jul 2021 16:14:31 +0800 Subject: [PATCH] bugfix --- kivyblocks/register.py | 2 + kivyblocks/tree.py | 12 ----- kivyblocks/udp_widget.py | 97 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 kivyblocks/udp_widget.py diff --git a/kivyblocks/register.py b/kivyblocks/register.py index 2129905..da37258 100644 --- a/kivyblocks/register.py +++ b/kivyblocks/register.py @@ -33,10 +33,12 @@ if kivy.platform in ['win','linux', 'macosx']: from .camerawithmic import ScreenWithMic from .camerawithmic import CameraWithMic from .scrollpanel import ScrollPanel +from .udp_widget import UdpWidget r = Factory.register if kivy.platform in ['win','linux', 'macosx']: r('ScreenWithMic', ScreenWithMic) +r('UdpWidget', UdpWidget) r('ScrollPanel', ScrollPanel) r('TextInput', TextInput) r('CameraWithMic', CameraWithMic) diff --git a/kivyblocks/tree.py b/kivyblocks/tree.py index 343e475..93abb21 100644 --- a/kivyblocks/tree.py +++ b/kivyblocks/tree.py @@ -64,9 +64,6 @@ class NodeTrigger(ButtonBehavior, EmptyBox): self.showOpenStatus() def on_press(self, *args): - Logger.info('NodeTrigger:on_press() called, open_status=%s', - self.open_status - ) self.open_status = False if self.open_status else True # self.showOpenStatus() @@ -77,7 +74,6 @@ class NodeTrigger(ButtonBehavior, EmptyBox): self.showOpenStatus() def showOpenStatus(self): - Logger.info('NodeTrigger:showOpenStatus() called') points = self.close_points if self.open_status: points = self.open_points @@ -181,14 +177,12 @@ class TreeNode(BoxLayout): return if self.data.children is None: - Logger.info('Tree : is a leaf node') return if self.data.children == []: self.treeObj.getUrlData(self.addChildren,self.data) return if len(self.nodes) == 0: - Logger.info('Tree : add subnodes') self.addChildren(self.data.children) else: Logger.info('Tree : not need add subnodes') @@ -492,11 +486,6 @@ class TextTree(Tree): class MenuTreeNode(TextTreeNode): def on_size(self, *args): - Logger.info('%s:on_size(),bgcolor=%s, fgcolor=%s,css=%s', - self.__class__.__name__, - self.content.bgcolor, - self.content.fgcolor, - self.content.csscls) self.node_box.width = self.width self.content.width = self.node_box.width - self.trigger.width self.text_widget.width = self.content.width - CSize(1) @@ -632,7 +621,6 @@ class PopupMenu(BoxLayout): Logger.info('PopupMenu: on_press fired') def onMenuItemTouch(self,o,d=None,v=None): - Logger.info('MenuTree: on_press fired,o=%s,d=%s,v=%s',o,d,v) data = { 'target':self.treeObj.target, 'menudata':d diff --git a/kivyblocks/udp_widget.py b/kivyblocks/udp_widget.py new file mode 100644 index 0000000..7034448 --- /dev/null +++ b/kivyblocks/udp_widget.py @@ -0,0 +1,97 @@ +import time +from kivy.event import EventDispatcher +from appPublic.udp_comm import UdpComm +from appPublic.dataencoder import DataEncoder + +class UdpWidget(EventDispatcher): + def __init__(self, udp_port=55000, cert_file=None, commands=[], + **kw): + super(UdpWidget, self).__init__(**kw) + self.udp_port = udp_port + self.commands = commands + self.block_commands = [] + self.udp_transport = UdpComm(udp_port, self.comm_callback) + host = self.upd_transport.host + self.dataencoder = DataEncoder(host, self.get_peer_pubkey) + self.inner_handlers = { + 'get_pubkey':self.resp_pubkey + } + for cmd in self.commands: + evt_name = 'on_%s' % cmd + setattr(self, evt_name, self.event_handler) + self.register_event_type(evt_name) + + def block_command(self, cmd): + if cmd not in self.command: + return + if cmd in self.block_command: + return + self.block_commands.append(cmd) + + def unblock_command(self, cmd): + if cmd not in self.block_command: + return + self.block_commands = [ c for c in self.block_command if c!=cmd ] + + def get_peer_pubkey(self, peer_id, timeout=2): + d = { + 'c':'get_pubkey', + 'd':{ + 'pubkey':self.dataencoder.my_text_publickey() + } + } + b = json.dumps(d).encode('utf-8') + self.udp_tranport.broadcast(b) + t1 = time.time() + t = t1 + t1 += timeout + while t1 > t: + time.sleep(0.1) + t = time.time() + if self.dataencoder.exist_peer_publickeys(): + return + raise Exception('timeout') + + def comm_callback(self, data, addr): + d = None + if data[:18] == b'0x00' * 18: + data = data[18:] + try: + d = json.loads(data.decode('utf-8')) + except Exception as e: + print(e, addr, data) + print_exc() + return + else: + d = self.dataencoder.unpack(addr[0], data) + if d is None: + return + if not isinstance(d, dict): + return + cmd = d['c'] + f = self.inner_handler(cmd) + if f: + f(d, addr) + return + if cmd in self.block_commands: + return + evt_name = 'on_%s' % cmd + evt_data = { + 'data': d, + 'addr': addr + } + self.dispatch(evt_name, evt_data) + + def event_handler(self, o, d): + Logger.info('UdpWidget: received data=%s', d) + + def resp_pubkey(self, data, addr): + pk = data['d']['pubkey'] + id = addr[0] + self.dataencoder.set_peer_text_pubkey(id, pk) + + def send(self, peer_id, data): + d = self.dataencoder.pack(peer_id, data) + addr = (peer_id, self.udp_port) + self.udp_transport.send(d, addr) +