← Back to team overview

openlp-core team mailing list archive

Re: [Merge] lp:~raoul-snyman/openlp/zeroconf into lp:openlp

 

See inline

Diff comments:

> === added file 'openlp/core/api/zeroconf.py'
> --- openlp/core/api/zeroconf.py	1970-01-01 00:00:00 +0000
> +++ openlp/core/api/zeroconf.py	2019-07-01 22:53:54 +0000
> @@ -0,0 +1,101 @@
> +# -*- coding: utf-8 -*-
> +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
> +
> +##########################################################################
> +# OpenLP - Open Source Lyrics Projection                                 #
> +# ---------------------------------------------------------------------- #
> +# Copyright (c) 2008-2019 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, 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 <https://www.gnu.org/licenses/>. #
> +##########################################################################
> +"""
> +The :mod:`~openlp.core.api.zeroconf` module runs a Zerconf server so that OpenLP can advertise the
> +RESTful API for devices on the network to discover.
> +"""
> +import socket
> +from time import sleep
> +
> +from zeroconf import ServiceInfo, Zeroconf
> +
> +from openlp.core.common import get_local_ip4
> +from openlp.core.common.registry import Registry
> +from openlp.core.common.settings import Settings
> +from openlp.core.threading import ThreadWorker, run_thread
> +
> +
> +class ZeroconfWorker(ThreadWorker):
> +    """
> +    This thread worker runs a Zeroconf service
> +    """
> +    address = None
> +    http_port = 4316
> +    ws_port = 4317
> +    _can_run = False
> +
> +    def __init__(self, ip_address, http_port=4316, ws_port=4317):
> +        """
> +        Create the worker for the Zeroconf service
> +        """
> +        super().__init__()
> +        self.address = socket.inet_aton(ip_address)
> +        self.http_port = http_port
> +        self.ws_port = ws_port
> +
> +    def can_run(self):
> +        """
> +        Check if the worker can continue to run. This is mostly so that we can override this method
> +        and test the class.
> +        """
> +        return self._can_run
> +
> +    def start(self):
> +        """
> +        Start the service
> +        """
> +        http_info = ServiceInfo('_http._tcp.local.', 'OpenLP._http._tcp.local.',
> +                                address=self.address, port=self.http_port, properties={})
> +        ws_info = ServiceInfo('_ws._tcp.local.', 'OpenLP._ws._tcp.local.',
> +                              address=self.address, port=self.ws_port, properties={})
> +        zc = Zeroconf()
> +        zc.register_service(http_info)
> +        zc.register_service(ws_info)
> +        self._can_run = True
> +        while self.can_run():
> +            sleep(0.1)
> +        zc.unregister_service(http_info)
> +        zc.unregister_service(ws_info)
> +        zc.close()
> +
> +    def stop(self):
> +        """
> +        Stop the service
> +        """
> +        self._can_run = False
> +
> +
> +def start_zeroconf():
> +    """
> +    Start the Zeroconf service
> +    """
> +    # When we're running tests, just skip this set up if this flag is set
> +    if Registry().get_flag('no_web_server'):
> +        return
> +    ifaces = get_local_ip4()
> +    for key in iter(ifaces):
> +        address = ifaces.get(key)['ip']
> +        break

My computer:
{
  'wlp3s0': {'ip': '192.168.1.179', 'broadcast': '192.168.1.255', 'netmask': '255.255.255.0', 'prefix': 24, 'localnet': '192.168.1.0'},
  'docker0': {'ip': '172.17.0.1', 'broadcast': '172.17.255.255', 'netmask': '255.255.0.0', 'prefix': 16, 'localnet': '172.17.0.0'},
  'tun0': {'ip': '10.3.117.251', 'broadcast': '10.3.119.255', 'netmask': '255.255.252.0', 'prefix': 22, 'localnet': '10.3.116.0'}
}
The first one is definitely the only valid one.

> +    http_port = Settings().value('api/port')
> +    ws_port = Settings().value('api/websocket port')
> +    worker = ZeroconfWorker(address, http_port, ws_port)
> +    run_thread(worker, 'api_zeroconf')


-- 
https://code.launchpad.net/~raoul-snyman/openlp/zeroconf/+merge/369554
Your team OpenLP Core is subscribed to branch lp:openlp.


References