Add some of the fetching logic.
[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     'DHT': 'apt_dht_Khashmir',
33 }
34
35 DHT_DEFAULTS = {
36     # magic id to use before we know a peer's id
37     'NULL_ID': 20 * '\0',
38     
39     # Kademlia "K" constant, this should be an even number
40     'K': '8',
41     
42     # SHA1 is 160 bits long
43     'HASH_LENGTH': '160',
44     
45     # checkpoint every this many seconds
46     'CHECKPOINT_INTERVAL': '15m', # fifteen minutes
47     
48     ### SEARCHING/STORING
49     # concurrent xmlrpc calls per find node/value request!
50     'CONCURRENT_REQS': '4',
51     
52     # how many hosts to post to
53     'STORE_REDUNDANCY': '3',
54     
55     ###  ROUTING TABLE STUFF
56     # how many times in a row a node can fail to respond before it's booted from the routing table
57     'MAX_FAILURES': '3',
58     
59     # never ping a node more often than this
60     'MIN_PING_INTERVAL': '15m', # fifteen minutes
61     
62     # refresh buckets that haven't been touched in this long
63     'BUCKET_STALENESS': '1h', # one hour
64     
65     ###  KEY EXPIRER
66     # time before expirer starts running
67     'KEINITIAL_DELAY': '15s', # 15 seconds - to clean out old stuff in persistent db
68     
69     # time between expirer runs
70     'KE_DELAY': '20m', # 20 minutes
71     
72     # expire entries older than this
73     'KE_AGE': '1h', # 60 minutes
74 }
75
76 class AptDHTConfigParser(SafeConfigParser):
77     """
78     Adds 'gettime' to ConfigParser to interpret the suffixes.
79     """
80     time_multipliers={
81         's': 1,    #seconds
82         'm': 60,   #minutes
83         'h': 3600, #hours
84         'd': 86400,#days
85         }
86
87     def gettime(self, section, option):
88         mult = 1
89         value = self.get(section, option)
90         if len(value) == 0:
91             raise ConfigError("Configuration parse error: [%s] %s" % (section, option))
92         suffix = value[-1].lower()
93         if suffix in self.time_multipliers.keys():
94             mult = self.time_multipliers[suffix]
95             value = value[:-1]
96         return int(value)*mult
97     def getstring(self, section, option):
98         return self.get(section,option)
99     def getstringlist(self, section, option):
100         return self.get(section,option).split()
101
102 config = AptDHTConfigParser(DEFAULTS)
103 config.add_section(config.get('DEFAULT', 'DHT'))
104 for k in DHT_DEFAULTS:
105     config.set(config.get('DEFAULT', 'DHT'), k, DHT_DEFAULTS[k])