1 # Written by Arno Bakker
\r
2 # see LICENSE.txt for license information
\r
4 # See www.tribler.org/trac/wiki/SuperpeerMode for considerations.
\r
15 from traceback import print_exc
\r
16 from threading import Condition
\r
18 from BaseLib.Core.API import *
\r
19 import BaseLib.Core.BitTornado.parseargs as parseargs
\r
20 from BaseLib.Core.Overlay.OverlayThreadingBridge import OverlayThreadingBridge
\r
21 import BaseLib.Core.BuddyCast.buddycast as BuddyCastMod
\r
22 BuddyCastMod.debug = True
\r
24 argsdef = [('nickname', '', 'name of the superpeer'),
\r
25 ('port', 7001, 'TCP+UDP listen port'),
\r
26 ('permid', '', 'filename containing EC keypair'),
\r
27 ('overlaylogpostfix', '', 'postfix of filename where overlay is saved to, hostname+date are prepended, new log for each day automatically, default: spPORT.log'),
\r
28 ('statedir', '.Tribler','dir to save session state'),
\r
29 ('installdir', '', 'source code install dir')]
\r
32 def get_usage(defs):
\r
33 return parseargs.formatDefinitions(defs,80)
\r
35 def olthread_start_session():
\r
36 """ This code is run by the OverlayThread """
\r
38 sscfg = SessionStartupConfig()
\r
40 sscfg.set_nickname(config['nickname'])
\r
41 sscfg.set_listen_port(config['port'])
\r
42 sscfg.set_state_dir(config['statedir'])
\r
43 if config['installdir'] != '':
\r
44 sscfg.set_install_dir(config['installdir'])
\r
46 sscfg.set_buddycast(True)
\r
47 sscfg.set_superpeer(True)
\r
48 sscfg.set_overlay_log(config['overlaylogpostfix'])
\r
49 if config['permid'] != '':
\r
50 sscfg.set_permid_keypair_filename(config['permid'])
\r
53 sscfg.set_torrent_collecting(False)
\r
54 sscfg.set_torrent_checking(False)
\r
55 sscfg.set_download_help(False)
\r
56 sscfg.set_dialback(False)
\r
57 sscfg.set_remote_query(False)
\r
58 sscfg.set_internal_tracker(False)
\r
61 session = Session(sscfg)
\r
65 if __name__ == "__main__":
\r
66 """ This code is run by the MainThread """
\r
68 config, fileargs = parseargs.parseargs(sys.argv, argsdef, presets = {})
\r
69 print >>sys.stderr,"superpeer: config is",config
\r
71 if config['overlaylogpostfix'] == '':
\r
72 config['overlaylogpostfix'] = 'sp'+str(config['port'])+'.log'
\r
75 # Currently we use an in-memory database for superpeers.
\r
76 # SQLite supports only per-thread in memory databases.
\r
77 # As our Session initialization is currently setup, the MainThread
\r
78 # creates the DB and adds the superpeer entries, and the OverlayThread
\r
79 # does most DB operations. So two threads accessing the DB.
\r
81 # To work around this I start the Session using the OverlayThread.
\r
82 # Dirty, but a simple solution.
\r
84 overlay_bridge = OverlayThreadingBridge.getInstance()
\r
85 overlay_bridge.add_task(olthread_start_session,0)
\r
88 # NetworkThread and OverlayThread will now do their work. The MainThread
\r
89 # running this here code should wait indefinitely to avoid exiting the
\r
94 # time.sleep(sys.maxint) has "issues" on 64bit architectures; divide it
\r
95 # by some value (2048) to solve problem
\r
96 time.sleep(sys.maxint/2048)
\r