I'm trying to use avahi4j zeroconf to find multiple hosts that offer the same service. When running a very simple test on my machine, I don't see any of the callbacks for new services (IServiceBrowserCallback
) nor for service resolution (IServiceResolverCallback
). For example, I have the following code (essentially just copied from some example code online, plus reading the apis from the javadoc):
object AvahiBootstapMain {
class Publisher(client: Client, group: EntryGroup) {
def close() = {
group.reset
client.release
}
}
def newPublisher(): Publisher = {
val client = new Client
client.start
val group: EntryGroup = client.createEntryGroup
val txtRecords = List("asdfasdf" + Math.random())
group.addService(Avahi4JConstants.AnyInterface,
Protocol.ANY,
"myName",
"_test._tcp",
null,
null,
1515,
txtRecords);
group.commit
new Publisher(client, group)
}
def newServiceBrowser(): Unit = {
val client = new Client
client.start
val browserCallback = new IServiceBrowserCallback {
def serviceCallback(interfaceNum: Int,
proto: Avahi4JConstants.Protocol,
browserEvent: Avahi4JConstants.BrowserEvent,
name: String,
typ: String,
domain: String,
lookupResultFlag: Int) = {
val args = Map(
"ifNum" -> interfaceNum,
"proto" -> proto,
"browserEvent" -> browserEvent,
"name" -> name,
"type" -> typ,
"domain" -> domain,
"lookupResultFlag" -> lookupResultFlag)
println(s"browserCallback: $args")
}
}
client.createServiceBrowser(
browserCallback,
Avahi4JConstants.AnyInterface,
Protocol.ANY,
"_test._tcp",
null,
0)
}
def newServiceResolver(): Unit = {
val client = new Client
client.start
val resolverCallback = new IServiceResolverCallback {
def resolverCallback(resolver: ServiceResolver,
interfaceNum: Int,
proto: Avahi4JConstants.Protocol,
resolverEvent: ServiceResolver.ServiceResolverEvent,
name: String,
typ: String,
domain: String,
hostname: String,
address: Address,
port: Int,
txtRecords: Array[String],
lookupResultFlag: Int) = {
val args = Map(
"ifNum" -> interfaceNum,
"proto" -> proto,
"resolverEvent" -> resolverEvent,
"name" -> name,
"type" -> typ,
"domain" -> domain,
"hostname" -> hostname,
"address" -> address,
"port" -> port,
"txtRecords" -> txtRecords.toList,
"lookupResultFlag" -> lookupResultFlag)
println(s"resolverCallback: $args")
}
}
client.createServiceResolver(
resolverCallback,
Avahi4JConstants.AnyInterface,
Protocol.ANY,
"myName",
"_test._tcp",
null,
Protocol.ANY,
0)
}
def main(args: Array[String]) {
val p = newPublisher()
newServiceBrowser()
newServiceResolver()
System.in.read()
p.close()
}
}
No matter how many concurrently running applications I have on my machine, the callbacks for service discovery are only called once:
Avahi4J v0.1-0
browserCallback: Map(name -> null, domain -> null, ifNum -> -1, proto -> ANY, lookupResultFlag -> 0, type -> _test._tcp, browserEvent -> CACHE_EXHAUSTED)
browserCallback: Map(name -> null, domain -> null, ifNum -> -1, proto -> ANY, lookupResultFlag -> 0, type -> _test._tcp, browserEvent -> NO_MORE)
browserCallback: Map(name -> myName, domain -> local, ifNum -> 2, proto -> INET6, lookupResultFlag -> 12, type -> _test._tcp, browserEvent -> NEW)
browserCallback: Map(name -> myName, domain -> local, ifNum -> 2, proto -> INET, lookupResultFlag -> 12, type -> _test._tcp, browserEvent -> NEW)
resolverCallback: Map(name -> myName, resolverEvent -> RESOLVER_FOUND, hostname -> ubuntu-2.local, domain -> local, ifNum -> 2, proto -> INET6, lookupResultFlag -> 13, port -> 1515, txtRecords -> List(asdfasdf0.6048590915795314), address -> IPv6: fe80::20c:29ff:fe7d:7c42, type -> _test._tcp)
Does anyone have any idea why these services are not discovering each other? Am I doing something wrong? If it's any help, I can see multiple services if I give them different names (in which case I can see multiple browse events -- however it seems I would have to create a new resolver for each name if I wanted to get more detailed information about each of the services for which the browser callback is fired).