diff --git a/forwarder.py b/forwarder.py index d78a90b..3cd2680 100644 --- a/forwarder.py +++ b/forwarder.py @@ -36,7 +36,7 @@ def send_queue_function(): object_app, new_dp = process_reforware(new_dp) receive_queues[add_plugins_string(object_app)].put(new_dp) else: - object_app, dp =process_reforware(dp) + object_app, dp = process_reforware(dp) receive_queues[add_plugins_string(object_app)].put(dp) diff --git a/plugins/input.py b/plugins/input.py index d487bb8..68eb346 100644 --- a/plugins/input.py +++ b/plugins/input.py @@ -7,9 +7,9 @@ receive_queue = receive_queues[__name__] def main(): - file_flag = False; + file_flag = False while True: - file_flag = False; + file_flag = False raw_data = input() if raw_data[:6] == '(file)': @@ -44,7 +44,10 @@ def find_the_last(indata): # find the last ":" index break last_index = copy.copy(first_index) last_index += 1 - while indata[last_index] == ' ': + try: + while indata[last_index] == ' ': + last_index += 1 + except IndexError: last_index += 1 return first_index, last_index diff --git a/plugins/net.py b/plugins/net.py index 6bafcf1..e624e00 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -94,6 +94,12 @@ class Netrecv: def check_send_queue(self): while True: dp = receive_queue.get() + + # debug code + if dp.body == b'stat': + print(self.stat) + continue + if dp.method == 'file': print('right') print(dp.head) @@ -104,7 +110,7 @@ class Netrecv: file = open(dp.head['filename'], 'rb') for data in file: conn.send(data) - print('sended') + print('sended') else: print('wrong') @@ -125,11 +131,20 @@ class Netrecv: conn.close() for id in self.stat: if (conn, addr) in self.stat[id]: - self.stat[id].remove(conn, addr) + self.stat[id].remove((conn, addr)) self.connection_list.remove((conn, addr)) print('Removed connection', str(addr)) + def say_hello(self, conn, addr): + dp = Datapack(head={'from': __name__}) + dp.app = 'net' + dp.encode() + conn.sendall(dp.encode_data) + print('hello package has been sent') + def process_connection(self, conn, addr): + self.say_hello(conn, addr) + print('Connection accept %s' % str(addr)) data = b'' while True: @@ -164,6 +179,20 @@ class Netrecv: break # try unpack # + # net config data package + if dp.app == 'net': + + dp_id = dp.head['id'] + local_id = self.stat.get(dp_id) + + if not local_id: # create if not exits + self.stat[dp_id] = [] + + if not (conn, addr) in self.stat[dp_id]: + self.stat[dp_id].append((conn, addr)) + + continue + if dp.method == 'file': length = int(dp.head['length']) data_length = len(data) @@ -262,21 +291,9 @@ class Netrecv: dp.body = data[:length] data = data[length:] - # net config data package - if dp.app == 'net': - dp_id = dp.head['id'] - local_id = self.stat.get(dp_id) - - if not local_id: # create if not exits - self.stat[dp_id] = [] - - if not (conn, addr) in self.stat[dp_id]: - self.stat[dp_id].append((conn, addr)) - - else: - dp.encode() - send_queue.put(dp) - print('###############\n' + dp.encode_data.decode() + '\n###############') + dp.encode() + send_queue.put(dp) + print('###############\n' + dp.encode_data.decode() + '\n###############') thread = threading.Thread(target=main, args=()) diff --git a/plugins/update.py b/plugins/update.py index 6090931..d4f542d 100644 --- a/plugins/update.py +++ b/plugins/update.py @@ -6,15 +6,42 @@ from forwarder import receive_queues, send_queue receive_queue = receive_queues[__name__] -remove_file_list = ['__init__.py'] +remove_file_list = ['__init__.py', 'netlist.txt', 'config.json', 'logger.log'] remove_dir_list = ['.git', '.idea', '__pycache__'] def main(): while True: dp = receive_queue.get() - dp.encode() - print(dp.encode_data.decode()) + + if dp.method == 'post': + if dp.body == b'compress': + print('Starting update') + compress = Compresser() + filelist = compress.get_filelist() + compress.compress_files(filelist) + print('Compress finished') + + elif dp.body == b'all': + print('Start update other client') + compress = Compresser() + filelist = compress.get_filelist() + compress.compress_files(filelist) + print('Compress finished') + + dp = Datapack(head={'from': __name__}) + dp.method = 'file' + dp.app = 'net:update' + dp.head['filename'] = 'resources/update.tar.xz' + + dp.encode() + + send_queue.put(dp) + + elif dp.method == 'file': + print('Starting update local file') + with tarfile.open(dp.head['filename'], 'r:xz') as f: + f.extractall() @@ -23,7 +50,7 @@ class Compresser: self.filelist = [] def compress_files(self, filelist): - with tarfile.open('update.tar.xz', 'w:xz') as f: + with tarfile.open('resources/update.tar.xz', 'w:xz') as f: for name in filelist: f.add(name) diff --git a/test_tool.py b/test_tool.py index f0c15b5..f02d4d4 100644 --- a/test_tool.py +++ b/test_tool.py @@ -7,7 +7,7 @@ s.bind(('127.0.0.1', 3966)) s.listen(100) id = '''post net msw/1.0 -id: miku +id: miku2 from: test length: 0 @@ -23,8 +23,13 @@ def process(conn, addr): if not data: conn.close() return - data = data.decode() - print(data) + try: + data = data.decode() + print(data) + except UnicodeDecodeError: + print('Decode error') + print(data[:39]) + while True: