Generalize the way to setup the server to use Litecoin.
[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, sys, traceback
19 import ConfigParser
20
21 import logging
22 logging.basicConfig()
23
24 if sys.maxsize <= 2**32:
25     print "Warning: it looks like you are using a 32bit system. You may experience crashes caused by mmap"
26
27
28 def attempt_read_config(config, filename):
29     try:
30         with open(filename, 'r') as f:
31             config.readfp(f)
32     except IOError:
33         pass
34
35 def create_config():
36     config = ConfigParser.ConfigParser()
37     # set some defaults, which will be overwritten by the config file
38     config.add_section('server')
39     config.set('server', 'banner', 'Welcome to Electrum!')
40     config.set('server', 'host', 'localhost')
41     config.set('server', 'stratum_tcp_port', '50001')
42     config.set('server', 'stratum_http_port', '8081')
43     config.set('server', 'password', '')
44     config.set('server', 'irc', 'yes')
45     config.set('server', 'irc_nick', '')
46     config.set('server', 'coin', '')
47     config.set('server', 'datadir', '')
48     config.add_section('database')
49     config.set('database', 'type', 'psycopg2')
50     config.set('database', 'database', 'abe')
51     config.set('database', 'limit', '1000')
52     config.set('server', 'backend', 'abe')
53
54     for path in ('/etc/', ''):
55         filename = path + 'electrum.conf'
56         attempt_read_config(config, filename)
57
58     try:
59         with open('/etc/electrum.banner', 'r') as f:
60             config.set('server','banner', f.read())
61     except IOError:
62         pass
63
64     return config
65
66 def run_rpc_command(command, stratum_tcp_port):
67     import socket, json
68     s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
69     s.connect(( host, int(stratum_tcp_port )))
70     method = 'server.' + command
71     request = json.dumps( { 'id':0, 'method':method, 'params':[password] } )
72     s.send(request + '\n')
73     msg = ''
74     while True:
75         o = s.recv(1024)
76         msg += o
77         if msg.find('\n') != -1: break
78     s.close()
79     r = json.loads(msg).get('result')
80
81     if command == 'info': 
82         now = time.time()
83         print 'type           address   sub  version  time' 
84         for item in r:
85             print '%4s   %15s   %3s  %7s  %.2f'%( item.get('name'), 
86                                                   item.get('address'), 
87                                                   item.get('subscriptions'), 
88                                                   item.get('version'), 
89                                                   (now - item.get('time')) )
90     else:
91         print r
92
93 if __name__ == '__main__':
94     config = create_config()
95     password = config.get('server', 'password')
96     host = config.get('server', 'host')
97     stratum_tcp_port = config.get('server', 'stratum_tcp_port')
98     stratum_http_port = config.get('server', 'stratum_http_port')
99
100     if len(sys.argv) > 1:
101         run_rpc_command(sys.argv[1], stratum_tcp_port)
102         sys.exit(0)
103
104     from processor import Dispatcher
105
106     from backends.irc import ServerProcessor
107     backend_name = config.get('server', 'backend')
108     try:
109         backend = __import__("backends." + backend_name,
110                              fromlist=["BlockchainProcessor"])
111     except ImportError:
112         sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
113         raise
114
115     print "Starting Electrum server on", host
116
117     # Create hub
118     dispatcher = Dispatcher()
119     shared = dispatcher.shared
120
121     # Create and register processors
122     chain_proc = backend.BlockchainProcessor(config)
123     dispatcher.register('blockchain', chain_proc)
124
125     server_proc = ServerProcessor(config)
126     dispatcher.register('server', server_proc)
127
128     transports = []
129     # Create various transports we need
130     if stratum_tcp_port:
131         from transports.stratum_tcp import TcpServer
132         tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
133         transports.append(tcp_server)
134
135     if stratum_http_port:
136         from transports.stratum_http import HttpServer
137         http_server = HttpServer(dispatcher, host, int(stratum_http_port))
138         transports.append(http_server)
139
140     for server in transports:
141         server.start()
142
143     while not shared.stopped():
144         time.sleep(1)
145     print "Server stopped"
146