more modularisation
[electrum-server.git] / server.py
1 #!/usr/bin/env python
2 # Copyright(C) 2012 thomasv@gitorious
3
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.
8 #
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.
13 #
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>.
17
18 import time, json, socket, operator, thread, ast, sys, re, traceback
19 import ConfigParser
20 from json import dumps, loads
21 import urllib
22 import threading
23
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')
36
37 try:
38     f = open('/etc/electrum.conf','r')
39     config.readfp(f)
40     f.close()
41 except:
42     print "Could not read electrum.conf. I will use the default values."
43
44 try:
45     f = open('/etc/electrum.banner','r')
46     config.set('server','banner', f.read())
47     f.close()
48 except:
49     pass
50
51
52 password = config.get('server','password')
53
54
55 from processor import Shared, Processor, Dispatcher
56 from stratum_http import HttpServer
57 from stratum import TcpServer
58 from native import NativeServer
59 from irc import Irc
60 from abe_backend import AbeStore
61
62 class AbeProcessor(Processor):
63     def process(self,request):
64         message_id = request['id']
65         method = request['method']
66         params = request.get('params',[])
67         #print request
68
69         result = ''
70         if method == 'numblocks.subscribe':
71             result = store.block_number
72         elif method == 'address.subscribe':
73             address = params[0]
74             store.watch_address(address)
75             status = store.get_status(address)
76             result = status
77         elif method == 'client.version':
78             #session.version = params[0]
79             pass
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':
85             address = params[0]
86             result = store.get_history( address ) 
87         elif method == 'transaction.broadcast':
88             txo = store.send_tx(params[0])
89             print "sent tx:", txo
90             result = txo 
91         else:
92             print "unknown method", request
93
94         if result!='':
95             response = { 'id':message_id, 'method':method, 'params':params, 'result':result }
96             self.push_response(response)
97
98     def get_status(self,addr):
99         return store.get_status(addr)
100
101
102
103
104
105
106 if __name__ == '__main__':
107
108     if len(sys.argv)>1:
109         import jsonrpclib
110         server = jsonrpclib.Server('http://%s:8081'%config.get('server','host'))
111         cmd = sys.argv[1]
112         if cmd == 'stop':
113             out = server.stop(password)
114         else:
115             out = "Unknown command: '%s'" % cmd
116         print out
117         sys.exit(0)
118
119     processor = AbeProcessor()
120     shared = Shared()
121     # Bind shared to processor since constructor is user defined
122     processor.shared = shared
123     processor.start()
124
125     irc = Irc(processor, config.get('server','host'), config.get('server','ircname'))
126     if (config.get('server','irc') == 'yes' ): irc.start()
127
128     # backend
129     store = AbeStore(config)
130
131     # dispatcher
132     dispatcher = Dispatcher(shared, processor)
133     dispatcher.start()
134
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),
140                    ]
141     for server in transports:
142         server.start()
143
144     print "starting Electrum server on", host
145     store.run(processor)
146     print "server stopped"
147