nativeServer does not work with libbitcoin
[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
58 from modules.irc import ServerProcessor
59 if use_libbitcoin:
60     from modules.python_bitcoin import LibBitcoinProcessor as BlockchainProcessor
61 else:
62     from modules.abe import AbeProcessor as BlockchainProcessor
63
64 if __name__ == '__main__':
65
66     if len(sys.argv)>1:
67         import jsonrpclib
68         server = jsonrpclib.Server('http://%s:8081'%host)
69         cmd = sys.argv[1]
70         if cmd == 'stop':
71             out = server.stop(password)
72         else:
73             out = "Unknown command: '%s'" % cmd
74         print out
75         sys.exit(0)
76
77     # Create hub
78     dispatcher = Dispatcher()
79     shared = dispatcher.shared
80
81     # Create and register processors
82     abe = BlockchainProcessor(config)
83     dispatcher.register('blockchain', abe)
84
85     sb = ServerProcessor(config)
86     dispatcher.register('server', sb)
87
88     # Create various transports we need
89     transports = [ 
90                    TcpServer(dispatcher, host, 50001),
91                    HttpServer(dispatcher, host, 8081),
92                    ]
93
94     # NativeServer cannot be used with libbitcoin
95     if not use_libbitcoin:
96         transports.append( NativeServer(shared, abe, sb, config.get('server','banner'), host, 50000) )
97
98     for server in transports:
99         server.start()
100
101     print "starting Electrum server on", host
102     while not shared.stopped():
103         time.sleep(1)
104     print "server stopped"