Crates.io | udis |
lib.rs | udis |
version | 0.1.3 |
source | src |
created_at | 2024-09-22 16:08:09.772002 |
updated_at | 2024-09-22 16:37:28.986308 |
description | A tiny local service discovery system |
homepage | https://github.com/duncanrhamill/udis |
repository | https://github.com/duncanrhamill/udis |
max_upload_size | |
id | 1383085 |
size | 52,224 |
udis
- a tiny local service discovery systemThere are many service discovery options available out there, but when it comes to local service discovery we mainly reach for mDNS. In most cases this is great, but sometimes it's nice just to have a really simple solution that isn't managed by the OS in any way.
udis
(micro discovery) is a dead simple system for providing local service
discovery with minimal fuss. The main use case is:
To find servers on the local network that host a kind of service on a particular port.
To achieve this, udis
uses UDP multicast to send notifications to a
"discovery network" - basically anyone who's listening - which include some
very basic information:
Each endpoint in the network will actively listen for notifications, and if it receives one it's interested in (e.g. a server receives a notification from a client looking for one of its services) the endpoint will re-notify the network of its presence. This means the order in which endpoints join the network doesn't matter.
Once an endpoint has found a service its interested in it's the users job to continue setting up communications, e.g. using the IP address and port number to connect their own socket.
Create a server which hosts a "hello" service on port 4112:
let udis = udis::Udis::new("server")
.host("hello", 4112)
.expect("Kind or port already hosted by this endpoint")
.build_sync()
.expect("Failed to build udis endpoint");
And then create a client which searches for the "hello" service:
let udis = udis::Udis::new("client")
.search("hello")
.build_sync()
.expect("Failed to build udis endpoint");
let service = udis.find_service().expect("Failed to find an endpoint with the `hello` service");
// prints: Found `hello` service hosted by `server` at 192.168.0.1:4112
println!(
"Found `{}` service hosted by `{}` at {}:{}",
service.kind,
service.name,
service.addr,
service.port);
Only the client
side receives notifications of services its interested in, so
the job of opening communications goes to the side that searched for the
service, not the service hoster.
For a complete example see the examples/client.rs
and
examples/server.rs
example files.
Under the hood the SyncUdis
endpoint type is implemented with a background
thread that communicates to the main thread with channels.
udis
supports async with the tokio
runtime, which can be enabled with the
tokio
feature. When enabled you can use the build_async
function on the
udis::builder::Builder
struct which creates a tokio task rather than a thread.
The udis notification packet is a simple JSON one, for example a server hosting a service of the kind "hello" on port 4112 might send this packet out:
{
"name": "server",
"addr": "192.168.0.1",
"services": [
{
"Host": {
"kind": "hello",
"port": 4112
}
}
]
}