使用avahi4j zeroconf发现运行相同服务的多台计算机

时间:2014-06-02 22:43:02

标签: java scala avahi

我尝试使用av​​ahi4j zeroconf查找提供相同服务的多台主机。在我的计算机上运行非常简单的测试时,我看不到新服务(IServiceBrowserCallback)和服务解析(IServiceResolverCallback)的任何回调。例如,我有以下代码(基本上只是从一些示例代码在线复制,再加上从javadoc读取apis):

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)

有谁知道为什么这些服务没有互相发现?难道我做错了什么?如果它有任何帮助,我可以看到多个服务,如果我给他们不同的名称(在这种情况下,我可以看到多个浏览事件 - 但是,如果我想,我似乎必须为每个名称创建一个新的解析器获取有关触发浏览器回调的每个服务的更多详细信息。)

0 个答案:

没有答案