-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_announcer.py
91 lines (75 loc) · 2.6 KB
/
service_announcer.py
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
# This file is a derivation of work on - and as such shares the same
# licence as - Linux Show Player
#
# Linux Show Player:
# Copyright 2012-2022 Francesco Ceruti <ceppofrancy@gmail.com>
#
# This file:
# Copyright 2022 s0600204
#
# Linux Show Player 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.
#
# Linux Show Player 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 Linux Show Player. If not, see <http://www.gnu.org/licenses/>.
import logging
import socket
from threading import Lock
from zeroconf import IPVersion, ServiceInfo, Zeroconf
from lisp.core.decorators import async_function
from lisp.core.util import get_lan_ip
from lisp.ui.ui_utils import translate
logger = logging.getLogger(__name__)
class QLabServiceAnnouncer:
ip_version = IPVersion.V4Only
service_type = "_qlab._tcp.local."
def __init__(self, port):
self._lock = Lock()
self._running = False
self._service = None
self._port = port
self._zconf_instance = Zeroconf(
ip_version=self.ip_version
)
self.build_service()
def build_service(self):
service_name = socket.gethostname()
self._service = ServiceInfo(
self.service_type,
f"{service_name} (Linux Show Player).{self.service_type}",
addresses=[socket.inet_pton(socket.AF_INET, get_lan_ip())],
port=self._port
)
@async_function
def start(self):
if self._running:
return
with self._lock:
self._zconf_instance.register_service(self._service)
self._running = True
logger.info(
translate(
"QLabAnnouncer", "Starting announcement of QLab service."
)
)
def stop(self):
if not self._running:
return
with self._lock:
self._running = False
self._zconf_instance.unregister_service(self._service)
logger.info(
translate(
"QLabAnnouncer", "Stopping announcement of QLab service."
)
)
def terminate(self):
self.stop()
self._zconf_instance.close()