display session type
[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         now = time.time()
76         print 'type           address   sub  version  time' 
77         for item in r:
78             print '%4s   %15s   %3s  %7s  %.2f'%( item.get('name'), 
79                                                   item.get('address'), 
80                                                   item.get('subscriptions'), 
81                                                   item.get('version'), 
82                                                   (now - item.get('time')) )
83
84 if __name__ == '__main__':
85     config = create_config()
86     password = config.get('server', 'password')
87     host = config.get('server', 'host')
88     stratum_tcp_port = config.get('server', 'stratum_tcp_port')
89     stratum_http_port = config.get('server', 'stratum_http_port')
90
91     if len(sys.argv) > 1:
92         run_rpc_command(sys.argv[1], stratum_tcp_port)
93         sys.exit(0)
94
95     from processor import Dispatcher
96
97     from backends.irc import ServerProcessor
98     backend_name = config.get('server', 'backend')
99     try:
100         backend = __import__("backends." + backend_name,
101                              fromlist=["BlockchainProcessor"])
102     except ImportError:
103         sys.stderr.write("Unknown backend '%s' specified\n" % backend_name)
104         raise
105
106     # Create hub
107     dispatcher = Dispatcher()
108     shared = dispatcher.shared
109
110     # Create and register processors
111     chain_proc = backend.BlockchainProcessor(config)
112     dispatcher.register('blockchain', chain_proc)
113
114     server_proc = ServerProcessor(config)
115     dispatcher.register('server', server_proc)
116
117     transports = []
118     # Create various transports we need
119     if stratum_tcp_port:
120         from transports.stratum_tcp import TcpServer
121         tcp_server = TcpServer(dispatcher, host, int(stratum_tcp_port))
122         transports.append(tcp_server)
123
124     if stratum_http_port:
125         from transports.stratum_http import HttpServer
126         http_server = HttpServer(dispatcher, host, int(stratum_http_port))
127         transports.append(http_server)
128
129     for server in transports:
130         server.start()
131
132     print "Starting Electrum server on", host
133     while not shared.stopped():
134         time.sleep(1)
135     print "Server stopped"
136