Skip to content

Commit

Permalink
Merge pull request #64 from quangvu3/master
Browse files Browse the repository at this point in the history
Upgrade to v0.2.0 (Final)
  • Loading branch information
sumoprojects authored Apr 20, 2019
2 parents c446a3a + 3910112 commit 887c536
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 54 deletions.
16 changes: 9 additions & 7 deletions app/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
QInputDialog, QLineEdit
from PySide.QtCore import QObject, Slot, Signal

from utils.common import print_money, print_money2, readFile
from utils.common import print_money, print_money2

from settings import APP_NAME, VERSION, DATA_DIR, COIN, makeDir, seed_languages
from utils.logger import log, LEVEL_ERROR, LEVEL_INFO
Expand Down Expand Up @@ -586,7 +586,6 @@ def new_wallet_ui(self):
QMessageBox.warning(self.ui, "Incorrect Wallet Password", "Wallet password is not correct!")
return


ret = self.ui.wallet_rpc_manager.rpc_request.close_wallet()
if ret['status'] == "ERROR":
error_message = ret['message']
Expand Down Expand Up @@ -632,7 +631,14 @@ def open_existing_wallet(self):
if os.path.normpath(os.path.dirname(wallet_filename)) != os.path.normpath(wallet_dir_path):
QMessageBox.warning(self.ui, \
'Open Wallet File',\
"""Error: Only wallet files at default location are available for opening""")
"""Error: Only wallet files at default location are available for opening.<br>
You can import wallet via 'New... > Import' feature instead.""")
return

if os.path.basename(wallet_filename) == os.path.basename(self.ui.wallet_info.wallet_filepath):
QMessageBox.warning(self.ui, \
'Open Wallet File',\
"""Error: Cannot open the same wallet!""")
return

while True:
Expand Down Expand Up @@ -682,10 +688,6 @@ def open_existing_wallet(self):

