warning to 32bit users
[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.add_section('database')
47     config.set('database', 'type', 'psycopg2')
48     config.set('database', 'database', 'abe')
49     config.set('database', 'limit', '1000')
50     config.set('server', 'backend', 'abe')
51
52     for path in ('/etc/', ''):
53         filename = path + 'electrum.conf'
54         attempt_read_config(config, filename)
55
56     try:
57         with open('/etc/electrum.banner', 'r') as f:
58             config.set('server','banner', f.read())
59     except IOError:
60         pass
61
62     return config
63
64 def run_rpc_command(command, stratum_tcp_port):
65     import socket, json
66     s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
67     s.connect(( host, int(stratum_tcp_port )))
68     method = 'server.' + command
69     request = json.dumps( { 'id':0, 'method':method, 'params':[password] } )
70     s.send(request + '\n')
71     msg = ''
72     while True:
73         o = s.recv(1024)
74         msg += o
75         if msg.find('\n') != -1: break
76     s.close()
77     r = json.loads(msg).get('result')
78
79     if command == 'info': 
80         now = time.time()
81         print 'type           address   sub  version  time' 
82         for item in r:
83             print '%4s   %15s   %3s  %7s  %.2f'%( item.get('name'), 
84                                                   item.get('address'), 
85                                                   item.get('subscriptions'), 
86                                                   item.get('version'), 
87                                                   (now - item.get('time')) )
88     else:
89         print r
90
91 if __name__ == '__main__':
92     config = create_config()
93     password = config.get('server', 'password')
94     host = config.get('server', 'host')
95     stratum_tcp_port = config.get('server', 'stratum_tcp_port')
96     stratum_http_port = config.get('server', 'stratum_http_port')
97
98     if len(sys.argv) > 1:
99         run_rpc_command(sys.argv[1], stratum_tcp_port)
100         sys.exit(0)
101
102     from processor import Dispatcher
103
104     from backends.irc import ServerProcessor
105     backend_name = config.get('server', 'backend')
106     try:
107         backend = __import__("backends." + backend_name,
108                              fromlist=["BlockchainProcessor"])
109     except ImportError:
110         sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
111         raise
112
113     print "Starting Electrum server on", host
114
115     # Create hub
116     dispatcher = Dispatcher()
117     shared = dispatcher.shared
118
119     # Create and register processors
120     chain_proc = backend.BlockchainProcessor(config)
121     dispatcher.register('blockchain', chain_proc)
122
123     server_proc = ServerProcessor(config)
124     dispatcher.register('server', server_proc)
125
126     transports = []
127     # Create various transports we need
128     if stratum_tcp_port:
129         from transports.stratum_tcp import TcpServer
130         tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
131         transports.append(tcp_server)
132
133     if stratum_http_port:
134         from transports.stratum_http import HttpServer
135         http_server = HttpServer(dispatcher, host, int(stratum_http_port))
136         transports.append(http_server)
137
138     for server in transports:
139         server.start()
140
141     while not shared.stopped():
142         time.sleep(1)
143     print "Server stopped"
144