-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v0.3-freeze' into devel
- Loading branch information
Showing
7 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
Local network discovery | ||
======================= | ||
|
||
General information | ||
------------------- | ||
|
||
Starting from v0.3 of the platform all everpl instances (everpl hubs) | ||
are able to be discovered in a local network by default. | ||
|
||
Hubs announce their presence and can be discovered using a Zeroconf | ||
(Avahi/Bonjour) protocol - Zero Configuration Networking protocol. | ||
This protocol allows services to announce their presence in the system, | ||
to assign constant domain names in a ".local" domain zone, to resolve | ||
such domain names and to look for a specific service in the system. | ||
|
||
For more information about Zeroconf you can read an article on Medium | ||
titled `"Bonjour Android, it’s Zeroconf"`_. It tells about Zeroconf | ||
protocols in general, about Bonjour/Avahi approach and how it relates | ||
with client applications and service discovery. | ||
|
||
.. _`"Bonjour Android, it’s Zeroconf"`: | ||
https://medium.com/@_tiwiz/bonjour-android-its-zeroconf-8e3d3fde760e | ||
|
||
Unfortunately, Zeroconf (and UDP multicast in general) isn't supported | ||
by modern web browsers. | ||
|
||
For more detailed information see: | ||
|
||
- DNS-SD protocol website: `<http://www.dns-sd.org/>`_, covers service | ||
discovery part of functionality; | ||
- mDNS protocol website: `<http://www.multicastdns.org/>`_, covers | ||
domain name association in ".local" domain zone; | ||
- and corresponding RFCs. | ||
|
||
For testing purposes you can use such handy tools as: | ||
|
||
- Avahi-Discover GUI utility for Linux: | ||
https://linux.die.net/man/1/avahi-discover | ||
- Service browser for Android: | ||
https://play.google.com/store/apps/details?id=com.druk.servicebrowser&hl=en_US | ||
- dns-sd CLI tool for macOS | ||
|
||
How to discover an everpl hub | ||
----------------------------- | ||
|
||
In order to discover an everpl hub you need to use one of the Zeroconf | ||
libraries (like build-in NSD for Android) and search for a service type | ||
``_everpl._tcp``. By default such devices will have a name defined as | ||
"everpl hub @ hostname". To access an everpl REST API on a device you | ||
can use name and port, defined in Hostname (Server) and Port fields of | ||
a discovery response correspondingly. | ||
|
||
Here is an example of a complete discovery response (as displayed by | ||
console avahi-browse utility): | ||
|
||
:: | ||
|
||
= virbr0 IPv4 everpl hub @ hostname_was_here _everpl._tcp local | ||
hostname = [hostname_was_here.local] | ||
address = [192.168.20.1] | ||
port = [10800] | ||
txt = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
""" | ||
This module contains an implementation of everpl announcement logic for | ||
local networks | ||
""" | ||
|
||
import socket | ||
|
||
import zeroconf | ||
|
||
|
||
class LocalAnnounce(object): | ||
""" | ||
This class controls the announcement of everpl instance presence in the | ||
local network using Zeroconf (Avahi / Bonjour) protocol | ||
""" | ||
_service_type = "_everpl._tcp.local." | ||
|
||
def __init__(self): | ||
""" | ||
Initializes Zeroconf instance and starts corresponding threads | ||
""" | ||
self._zeroconf = zeroconf.Zeroconf() | ||
|
||
def create_server( | ||
self, instance_name: str = None, | ||
address: str = "127.0.0.1", port: str = 10800, | ||
server_host: str = None | ||
) -> None: | ||
""" | ||
Initializes zeroconf service (everpl hub) announcement with the | ||
specified optional params | ||
:param instance_name: the name of this service (instance, hub); | ||
set to the "everpl hub @ hostname._everpl.tcp" by default | ||
:param address: an IP address of this instance; set to the 127.0.0.1 | ||
by default | ||
:param port: the port of announced service; set to the 10800 by default | ||
:param server_host: hostname of this instance; set to the | ||
"hostname.local" by if not set specifically (if server_host is | ||
None or is equal to the "0.0.0.0" address) | ||
:return: None | ||
""" | ||
hostname = socket.gethostname() | ||
|
||
if instance_name is None: | ||
instance_name = "everpl hub @ %s.%s" % ( | ||
hostname, self._service_type | ||
) | ||
|
||
coded_address = socket.inet_aton(address) | ||
|
||
if server_host is None or server_host == "0.0.0.0": | ||
server_host = "%s.local" % hostname | ||
|
||
txt_properties = {} | ||
|
||
service_info = zeroconf.ServiceInfo( | ||
type_=self._service_type, | ||
name=instance_name, | ||
address=coded_address, | ||
port=port, | ||
properties=txt_properties, | ||
server=server_host | ||
) | ||
|
||
self._zeroconf.register_service(service_info) | ||
|
||
def shutdown_server(self) -> None: | ||
""" | ||
Unregisters everpl zeroconf service. Stops announcement of this hub | ||
:return: None | ||
""" | ||
self._zeroconf.unregister_all_services() | ||
self._zeroconf.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters