add server info command. send command line commands with tcp instead of http
[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 def attempt_read_config(config, filename):
22     try:
23         with open(filename, 'r') as f:
24             config.readfp(f)
25     except IOError:
26         pass
27
28 def create_config():
29     config = ConfigParser.ConfigParser()
30     # set some defaults, which will be overwritten by the config file
31     config.add_section('server')
32     config.set('server', 'banner', 'Welcome to Electrum!')
33     config.set('server', 'host', 'localhost')
34     config.set('server', 'native_port', '50000')
35     config.set('server', 'stratum_tcp_port', '50001')
36     config.set('server', 'stratum_http_port', '8081')
37     config.set('server', 'password', '')
38     config.set('server', 'irc', 'yes')
39     config.set('server', 'irc_nick', '')
40     config.add_section('database')
41     config.set('database', 'type', 'psycopg2')
42     config.set('database', 'database', 'abe')
43     config.set('server', 'backend', 'abe')
44
45     for path in ('/etc/', ''):
46         filename = path + 'electrum.conf'
47         attempt_read_config(config, filename)
48
49     try:
50         with open('/etc/electrum.banner', 'r') as f:
51             config.set('server','banner', f.read())
52     except IOError:
53         pass
54
55     return config
56
57 def run_rpc_command(command, stratum_tcp_port):
58     import socket, json
59     s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
60     s.connect(( host, int(stratum_tcp_port )))
61     method = 'server.' + command
62     request = json.dumps( { 'id':0, 'method':method, 'params':[password] } )
63     s.send(request + '\n')
64     msg = s.recv(1024)
65     s.close()
66     print json.loads(msg).get('result')
67
68 if __name__ == '__main__':
69     config = create_config()
70     password = config.get('server', 'password')
71     host = config.get('server', 'host')
72     native_port = config.get('server', 'native_port')
73     stratum_tcp_port = config.get('server', 'stratum_tcp_port')
74     stratum_http_port = config.get('server', 'stratum_http_port')
75
76     if len(sys.argv) > 1:
77         run_rpc_command(sys.argv[1], stratum_tcp_port)
78         sys.exit(0)
79
80     from processor import Dispatcher
81     from transports.stratum_http import HttpServer
82     from transports.stratum_tcp import TcpServer
83     from transports.native import NativeServer
84
85     from backends.irc import ServerProcessor
86     backend_name = config.get('server', 'backend')
87     if backend_name == "libbitcoin":
88         # NativeServer cannot be used with libbitcoin
89         native_port = None
90         config.set('server', 'native_port', '')
91     try:
92         backend = __import__("backends." + backend_name,
93                              fromlist=["BlockchainProcessor"])
94     except ImportError:
95         sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
96         raise
97
98     # Create hub
99     dispatcher = Dispatcher()
100     shared = dispatcher.shared
101
102     # Create and register processors
103     chain_proc = backend.BlockchainProcessor(config)
104     dispatcher.register('blockchain', chain_proc)
105
106     server_proc = ServerProcessor(config)
107     dispatcher.register('server', server_proc)
108
109     transports = []
110     # Create various transports we need
111     if native_port is not None:
112         server_banner = config.get('server','banner')
113         native_server = NativeServer(shared, chain_proc, server_proc,
114                                      server_banner, host, int(native_port))
115         transports.append(native_server)
116
117     if stratum_tcp_port is not None:
118         tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
119         transports.append(tcp_server)
120
121     if stratum_http_port is not None:
122         http_server = HttpServer(dispatcher, host, int(stratum_http_port))
123         transports.append(http_server)
124
125     for server in transports:
126         server.start()
127
128     print "Starting Electrum server on", host
129     while not shared.stopped():
130         time.sleep(1)
131     print "Server stopped"
132