/** * 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"); }
/** This method resets (un-publishes) the service */ public void resetService() { int result; // reset group System.out.println("Resetting group"); result = group.reset(); if (result != Avahi4JConstants.AVAHI_OK) { System.out.println("Error resetting group: " + Avahi4JConstants.getErrorString(result)); } else { System.out.println("done"); } }
/** This method updates the TXT records of the previously created service */ public void updateService() throws Exception { int result; // TXT records Vector<String> records = new Vector<String>(); // update records records.add("record1=1"); records.add("UpdatedRecord2=NewUpdatedValue2"); System.out.println("\n\nUpdating service"); result = group.updateService( Avahi4JConstants.AnyInterface, Protocol.ANY, "TestService", "_test._tcp", null, records); if (result != Avahi4JConstants.AVAHI_OK) { System.out.println("Error updating service: " + Avahi4JConstants.getErrorString(result)); } else { System.out.println("done"); } }
/** This method releases the {@link EntryGroup} and {@link Client} */ public void stop() { group.release(); client.stop(); client.release(); }