diff --git a/mswp.py b/mswp.py index a90719e..c749954 100644 --- a/mswp.py +++ b/mswp.py @@ -27,11 +27,11 @@ support many lines...] ''' BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096) - +ID = jsondata.try_to_read_jsondata('id', 'unknown_id') class Datapack: def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'', file=None, gen_flag=True): - self.id = jsondata.try_to_read_jsondata('id', 'unknown_id') + self.id = ID if head is None: head = {} self.head = head @@ -93,15 +93,14 @@ class Datapack: ndp = copy.copy(self) ndp.app = ndp.head['from'] ndp.method = 'reply' - ndp.head['to'] = ndp.id - ndp.id = self.id + ndp.head['to'] = self.head['id'] + ndp.id = ID return ndp def process_plugins_name(name): - if 'plugins/' in name and '.py' in name: - name = name.replace('plugins/', '') - name = name.replace('.py', '') + if 'plugins.' in name: + name = name.replace('plugins.', '') return name else: return name diff --git a/plugins/net.py b/plugins/net.py index ab3069c..9c177ba 100644 --- a/plugins/net.py +++ b/plugins/net.py @@ -2,6 +2,7 @@ import threading import socket import copy import queue +import json import os import time from mswp import Datapack @@ -56,6 +57,8 @@ class Network_controller: # manage id and connection dp.app = 'net' dp.method = 'get' dp.body = b'mht' + + print('Send mht request', dp) send_queue.put(dp) @@ -102,9 +105,24 @@ class Network_controller: # manage id and connection def process_command(self, dp): if dp.body == b'status': print('Online %s' % str(list(self.id_dict.keys()))) - elif dp.body == b'mht': + elif dp.body == b'mht' and dp.method == 'get': ndp = dp.reply() + + connection_list = [] + with self.lock: + for id in self.id_dict: + connections = self.id_dict[id] + for connection in connections: + ip, port = connection.conn.getpeername() + listen_port = connection.listen_port + connection_list.append((ip, listen_port)) + ndp.body = json.dumps(connection_list).encode() + + send_queue.put(ndp) + + else: + print('Received unknown command', dp) def start_sending_dp(self): @@ -194,6 +212,7 @@ class Connection: self.padding_queue = queue.Queue() self.thread_send = None self.positive = positive + self.listen_port = None self.thread_recv = threading.Thread(target=self._init, args=(), daemon=True) self.thread_recv.start() @@ -280,6 +299,7 @@ class Connection: ------------------------------- post handshake msw/0.1 id: [yourID] + listen_port: [3900] length: 0 ------------------------------- @@ -311,6 +331,7 @@ class Connection: return 3, dp.head.get('flag') self.id = dp.head['id'] + self.listen_port = dp.head.get('listen_port') if self.id == jsondata.try_to_read_jsondata('id', 'unknown_id'): print('you connect to your self') @@ -325,6 +346,7 @@ class Connection: def send_id(self): dp = Datapack(head={'from': __name__}) dp.app = 'handshake' + dp.head['listen_port'] = str(jsondata.try_to_read_jsondata('listen_port', 3900)) dp.encode() self.conn.sendall(dp.encode_data)