Skip to content

Commit

Permalink
Merge pull request #2151 from devos50/twistd_add_optios
Browse files Browse the repository at this point in the history
Improved Tribler twistd plugin
  • Loading branch information
whirm committed May 26, 2016
2 parents 8702acf + 733f9cf commit dcccc89
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 73 deletions.
60 changes: 52 additions & 8 deletions Tribler/Core/APIImplementation/LaunchManyCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from Tribler.Core.exceptions import DuplicateDownloadException
from Tribler.Core.simpledefs import NTFY_DISPERSY, NTFY_STARTED, NTFY_TORRENTS, NTFY_UPDATE
from Tribler.Main.globals import DefaultDownloadStartupConfig
from Tribler.community.tunnel.tunnel_community import TunnelSettings
from Tribler.dispersy.taskmanager import TaskManager
from Tribler.dispersy.util import blockingCallFromThread, blocking_call_on_reactor_thread

Expand Down Expand Up @@ -95,7 +96,7 @@ def __init__(self):
self.torrent_checker = None
self.tunnel_community = None

def register(self, session, sesslock, autoload_discovery=True):
def register(self, session, sesslock):
if not self.registered:
self.registered = True

Expand Down Expand Up @@ -148,7 +149,6 @@ def register(self, session, sesslock, autoload_discovery=True):
self.videoplayer = VideoPlayer(self.session)

# Dispersy
self.session.dispersy_member = None
self.tftp_handler = None
if self.session.get_dispersy():
from Tribler.dispersy.dispersy import Dispersy
Expand All @@ -170,16 +170,16 @@ def register(self, session, sesslock, autoload_discovery=True):
self.search_manager.initialize()

if not self.initComplete:
self.init(autoload_discovery)
self.init()

def init(self, autoload_discovery):
def init(self):
if self.dispersy:
from Tribler.dispersy.community import HardKilledCommunity

self._logger.info("lmc: Starting Dispersy...")

now = timemod.time()
success = self.dispersy.start(autoload_discovery)
success = self.dispersy.start(self.session.autoload_discovery)

diff = timemod.time() - now
if success:
Expand All @@ -205,18 +205,62 @@ def init(self, autoload_discovery):

@blocking_call_on_reactor_thread
def load_communities():
# load communities
self._logger.info("tribler: Preparing communities...")
now_time = timemod.time()
default_kwargs = {'tribler_session': self.session}

# Search Community
if self.session.get_enable_torrent_search():
from Tribler.community.search.community import SearchCommunity
self.dispersy.define_auto_load(SearchCommunity, self.session.dispersy_member, load=True,
kargs={'tribler_session': self.session})
kargs=default_kwargs)

# AllChannel Community
if self.session.get_enable_channel_search():
from Tribler.community.allchannel.community import AllChannelCommunity
self.dispersy.define_auto_load(AllChannelCommunity, self.session.dispersy_member, load=True,
kargs={'tribler_session': self.session})
kargs=default_kwargs)

# Bartercast Community
if self.session.get_barter_community_enabled():
from Tribler.community.bartercast4.community import BarterCommunity
self.dispersy.define_auto_load(BarterCommunity, self.session.dispersy_member, load=True)

# Channel Community
if self.session.get_channel_community_enabled():
from Tribler.community.channel.community import ChannelCommunity
self.dispersy.define_auto_load(ChannelCommunity,
self.session.dispersy_member, load=True, kargs=default_kwargs)

# PreviewChannel Community
if self.session.get_preview_channel_community_enabled():
from Tribler.community.channel.preview import PreviewChannelCommunity
self.dispersy.define_auto_load(PreviewChannelCommunity,
self.session.dispersy_member, kargs=default_kwargs)

# Define settings for the Tunnel and Multichain community
keypair = self.dispersy.crypto.generate_key(u"curve25519")
dispersy_member = self.dispersy.get_member(private_key=self.dispersy.crypto.key_to_bin(keypair),)

