1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
#!/usr/bin/env python
# Copyright (C) 2012, Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import locale
import logging
from gettext import gettext as _
import active_document as ad
import restful_document as rd
import sugar_network_webui as webui
from sugar_network import bus as client
from active_toolkit import optparse, coroutine
from sugar_network_server import env, stats, application, documents
from sugar_network_server.mount import Mount
class Application(application.Daemon):
httpd = None
subscriber = None
def run(self):
ssl_args = {}
if env.keyfile.value:
ssl_args['keyfile'] = env.keyfile.value
if env.certfile.value:
ssl_args['certfile'] = env.certfile.value
volume = ad.SingleVolume(env.data_root.value, documents())
env.volume.update(volume)
mount = Mount(volume)
self.httpd = coroutine.WSGIServer((env.host.value, env.port.value),
rd.Router(mount), **ssl_args)
self.subscriber = rd.SubscribeSocket(volume,
env.host.value, env.subscribe_port.value)
def populate_index():
for cls in volume.values():
for __ in cls.populate():
coroutine.dispatch()
reindexer = coroutine.spawn(populate_index)
web_server = None
logging.info(_('Listening for requests on %s:%s'),
env.host.value, env.port.value)
if webui.webui.value:
# XXX Until implementing regular web users
from sugar_network import sugar
sugar.nickname = lambda: 'demo'
sugar.color = lambda: '#000000,#000000'
# Point client API to `self._mounts` directly passing over IPC
client.Request.connection = mount
host = (webui.webui_host.value, webui.webui_port.value)
logging.info(_('Start Web server on %s:%s port'), *host)
web_server = coroutine.WSGIServer(host, webui.get_app())
coroutine.spawn(web_server.serve_forever)
try:
coroutine.joinall([
coroutine.spawn(self.httpd.serve_forever),
coroutine.spawn(self.subscriber.serve_forever),
])
finally:
self.httpd.stop()
self.subscriber.stop()
reindexer.kill()
if web_server is not None:
web_server.stop()
volume.close()
def shutdown(self):
self.httpd.stop()
self.subscriber.stop()
locale.setlocale(locale.LC_ALL, '')
optparse.Option.seek('main', application)
optparse.Option.seek('webui', webui)
optparse.Option.seek('sugar-network-server', env)
optparse.Option.seek('stats', stats)
optparse.Option.seek('restful-document', rd)
optparse.Option.seek('active-document', ad)
app = Application(
name='sugar-network-server',
description=_('Sugar Network server'),
epilog=_('See http://wiki.sugarlabs.org/go/Sugar_Network ' \
'for details.'),
version=env.VERSION,
config_files=[
'/etc/sugar-network-server/config',
'~/.config/sugar-network-server/config',
])
app.start()
|