create dispatcher class; redefine processors as threads
[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', 'port', '50000')
27 config.set('server', 'password', '')
28 config.set('server', 'irc', 'yes')
29 config.set('server', 'ircname', 'Electrum server')
30 config.add_section('database')
31 config.set('database', 'type', 'psycopg2')
32 config.set('database', 'database', 'abe')
33
34 try:
35     f = open('/etc/electrum.conf','r')
36     config.readfp(f)
37     f.close()
38 except:
39     print "Could not read electrum.conf. I will use the default values."
40
41 try:
42     f = open('/etc/electrum.banner','r')
43     config.set('server','banner', f.read())
44     f.close()
45 except:
46     pass
47
48 password = config.get('server','password')
49 host = config.get('server','host')
50 use_libbitcoin = False
51
52
53 from processor import Dispatcher
54 from transports.stratum_http import HttpServer
55 from transports.stratum_tcp import TcpServer
56 from transports.native import NativeServer
57 from irc import ServerProcessor
58 from abe_backend import AbeProcessor
59
60 if use_libbitcoin:
61     from modules.python_bitcoin import LibBitcoinProcessor as BlockchainProcessor
62 else:
63     from abe_backend import AbeProcessor as BlockchainProcessor
64
65 if __name__ == '__main__':
66
67     if len(sys.argv)>1:
68         import jsonrpclib
69         server = jsonrpclib.Server('http://%s:8081'%host)
70         cmd = sys.argv[1]
71         if cmd == 'stop':
72             out = server.stop(password)
73         else:
74             out = "Unknown command: '%s'" % cmd
75         print out
76         sys.exit(0)
77
78     # Create hub
79     dispatcher = Dispatcher()
80     shared = dispatcher.shared
81
82     # Create and register processors
83     abe = BlockchainProcessor(config)
84     dispatcher.register('blockchain', abe)
85
86     sb = ServerProcessor(config)
87     dispatcher.register('server', sb)
88
89     # Create various transports we need
90     transports = [ NativeServer(shared, abe, sb, config.get('server','banner'), host, 50000),
91                    TcpServer(dispatcher, host, 50001),
92                    HttpServer(dispatcher, host, 8081),
93                    ]
94     for server in transports:
95         server.start()
96
97     print "starting Electrum server on", host
98     while not shared.stopped():
99         time.sleep(1)
100     print "server stopped"