97a9773542cb60cd32d0bac65ea1889dca2ca948
[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, json, socket, operator, thread, ast, sys, re, traceback
19 import ConfigParser
20 from json import dumps, loads
21 import urllib
22 import threading
23
24 config = ConfigParser.ConfigParser()
25 # set some defaults, which will be overwritten by the config file
26 config.add_section('server')
27 config.set('server','banner', 'Welcome to Electrum!')
28 config.set('server', 'host', 'localhost')
29 config.set('server', 'port', '50000')
30 config.set('server', 'password', '')
31 config.set('server', 'irc', 'yes')
32 config.set('server', 'ircname', 'Electrum server')
33 config.add_section('database')
34 config.set('database', 'type', 'psycopg2')
35 config.set('database', 'database', 'abe')
36
37 try:
38     f = open('/etc/electrum.conf','r')
39     config.readfp(f)
40     f.close()
41 except:
42     print "Could not read electrum.conf. I will use the default values."
43
44 try:
45     f = open('/etc/electrum.banner','r')
46     config.set('server','banner', f.read())
47     f.close()
48 except:
49     pass
50
51
52 password = config.get('server','password')
53
54
55 from processor import Shared, Processor, Dispatcher
56 from stratum_http import HttpServer
57 from stratum import TcpServer
58 from native import NativeServer
59
60
61 import irc 
62 import abe_backend 
63 from processor import Processor
64
65
66
67
68
69
70 if __name__ == '__main__':
71
72     if len(sys.argv)>1:
73         import jsonrpclib
74         server = jsonrpclib.Server('http://%s:8081'%config.get('server','host'))
75         cmd = sys.argv[1]
76         if cmd == 'stop':
77             out = server.stop(password)
78         else:
79             out = "Unknown command: '%s'" % cmd
80         print out
81         sys.exit(0)
82
83     processor = Processor()
84     shared = Shared()
85     # Bind shared to processor since constructor is user defined
86     processor.shared = shared
87     processor.start()
88
89     abe = abe_backend.AbeBackend(config, processor)
90     processor.register('blockchain', abe.process)
91
92     sb = irc.ServerBackend(config, processor)
93     processor.register('server', sb.process)
94
95     # dispatcher
96     dispatcher = Dispatcher(shared, processor)
97     dispatcher.start()
98
99     # Create various transports we need
100     host = config.get('server','host')
101     transports = [ NativeServer(shared, abe.store, sb.irc, config.get('server','banner'), host, 50000),
102                    TcpServer(shared, processor, host, 50001),
103                    HttpServer(shared, processor, host, 8081),
104                    ]
105     for server in transports:
106         server.start()
107
108     print "starting Electrum server on", host
109     while not shared.stopped():
110         time.sleep(1)
111     print "server stopped"
112