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 print json.loads(msg).get('result')
68 if __name__ == '__main__':
69 config = create_config()
70 password = config.get('server', 'password')
71 host = config.get('server', 'host')
72 native_port = config.get('server', 'native_port')
73 stratum_tcp_port = config.get('server', 'stratum_tcp_port')
74 stratum_http_port = config.get('server', 'stratum_http_port')
77 run_rpc_command(sys.argv[1], stratum_tcp_port)
80 from processor import Dispatcher
81 from transports.stratum_http import HttpServer
82 from transports.stratum_tcp import TcpServer
83 from transports.native import NativeServer
85 from backends.irc import ServerProcessor
86 backend_name = config.get('server', 'backend')
87 if backend_name == "libbitcoin":
88 # NativeServer cannot be used with libbitcoin
90 config.set('server', 'native_port', '')
92 backend = __import__("backends." + backend_name,
93 fromlist=["BlockchainProcessor"])
95 sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
99 dispatcher = Dispatcher()
100 shared = dispatcher.shared
102 # Create and register processors
103 chain_proc = backend.BlockchainProcessor(config)
104 dispatcher.register('blockchain', chain_proc)
106 server_proc = ServerProcessor(config)
107 dispatcher.register('server', server_proc)
110 # Create various transports we need
111 if native_port is not None:
112 server_banner = config.get('server','banner')
113 native_server = NativeServer(shared, chain_proc, server_proc,
114 server_banner, host, int(native_port))
115 transports.append(native_server)
117 if stratum_tcp_port is not None:
118 tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
119 transports.append(tcp_server)
121 if stratum_http_port is not None:
122 http_server = HttpServer(dispatcher, host, int(stratum_http_port))
123 transports.append(http_server)
125 for server in transports:
128 print "Starting Electrum server on", host
129 while not shared.stopped():
131 print "Server stopped"