if self.session.get_enable_multichain():
from Tribler.community.multichain.community import MultiChainCommunity
# Start the multichain community and hook in the multichain scheduler.
self.dispersy.define_auto_load(MultiChainCommunity, dispersy_member, load=True)

if self.session.get_tunnel_community_enabled():
from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity
# The multichain community MUST be auto_loaded before the tunnel community,
# because it must be unloaded after the tunnel, so that the tunnel closures can be signed
tunnel_settings = TunnelSettings(self.session.get_install_dir(), tribler_session=self.session)
tunnel_kwargs = {'tribler_session': self.session, 'settings': tunnel_settings}
self.tunnel_community = self.dispersy.define_auto_load(
HiddenTunnelCommunity, dispersy_member, load=True, kargs=tunnel_kwargs)[0]

self.session.set_anon_proxy_settings(2, ("127.0.0.1",
self.session.get_tunnel_community_socks5_listen_ports()))

self._logger.info("tribler: communities are ready in %.2f seconds", timemod.time() - now_time)

load_communities()

if self.session.get_enable_channel_search():
Expand Down
2 changes: 2 additions & 0 deletions Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class LibtorrentDownloadImpl(DownloadConfigInterface):
""" Download subclass that represents a libtorrent download."""

def __init__(self, session, tdef):
super(LibtorrentDownloadImpl, self).__init__()

self._logger = logging.getLogger(self.__class__.__name__)

self.dllock = NoDispersyRLock()
Expand Down
3 changes: 2 additions & 1 deletion Tribler/Core/Session.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def set_and_create_dir(dirname, setter, default_dir):
self.save_pstate_sessconfig()

self.sqlite_db = None
self.dispersy_member = None

self.autoload_discovery = autoload_discovery

Expand Down Expand Up @@ -433,7 +434,7 @@ def start(self):
""" Create the LaunchManyCore instance and start it"""

# Create engine with network thread
self.lm.register(self, self.sesslock, autoload_discovery=self.autoload_discovery)
self.lm.register(self, self.sesslock)

self.sessconfig.set_callback(self.lm.sessconfig_changed_callback)

Expand Down
62 changes: 62 additions & 0 deletions Tribler/Core/SessionConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def set_listen_port_runtime(self, port):
"""
self.selected_ports['~'.join(('general', 'minport'))] = port

#
# Tunnel Community settings
#

def set_tunnel_community_socks5_listen_ports(self, ports):
self.sessconfig.set(u'tunnel_community', u'socks5_listen_ports', ports)

Expand All @@ -214,6 +218,20 @@ def get_tunnel_community_exitnode_enabled(self):
@return Boolean. """
return self.sessconfig.get(u'tunnel_community', u'exitnode_enabled')

def set_tunnel_community_enabled(self, value):
"""
Enable or disable the tunnel community.
:param value: A boolean indicating whether the tunnel community should be enabled
"""
self.sessconfig.set(u'tunnel_community', u'enabled', value)

def get_tunnel_community_enabled(self):
"""
Returns whether the tunnel community is enabled.
:return: A boolean indicating whether the tunnel community is enabled
"""
return self.sessconfig.get(u'tunnel_community', u'enabled')

