openlp/openlp/core/api/http/server.py

77 lines
2.9 KiB
Python
Raw Normal View History

2016-06-04 10:50:43 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2016 OpenLP Developers #
# --------------------------------------------------------------------------- #
# 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 #
###############################################################################
2016-06-05 21:16:13 +00:00
"""
2016-06-05 21:16:13 +00:00
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.
"""
2016-06-05 16:20:39 +00:00
import logging
2016-06-05 21:16:13 +00:00
from PyQt5 import QtCore
from waitress import serve
2016-06-14 22:03:08 +00:00
from openlp.core.api.http import application
2016-07-09 11:21:25 +00:00
from openlp.core.common import RegistryProperties, OpenLPMixin, Settings
2016-06-05 16:20:39 +00:00
log = logging.getLogger(__name__)
2016-06-04 10:50:43 +00:00
2016-06-14 22:03:08 +00:00
class HttpWorker(QtCore.QObject):
"""
2016-06-05 21:16:13 +00:00
A special Qt thread class to allow the HTTP server to run at the same time as the UI.
"""
def __init__(self):
"""
2016-06-05 21:16:13 +00:00
Constructor for the thread class.
2016-06-04 10:50:43 +00:00
2016-06-05 21:16:13 +00:00
:param server: The http server class.
"""
2016-06-16 20:50:01 +00:00
super(HttpWorker, self).__init__()
2016-06-04 10:50:43 +00:00
2016-06-16 20:50:01 +00:00
def run(self):
2016-06-04 10:50:43 +00:00
"""
2016-06-05 21:16:13 +00:00
Run the thread.
"""
2016-07-09 11:21:25 +00:00
address = Settings().value('api/ip address')
port = Settings().value('api/port')
serve(application, host=address, port=port)
2016-06-05 21:16:13 +00:00
def stop(self):
2016-06-06 19:56:38 +00:00
pass
2016-06-14 22:03:08 +00:00
class HttpServer(RegistryProperties, OpenLPMixin):
"""
2016-06-05 21:16:13 +00:00
Wrapper round a server instance
"""
def __init__(self):
"""
2016-06-13 19:51:46 +00:00
Initialise the http server, and start the http server
"""
2016-06-14 22:03:08 +00:00
super(HttpServer, self).__init__()
self.worker = HttpWorker()
2016-06-16 20:50:01 +00:00
self.thread = QtCore.QThread()
2016-06-06 19:56:38 +00:00
self.worker.moveToThread(self.thread)
2016-06-16 20:50:01 +00:00
self.thread.started.connect(self.worker.run)
2016-06-06 19:56:38 +00:00
self.thread.start()