process error, now can restart

This commit is contained in:
2020-03-28 16:11:50 +08:00
parent 014723a029
commit 6e2953e4e0
12 changed files with 75 additions and 34 deletions

15
bug.py
View File

@@ -1,15 +0,0 @@
class A:
def __init__(self, arg=None):
if arg is None:
self.arg = {}
def set(self):
self.arg['something'] = 'something'
a1 = A()
a1.set()
a2 = A()
print(a2.arg)
# output is {'something': 'something'}

View File

@@ -1,6 +1,7 @@
import threading import threading
import json import json
import time import time
import queue
class Jsondata: class Jsondata:
@@ -11,7 +12,7 @@ class Jsondata:
self.raw_jsondata = jsondata self.raw_jsondata = jsondata
self.auto_save = auto_save self.auto_save = auto_save
self.auto_save_time = auto_save_time self.auto_save_time = auto_save_time
self.thread = threading.Thread(target=self.run, args=()) self.thread = threading.Thread(target=self.run, args=(), daemon=True)
self.thread.start() self.thread.start()
def try_to_read_jsondata(self, key, or_value, template=0, output=True): def try_to_read_jsondata(self, key, or_value, template=0, output=True):
@@ -37,4 +38,5 @@ class Jsondata:
pass pass
global_config = {} global_config = {}
msw_queue = queue.Queue()
jsondata = Jsondata() jsondata = Jsondata()

View File

@@ -51,5 +51,5 @@ def process_reforware(dp):
return dp.app, dp return dp.app, dp
thread = threading.Thread(target=send_queue_function, args=()) thread = threading.Thread(target=send_queue_function, args=(), daemon=True)
thread.start() thread.start()

View File

@@ -3,3 +3,9 @@ plugins.input: asdfasf
plugins.input: alskdfaj plugins.input: alskdfaj
plugins.input: alsdkfj plugins.input: alsdkfj
plugins.input: testtest plugins.input: testtest
plugins.input: hello
plugins.input: asldjfalsdjf
plugins.input: lajsdflajsdlkf
plugins.input: alsdjflasjdlkfjasldfjalsdfj
network:
network:

10
main.py Normal file
View File

@@ -0,0 +1,10 @@
import os
import os.path
import sys
python = sys.executable
while True:
code = os.system('python msw.py')
if code:
break

7
msw.py
View File

@@ -4,9 +4,9 @@ PATH = os.path.dirname(os.path.abspath(__file__))
sys.path.append(PATH) sys.path.append(PATH)
os.chdir(PATH) os.chdir(PATH)
from mswp import Datapack
import threading import threading
from config import jsondata, global_config from mswp import Datapack
from config import jsondata, global_config, msw_queue
print('Building plugins import script...') print('Building plugins import script...')
@@ -36,3 +36,6 @@ import plugins
print('Plugins import finished') print('Plugins import finished')
# restart
code = msw_queue.get()
sys.exit(code)

10
mswp.py
View File

@@ -1,6 +1,9 @@
import os import os
import random
import hashlib
from config import jsondata from config import jsondata
''' '''
A datapack must like: A datapack must like:
--------------------- ---------------------
@@ -22,7 +25,7 @@ BUFFSIZE = jsondata.try_to_read_jsondata('buffsize', 4096)
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'',
check_head=True, file=None): check_head=True, file=None, gen_flag=True):
self.id = jsondata.try_to_read_jsondata('id', 'unknown_id') self.id = jsondata.try_to_read_jsondata('id', 'unknown_id')
if head is None: if head is None:
head = {} head = {}
@@ -41,6 +44,11 @@ class Datapack:
self.head = head self.head = head
self.body = body self.body = body
self.encode_data = b'' self.encode_data = b''
if gen_flag:
randseed = str(random.random()).encode()
h = hashlib.blake2b(digest_size = 4)
h.update(randseed)
self.head['flag'] = h.hexdigest()
def encode(self): def encode(self):
if self.method == 'file': if self.method == 'file':

View File

@@ -3,16 +3,32 @@ import copy
import os import os
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 msw_queue
receive_queue = receive_queues[__name__] receive_queue = receive_queues[__name__]
def main(): def main():
while True:
try:
_main()
except Exception as e:
print('Error in %s, %s: %s' % (__name__, type(e), str(e)))
def _main():
file_flag = False file_flag = False
while True: while True:
file_flag = False file_flag = False
net_flag = False net_flag = False
raw_data = input() raw_data = input()
if raw_data == 'restart':
msw_queue.put(0)
break
if raw_data == 'exit':
msw_queue.put(1)
break
if raw_data[:6] == '(file)': # like "(file)log: filename.exe" if raw_data[:6] == '(file)': # like "(file)log: filename.exe"
raw_data = raw_data[6:] raw_data = raw_data[6:]
file_flag = True file_flag = True
@@ -61,5 +77,5 @@ def find_the_last(indata): # find the last ":" index
return first_index, last_index return first_index, last_index
thread = threading.Thread(target=main, args=()) thread = threading.Thread(target=main, args=(), daemon=True)
thread.start() thread.start()

