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, json, socket, operator, thread, ast, sys, re, traceback
20 from json import dumps, loads
24 config = ConfigParser.ConfigParser()
25 # set some defaults, which will be overwritten by the config file
26 config.add_section('server')
27 config.set('server','banner', 'Welcome to Electrum!')
28 config.set('server', 'host', 'localhost')
29 config.set('server', 'port', '50000')
30 config.set('server', 'password', '')
31 config.set('server', 'irc', 'yes')
32 config.set('server', 'ircname', 'Electrum server')
33 config.add_section('database')
34 config.set('database', 'type', 'psycopg2')
35 config.set('database', 'database', 'abe')
38 f = open('/etc/electrum.conf','r')
42 print "Could not read electrum.conf. I will use the default values."
45 f = open('/etc/electrum.banner','r')
46 config.set('server','banner', f.read())
52 password = config.get('server','password')
55 from processor import Shared, Processor, Dispatcher
56 from stratum_http import HttpServer
57 from stratum import TcpServer
58 from native import NativeServer
60 from abe_backend import AbeStore
62 class AbeProcessor(Processor):
63 def process(self,request):
64 message_id = request['id']
65 method = request['method']
66 params = request.get('params',[])
70 if method == 'numblocks.subscribe':
71 result = store.block_number
72 elif method == 'address.subscribe':
74 store.watch_address(address)
75 status = store.get_status(address)
77 elif method == 'client.version':
78 #session.version = params[0]
80 elif method == 'server.banner':
81 result = config.get('server','banner').replace('\\n','\n')
82 elif method == 'server.peers':
83 result = irc.get_peers()
84 elif method == 'address.get_history':
86 result = store.get_history( address )
87 elif method == 'transaction.broadcast':
88 txo = store.send_tx(params[0])
92 print "unknown method", request
95 response = { 'id':message_id, 'method':method, 'params':params, 'result':result }
96 self.push_response(response)
98 def get_status(self,addr):
99 return store.get_status(addr)
106 if __name__ == '__main__':
110 server = jsonrpclib.Server('http://%s:8081'%config.get('server','host'))
113 out = server.stop(password)
115 out = "Unknown command: '%s'" % cmd
119 processor = AbeProcessor()
121 # Bind shared to processor since constructor is user defined
122 processor.shared = shared
125 irc = Irc(processor, config.get('server','host'), config.get('server','ircname'))
126 if (config.get('server','irc') == 'yes' ): irc.start()
129 store = AbeStore(config)
132 dispatcher = Dispatcher(shared, processor)
135 host = config.get('server','host')
136 # Create various transports we need
137 transports = [ NativeServer(shared, store, irc, config.get('server','banner'), host, 50000),
138 TcpServer(shared, processor, host, 50001),
139 HttpServer(shared, processor, host, 8081),
141 for server in transports:
144 print "starting Electrum server on", host
146 print "server stopped"