can specify backend in conf file
[electrum-server.git] / server.py
1 #!/usr/bin/env python
2 # Copyright(C) 2012 thomasv@gitorious
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public
15 # License along with this program.  If not, see
16 # <http://www.gnu.org/licenses/agpl.html>.
17
18 import time, sys, traceback
19 import ConfigParser
20
21 config = ConfigParser.ConfigParser()
22 # set some defaults, which will be overwritten by the config file
23 config.add_section('server')
24 config.set('server','banner', 'Welcome to Electrum!')
25 config.set('server', 'host', 'localhost')
26 config.set('server', 'native_port', '50000')
27 config.set('server', 'stratum_tcp_port', '50001')
28 config.set('server', 'stratum_http_port', '8081')
29 config.set('server', 'password', '')
30 config.set('server', 'irc', 'yes')
31 config.set('server', 'irc_nick', '')
32 config.add_section('database')
33 config.set('database', 'type', 'psycopg2')
34 config.set('database', 'database', 'abe')
35 config.set('server', 'backend', 'abe')
36
37 for path in ('', '/etc/'):
38     filename = path + 'electrum.conf'
39     try:
40         with open(filename, 'r') as f:
41             config.readfp(f)
42     except IOError:
43         print "Could not read %s. Falling back." % filename
44
45 try:
46     with open('/etc/electrum.banner', 'r') as f:
47         config.set('server','banner', f.read())
48 except IOError:
49     pass
50
51 password = config.get('server','password')
52 host = config.get('server','host')
53 native_port = config.get('server','native_port')
54 stratum_tcp_port = config.get('server','stratum_tcp_port')
55 stratum_http_port = config.get('server','stratum_http_port')
56
57 from processor import Dispatcher
58 from transports.stratum_http import HttpServer
59 from transports.stratum_tcp import TcpServer
60 from transports.native import NativeServer
61
62 from modules.irc import ServerProcessor
63 backend_name = config.get('server', 'backend')
64 if backend_name == "libbitcoin":
65     # NativeServer cannot be used with libbitcoin
66     native_port = None
67     from modules.python_bitcoin import BlockchainProcessor
68 elif backend_name == "abe":
69     from modules.abe import AbeProcessor as BlockchainProcessor
70 else:
71     raise Exception('Unknown backend specified')
72
73 if __name__ == '__main__':
74
75     if len(sys.argv)>1:
76         import jsonrpclib
77         server = jsonrpclib.Server('http://%s:%s'%(host,stratum_http_port))
78         cmd = sys.argv[1]
79         if cmd == 'stop':
80             out = server.server.stop(password)
81         else:
82             out = "Unknown command: '%s'" % cmd
83         print out
84         sys.exit(0)
85
86     # Create hub
87     dispatcher = Dispatcher()
88     shared = dispatcher.shared
89
90     # Create and register processors
91     abe = BlockchainProcessor(config)
92     dispatcher.register('blockchain', abe)
93
94     sb = ServerProcessor(config)
95     dispatcher.register('server', sb)
96
97     # Create various transports we need
98     transports = []
99     if native_port: transports.append( NativeServer(shared, abe, sb, config.get('server','banner'), host, int(native_port)) )
100     if stratum_tcp_port: transports.append( TcpServer(dispatcher, host, int(stratum_tcp_port)) )
101     if stratum_http_port: transports.append( HttpServer(dispatcher, host, int(stratum_http_port)) )
102     for server in transports:
103         server.start()
104
105     print "starting Electrum server on", host
106     while not shared.stopped():
107         time.sleep(1)
108     print "server stopped"