2013-02-27 18:22:24 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import posixpath
|
|
|
|
from urllib import unquote
|
|
|
|
|
2013-09-25 10:01:01 +02:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.handlers.wsgi import WSGIHandler as Django_WSGIHandler
|
|
|
|
from django.utils.translation import ugettext as _
|
2013-10-26 10:42:48 +02:00
|
|
|
from sockjs.tornado import SockJSRouter, SockJSConnection
|
2013-02-27 18:22:24 +01:00
|
|
|
from tornado.httpserver import HTTPServer
|
|
|
|
from tornado.ioloop import IOLoop
|
2013-09-25 10:01:01 +02:00
|
|
|
from tornado.options import parse_command_line
|
|
|
|
from tornado.web import Application, FallbackHandler, StaticFileHandler
|
2013-02-27 18:22:24 +01:00
|
|
|
from tornado.wsgi import WSGIContainer
|
|
|
|
|
|
|
|
|
2013-02-16 16:19:20 +01:00
|
|
|
class DjangoStaticFileHandler(StaticFileHandler):
|
2013-02-27 18:22:24 +01:00
|
|
|
"""Handels static data by using the django finders."""
|
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
"""Overwrite some attributes."""
|
|
|
|
self.root = ''
|
|
|
|
self.default_filename = None
|
|
|
|
|
|
|
|
def get(self, path, include_body=True):
|
|
|
|
from django.contrib.staticfiles import finders
|
|
|
|
normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
|
|
|
|
absolute_path = finders.find(normalized_path)
|
2013-02-16 16:19:20 +01:00
|
|
|
return super(DjangoStaticFileHandler, self).get(absolute_path, include_body)
|
2013-02-27 18:22:24 +01:00
|
|
|
|
|
|
|
|
2013-10-26 10:42:48 +02:00
|
|
|
class ProjectorSocketHandler(SockJSConnection):
|
2013-10-03 21:49:51 +02:00
|
|
|
"""
|
|
|
|
Handels the websocket for the projector.
|
|
|
|
"""
|
2013-08-04 12:59:11 +02:00
|
|
|
waiters = set()
|
|
|
|
|
2013-10-26 10:42:48 +02:00
|
|
|
def on_open(self, info):
|
2013-08-04 12:59:11 +02:00
|
|
|
ProjectorSocketHandler.waiters.add(self)
|
|
|
|
|
|
|
|
def on_close(self):
|
|
|
|
ProjectorSocketHandler.waiters.remove(self)
|
|
|
|
|
|
|
|
@classmethod
|
2013-10-17 15:32:54 +02:00
|
|
|
def send_updates(cls, data):
|
2013-08-04 12:59:11 +02:00
|
|
|
for waiter in cls.waiters:
|
2013-10-26 10:42:48 +02:00
|
|
|
waiter.send(data)
|
2013-08-04 12:59:11 +02:00
|
|
|
|
|
|
|
|
2013-03-27 15:53:31 +01:00
|
|
|
def run_tornado(addr, port, reload=False):
|
|
|
|
# Don't try to read the command line args from openslides
|
|
|
|
parse_command_line(args=[])
|
|
|
|
|
2013-06-13 16:30:07 +02:00
|
|
|
# Print listening address and port to command line
|
|
|
|
if addr == '0.0.0.0':
|
|
|
|
url_string = _("the machine's local ip address")
|
|
|
|
else:
|
|
|
|
url_string = 'http://%s:%s' % (addr, port)
|
|
|
|
print _("Starting OpenSlides' tornado webserver listening to %(url_string)s") % {'url_string': url_string}
|
|
|
|
|
2013-10-26 10:42:48 +02:00
|
|
|
socket_js_router = SockJSRouter(ProjectorSocketHandler, '/projector/socket')
|
|
|
|
|
2013-03-27 15:53:31 +01:00
|
|
|
# Start the application
|
2013-02-27 18:22:24 +01:00
|
|
|
app = WSGIContainer(Django_WSGIHandler())
|
2013-10-26 10:42:48 +02:00
|
|
|
tornado_app = Application(socket_js_router.urls + [
|
2013-02-16 16:19:20 +01:00
|
|
|
(r"%s(.*)" % settings.STATIC_URL, DjangoStaticFileHandler),
|
|
|
|
(r'%s(.*)' % settings.MEDIA_URL, StaticFileHandler, {'path': settings.MEDIA_ROOT}),
|
2013-08-04 12:59:11 +02:00
|
|
|
('.*', FallbackHandler, dict(fallback=app))
|
|
|
|
], debug=reload)
|
2013-02-27 18:22:24 +01:00
|
|
|
|
|
|
|
server = HTTPServer(tornado_app)
|
|
|
|
server.listen(port=port,
|
|
|
|
address=addr)
|
|
|
|
IOLoop.instance().start()
|