Skip to content

Commit

Permalink
Starting to convert session to its own component. Removed ref to `sel…
Browse files Browse the repository at this point in the history
…f.storage` from Daemon.py
  • Loading branch information
hackrush01 committed Apr 29, 2018
1 parent 3c8442f commit fa0c9cb
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 43 deletions.
83 changes: 81 additions & 2 deletions lbrynet/daemon/Components.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from lbrynet.database.storage import SQLiteStorage
from lbrynet.core.Wallet import LBRYumWallet
from lbrynet.daemon.Component import Component
# from lbrynet.daemon import ComponentManager
from lbrynet.core.Session import Session

log = logging.getLogger(__name__)

# settings must be initialized before this file is imported

DATABASE_COMPONENT = "database"
WALLET_COMPONENT = "wallet"
SESSION_COMPONENT = "session"


class DatabaseComponent(Component):
Expand Down Expand Up @@ -94,7 +95,7 @@ def stop(cls):

class WalletComponent(Component):
component_name = WALLET_COMPONENT
depends_on = ['database']
depends_on = [DATABASE_COMPONENT]
wallet = None

@staticmethod
Expand Down Expand Up @@ -133,3 +134,81 @@ def setup(cls):
@defer.inlineCallbacks
def stop(cls):
yield cls.wallet.stop()


class SessionComponent(Component):
component_name = SESSION_COMPONENT
depends_on = [DATABASE_COMPONENT, WALLET_COMPONENT]
session = None

@staticmethod
def get_db_dir():
return conf.settings['data_dir']

@staticmethod
def get_node_id():
return conf.settings.node_id

@staticmethod
def get_blobfile_dir():
if conf.settings['BLOBFILES_DIR'] == "blobfiles":
return os.path.join(SessionComponent.get_db_dir(), "blobfiles")
else:
log.info("Using non-default blobfiles directory: %s", conf.settings['BLOBFILES_DIR'])
return conf.settings['BLOBFILES_DIR']

@staticmethod
def get_dht_node_port():
return conf.settings['dht_node_port']

@staticmethod
def get_known_dht_nodes():
return conf.settings['known_dht_nodes']

@staticmethod
def get_peer_port():
return conf.settings['peer_port']

@staticmethod
def use_upnp():
return conf.settings['use_upnp']

@staticmethod
def is_generous_host():
return conf.settings['is_generous_host']

@staticmethod
def get_external_ip():
from lbrynet.core.system_info import get_platform
platform = get_platform(get_ip=True)
return platform['ip']

@classmethod
@defer.inlineCallbacks
def setup(cls):
wallet = WalletComponent.wallet
storage = DatabaseComponent.storage

log.info("in session setup")

cls.session = Session(
conf.settings['data_rate'],
db_dir=cls.get_db_dir(),
node_id=cls.get_node_id(),
blob_dir=cls.get_blobfile_dir(),
dht_node_port=cls.get_dht_node_port(),
known_dht_nodes=cls.get_known_dht_nodes(),
peer_port=cls.get_peer_port(),
use_upnp=cls.use_upnp(),
wallet=wallet,
is_generous=cls.is_generous_host(),
external_ip=cls.get_external_ip(),
storage=storage
)

yield cls.session.setup()

@classmethod
@defer.inlineCallbacks
def stop(cls):
yield cls.session.shut_down()
80 changes: 39 additions & 41 deletions lbrynet/daemon/Daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import signal
from copy import deepcopy
from twisted.web import server
from twisted.internet import defer, threads, error, reactor
from twisted.internet import defer, error, reactor
from twisted.internet.task import LoopingCall
from twisted.python.failure import Failure

Expand All @@ -23,9 +23,7 @@

# TODO: importing this when internet is disabled raises a socket.gaierror
from lbrynet.core.system_info import get_lbrynet_version
from lbrynet.database.storage import SQLiteStorage
from lbrynet import conf
from lbrynet.conf import LBRYCRD_WALLET, LBRYUM_WALLET
from lbrynet.reflector import reupload
from lbrynet.reflector import ServerFactory as reflector_server_factory
from lbrynet.core.log_support import configure_loggly_handler
Expand All @@ -40,8 +38,6 @@
from lbrynet.core import utils, system_info
from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier, download_sd_blob
from lbrynet.core.StreamDescriptor import EncryptedFileStreamType
from lbrynet.core.Session import Session
from lbrynet.core.Wallet import LBRYumWallet
from lbrynet.core.looping_call_manager import LoopingCallManager
from lbrynet.core.server.BlobRequestHandler import BlobRequestHandlerFactory
from lbrynet.core.server.ServerProtocol import ServerProtocolFactory
Expand Down Expand Up @@ -174,13 +170,13 @@ class Daemon(AuthJSONRPCServer):

def __init__(self, analytics_manager):
AuthJSONRPCServer.__init__(self, conf.settings['use_auth_http'])
self.db_dir = conf.settings['data_dir']
# self.db_dir = conf.settings['data_dir']
self.download_directory = conf.settings['download_directory']
if conf.settings['BLOBFILES_DIR'] == "blobfiles":
self.blobfile_dir = os.path.join(self.db_dir, "blobfiles")
else:
log.info("Using non-default blobfiles directory: %s", conf.settings['BLOBFILES_DIR'])
self.blobfile_dir = conf.settings['BLOBFILES_DIR']
# if conf.settings['BLOBFILES_DIR'] == "blobfiles":
# self.blobfile_dir = os.path.join(self.db_dir, "blobfiles")
# else:
# log.info("Using non-default blobfiles directory: %s", conf.settings['BLOBFILES_DIR'])
# self.blobfile_dir = conf.settings['BLOBFILES_DIR']
self.data_rate = conf.settings['data_rate']
self.max_key_fee = conf.settings['max_key_fee']
self.disable_max_key_fee = conf.settings['disable_max_key_fee']
Expand All @@ -190,8 +186,8 @@ def __init__(self, analytics_manager):
self.delete_blobs_on_remove = conf.settings['delete_blobs_on_remove']
self.peer_port = conf.settings['peer_port']
self.reflector_port = conf.settings['reflector_port']
self.dht_node_port = conf.settings['dht_node_port']
self.use_upnp = conf.settings['use_upnp']
# self.dht_node_port = conf.settings['dht_node_port']
# self.use_upnp = conf.settings['use_upnp']
self.auto_renew_claim_height_delta = conf.settings['auto_renew_claim_height_delta']

