我正在尝试使用 avahi4j zeroconf 来查找提供相同服务的多个主机。在我的机器上运行一个非常简单的测试时,我看不到任何新服务 ( IServiceBrowserCallback
) 或服务解析 ( IServiceResolverCallback
) 的回调。例如,我有以下代码(基本上只是从一些示例代码在线复制,加上从 javadoc 中读取 api):
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()
}
}
无论我的机器上有多少并发运行的应用程序,服务发现的回调只被调用一次:
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)
有谁知道为什么这些服务没有发现彼此?难道我做错了什么?如果有任何帮助,如果我给它们提供不同的名称,我可以看到多个服务(在这种情况下,我可以看到多个浏览事件——但是,如果我想获得更多详细信息,我似乎必须为每个名称创建一个新的解析器关于触发浏览器回调的每个服务)。