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
24 def attempt_read_config(config, filename):
26 with open(filename, 'r') as f:
32 config = ConfigParser.ConfigParser()
33 # set some defaults, which will be overwritten by the config file
34 config.add_section('server')
35 config.set('server', 'banner', 'Welcome to Electrum!')
36 config.set('server', 'host', 'localhost')
37 config.set('server', 'stratum_tcp_port', '50001')
38 config.set('server', 'stratum_http_port', '8081')
39 config.set('server', 'password', '')
40 config.set('server', 'irc', 'yes')
41 config.set('server', 'irc_nick', '')
42 config.add_section('database')
43 config.set('database', 'type', 'psycopg2')
44 config.set('database', 'database', 'abe')
45 config.set('server', 'backend', 'abe')
47 for path in ('/etc/', ''):
48 filename = path + 'electrum.conf'
49 attempt_read_config(config, filename)
52 with open('/etc/electrum.banner', 'r') as f:
53 config.set('server','banner', f.read())
59 def run_rpc_command(command, stratum_tcp_port):
61 s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
62 s.connect(( host, int(stratum_tcp_port )))
63 method = 'server.' + command
64 request = json.dumps( { 'id':0, 'method':method, 'params':[password] } )
65 s.send(request + '\n')
70 if msg.find('\n') != -1: break
72 r = json.loads(msg).get('result')
73 if command == 'stop': print r
74 elif command == 'info':
76 print 'type address sub version time'
78 print '%4s %15s %3s %7s %.2f'%( item.get('name'),
80 item.get('subscriptions'),
82 (now - item.get('time')) )
84 if __name__ == '__main__':
85 config = create_config()
86 password = config.get('server', 'password')
87 host = config.get('server', 'host')
88 stratum_tcp_port = config.get('server', 'stratum_tcp_port')
89 stratum_http_port = config.get('server', 'stratum_http_port')
92 run_rpc_command(sys.argv[1], stratum_tcp_port)
95 from processor import Dispatcher
97 from backends.irc import ServerProcessor
98 backend_name = config.get('server', 'backend')
100 backend = __import__("backends." + backend_name,
101 fromlist=["BlockchainProcessor"])
103 sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
107 dispatcher = Dispatcher()
108 shared = dispatcher.shared
110 # Create and register processors
111 chain_proc = backend.BlockchainProcessor(config)
112 dispatcher.register('blockchain', chain_proc)
114 server_proc = ServerProcessor(config)
115 dispatcher.register('server', server_proc)
118 # Create various transports we need
120 from transports.stratum_tcp import TcpServer
121 tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
122 transports.append(tcp_server)
124 if stratum_http_port:
125 from transports.stratum_http import HttpServer
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"