reply bug

This commit is contained in:
2020-03-31 18:22:59 +08:00
parent 7d071f6b2f
commit adc6f65bd1
2 changed files with 29 additions and 8 deletions

13
mswp.py
View File

@@ -27,11 +27,11 @@ support many lines...]
''' '''
BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096) BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096)
ID = jsondata.try_to_read_jsondata('id', 'unknown_id')
class Datapack: class Datapack:
def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'', def __init__(self, method='post', app='all', version='msw/0.1', head=None, body=b'',
file=None, gen_flag=True): file=None, gen_flag=True):
self.id = jsondata.try_to_read_jsondata('id', 'unknown_id') self.id = ID
if head is None: if head is None:
head = {} head = {}
self.head = head self.head = head
@@ -93,15 +93,14 @@ class Datapack:
ndp = copy.copy(self) ndp = copy.copy(self)
ndp.app = ndp.head['from'] ndp.app = ndp.head['from']
ndp.method = 'reply' ndp.method = 'reply'
ndp.head['to'] = ndp.id ndp.head['to'] = self.head['id']
ndp.id = self.id ndp.id = ID
return ndp return ndp
def process_plugins_name(name): def process_plugins_name(name):
if 'plugins/' in name and '.py' in name: if 'plugins.' in name:
name = name.replace('plugins/', '') name = name.replace('plugins.', '')
name = name.replace('.py', '')
return name return name
else: else:
return name return name

View File

@@ -2,6 +2,7 @@ import threading
import socket import socket
import copy import copy
import queue import queue
import json
import os import os
import time import time
from mswp import Datapack from mswp import Datapack
@@ -56,6 +57,8 @@ class Network_controller: # manage id and connection
dp.app = 'net' dp.app = 'net'
dp.method = 'get' dp.method = 'get'
dp.body = b'mht' dp.body = b'mht'
print('Send mht request', dp)
send_queue.put(dp) send_queue.put(dp)
@@ -102,9 +105,24 @@ class Network_controller: # manage id and connection
def process_command(self, dp): def process_command(self, dp):
if dp.body == b'status': if dp.body == b'status':
print('Online %s' % str(list(self.id_dict.keys()))) 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() 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): def start_sending_dp(self):
@@ -194,6 +212,7 @@ class Connection:
self.padding_queue = queue.Queue() self.padding_queue = queue.Queue()
self.thread_send = None self.thread_send = None
self.positive = positive self.positive = positive
self.listen_port = None
self.thread_recv = threading.Thread(target=self._init, args=(), daemon=True) self.thread_recv = threading.Thread(target=self._init, args=(), daemon=True)
self.thread_recv.start() self.thread_recv.start()
@@ -280,6 +299,7 @@ class Connection:
------------------------------- -------------------------------
post handshake msw/0.1 post handshake msw/0.1
id: [yourID] id: [yourID]
listen_port: [3900]
length: 0 length: 0
------------------------------- -------------------------------
@@ -311,6 +331,7 @@ class Connection:
return 3, dp.head.get('flag') return 3, dp.head.get('flag')
self.id = dp.head['id'] self.id = dp.head['id']
self.listen_port = dp.head.get('listen_port')
if self.id == jsondata.try_to_read_jsondata('id', 'unknown_id'): if self.id == jsondata.try_to_read_jsondata('id', 'unknown_id'):
print('you connect to your self') print('you connect to your self')
@@ -325,6 +346,7 @@ class Connection:
def send_id(self): def send_id(self):
dp = Datapack(head={'from': __name__}) dp = Datapack(head={'from': __name__})
dp.app = 'handshake' dp.app = 'handshake'
dp.head['listen_port'] = str(jsondata.try_to_read_jsondata('listen_port', 3900))
dp.encode() dp.encode()
self.conn.sendall(dp.encode_data) self.conn.sendall(dp.encode_data)