openlp/openlp/core/api/websockets.py

127 lines
5.0 KiB
Python
Raw Normal View History

2016-06-05 05:49:27 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2017-12-29 09:15:48 +00:00
# Copyright (c) 2008-2018 OpenLP Developers #
2016-06-05 05:49:27 +00:00
# --------------------------------------------------------------------------- #
# 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; version 2 of the License. #
# #
# 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, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
"""
The :mod:`http` module contains the API web server. This is a lightweight web server used by remotes to interact
with OpenLP. It uses JSON to communicate with the remotes.
"""
import asyncio
2016-09-30 19:50:48 +00:00
import json
2016-06-05 05:49:27 +00:00
import logging
import time
from websockets import serve
2016-06-05 05:49:27 +00:00
2017-10-23 22:09:57 +00:00
from openlp.core.common.mixins import LogMixin, RegistryProperties
from openlp.core.common.registry import Registry
2017-10-07 07:05:07 +00:00
from openlp.core.common.settings import Settings
from openlp.core.threading import ThreadWorker, run_thread
2016-06-05 05:49:27 +00:00
log = logging.getLogger(__name__)
async def handle_websocket(request, path):
"""
Handle web socket requests and return the poll information.
Check ever 0.2 seconds to get the latest position and send if changed.
Only gets triggered when 1st client attaches
:param request: request from client
:param path: determines the endpoints supported
:return:
"""
log.debug('WebSocket handler registered with client')
previous_poll = None
previous_main_poll = None
poller = Registry().get('poller')
if path == '/state':
while True:
current_poll = poller.poll()
if current_poll != previous_poll:
await request.send(json.dumps(current_poll).encode())
previous_poll = current_poll
await asyncio.sleep(0.2)
elif path == '/live_changed':
while True:
main_poll = poller.main_poll()
if main_poll != previous_main_poll:
await request.send(main_poll)
previous_main_poll = main_poll
await asyncio.sleep(0.2)
class WebSocketWorker(ThreadWorker, RegistryProperties, LogMixin):
2016-06-05 05:49:27 +00:00
"""
2016-06-13 19:51:46 +00:00
A special Qt thread class to allow the WebSockets server to run at the same time as the UI.
2016-06-05 05:49:27 +00:00
"""
def start(self):
2016-06-05 05:49:27 +00:00
"""
Run the worker.
2016-06-05 05:49:27 +00:00
"""
address = Settings().value('api/ip address')
port = Settings().value('api/websocket port')
# Start the event loop
self.event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.event_loop)
# Create the websocker server
loop = 1
self.server = None
while not self.server:
try:
self.server = serve(handle_websocket, address, port)
log.debug('WebSocket server started on {addr}:{port}'.format(addr=address, port=port))
except Exception as e:
log.exception('Failed to start WebSocket server')
loop += 1
time.sleep(0.1)
if not self.server and loop > 3:
log.error('Unable to start WebSocket server {addr}:{port}, giving up'.format(addr=address, port=port))
if self.server:
# If the websocket server exists, start listening
self.event_loop.run_until_complete(self.server)
self.event_loop.run_forever()
self.quit.emit()
2016-06-05 05:49:27 +00:00
def stop(self):
"""
Stop the websocket server
"""
if hasattr(self.server, 'ws_server'):
self.server.ws_server.close()
elif hasattr(self.server, 'server'):
self.server.server.close()
self.event_loop.stop()
self.event_loop.close()
2016-06-05 05:49:27 +00:00
2017-10-23 22:09:57 +00:00
class WebSocketServer(RegistryProperties, LogMixin):
2016-06-05 05:49:27 +00:00
"""
Wrapper round a server instance
"""
2016-06-06 19:56:38 +00:00
def __init__(self):
2016-06-05 05:49:27 +00:00
"""
2016-06-16 20:50:01 +00:00
Initialise and start the WebSockets server
2016-06-05 05:49:27 +00:00
"""
2016-06-14 20:56:50 +00:00
super(WebSocketServer, self).__init__()
if not Registry().get_flag('no_web_server'):
worker = WebSocketWorker()
run_thread(worker, 'websocket_server')