directory for transports
[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
57
58 from transports.stratum_http import HttpServer
59 from transports.stratum_tcp import TcpServer
60 from transports.native import NativeServer
61
62
63 import irc 
64 import abe_backend 
65 from processor import Processor
66
67
68
69
70
71
72 if __name__ == '__main__':
73
74     if len(sys.argv)>1:
75         import jsonrpclib
76         server = jsonrpclib.Server('http://%s:8081'%config.get('server','host'))
77         cmd = sys.argv[1]
78         if cmd == 'stop':
79             out = server.stop(password)
80         else:
81             out = "Unknown command: '%s'" % cmd
82         print out
83         sys.exit(0)
84
85     processor = Processor()
86     shared = Shared()
87     # Bind shared to processor since constructor is user defined
88     processor.shared = shared
89     processor.start()
90
91     abe = abe_backend.AbeBackend(config, processor)
92     processor.register('blockchain', abe.process)
93
94     sb = irc.ServerBackend(config, processor)
95     processor.register('server', sb.process)
96
97     # dispatcher
98     dispatcher = Dispatcher(shared, processor)
99     dispatcher.start()
100
101     # Create various transports we need
102     host = config.get('server','host')
103     transports = [ NativeServer(shared, abe.store, sb.irc, config.get('server','banner'), host, 50000),
104                    TcpServer(shared, processor, host, 50001),
105                    HttpServer(shared, processor, host, 8081),
106                    ]
107     for server in transports:
108         server.start()
109
110     print "starting Electrum server on", host
111     while not shared.stopped():
112         time.sleep(1)
113     print "server stopped"
114