本文整理匯總了Java中javax.jmdns.JmDNS.addServiceListener方法的典型用法代碼示例。如果您正苦於以下問題:Java JmDNS.addServiceListener方法的具體用法?Java JmDNS.addServiceListener怎麽用?Java JmDNS.addServiceListener使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jmdns.JmDNS
的用法示例。
在下文中一共展示了JmDNS.addServiceListener方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addServiceListener
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Override
public void addServiceListener(String type, ServiceListener listener) {
final String loType = type.toLowerCase();
List<ServiceListener> list = _serviceListeners.get(loType);
if (list == null) {
_serviceListeners.putIfAbsent(loType, new LinkedList<ServiceListener>());
list = _serviceListeners.get(loType);
}
if (list != null) {
synchronized (list) {
if (!list.contains(listener)) {
list.add(listener);
}
}
}
for (JmDNS mDNS : _knownMDNS.values()) {
mDNS.addServiceListener(type, listener);
}
}
示例2: addServiceListener
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Override
public void addServiceListener(String type, ServiceListener listener) {
final String loType = type.toLowerCase();
List<ServiceListener> list = _serviceListeners.get(loType);
if (list == null) {
_serviceListeners.putIfAbsent(loType, new LinkedList<ServiceListener>());
list = _serviceListeners.get(loType);
}
if (list != null) {
synchronized (list) {
if (!list.contains(listener)) {
list.add(listener);
}
}
}
for (JmDNS mDNS : this.getDNS()) {
mDNS.addServiceListener(type, listener);
}
}
示例3: ChromecastMgr
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
public ChromecastMgr(JmDNS mDNS) throws IOException {
this.mDNS = mDNS;
renderer = RendererConfiguration.getRendererConfigurationByName("Chromecast");
mDNS.addServiceListener(ChromeCast.SERVICE_TYPE, this);
chromeCasts = new ArrayList<>();
if (!PMS.getConfiguration().isChromecastDbg()) {
ch.qos.logback.classic.Logger logger =
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger("su.litvak.chromecast.api.v2");
logger.setLevel(Level.OFF);
}
}
示例4: main
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
public static void main(String[] argv) throws Exception{
JmDNS jmDNS = new JmDNSImpl(InetAddress.getLocalHost(), "karl");
jmDNS.addServiceTypeListener(new DiscoverServices.SampleListener());
jmDNS.addServiceListener(SocketAppender.ZONE, new DiscoverServices.SampleListener());
//String type, String name, String subtype, int port, int weight, int priority, boolean persistent, String text
// ServiceInfo serviceInfo = new ServiceInfoImpl("log4j", "log4j-agent", "log4j", 4561, 1, 1, true, "log4j-agent-text");
// jmDNS.registerService(serviceInfo);
// System.out.println(jmDNS);
}
示例5: addServiceListener
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Override
public void addServiceListener(String type, ServiceListener listener)
{
for (JmDNS mDNS : _knownMDNS.values())
{
mDNS.addServiceListener(type, listener);
}
}
示例6: testListenForServiceOnOtherRegistry
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testListenForServiceOnOtherRegistry() throws IOException {
System.out.println("Unit Test: testListenForServiceOnOtherRegistry()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
Capture<ServiceEvent> capServiceAddedEvent = new Capture<ServiceEvent>();
Capture<ServiceEvent> capServiceResolvedEvent = new Capture<ServiceEvent>();
// Expect the listener to be called once and capture the result
serviceListenerMock.serviceAdded(capture(capServiceAddedEvent));
serviceListenerMock.serviceResolved(capture(capServiceResolvedEvent));
replay(serviceListenerMock);
registry = JmDNS.create();
registry.addServiceListener(service.getType(), serviceListenerMock);
//
newServiceRegistry = JmDNS.create();
newServiceRegistry.registerService(service);
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
assertTrue("We did not get the service added event.", capServiceAddedEvent.hasCaptured());
ServiceInfo info = capServiceAddedEvent.getValue().getInfo();
assertEquals("We did not get the right name for the resolved service:", service.getName(), info.getName());
assertEquals("We did not get the right type for the resolved service:", service.getType(), info.getType());
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
assertTrue("We did not get the service resolved event.", capServiceResolvedEvent.hasCaptured());
verify(serviceListenerMock);
Object result = capServiceResolvedEvent.getValue().getInfo();
assertEquals("Did not get the expected service info: ", service, result);
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
}
}
示例7: main
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
try {
// Activate these lines to see log messages of JmDNS
boolean log = false;
if (log) {
Logger logger = Logger.getLogger(JmDNS.class.getName());
ConsoleHandler handler = new ConsoleHandler();
logger.addHandler(handler);
logger.setLevel(Level.FINER);
handler.setLevel(Level.FINER);
}
final JmDNS jmdns = JmDNS.create();
jmdns.addServiceListener("_http._tcp.local.", new SampleListener());
System.out.println("Press q and Enter, to quit");
int b;
while ((b = System.in.read()) != -1 && (char) b != 'q') {
/* Stub */
}
jmdns.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
示例8: testRenewExpiringRequests
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testRenewExpiringRequests() throws IOException, InterruptedException {
System.out.println("Unit Test: testRenewExpiringRequests()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
// To test for expiring TTL
DNSStateTask.setDefaultTTL(1 * 60);
registry = JmDNS.create("Listener");
registry.addServiceListener(service.getType(), serviceListenerMock);
//
newServiceRegistry = JmDNS.create("Registry");
newServiceRegistry.registerService(service);
List<ServiceEvent> servicesAdded = serviceListenerMock.servicesAdded();
assertTrue("We did not get the service added event.", servicesAdded.size() == 1);
ServiceInfo[] services = registry.list(service.getType());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
// wait for the TTL
Thread.sleep(2 * 60 * 1000);
services = registry.list(service.getType());
assertEquals("We should see the service after the renewal: ", 1, services.length);
assertEquals(service, services[0]);
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
DNSStateTask.setDefaultTTL(DNSConstants.DNS_TTL);
}
}
示例9: testSubtype
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testSubtype() throws IOException {
System.out.println("Unit Test: testSubtype()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
registry = JmDNS.create("Listener");
registry.addServiceListener(service.getType(), serviceListenerMock);
//
newServiceRegistry = JmDNS.create("Registry");
newServiceRegistry.registerService(printer);
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesAdded = serviceListenerMock.servicesAdded();
assertEquals("We did not get the service added event.", 1, servicesAdded.size());
ServiceInfo info = servicesAdded.get(servicesAdded.size() - 1).getInfo();
assertEquals("We did not get the right name for the resolved service:", printer.getName(), info.getName());
assertEquals("We did not get the right type for the resolved service:", printer.getType(), info.getType());
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service resolved event.", 1, servicesResolved.size());
ServiceInfo result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertNotNull("Did not get the expected service info: ", result);
assertEquals("Did not get the expected service info: ", printer, result);
assertEquals("Did not get the expected service info subtype: ", printer.getSubtype(), result.getSubtype());
assertEquals("Did not get the expected service info text: ", printer.getPropertyString(serviceKey), result.getPropertyString(serviceKey));
serviceListenerMock.reset();
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
}
}
示例10: testListenForMyServiceAndList
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testListenForMyServiceAndList() throws IOException {
System.out.println("Unit Test: testListenForMyServiceAndList()");
JmDNS registry = null;
try {
Capture<ServiceEvent> capServiceAddedEvent = new Capture<ServiceEvent>();
Capture<ServiceEvent> capServiceResolvedEvent = new Capture<ServiceEvent>();
// Expect the listener to be called once and capture the result
serviceListenerMock.serviceAdded(capture(capServiceAddedEvent));
serviceListenerMock.serviceResolved(capture(capServiceResolvedEvent));
replay(serviceListenerMock);
registry = JmDNS.create();
registry.addServiceListener(service.getType(), serviceListenerMock);
registry.registerService(service);
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
assertTrue("We did not get the service added event.", capServiceAddedEvent.hasCaptured());
ServiceInfo info = capServiceAddedEvent.getValue().getInfo();
assertEquals("We did not get the right name for the resolved service:", service.getName(), info.getName());
assertEquals("We did not get the right type for the resolved service:", service.getType(), info.getType());
// This will force the resolution of the service which in turn will get the listener called with a service resolved event.
// The info associated with a service resolved event has all the information available.
// Which in turn populates the ServiceInfo opbjects returned by JmDNS.list.
ServiceInfo[] services = registry.list(info.getType());
assertEquals("We did not get the expected number of services: ", 1, services.length);
assertEquals("The service returned was not the one expected", service, services[0]);
assertTrue("We did not get the service resolved event.", capServiceResolvedEvent.hasCaptured());
verify(serviceListenerMock);
ServiceInfo resolvedInfo = capServiceResolvedEvent.getValue().getInfo();
assertEquals("Did not get the expected service info: ", service, resolvedInfo);
} finally {
if (registry != null) registry.close();
}
}
示例11: addServiceListener
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Override
public void addServiceListener(String type, ServiceListener listener) {
for (JmDNS mDNS : _knownMDNS.values()) {
mDNS.addServiceListener(type, listener);
}
}
示例12: main
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
public static void main(String argv[]) throws IOException {
int argc = argv.length;
boolean debug = false;
InetAddress intf = null;
if ((argc > 0) && "-d".equals(argv[0])) {
System.arraycopy(argv, 1, argv, 0, --argc);
{
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.FINEST);
for (Enumeration<String> enumerator = LogManager.getLogManager().getLoggerNames(); enumerator.hasMoreElements();) {
String loggerName = enumerator.nextElement();
Logger logger = Logger.getLogger(loggerName);
logger.addHandler(handler);
logger.setLevel(Level.FINEST);
}
}
debug = true;
}
if ((argc > 1) && "-i".equals(argv[0])) {
intf = InetAddress.getByName(argv[1]);
System.arraycopy(argv, 2, argv, 0, argc -= 2);
}
if (intf == null) {
intf = InetAddress.getLocalHost();
}
JmDNS jmdns = JmDNS.create(intf, "Browser");
if ((argc == 0) || ((argc >= 1) && "-browse".equals(argv[0]))) {
new Browser(JmmDNS.Factory.getInstance());
for (int i = 2; i < argc; i++) {
jmdns.registerServiceType(argv[i]);
}
} else if ((argc == 1) && "-bt".equals(argv[0])) {
jmdns.addServiceTypeListener(new SampleListener());
} else if ((argc == 3) && "-bs".equals(argv[0])) {
jmdns.addServiceListener(argv[1] + "." + argv[2], new SampleListener());
} else if ((argc > 4) && "-rs".equals(argv[0])) {
String type = argv[2] + "." + argv[3];
String name = argv[1];
Hashtable<String, Object> props = null;
for (int i = 5; i < argc; i++) {
int j = argv[i].indexOf('=');
if (j < 0) {
throw new RuntimeException("not key=val: " + argv[i]);
}
if (props == null) {
props = new Hashtable<String, Object>();
}
props.put(argv[i].substring(0, j), argv[i].substring(j + 1));
}
jmdns.registerService(ServiceInfo.create(type, name, Integer.parseInt(argv[4]), 0, 0, props));
// This while loop keeps the main thread alive
while (true) {
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
break;
}
}
} else if ((argc == 2) && "-f".equals(argv[0])) {
new Responder(JmDNS.create(intf, "Responder"), argv[1]);
} else if (!debug) {
System.out.println();
System.out.println("jmdns:");
System.out.println(" -d - output debugging info");
System.out.println(" -i <addr> - specify the interface address");
System.out.println(" -browse [<type>...] - GUI browser (default)");
System.out.println(" -bt - browse service types");
System.out.println(" -bs <type> <domain> - browse services by type");
System.out.println(" -rs <name> <type> <domain> <port> <txt> - register service");
System.out.println(" -f <file> - rendezvous responder");
System.out.println();
System.exit(1);
}
}
示例13: testListenForTextUpdateOnOtherRegistry
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testListenForTextUpdateOnOtherRegistry() throws IOException, InterruptedException {
System.out.println("Unit Test: testListenForTextUpdateOnOtherRegistry()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
registry = JmDNS.create("Listener");
registry.addServiceListener(service.getType(), serviceListenerMock);
//
newServiceRegistry = JmDNS.create("Registry");
newServiceRegistry.registerService(service);
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesAdded = serviceListenerMock.servicesAdded();
assertEquals("We did not get the service added event.", 1, servicesAdded.size());
ServiceInfo info = servicesAdded.get(servicesAdded.size() - 1).getInfo();
assertEquals("We did not get the right name for the resolved service:", service.getName(), info.getName());
assertEquals("We did not get the right type for the resolved service:", service.getType(), info.getType());
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service resolved event.", 1, servicesResolved.size());
ServiceInfo result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertNotNull("Did not get the expected service info: ", result);
assertEquals("Did not get the expected service info: ", service, result);
assertEquals("Did not get the expected service info text: ", service.getPropertyString(serviceKey), result.getPropertyString(serviceKey));
serviceListenerMock.reset();
String text = "Test improbable web server";
Map<String, byte[]> properties = new HashMap<String, byte[]>();
properties.put(serviceKey, text.getBytes());
service.setText(properties);
Thread.sleep(3000);
servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service text updated event.", 1, servicesResolved.size());
result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertEquals("Did not get the expected service info text: ", text, result.getPropertyString(serviceKey));
serviceListenerMock.reset();
text = "Test more improbable web server";
properties = new HashMap<String, byte[]>();
properties.put(serviceKey, text.getBytes());
service.setText(properties);
Thread.sleep(3000);
servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service text updated event.", 1, servicesResolved.size());
result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertEquals("Did not get the expected service info text: ", text, result.getPropertyString(serviceKey));
serviceListenerMock.reset();
text = "Test even more improbable web server";
properties = new HashMap<String, byte[]>();
properties.put(serviceKey, text.getBytes());
service.setText(properties);
Thread.sleep(3000);
servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service text updated event.", 1, servicesResolved.size());
result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertEquals("Did not get the expected service info text: ", text, result.getPropertyString(serviceKey));
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
}
}
示例14: testRegisterEmptyTXTField
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testRegisterEmptyTXTField() throws IOException, InterruptedException {
System.out.println("Unit Test: testRegisterEmptyTXTField()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
registry = JmDNS.create("Listener");
registry.addServiceListener(service.getType(), serviceListenerMock);
//
newServiceRegistry = JmDNS.create("Registry");
newServiceRegistry.registerService(service);
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesAdded = serviceListenerMock.servicesAdded();
assertEquals("We did not get the service added event.", 1, servicesAdded.size());
ServiceInfo info = servicesAdded.get(servicesAdded.size() - 1).getInfo();
assertEquals("We did not get the right name for the resolved service:", service.getName(), info.getName());
assertEquals("We did not get the right type for the resolved service:", service.getType(), info.getType());
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service resolved event.", 1, servicesResolved.size());
ServiceInfo result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertNotNull("Did not get the expected service info: ", result);
assertEquals("Did not get the expected service info: ", service, result);
assertEquals("Did not get the expected service info text: ", service.getPropertyString(serviceKey), result.getPropertyString(serviceKey));
serviceListenerMock.reset();
Map<String, byte[]> properties = new HashMap<String, byte[]>();
service.setText(properties);
Thread.sleep(4000);
servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service text updated event.", 1, servicesResolved.size());
result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertNull("Did not get the expected service info text: ", result.getPropertyString(serviceKey));
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
}
}
示例15: testRegisterCaseSensitiveField
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testRegisterCaseSensitiveField() throws IOException {
System.out.println("Unit Test: testRegisterCaseSensitiveField()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
String text = "Test hypothetical Web Server";
Map<String, byte[]> properties = new HashMap<String, byte[]>();
properties.put(serviceKey, text.getBytes());
service = ServiceInfo.create("_Html._Tcp.local.", "Apache-SomeUniqueId", 80, 0, 0, true, properties);
registry = JmDNS.create("Listener");
registry.addServiceListener(service.getType(), serviceListenerMock);
//
newServiceRegistry = JmDNS.create("Registry");
newServiceRegistry.registerService(service);
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesAdded = serviceListenerMock.servicesAdded();
assertEquals("We did not get the service added event.", 1, servicesAdded.size());
ServiceInfo info = servicesAdded.get(servicesAdded.size() - 1).getInfo();
assertEquals("We did not get the right name for the resolved service:", service.getName(), info.getName());
assertEquals("We did not get the right type for the resolved service:", service.getType(), info.getType());
// We get the service added event when we register the service. However the service has not been resolved at this point.
// The info associated with the event only has the minimum information i.e. name and type.
List<ServiceEvent> servicesResolved = serviceListenerMock.servicesResolved();
assertEquals("We did not get the service resolved event.", 1, servicesResolved.size());
ServiceInfo result = servicesResolved.get(servicesResolved.size() - 1).getInfo();
assertNotNull("Did not get the expected service info: ", result);
assertEquals("Did not get the expected service info: ", service, result);
assertEquals("Did not get the expected service info text: ", service.getPropertyString(serviceKey), result.getPropertyString(serviceKey));
ServiceInfo[] infos = registry.list(service.getType());
assertEquals("We did not get the right list of service info.", 1, infos.length);
assertEquals("Did not get the expected service info: ", service, infos[0]);
assertEquals("Did not get the expected service info text: ", service.getPropertyString(serviceKey), infos[0].getPropertyString(serviceKey));
infos = registry.list(service.getType().toLowerCase());
assertEquals("We did not get the right list of service info.", 1, infos.length);
assertEquals("Did not get the expected service info: ", service, infos[0]);
assertEquals("Did not get the expected service info text: ", service.getPropertyString(serviceKey), infos[0].getPropertyString(serviceKey));
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
}
}