remove support for native protocol
[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 def attempt_read_config(config, filename):
25     try:
26         with open(filename, 'r') as f:
27             config.readfp(f)
28     except IOError:
29         pass
30
31 def create_config():
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')
46
47     for path in ('/etc/', ''):
48         filename = path + 'electrum.conf'
49         attempt_read_config(config, filename)
50
51     try:
52         with open('/etc/electrum.banner', 'r') as f:
53             config.set('server','banner', f.read())
54     except IOError:
55         pass
56
57     return config
58
59 def run_rpc_command(command, stratum_tcp_port):
60     import socket, json
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')
66     msg = ''
67     while True:
68         o = s.recv(1024)
69         msg += o
70         if msg.find('\n') != -1: break
71     s.close()
72     r = json.loads(msg).get('result')
73     if command == 'stop': print r
74     elif command == 'info': 
75         for item in r:
76             print '%15s   %3s  %7s'%( item.get('address'), item.get('subscriptions'), item.get('version') )
77
78 if __name__ == '__main__':
79     config = create_config()
80     password = config.get('server', 'password')
81     host = config.get('server', 'host')
82     stratum_tcp_port = config.get('server', 'stratum_tcp_port')
83     stratum_http_port = config.get('server', 'stratum_http_port')
84
85     if len(sys.argv) > 1:
86         run_rpc_command(sys.argv[1], stratum_tcp_port)
87         sys.exit(0)
88
89     from processor import Dispatcher
90
91     from backends.irc import ServerProcessor
92     backend_name = config.get('server', 'backend')
93     try:
94         backend = __import__("backends." + backend_name,
95                              fromlist=["BlockchainProcessor"])
96     except ImportError:
97         sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
98         raise
99
100     # Create hub
101     dispatcher = Dispatcher()
102     shared = dispatcher.shared
103
104     # Create and register processors
105     chain_proc = backend.BlockchainProcessor(config)
106     dispatcher.register('blockchain', chain_proc)
107
108     server_proc = ServerProcessor(config)
109     dispatcher.register('server', server_proc)
110
111     transports = []
112     # Create various transports we need
113     if stratum_tcp_port:
114         from transports.stratum_tcp import TcpServer
115         tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
116         transports.append(tcp_server)
117
118     if stratum_http_port:
119         from transports.stratum_http import HttpServer
120         http_server = HttpServer(dispatcher, host, int(stratum_http_port))
121         transports.append(http_server)
122
123     for server in transports:
124         server.start()
125
126     print "Starting Electrum server on", host
127     while not shared.stopped():
128         time.sleep(1)
129     print "Server stopped"
130