Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.

public final class Discovery

  1. Object
  2. Discovery

Finds and is found on the local network over multicast DNS (Bonjour, DNS-SD): the same records the client’s BonjourBrowser and BonjourPublisher read and write, so a phone app discovers a device’s backend with no address configured, and a device finds a gateway the same way.

Discovery.Advertisement ad = Discovery.advertise("Hall Thermostat", "_thermo._tcp", 8080,
        Collections.singletonMap("api", "/v1"));
Discovery.browse("_gateway._tcp", new Discovery.Listener() {
    public void found(Discovery.Service s) { connect(s.getHost(), s.getPort()); }
    public void lost(Discovery.Service s) { }
});

The protocol runs on one daemon thread, and listeners are called on it: a server has no event dispatch thread, so a listener that does real work hands it to a task. The thread exits when nothing is advertised or browsed. A name another host already uses gets a numeric suffix, as Bonjour does; Advertisement.getName() says which name was taken. IPv4 only.

Nested types

interface Discovery.ListenerReceives what a browse(String, Listener) finds.
class Discovery.ServiceA service seen on the network.
class Discovery.AdvertisementA running advertisement.
class Discovery.BrowseA running browse.

Methods

public static Discovery.Advertisement advertise(String name, String type, int port, Map<String, String> txt)Advertises a service on port.
public static Discovery.Browse browse(String type, Discovery.Listener listener)Browses for a type until Browse.close().

Inherited methods

Method details

advertise

public static Discovery.Advertisement advertise(String name, String type, int port, Map<String, String> txt)
Advertises a service on port. type is _name._tcp or _name._udp; txt may be null.

browse

public static Discovery.Browse browse(String type, Discovery.Listener listener)
Browses for a type until Browse.close().