Only use the ThrottlingProtocol if the producer is streaming.
[quix0rs-apt-p2p.git] / apt-dht.py
1 #!/usr/bin/env python
2
3 # Load apt-dht application
4 #
5 # There are two ways apt-dht can be started:
6 #  1. twistd -y apt-dht
7 #     - twistd will load this file and execute the app
8 #       in 'application' variable
9 #  2. from command line
10 #     - __name__ will be '__main__'
11
12 import pwd,sys
13
14 from twisted.application import service, internet, app, strports
15 from twisted.internet import reactor
16 from twisted.python import usage, log
17 from twisted.web2 import channel
18
19 from apt_dht.apt_dht_conf import config, version, DEFAULT_CONFIG_FILES
20 from apt_dht.interfaces import IDHT
21
22 config_file = ''
23
24 if __name__ == '__main__':
25     # Parse command line parameters when started on command line
26     class AptDHTOptions(usage.Options):
27         optFlags = [
28             ['help', 'h', 'Print this help message'],
29             ]
30         optParameters = [
31             ['config-file', 'c', '', "Configuration file"],
32             ['log-file', 'l', '-', "File to log to, - for stdout"],
33             ]
34         longdesc="apt-dht is a peer-to-peer downloader for apt users"
35         def opt_version(self):
36             print "apt-dht %s" % version.short()
37             sys.exit(0)
38
39     opts = AptDHTOptions()
40     try:
41         opts.parseOptions()
42     except usage.UsageError, ue:
43         print '%s: %s' % (sys.argv[0], ue)
44         sys.exit(1)
45
46     config_file = opts.opts['config-file']
47     log_file = opts.opts['log-file']
48     if log_file == '-':
49         f = sys.stdout
50     else:
51         f = open(log_file, 'w')
52     log.startLogging(f, setStdout=1)
53
54 log.msg("Loading config files: '%s'" % "', '".join(DEFAULT_CONFIG_FILES + [config_file]))
55 config_read = config.read(DEFAULT_CONFIG_FILES + [config_file])
56 log.msg("Successfully loaded config files: '%s'" % "', '".join(config_read))
57 if config.has_option('DEFAULT', 'username') and config.get('DEFAULT', 'username'):
58     uid,gid = pwd.getpwnam(config.get('DEFAULT', 'username'))[2:4]
59 else:
60     uid,gid = None,None
61
62 log.msg('Starting application')
63 application = service.Application("apt-dht", uid, gid)
64 #print service.IProcess(application).processName
65 #service.IProcess(application).processName = 'apt-dht'
66
67 log.msg('Starting DHT')
68 DHT = __import__(config.get('DEFAULT', 'DHT')+'.DHT', globals(), locals(), ['DHT'])
69 assert IDHT.implementedBy(DHT.DHT), "You must provide a DHT implementation that implements the IDHT interface."
70 myDHT = DHT.DHT()
71
72 if not config.getboolean('DEFAULT', 'DHT-only'):
73     log.msg('Starting main application server')
74     from apt_dht.apt_dht import AptDHT
75     myapp = AptDHT(myDHT)
76     factory = myapp.getHTTPFactory()
77     s = strports.service('tcp:'+config.get('DEFAULT', 'port'), factory)
78     s.setServiceParent(application)
79 else:
80     myDHT.loadConfig(config, config.get('DEFAULT', 'DHT'))
81     myDHT.join()
82
83 if __name__ == '__main__':
84     # Run on command line
85     service.IServiceCollection(application).privilegedStartService()
86     service.IServiceCollection(application).startService()
87     reactor.run()