2 # Copyright(C) 2012 thomasv@gitorious
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.
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.
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>.
18 import time, sys, traceback
21 def attempt_read_config(config, filename):
23 with open(filename, 'r') as f:
29 config = ConfigParser.ConfigParser()
30 # set some defaults, which will be overwritten by the config file
31 config.add_section('server')
32 config.set('server', 'banner', 'Welcome to Electrum!')
33 config.set('server', 'host', 'localhost')
34 config.set('server', 'native_port', '50000')
35 config.set('server', 'stratum_tcp_port', '50001')
36 config.set('server', 'stratum_http_port', '8081')
37 config.set('server', 'password', '')
38 config.set('server', 'irc', 'yes')
39 config.set('server', 'irc_nick', '')
40 config.add_section('database')
41 config.set('database', 'type', 'psycopg2')
42 config.set('database', 'database', 'abe')
43 config.set('server', 'backend', 'abe')
45 for path in ('/etc/', ''):
46 filename = path + 'electrum.conf'
47 attempt_read_config(config, filename)
50 with open('/etc/electrum.banner', 'r') as f:
51 config.set('server','banner', f.read())
57 def run_rpc_command(command, stratum_tcp_port):
59 s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
60 s.connect(( host, int(stratum_tcp_port )))
61 method = 'server.' + command
62 request = json.dumps( { 'id':0, 'method':method, 'params':[password] } )
63 s.send(request + '\n')
66 r = json.loads(msg).get('result')
67 if command == 'stop': print r
68 elif command == 'info':
70 print '%15s %3s %7s'%( item.get('address'), item.get('subscriptions'), item.get('version') )
72 if __name__ == '__main__':
73 config = create_config()
74 password = config.get('server', 'password')
75 host = config.get('server', 'host')
76 native_port = config.get('server', 'native_port')
77 stratum_tcp_port = config.get('server', 'stratum_tcp_port')
78 stratum_http_port = config.get('server', 'stratum_http_port')
81 run_rpc_command(sys.argv[1], stratum_tcp_port)
84 from processor import Dispatcher
85 from transports.stratum_http import HttpServer
86 from transports.stratum_tcp import TcpServer
87 from transports.native import NativeServer
89 from backends.irc import ServerProcessor
90 backend_name = config.get('server', 'backend')
91 if backend_name == "libbitcoin":
92 # NativeServer cannot be used with libbitcoin
94 config.set('server', 'native_port', '')
96 backend = __import__("backends." + backend_name,
97 fromlist=["BlockchainProcessor"])
99 sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
103 dispatcher = Dispatcher()
104 shared = dispatcher.shared
106 # Create and register processors
107 chain_proc = backend.BlockchainProcessor(config)
108 dispatcher.register('blockchain', chain_proc)
110 server_proc = ServerProcessor(config)
111 dispatcher.register('server', server_proc)
114 # Create various transports we need
116 server_banner = config.get('server','banner')
117 native_server = NativeServer(shared, chain_proc, server_proc,
118 server_banner, host, int(native_port))
119 transports.append(native_server)
122 tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
123 transports.append(tcp_server)
125 if stratum_http_port:
126 http_server = HttpServer(dispatcher, host, int(stratum_http_port))
127 transports.append(http_server)
129 for server in transports:
132 print "Starting Electrum server on", host
133 while not shared.stopped():
135 print "Server stopped"