self.startup_status = STARTUP_STAGES[0]
Expand Down Expand Up @@ -222,7 +218,7 @@ def __init__(self, analytics_manager):
self.looping_call_manager = LoopingCallManager(calls)
self.sd_identifier = StreamDescriptorIdentifier()
self.lbry_file_manager = None
self.storage = None
# self.storage = None
self.wallet = None

@defer.inlineCallbacks
Expand All @@ -238,9 +234,10 @@ def setup(self):

yield self._initial_setup()
yield self.component_manager.setup()
self.storage = self.component_manager.get_component("database").storage
# self.storage = self.component_manager.get_component("database").storage
self.wallet = self.component_manager.get_component("wallet").wallet
yield self._get_session()
self.startup_status = STARTUP_STAGES[2]
self.session = self.component_manager.get_component("session").session
yield self._check_wallet_locked()
yield self._start_analytics()
yield add_lbry_file_to_sd_identifier(self.sd_identifier)
Expand Down Expand Up @@ -457,7 +454,7 @@ def _update_settings(self, settings):
else:
converted = setting_type(settings[key])
conf.settings.update({key: converted},
data_types=(conf.TYPE_RUNTIME, conf.TYPE_PERSISTED))
data_types=(conf.TYPE_RUNTIME, conf.TYPE_PERSISTED))
conf.settings.save_conf_file_settings()

self.data_rate = conf.settings['data_rate']
Expand All @@ -480,25 +477,25 @@ def _start_analytics(self):
if not self.analytics_manager.is_started:
self.analytics_manager.start()

@defer.inlineCallbacks
def _get_session(self):
self.session = Session(
conf.settings['data_rate'],
db_dir=self.db_dir,
node_id=self.node_id,
blob_dir=self.blobfile_dir,
dht_node_port=self.dht_node_port,
known_dht_nodes=conf.settings['known_dht_nodes'],
peer_port=self.peer_port,
use_upnp=self.use_upnp,
wallet=self.wallet,
is_generous=conf.settings['is_generous_host'],
external_ip=self.platform['ip'],
storage=self.storage
)
self.startup_status = STARTUP_STAGES[2]

yield self.session.setup()
# @defer.inlineCallbacks
# def _get_session(self):
# self.session = Session(
# conf.settings['data_rate'],
# db_dir=self.db_dir,
# node_id=self.node_id,
# blob_dir=self.blobfile_dir,
# dht_node_port=self.dht_node_port,
# known_dht_nodes=conf.settings['known_dht_nodes'],
# peer_port=self.peer_port,
# use_upnp=self.use_upnp,
# wallet=self.wallet,
# is_generous=conf.settings['is_generous_host'],
# external_ip=self.platform['ip'],
# storage=self.storage
# )
# self.startup_status = STARTUP_STAGES[2]
#
# yield self.session.setup()

@defer.inlineCallbacks
def _check_wallet_locked(self):
Expand Down Expand Up @@ -635,7 +632,8 @@ def _publish_stream(self, name, bid, claim_dict, file_path=None, certificate_id=
certificate_id)
parse_lbry_uri(name)
if not file_path:
stream_hash = yield self.session.storage.get_stream_hash_for_sd_hash(claim_dict['stream']['source']['source'])
stream_hash = yield self.session.storage.get_stream_hash_for_sd_hash(
claim_dict['stream']['source']['source'])
claim_out = yield publisher.publish_stream(name, bid, claim_dict, stream_hash, claim_address,
change_address)
else:
Expand Down Expand Up @@ -1760,11 +1758,11 @@ def jsonrpc_channel_new(self, channel_name, amount):
if balance <= MAX_UPDATE_FEE_ESTIMATE:
raise InsufficientFundsError(
"Insufficient funds, please deposit additional LBC. Minimum additional LBC needed {}"
. format(MAX_UPDATE_FEE_ESTIMATE - balance))
.format(MAX_UPDATE_FEE_ESTIMATE - balance))
elif amount > max_bid_amount:
raise InsufficientFundsError(
"Please lower the bid value, the maximum amount you can specify for this channel is {}"
.format(max_bid_amount))
.format(max_bid_amount))

result = yield self.session.wallet.claim_new_channel(channel_name, amount)
self.analytics_manager.send_new_channel()
Expand Down Expand Up @@ -1941,11 +1939,11 @@ def jsonrpc_publish(self, name, bid, metadata=None, file_path=None, fee=None, ti
if balance <= MAX_UPDATE_FEE_ESTIMATE:
raise InsufficientFundsError(
"Insufficient funds, please deposit additional LBC. Minimum additional LBC needed {}"
.format(MAX_UPDATE_FEE_ESTIMATE - balance))
.format(MAX_UPDATE_FEE_ESTIMATE - balance))
elif bid > max_bid_amount:
raise InsufficientFundsError(
"Please lower the bid value, the maximum amount you can specify for this claim is {}."
.format(max_bid_amount))
.format(max_bid_amount))

metadata = metadata or {}
if fee is not None:
Expand Down

0 comments on commit fa0c9cb

Please sign in to comment.