while not self.ui.wallet_rpc_manager.is_ready():
self.app_process_events(0.1)
# self.app_process_events(10)
# self.on_open_existing_wallet_complete_event.emit()
# QMessageBox.information(self.ui, "Wallet Loaded", "Wallet ["
# + os.path.basename(wallet_filename) + "] successfully loaded")
except Exception, err:
log(str(err), LEVEL_ERROR)
ret = self.ui.wallet_rpc_manager.rpc_request.open_wallet(
Expand Down
71 changes: 24 additions & 47 deletions manager/ProcessManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,23 @@
from subprocess import Popen, PIPE, STDOUT
import signal
from threading import Thread
from multiprocessing import Event
from time import sleep
from uuid import uuid4

from utils.logger import log, LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO

from rpc import WalletRPCRequest

CREATE_NO_WINDOW = 0x08000000 if sys.platform == "win32" else 0 # disable creating the window
DETACHED_PROCESS = 0x00000008 # forcing the child to have no console at all
BELOW_NORMAL_PRIORITY_CLASS = 0x00004000

def signal_handler(signal, frame):
sleep(1)
print('Ctrl+C received')

signal.signal(signal.SIGINT, signal_handler)

class ProcessManager(Thread):
def __init__(self, proc_args, proc_name=""):
def __init__(self, bin_path, proc_args, proc_name=""):
super(ProcessManager, self).__init__()
args_array = proc_args.encode( sys.getfilesystemencoding() ).split(u' ')
args_array = proc_args.encode(sys.getfilesystemencoding()).split(u' ')
args_array.insert(0, bin_path.encode(sys.getfilesystemencoding()))
self.proc = Popen(args_array,
shell=True,
stdout=PIPE, stderr=STDOUT, stdin=PIPE)
self.proc_name = proc_name
self.daemon = False
self.daemon = True
log("[%s] started" % proc_name, LEVEL_INFO, self.proc_name)

def run(self):
Expand All @@ -57,18 +47,16 @@ def send_command(self, cmd):
def get_pid(self):
return self.proc.pid


def is_proc_running(self):
return (self.proc.poll() is None)



class SumokoindManager(ProcessManager):
def __init__(self, resources_path, log_level=0, block_sync_size=20, limit_rate_up=2048, limit_rate_down=8192):
proc_args = u'%s/bin/sumokoind --log-level %d --block-sync-size %d --limit-rate-up %d --limit-rate-down %d' % (resources_path, log_level, block_sync_size, limit_rate_up, limit_rate_down)
super(SumokoindManager, self).__init__(proc_args, "sumokoind")
self.synced = Event()
self.stopped = Event()
bin_path = u'%s/bin/sumokoind' % resources_path
proc_args = u'--log-level %d --block-sync-size %d --limit-rate-up %d --limit-rate-down %d' % (log_level, block_sync_size, limit_rate_up, limit_rate_down)
super(SumokoindManager, self).__init__(bin_path, proc_args, "sumokoind")

def run(self):
err_str = "ERROR"
Expand All @@ -82,8 +70,6 @@ def run(self):
if not self.proc.stdout.closed:
self.proc.stdout.close()

self.stopped.set()


def stop(self):
try:
Expand All @@ -110,37 +96,28 @@ def __init__(self, resources_path, wallet_dir_path, app, log_level=1):
self.rpc_user_name = str(uuid4().hex)[:12]
self.rpc_password = str(uuid4().hex)[:12]
wallet_log_dir_path = os.path.abspath(os.path.join(wallet_dir_path, ".." , "logs", "sumo-wallet-rpc-bin.log"))
wallet_rpc_args = u'%s/bin/sumo-wallet-rpc --wallet-dir %s --log-file %s --rpc-bind-port 19738 --rpc-login %s:%s --log-level %d' \
% (resources_path, wallet_dir_path, wallet_log_dir_path, self.rpc_user_name, self.rpc_password, log_level)

super(WalletRPCManager, self).__init__(wallet_rpc_args, "sumo-wallet-rpc")
bin_path = u'%s/bin/sumo-wallet-rpc' % resources_path
wallet_rpc_args = u'--wallet-dir %s --log-file %s --rpc-bind-port 19738 --rpc-login %s:%s --log-level %d' \
% (wallet_dir_path, wallet_log_dir_path, self.rpc_user_name, self.rpc_password, log_level)
super(WalletRPCManager, self).__init__(bin_path, wallet_rpc_args, "sumo-wallet-rpc")
self.rpc_request = WalletRPCRequest(app, self.rpc_user_name, self.rpc_password)

self._ready = False
self._block_height = 0
self._block_hash = None
# self.last_log_lines = []
self._stopped = False

def run(self):
rpc_ready_strs = ["Binding on 127.0.0.1", "Starting wallet rpc server", "Run net_service loop", "Refresh done"]
err_str = "ERROR"
height_regex = re.compile(r"Processed block: \<([a-z0-9]+)\>, height (\d+)")
# height_regex2 = re.compile(r"Skipped block by height: (\d+)")
# height_regex3 = re.compile(r"Skipped block by timestamp, height: (\d+)")

for line in iter(self.proc.stdout.readline, b''):
m_height = height_regex.search(line)
if m_height:
self._block_hash = m_height.group(1)
self._block_height = m_height.group(2)
self._ready = False
# if not m_height:
# m_height = height_regex2.search(line)
# if m_height: self._block_height = m_height.group(1)
# if not m_height:
# m_height = height_regex3.search(line)
# if m_height: self._block_height = m_height.group(1)

if not self._ready and any(s in line for s in rpc_ready_strs):
self._ready = True
Expand All @@ -156,8 +133,8 @@ def run(self):
if self._stopped:
break

if not self.proc.stdout.closed:
self.proc.stdout.close()
# if not self.proc.stdout.closed:
# self.proc.stdout.close()

def is_ready(self):
return self._ready
Expand All @@ -177,18 +154,19 @@ def reset_block_height(self):
def stop(self):
self._stopped = True
self.rpc_request.stop_wallet(no_wait=True)
sleep(2)
counter = 1
while True:
while self.is_proc_running():
try:
if counter < 30:
if counter % 10 == 0:
try:
os.kill(self.proc.pid, signal.CTRL_C_EVENT)
except KeyboardInterrupt:
pass
sleep(1)
if not self.is_proc_running():
break
if counter < 20:
if counter % 5 == 0:
if hasattr(signal, 'CTRL_C_EVENT'):
try:
os.kill(self.proc.pid, signal.CTRL_C_EVENT)
except KeyboardInterrupt:
pass
else:
os.kill(self.proc.pid, signal.SIGINT)
sleep(1)
counter += 1
else:
Expand All @@ -197,6 +175,5 @@ def stop(self):
break
except:
break
self._ready = Event()
log("[%s] stopped" % self.proc_name, LEVEL_INFO, self.proc_name)

0 comments on commit 887c536

Please sign in to comment.