Add more to khashmir's DHT implementation.
[quix0rs-apt-p2p.git] / apt_dht / apt_dht_conf.py
1
2 import os, sys
3 from ConfigParser import SafeConfigParser
4
5 from twisted.python import log, versions
6
7 class ConfigError(Exception):
8     def __init__(self, message):
9         self.message = message
10     def __str__(self):
11         return repr(self.message)
12
13 version = versions.Version('apt-dht', 0, 0, 0)
14 home = os.path.expandvars('${HOME}')
15 if home == '${HOME}' or not os.path.isdir(home):
16     home = os.path.expanduser('~')
17     if not os.path.isdir(home):
18         home = os.path.abspath(os.path.dirname(sys.argv[0]))
19
20 DEFAULTS = {
21
22     # Port to listen on for all requests (TCP and UDP)
23     'port': '9977',
24     
25     # Directory to store the downloaded files in
26     'cache_dir': home + '/.apt-dht/cache',
27     
28     # User name to try and run as
29     'username': '',
30
31     # Which DHT implementation to use.
32     # It must be possile to do "from <DHT> import DHT" to get a class that
33     # implements the IDHT interface.
34     'DHT': 'apt_dht_Khashmir.DHT',
35
36     # Whether to only run the DHT (for providing a login node)
37     'DHT-only': 'no',
38 }
39
40 DHT_DEFAULTS = {
41     # bootstrap nodes to contact to join the DHT
42     'bootstrap': """www.camrdale.org:9977
43         steveholt.hopto.org:9977""",
44     
45     # magic id to use before we know a peer's id
46     'NULL_ID': 20 * '\0',
47     
48     # Kademlia "K" constant, this should be an even number
49     'K': '8',
50     
51     # SHA1 is 160 bits long
52     'HASH_LENGTH': '160',
53     
54     # checkpoint every this many seconds
55     'CHECKPOINT_INTERVAL': '15m', # fifteen minutes
56     
57     ### SEARCHING/STORING
58     # concurrent xmlrpc calls per find node/value request!
59     'CONCURRENT_REQS': '4',
60     
61     # how many hosts to post to
62     'STORE_REDUNDANCY': '3',
63     
64     ###  ROUTING TABLE STUFF
65     # how many times in a row a node can fail to respond before it's booted from the routing table
66     'MAX_FAILURES': '3',
67     
68     # never ping a node more often than this
69     'MIN_PING_INTERVAL': '15m', # fifteen minutes
70     
71     # refresh buckets that haven't been touched in this long
72     'BUCKET_STALENESS': '1h', # one hour
73     
74     ###  KEY EXPIRER
75     # time before expirer starts running
76     'KEINITIAL_DELAY': '15s', # 15 seconds - to clean out old stuff in persistent db
77     
78     # time between expirer runs
79     'KE_DELAY': '20m', # 20 minutes
80     
81     # expire entries older than this
82     'KE_AGE': '1h', # 60 minutes
83 }
84
85 class AptDHTConfigParser(SafeConfigParser):
86     """
87     Adds 'gettime' to ConfigParser to interpret the suffixes.
88     """
89     time_multipliers={
90         's': 1,    #seconds
91         'm': 60,   #minutes
92         'h': 3600, #hours
93         'd': 86400,#days
94         }
95
96     def gettime(self, section, option):
97         mult = 1
98         value = self.get(section, option)
99         if len(value) == 0:
100             raise ConfigError("Configuration parse error: [%s] %s" % (section, option))
101         suffix = value[-1].lower()
102         if suffix in self.time_multipliers.keys():
103             mult = self.time_multipliers[suffix]
104             value = value[:-1]
105         return int(value)*mult
106     def getstring(self, section, option):
107         return self.get(section,option)
108     def getstringlist(self, section, option):
109         return self.get(section,option).split()
110
111 config = AptDHTConfigParser(DEFAULTS)
112 config.add_section(config.get('DEFAULT', 'DHT'))
113 for k in DHT_DEFAULTS:
114     config.set(config.get('DEFAULT', 'DHT'), k, DHT_DEFAULTS[k])