net bug fix and update function
This commit is contained in:
@@ -36,7 +36,7 @@ def send_queue_function():
|
|||||||
object_app, new_dp = process_reforware(new_dp)
|
object_app, new_dp = process_reforware(new_dp)
|
||||||
receive_queues[add_plugins_string(object_app)].put(new_dp)
|
receive_queues[add_plugins_string(object_app)].put(new_dp)
|
||||||
else:
|
else:
|
||||||
object_app, dp =process_reforware(dp)
|
object_app, dp = process_reforware(dp)
|
||||||
receive_queues[add_plugins_string(object_app)].put(dp)
|
receive_queues[add_plugins_string(object_app)].put(dp)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,9 @@ receive_queue = receive_queues[__name__]
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
file_flag = False;
|
file_flag = False
|
||||||
while True:
|
while True:
|
||||||
file_flag = False;
|
file_flag = False
|
||||||
raw_data = input()
|
raw_data = input()
|
||||||
|
|
||||||
if raw_data[:6] == '(file)':
|
if raw_data[:6] == '(file)':
|
||||||
@@ -44,7 +44,10 @@ def find_the_last(indata): # find the last ":" index
|
|||||||
break
|
break
|
||||||
last_index = copy.copy(first_index)
|
last_index = copy.copy(first_index)
|
||||||
last_index += 1
|
last_index += 1
|
||||||
while indata[last_index] == ' ':
|
try:
|
||||||
|
while indata[last_index] == ' ':
|
||||||
|
last_index += 1
|
||||||
|
except IndexError:
|
||||||
last_index += 1
|
last_index += 1
|
||||||
return first_index, last_index
|
return first_index, last_index
|
||||||
|
|
||||||
|
|||||||
@@ -94,6 +94,12 @@ class Netrecv:
|
|||||||
def check_send_queue(self):
|
def check_send_queue(self):
|
||||||
while True:
|
while True:
|
||||||
dp = receive_queue.get()
|
dp = receive_queue.get()
|
||||||
|
|
||||||
|
# debug code
|
||||||
|
if dp.body == b'stat':
|
||||||
|
print(self.stat)
|
||||||
|
continue
|
||||||
|
|
||||||
if dp.method == 'file':
|
if dp.method == 'file':
|
||||||
print('right')
|
print('right')
|
||||||
print(dp.head)
|
print(dp.head)
|
||||||
@@ -104,7 +110,7 @@ class Netrecv:
|
|||||||
file = open(dp.head['filename'], 'rb')
|
file = open(dp.head['filename'], 'rb')
|
||||||
for data in file:
|
for data in file:
|
||||||
conn.send(data)
|
conn.send(data)
|
||||||
print('sended')
|
print('sended')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('wrong')
|
print('wrong')
|
||||||
@@ -125,11 +131,20 @@ class Netrecv:
|
|||||||
conn.close()
|
conn.close()
|
||||||
for id in self.stat:
|
for id in self.stat:
|
||||||
if (conn, addr) in self.stat[id]:
|
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))
|
self.connection_list.remove((conn, addr))
|
||||||
print('Removed connection', str(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):
|
def process_connection(self, conn, addr):
|
||||||
|
self.say_hello(conn, addr)
|
||||||
|
|
||||||
print('Connection accept %s' % str(addr))
|
print('Connection accept %s' % str(addr))
|
||||||
data = b''
|
data = b''
|
||||||
while True:
|
while True:
|
||||||
@@ -164,6 +179,20 @@ class Netrecv:
|
|||||||
break
|
break
|
||||||
# try unpack #
|
# 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':
|
if dp.method == 'file':
|
||||||
length = int(dp.head['length'])
|
length = int(dp.head['length'])
|
||||||
data_length = len(data)
|
data_length = len(data)
|
||||||
@@ -262,21 +291,9 @@ class Netrecv:
|
|||||||
dp.body = data[:length]
|
dp.body = data[:length]
|
||||||
data = data[length:]
|
data = data[length:]
|
||||||
|
|
||||||
# net config data package
|
dp.encode()
|
||||||
if dp.app == 'net':
|
send_queue.put(dp)
|
||||||
dp_id = dp.head['id']
|
print('###############\n' + dp.encode_data.decode() + '\n###############')
|
||||||
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###############')
|
|
||||||
|
|
||||||
|
|
||||||
thread = threading.Thread(target=main, args=())
|
thread = threading.Thread(target=main, args=())
|
||||||
|
|||||||
@@ -6,15 +6,42 @@ from forwarder import receive_queues, send_queue
|
|||||||
receive_queue = receive_queues[__name__]
|
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__']
|
remove_dir_list = ['.git', '.idea', '__pycache__']
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
while True:
|
while True:
|
||||||
dp = receive_queue.get()
|
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 = []
|
self.filelist = []
|
||||||
|
|
||||||
def compress_files(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:
|
for name in filelist:
|
||||||
f.add(name)
|
f.add(name)
|
||||||
|
|
||||||
|
|||||||
11
test_tool.py
11
test_tool.py
@@ -7,7 +7,7 @@ s.bind(('127.0.0.1', 3966))
|
|||||||
s.listen(100)
|
s.listen(100)
|
||||||
|
|
||||||
id = '''post net msw/1.0
|
id = '''post net msw/1.0
|
||||||
id: miku
|
id: miku2
|
||||||
from: test
|
from: test
|
||||||
length: 0
|
length: 0
|
||||||
|
|
||||||
@@ -23,8 +23,13 @@ def process(conn, addr):
|
|||||||
if not data:
|
if not data:
|
||||||
conn.close()
|
conn.close()
|
||||||
return
|
return
|
||||||
data = data.decode()
|
try:
|
||||||
print(data)
|
data = data.decode()
|
||||||
|
print(data)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
print('Decode error')
|
||||||
|
print(data[:39])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Reference in New Issue
Block a user