View File

@@ -14,6 +14,6 @@ def main():
from_app_name = 'Unknown' from_app_name = 'Unknown'
f.write(from_app_name + ': ' + dp.body.decode() + '\n') f.write(from_app_name + ': ' + dp.body.decode() + '\n')
thread = threading.Thread(target=main, args=()) thread = threading.Thread(target=main, args=(), daemon=True)
thread.start() thread.start()

View File

@@ -26,13 +26,13 @@ class Network_controller: # manage id and connection
self.all_connection_list = [] self.all_connection_list = []
self.wheel_queue = queue.Queue() self.wheel_queue = queue.Queue()
self.start_wheel_thread = threading.Thread(target=self.start_wheel, args=()) self.start_wheel_thread = threading.Thread(target=self.start_wheel, args=(), daemon=True)
self.start_wheel_thread.start() self.start_wheel_thread.start()
self.start_accpet_connection_thread = threading.Thread(target=self.start_accpet_connection, args=()) self.start_accpet_connection_thread = threading.Thread(target=self.start_accpet_connection, args=(), daemon=True)
self.start_accpet_connection_thread.start() self.start_accpet_connection_thread.start()
self.start_sending_dp_thread = threading.Thread(target=self.start_sending_dp, args=()) self.start_sending_dp_thread = threading.Thread(target=self.start_sending_dp, args=(), daemon=True)
self.start_sending_dp_thread.start() self.start_sending_dp_thread.start()
@@ -127,21 +127,21 @@ class Connection:
self.buff = b'' self.buff = b''
self.padding_queue = queue.Queue() self.padding_queue = queue.Queue()
self.thread_recv = threading.Thread(target=self._init, args=()) self.thread_recv = threading.Thread(target=self._init, args=(), daemon=True)
self.thread_recv.start() self.thread_recv.start()
self.thread_send = None self.thread_send = None
def _init(self): # init to check connection id, threading def _init(self): # init to check connection id, threading
err_code = self.check_id() err_code, flag = self.check_id()
if err_code: if err_code:
print('Init connection failed, connection closed') print('<%s> Init connection failed, connection closed, code: %s' % (flag, err_code))
self.conn.close() self.conn.close()
self.netowrk_controller.set_connection(self.id, self) self.netowrk_controller.set_connection(self.id, self)
self.thread_send = threading.Thread(target=self.send_func, args=()) self.thread_send = threading.Thread(target=self.send_func, args=(), daemon=True)
self.thread_send.start() self.thread_send.start()
self.receive() self.receive()
@@ -213,19 +213,29 @@ class Connection:
length: 0 length: 0
------------------------------- -------------------------------
error code list:
1: not get "id" in head
2: receive data failed
3: appname is not handshake
''' '''
data = self.conn.recv(BUFFSIZE) data = self.conn.recv(BUFFSIZE)
if not data: if not data:
return 2 return 2, ''
self.buff += data self.buff += data
dp = Datapack() dp = Datapack()
dp.encode_data = self.buff # maybe here needs to use copy.copy(self.buff) dp.encode_data = self.buff # maybe here needs to use copy.copy(self.buff)
self.buff = dp.decode(only_head=True) self.buff = dp.decode(only_head=True)
if not dp.head.get('id'): if not dp.head.get('id'):
return 1 return 1, dp.head.get('flag')
if not dp.app == 'handshake':
return 3, dp.head.get('flag')
self.id = dp.head['id'] self.id = dp.head['id']
return 0, dp.head.get('flag')
def sendall(self, dp): def sendall(self, dp):
self.padding_queue.put(dp) self.padding_queue.put(dp)
@@ -246,5 +256,5 @@ class Connection:
self.conn.sendall(dp.encode_data) self.conn.sendall(dp.encode_data)
thread = threading.Thread(target=main, args=()) thread = threading.Thread(target=main, args=(), daemon=True)
thread.start() thread.start()

View File

@@ -70,5 +70,5 @@ class Compresser:
return filelist return filelist
thread = threading.Thread(target=main, args=()) thread = threading.Thread(target=main, args=(), daemon=True)
thread.start() thread.start()

View File

@@ -6,6 +6,7 @@ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
id = b'''post handshake msw/1.0 id = b'''post handshake msw/1.0
id: miku2 id: miku2
length: 0 length: 0
flag: ee20aeff
''' '''