#
# BarterCommunity settings
#
Expand Down Expand Up @@ -618,6 +636,10 @@ def set_preferred_playback_mode(self, mode):
"""
self.sessconfig.set(u'video', u'preferredmode', mode)

#
# Search Community
#

def get_enable_torrent_search(self):
""" Gets if to enable torrent search (SearchCommunity).
:return: True or False.
Expand All @@ -630,6 +652,10 @@ def set_enable_torrent_search(self, mode):
"""
self.sessconfig.set(u'search_community', u'enabled', mode)

#
# AllChannel Community
#

def get_enable_channel_search(self):
""" Gets if to enable torrent search (AllChannelCommunity).
:return: True or False.
Expand All @@ -642,6 +668,42 @@ def set_enable_channel_search(self, mode):
"""
self.sessconfig.set(u'allchannel_community', u'enabled', mode)

#
# Channel Community
#

def set_channel_community_enabled(self, value):
"""
Enable or disable the channel community.
:param value: A boolean indicating whether the channel community should be enabled
"""
self.sessconfig.set(u'channel_community', u'enabled', value)

def get_channel_community_enabled(self):
"""
Returns whether the channel community is enabled.
:return: A boolean indicating whether the channel community is enabled
"""
return self.sessconfig.get(u'channel_community', u'enabled')

#
# PreviewChannel Community
#

def set_preview_channel_community_enabled(self, value):
"""
Enable or disable the preview channel community.
:param value: A boolean indicating whether the preview channel community should be enabled
"""
self.sessconfig.set(u'preview_channel_community', u'enabled', value)

def get_preview_channel_community_enabled(self):
"""
Returns whether the preview channel community is enabled.
:return: A boolean indicating whether the preview channel community is enabled
"""
return self.sessconfig.get(u'preview_channel_community', u'enabled')

def get_enable_metadata(self):
"""
Gets if to enable metadata.
Expand Down
12 changes: 11 additions & 1 deletion Tribler/Core/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
# Version 11: Added a default whether we should upgrade or not.
# Version 12: Added watch folder options.
# Version 13: Added HTTP API options.
# Version 14: Added option to enable/disable channel, previewchannel and tunnel community.

SESSDEFAULTS_VERSION = 13
SESSDEFAULTS_VERSION = 14
sessdefaults = OrderedDict()

# General Tribler settings
Expand Down Expand Up @@ -69,6 +70,14 @@
sessdefaults['allchannel_community'] = OrderedDict()
sessdefaults['allchannel_community']['enabled'] = True

# Channel community section
sessdefaults['channel_community'] = OrderedDict()
sessdefaults['channel_community']['enabled'] = True

# PreviewChannel community section
sessdefaults['preview_channel_community'] = OrderedDict()
sessdefaults['preview_channel_community']['enabled'] = True

# Search community section
sessdefaults['search_community'] = OrderedDict()
sessdefaults['search_community']['enabled'] = True
Expand All @@ -77,6 +86,7 @@
sessdefaults['tunnel_community'] = OrderedDict()
sessdefaults['tunnel_community']['socks5_listen_ports'] = [-1] * 5
sessdefaults['tunnel_community']['exitnode_enabled'] = False
sessdefaults['tunnel_community']['enabled'] = True

