instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Tools / superpeer.py
1 # Written by Arno Bakker \r
2 # see LICENSE.txt for license information\r
3 #\r
4 # See www.tribler.org/trac/wiki/SuperpeerMode for considerations.\r
5 #\r
6 \r
7 \r
8 import sys\r
9 import os\r
10 import shutil\r
11 import time\r
12 import tempfile\r
13 import random\r
14 import urllib2\r
15 from traceback import print_exc\r
16 from threading import Condition\r
17 \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
23 \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
30 \r
31 \r
32 def get_usage(defs):\r
33     return parseargs.formatDefinitions(defs,80)\r
34     \r
35 def olthread_start_session():\r
36     """ This code is run by the OverlayThread """\r
37     \r
38     sscfg = SessionStartupConfig()\r
39 \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
45 \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
51     \r
52     # Disable features\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
59     \r
60     global session\r
61     session = Session(sscfg)\r
62 \r
63 \r
64 \r
65 if __name__ == "__main__":\r
66     """ This code is run by the MainThread """\r
67 \r
68     config, fileargs = parseargs.parseargs(sys.argv, argsdef, presets = {})\r
69     print >>sys.stderr,"superpeer: config is",config\r
70 \r
71     if config['overlaylogpostfix'] == '':\r
72         config['overlaylogpostfix'] = 'sp'+str(config['port'])+'.log'\r
73 \r
74     #\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
80     #\r
81     # To work around this I start the Session using the OverlayThread.\r
82     # Dirty, but a simple solution.\r
83     # \r
84     overlay_bridge = OverlayThreadingBridge.getInstance()\r
85     overlay_bridge.add_task(olthread_start_session,0)\r
86     \r
87     #\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
90     # process.\r
91     #\r
92     try:\r
93         while True:\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
97     except:\r
98         print_exc()\r
99     \r
100     global session\r
101     session.shutdown()\r
102     time.sleep(3)\r
103