add network receive function and some ajust but no test
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
{
|
{
|
||||||
"test": "test",
|
"test": "test",
|
||||||
"listen": 3900
|
"listen_port": 3900,
|
||||||
|
"listen_addr": "127.0.0.1",
|
||||||
|
"listen_num": 39,
|
||||||
|
"recv_buff": 4096
|
||||||
}
|
}
|
||||||
16
config.py
16
config.py
@@ -4,8 +4,6 @@ import json
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Jsondata:
|
class Jsondata:
|
||||||
def __init__(self, auto_save=False, auto_save_time=10):
|
def __init__(self, auto_save=False, auto_save_time=10):
|
||||||
with open('config.json', 'r') as f:
|
with open('config.json', 'r') as f:
|
||||||
@@ -17,11 +15,17 @@ class Jsondata:
|
|||||||
self.thread = threading.Thread(target=self.run, args=())
|
self.thread = threading.Thread(target=self.run, args=())
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
|
|
||||||
def get(self, key):
|
def try_to_read_jsondata(self, key, or_value, template=0):
|
||||||
try:
|
if key in self.raw_jsondata.keys():
|
||||||
return self.raw_jsondata[key]
|
return self.raw_jsondata[key]
|
||||||
except:
|
else:
|
||||||
return False
|
print('Error: could not find key value in file "config.json"\n'
|
||||||
|
'Please set the key "%s" in file "config.json"\n'
|
||||||
|
'Or MSW will set it as %s' % (key, or_value))
|
||||||
|
return or_value
|
||||||
|
|
||||||
|
def get(self, key):
|
||||||
|
return self.raw_jsondata.get(key)
|
||||||
|
|
||||||
def set(self, key, value):
|
def set(self, key, value):
|
||||||
self.raw_jsondata[key] = value
|
self.raw_jsondata[key] = value
|
||||||
|
|||||||
4
mswp.py
4
mswp.py
@@ -1,9 +1,9 @@
|
|||||||
class Datapack:
|
class Datapack:
|
||||||
def __init__(self, method='post', app='all', version='msw/1.0', head={}, body=b''):
|
def __init__(self, method='post', app='all', version='msw/1.0', head={}, body=b'', check_head=True):
|
||||||
self.method = method
|
self.method = method
|
||||||
self.app = app
|
self.app = app
|
||||||
self.version = version
|
self.version = version
|
||||||
if not head:
|
if not head and check_head:
|
||||||
self.head = {'nohead': "true"}
|
self.head = {'nohead': "true"}
|
||||||
else:
|
else:
|
||||||
self.head = head
|
self.head = head
|
||||||
|
|||||||
@@ -3,11 +3,15 @@ import socket
|
|||||||
import queue
|
import queue
|
||||||
from mswp import Datapack
|
from mswp import Datapack
|
||||||
from forwarder import receive_queues, send_queue
|
from forwarder import receive_queues, send_queue
|
||||||
|
from config import jsondata
|
||||||
receive_queue = receive_queues[__name__]
|
receive_queue = receive_queues[__name__]
|
||||||
|
|
||||||
|
RECV_BUFF = jsondata.try_to_read_jsondata('recv_buff', 4096)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
netlist = Netlist()
|
netlist = Netlist()
|
||||||
|
netrecv = Netrecv()
|
||||||
while True:
|
while True:
|
||||||
dp = receive_queue.get()
|
dp = receive_queue.get()
|
||||||
dp.encode()
|
dp.encode()
|
||||||
@@ -25,15 +29,67 @@ def process_hostname(hostname):
|
|||||||
return ip
|
return ip
|
||||||
|
|
||||||
|
|
||||||
|
def read_netlisttxt_file():
|
||||||
|
try:
|
||||||
|
with open('netlist.txt', 'r') as f:
|
||||||
|
raw_data = f.read()
|
||||||
|
return raw_data
|
||||||
|
except Exception as e:
|
||||||
|
print('Error: %s, %s\n'
|
||||||
|
'If you are the first time to run this program, \n'
|
||||||
|
'Please use "netlist_sample.txt" to create "netlist.txt", \n'
|
||||||
|
'Program will continue...' % (type(e), str(e)))
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
class Netrecv:
|
||||||
|
def __init__(self):
|
||||||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # initial socket, bind and listen, start to accept
|
||||||
|
addr = jsondata.try_to_read_jsondata('listen_addr', '127.0.0.1')
|
||||||
|
port = jsondata.try_to_read_jsondata('listen_port', 3900)
|
||||||
|
print('MSW now trying to bind the network %s, please allow it' % str((addr, port)))
|
||||||
|
s.bind((addr, port))
|
||||||
|
listen_num = jsondata.try_to_read_jsondata('listen_num', 39)
|
||||||
|
s.listen(listen_num)
|
||||||
|
self.s = s
|
||||||
|
self.thread = threading.Thread(target=self.check_accpet_connection, args=())
|
||||||
|
self.thread.start()
|
||||||
|
self.connection_list = []
|
||||||
|
self.connection_process_thread_list =[]
|
||||||
|
|
||||||
|
def check_accpet_connection(self):
|
||||||
|
while True:
|
||||||
|
conn, addr = self.s.accept()
|
||||||
|
self.connection_list.append((conn, addr))
|
||||||
|
connection_thread = threading.Thread(target=self.process_connection, args=())
|
||||||
|
self.connection_process_thread_list.append(connection_thread)
|
||||||
|
connection_thread.start()
|
||||||
|
|
||||||
|
def process_connection(self, conn, addr):
|
||||||
|
print('Connection accpet %s' % str(addr))
|
||||||
|
while True:
|
||||||
|
data = conn.recv(RECV_BUFF) # here needs to check whether the package is continued
|
||||||
|
dp = Datapack(check_head=False)
|
||||||
|
dp.encode_data = data
|
||||||
|
dp.decode()
|
||||||
|
send_queue.put(dp)
|
||||||
|
|
||||||
|
|
||||||
class Netlist: # contain net list and network controller
|
class Netlist: # contain net list and network controller
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.send_queue = queue.Queue()
|
self.send_queue = queue.Queue()
|
||||||
with open('netlist.txt', 'r') as f:
|
raw_data = read_netlisttxt_file()
|
||||||
raw_data = f.read()
|
|
||||||
lines = raw_data.split('\n')
|
lines = raw_data.split('\n')
|
||||||
ips = []
|
ips = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
ip, port = line.split(':')
|
ip_port = line.split(':')
|
||||||
|
if len(ip_port) == 1:
|
||||||
|
ip = ip_port[0]
|
||||||
|
if not ip: # Check whether ip is null
|
||||||
|
continue
|
||||||
|
port = jsondata.get('listen_port')
|
||||||
|
if not port:
|
||||||
|
port = 3900
|
||||||
ip = process_hostname(ip)
|
ip = process_hostname(ip)
|
||||||
port = int(port)
|
port = int(port)
|
||||||
ips.append((ip, port))
|
ips.append((ip, port))
|
||||||
@@ -56,13 +112,13 @@ class Netlist: # contain net list and network controller
|
|||||||
conn = self.addr_to_conn[addr]
|
conn = self.addr_to_conn[addr]
|
||||||
print('Connection %s has connected' % str(addr))
|
print('Connection %s has connected' % str(addr))
|
||||||
while True:
|
while True:
|
||||||
data = conn.recv(4096)
|
data = conn.recv(RECV_BUFF)
|
||||||
if not data:
|
if not data:
|
||||||
print('disconnected with %s' % str(addr))
|
print('disconnected with %s' % str(addr))
|
||||||
conn.close()
|
conn.close()
|
||||||
return
|
return
|
||||||
data = data.decode()
|
data = data.decode()
|
||||||
print(data)
|
print(data) # here needs to be add more functions
|
||||||
|
|
||||||
def check_queue(self):
|
def check_queue(self):
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Reference in New Issue
Block a user