示例#1
0
 /**
  * This method builds the test object.
  *
  * @throws Avahi4JException if there is an error creating or starting the Avahi4J {@link Client}.
  */
 public TestServicePublish() throws Avahi4JException {
   records = new Vector<String>();
   client = new Client(this);
   client.start();
   System.out.println("FQDN: " + client.getFQDN());
   System.out.println("Hostname: " + client.getHostName());
   System.out.println("domain name: " + client.getDomainName());
   System.out.println("state: " + client.getState());
 }
示例#2
0
  /**
   * This method creates the {@link EntryGroup}, adds some TXT records, a name, a service type and
   * port number. It then proceeds to publish the service. If there is a name conflict, it asks
   * Avahi4J for an alternate name and tries again.
   *
   * @throws Avahi4JException if there is an error creating the {@link EntryGroup}
   */
  public void addService() throws Avahi4JException {
    int result;

    // create group
    group = client.createEntryGroup(this);

    // create some fake TXT records
    records.add("record1=1");
    records.add("record2=2");

    // add service
    System.out.println("\n\nAdding new service to group");
    result =
        group.addService(
            Avahi4JConstants.AnyInterface,
            Protocol.ANY,
            "TestService",
            "_test._tcp",
            null,
            null,
            1515,
            records);
    if (result != Avahi4JConstants.AVAHI_OK) {
      System.out.println(
          "Error adding service to group: " + Avahi4JConstants.getErrorString(result));

      // try with an alternate name
      String newName = EntryGroup.findAlternativeServiceName("TestService");
      System.out.println("\n\nRe-trying with new service name: " + newName);
      result =
          group.addService(
              Avahi4JConstants.AnyInterface,
              Protocol.ANY,
              newName,
              "_test._tcp",
              null,
              null,
              1515,
              records);
      if (result != Avahi4JConstants.AVAHI_OK)
        System.out.println(
            "Error adding service to group: " + Avahi4JConstants.getErrorString(result));
    }

    // commit service
    System.out.println("Committing group");
    result = group.commit();
    if (result != Avahi4JConstants.AVAHI_OK)
      System.out.println("Error committing group: " + Avahi4JConstants.getErrorString(result));

    System.out.println("done");
  }
示例#3
0
 /** This method releases the {@link EntryGroup} and {@link Client} */
 public void stop() {
   group.release();
   client.stop();
   client.release();
 }