# Multichain community section
sessdefaults['multichain'] = OrderedDict()
Expand Down
68 changes: 8 additions & 60 deletions Tribler/Main/tribler_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ def __init__(self, params, installdir, autoload_discovery=True,

session.notifier.notify(NTFY_STARTUP_TICK, NTFY_INSERT, None, 'Starting API')
wx.Yield()
s = self.startAPI(session)

self._logger.info('Tribler Version: %s Build: %s', version_id, commit_id)

Expand All @@ -151,8 +150,10 @@ def __init__(self, params, installdir, autoload_discovery=True,

session.notifier.notify(NTFY_STARTUP_TICK, NTFY_INSERT, None, 'Starting session and upgrading database (it may take a while)')
wx.Yield()
s.start()
self.dispersy = s.lm.dispersy

session.start()
self.dispersy = session.lm.dispersy
self.dispersy.attach_progress_handler(self.progressHandler)

session.notifier.notify(NTFY_STARTUP_TICK, NTFY_INSERT, None, 'Loading userdownloadchoice')
wx.Yield()
Expand Down Expand Up @@ -182,12 +183,12 @@ def __init__(self, params, installdir, autoload_discovery=True,
maxup = self.utility.read_config('maxuploadrate')
maxdown = self.utility.read_config('maxdownloadrate')
# set speed limits using LibtorrentMgr
s.set_max_upload_speed(maxup)
s.set_max_download_speed(maxdown)
session.set_max_upload_speed(maxup)
session.set_max_download_speed(maxdown)

# Only allow updates to come in after we defined ratelimiter
self.prevActiveDownloads = []
s.set_download_states_callback(self.sesscb_states_callback)
session.set_download_states_callback(self.sesscb_states_callback)

# Schedule task for checkpointing Session, to avoid hash checks after
# crashes.
Expand All @@ -214,7 +215,7 @@ def __init__(self, params, installdir, autoload_discovery=True,
# Arno, 2011-06-15: VLC 1.1.10 pops up separate win, don't have two.
self.frame.videoframe = None
if PLAYBACKMODE_INTERNAL in return_feasible_playback_modes():
vlcwrap = s.lm.videoplayer.get_vlcwrap()
vlcwrap = session.lm.videoplayer.get_vlcwrap()
wx.CallLater(3000, vlcwrap._init_vlc)
self.frame.videoframe = VideoDummyFrame(self.frame.videoparentpanel, self.utility, vlcwrap)

Expand Down Expand Up @@ -418,59 +419,6 @@ def PostInit2(self):

startWorker(None, self.loadSessionCheckpoint, delay=5.0, workerType="ThreadPool")

def startAPI(self, session):
@call_on_reactor_thread
def define_communities(*args):
assert isInIOThread()
from Tribler.community.channel.community import ChannelCommunity
from Tribler.community.channel.preview import PreviewChannelCommunity
from Tribler.community.tunnel.tunnel_community import TunnelSettings
from Tribler.community.tunnel.hidden_community import HiddenTunnelCommunity
from Tribler.community.multichain.community import MultiChainCommunity

# make sure this is only called once
session.remove_observer(define_communities)

dispersy = session.get_dispersy_instance()

self._logger.info("tribler: Preparing communities...")
now = time()

dispersy.attach_progress_handler(self.progressHandler)

default_kwargs = {'tribler_session': session}
# must be called on the Dispersy thread
if session.get_barter_community_enabled():
from Tribler.community.bartercast4.community import BarterCommunity
dispersy.define_auto_load(BarterCommunity, session.dispersy_member, load=True)

# load metadata community
dispersy.define_auto_load(ChannelCommunity, session.dispersy_member, load=True, kargs=default_kwargs)
dispersy.define_auto_load(PreviewChannelCommunity, session.dispersy_member, kargs=default_kwargs)

keypair = dispersy.crypto.generate_key(u"curve25519")
dispersy_member = dispersy.get_member(private_key=dispersy.crypto.key_to_bin(keypair),)
settings = TunnelSettings(session.get_install_dir(), tribler_session=session)
tunnel_kwargs = {'tribler_session': session, 'settings': settings}

if self.sconfig.get_enable_multichain():
# Start the multichain community and hook in the multichain scheduler.
multichain = dispersy.define_auto_load(MultiChainCommunity, dispersy_member, load=True)[0]

# The multichain community MUST be auto_loaded before the tunnel community,
# because it must be unloaded after the tunnel, so that the tunnel closures can be signed
self.tunnel_community = dispersy.define_auto_load(HiddenTunnelCommunity, dispersy_member, load=True,
kargs=tunnel_kwargs)[0]

session.set_anon_proxy_settings(2, ("127.0.0.1", session.get_tunnel_community_socks5_listen_ports()))

diff = time() - now
self._logger.info("tribler: communities are ready in %.2f seconds", diff)

session.add_observer(define_communities, NTFY_DISPERSY, [NTFY_STARTED])

return session

@forceWxThread
def sesscb_ntfy_myprefupdates(self, subject, changeType, objectID, *args):
if self._frame_and_ready():
Expand Down

0 comments on commit dcccc89

Please sign in to comment.