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':
77 print '%15s %3s %7s %.2f'%( item.get('address'), item.get('subscriptions'), item.get('version'), (now - item.get('time')) )
79 if __name__ == '__main__':
80 config = create_config()
81 password = config.get('server', 'password')
82 host = config.get('server', 'host')
83 stratum_tcp_port = config.get('server', 'stratum_tcp_port')
84 stratum_http_port = config.get('server', 'stratum_http_port')
87 run_rpc_command(sys.argv[1], stratum_tcp_port)
90 from processor import Dispatcher
92 from backends.irc import ServerProcessor
93 backend_name = config.get('server', 'backend')
95 backend = __import__("backends." + backend_name,
96 fromlist=["BlockchainProcessor"])
98 sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
102 dispatcher = Dispatcher()
103 shared = dispatcher.shared
105 # Create and register processors
106 chain_proc = backend.BlockchainProcessor(config)
107 dispatcher.register('blockchain', chain_proc)
109 server_proc = ServerProcessor(config)
110 dispatcher.register('server', server_proc)
113 # Create various transports we need
115 from transports.stratum_tcp import TcpServer
116 tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
117 transports.append(tcp_server)
119 if stratum_http_port:
120 from transports.stratum_http import HttpServer
121 http_server = HttpServer(dispatcher, host, int(stratum_http_port))
122 transports.append(http_server)
124 for server in transports:
127 print "Starting Electrum server on", host
128 while not shared.stopped():
130 print "Server stopped"