MCPcopy Index your code
hub / github.com/faceless2/zeroconf

github.com/faceless2/zeroconf @1.0.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.0.3 ↗ · + Follow
178 symbols 462 edges 7 files 88 documented · 49%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Zeroconf

Zeroconf is a simple Java implementation of Multicast DNS Service Discovery, aka the service discovery bit of Zeroconf. Originally written as a quick hack to avoid having to use https://github.com/jmdns/jmdns, it has evolved into something that can both announce and listen for Services:

Here's a simple example which announces a service on all interfaces on the local machine:

import com.bfo.zeroconf.*;

Zeroconf zc = new Zeroconf();
Service service = new Service.Builder()
                    .setName("MyWeb")
                    .setType("_http._tcp")
                    .setPort(8080)
                    .put("path", "/path/to/service")
                    .build(zc);
service.announce();
// time passes
service.cancel();
// time passes
zc.close();

To set custom TTLs for each mDNS record type:

import com.bfo.zeroconf.*;

Zeroconf zc = new Zeroconf();
Service.Builder builder = new Service.Builder()
                    .setName("MyWeb")
                    .setType("_http._tcp")
                    .setPort(8080)
                    .put("path", "/path/to/service");

// Custom TTLs for each mDNS record type.
int ttl = 120;
builder.setTTL_PTR(ttl);
builder.setTTL_SRV(ttl);
builder.setTTL_TXT(ttl);
builder.setTTL_A(ttl);
builder.build(zc);

// Announce the service.
service.announce();

And to listen, either add a Listener for events or use the live, thread-safe Collection of Services.

import com.bfo.zeroconf.*;

Zeroconf zc = new Zeroconf();
zc.addListener(new ZeroconfListener() {
    public void serviceNamed(String type, String name) {
        if ("_http._tcp".equals(type)) {
            zc.query(type, name);  // Ask for details on any announced HTTP services
        }
    }
    public void serviceAnnounced(Service service) {
        // A new service has just been announced
    }
});

zc.query("_http._tcp", null); // Ask for any HTTP services

// time passes
for (Service s : zc.getServices()) {
  if (s.getType().equals("_http._tcp") {
     // A service has been announced at some point in the past, and has not yet expired.
   }
}

// time passes
zc.close();

To disable IPv6 support, and to only listen on a single network interface with a given IP address, use:

Zeroconf zc = new Zeroconf();
zc.setIPv6(false);
zc.setLocalHostName(ip);

// Add the single NIC with the given IP.
NetworkInterface nic = NetworkUtils.findIPv4NICForIP(ip);
if (nic != null) {
    // Remove all configured NICs.
    zc.getNetworkInterfaces().clear();

    // Add the IPv4 NIC.
    zc.getNetworkInterfaces().add(nic);
}

To build

Run ant to build the Jar in the build directory, and javadoc in the doc directory.

Extension points exported contracts — how you extend this code

ZeroconfListener (Interface)
An interface that can be added to the Zeroconf class to be notified about events. All methods on the interface have a de [1 …
src/main/com/bfo/zeroconf/ZeroconfListener.java

Core symbols most depended-on inside this repo

add
called by 35
src/main/com/bfo/zeroconf/Zeroconf.java
getType
called by 32
src/main/com/bfo/zeroconf/Service.java
toString
called by 25
src/main/com/bfo/zeroconf/Stringify.java
log
called by 23
src/main/com/bfo/zeroconf/Zeroconf.java
put
called by 22
src/main/com/bfo/zeroconf/Service.java
size
called by 21
src/main/com/bfo/zeroconf/Zeroconf.java
getName
called by 20
src/main/com/bfo/zeroconf/Service.java
remove
called by 19
src/main/com/bfo/zeroconf/Zeroconf.java

Shape

Method 166
Class 11
Interface 1

Languages

Java100%

Modules by API surface

src/main/com/bfo/zeroconf/Zeroconf.java58 symbols
src/main/com/bfo/zeroconf/Service.java47 symbols
src/main/com/bfo/zeroconf/Record.java29 symbols
src/test/com/bfo/zeroconf/Test.java14 symbols
src/main/com/bfo/zeroconf/Packet.java14 symbols
src/main/com/bfo/zeroconf/ZeroconfListener.java12 symbols
src/main/com/bfo/zeroconf/Stringify.java4 symbols

For agents

$ claude mcp add zeroconf \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact