本文整理匯總了Java中javax.jmdns.JmDNS.list方法的典型用法代碼示例。如果您正苦於以下問題:Java JmDNS.list方法的具體用法?Java JmDNS.list怎麽用?Java JmDNS.list使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jmdns.JmDNS
的用法示例。
在下文中一共展示了JmDNS.list方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: search
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
/**
* Search for airplay devices within the given timeout, in ms.
*/
public List<AirPlay> search(int timeout) throws IOException {
JmDNS jmdns = JmDNS.create(InetAddress.getByName("0.0.0.0"), null);
ServiceInfo[] services = jmdns.list(DNSSD_TYPE, timeout);
List<AirPlay> airplays = new ArrayList<AirPlay>();
for(int i = 0; i < services.length; i++) {
ServiceInfo service = services[i];
Inet4Address[] addresses = service.getInet4Addresses();
if(addresses.length >= 1) {
airplays.add(new AirPlay(service.getName(),
addresses[0].getHostAddress(),
service.getPort()));
}
}
jmdns.close();
return airplays;
}
示例2: testUnregisterService
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testUnregisterService() throws IOException, InterruptedException {
System.out.println("Unit Test: testUnregisterService()");
JmDNS registry = null;
try {
registry = JmDNS.create();
registry.registerService(service);
ServiceInfo[] services = registry.list(service.getType());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
// now unregister and make sure it's gone
registry.unregisterService(services[0]);
// According to the spec the record disappears from the cache 1s after it has been unregistered
// without sleeping for a while, the service would not be unregistered fully
Thread.sleep(1500);
services = registry.list(service.getType());
assertTrue("We should not see the service we just unregistered: ", services == null || services.length == 0);
} finally {
if (registry != null) registry.close();
}
}
示例3: testListMyServiceIPV6
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testListMyServiceIPV6() throws IOException {
System.out.println("Unit Test: testListMyServiceIPV6()");
JmDNS registry = null;
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface interfaze = NetworkInterface.getByInetAddress(address);
for (Enumeration<InetAddress> iaenum = interfaze.getInetAddresses(); iaenum.hasMoreElements();) {
InetAddress interfaceAddress = iaenum.nextElement();
if (interfaceAddress instanceof Inet6Address) {
address = interfaceAddress;
}
}
registry = JmDNS.create(address);
registry.registerService(service);
ServiceInfo[] services = registry.list(service.getType());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
} finally {
if (registry != null) registry.close();
}
}
示例4: __discoverIOTDevices
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
private static List<IOTAddress> __discoverIOTDevices()
{
log.debug("__discoverIOTDevices() entrance");
try
{
final JmDNS mdnsService = JmDNS.create();
log.debug("__discoverIOTDevices() JmDNS.create() finished");
ServiceInfo[] serviceInfoArray = mdnsService.list(IOT_SERVICE_TYPE, timeout);
List<IOTAddress> iotAddressList = __parseSeviceInfoArray2IOTAddressList(serviceInfoArray);
// close the JmDNS asyn, for close() will take too much time, so we close it asyn
EspBaseApiUtil.submit(new Runnable()
{
@Override
public void run()
{
try
{
mdnsService.close();
}
catch (IOException igrnoeException)
{
}
}
});
return iotAddressList;
}
catch (IOException e)
{
e.printStackTrace();
}
log.debug("__discoverIOTDevices() exit with emptyList");
return Collections.emptyList();
}
示例5: 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);
}
}
示例6: testRegisterService
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testRegisterService() throws IOException {
System.out.println("Unit Test: testRegisterService()");
JmDNS registry = null;
try {
registry = JmDNS.create();
registry.registerService(service);
ServiceInfo[] services = registry.list(service.getType());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
} finally {
if (registry != null) registry.close();
}
}
示例7: testUnregisterAndReregisterService
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testUnregisterAndReregisterService() throws IOException, InterruptedException {
System.out.println("Unit Test: testUnregisterAndReregisterService()");
JmDNS registry = null;
try {
registry = JmDNS.create();
registry.registerService(service);
ServiceInfo[] services = registry.list(service.getType());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
// now unregister and make sure it's gone
registry.unregisterService(services[0]);
// According to the spec the record disappears from the cache 1s after it has been unregistered
// without sleeping for a while, the service would not be unregistered fully
Thread.sleep(1500);
services = registry.list(service.getType());
assertTrue("We should not see the service we just unregistered: ", services == null || services.length == 0);
registry.registerService(service);
Thread.sleep(5000);
services = registry.list(service.getType());
assertTrue("We should see the service we just reregistered: ", services != null && services.length > 0);
} finally {
if (registry != null) registry.close();
}
}
示例8: testListMyService
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testListMyService() throws IOException {
System.out.println("Unit Test: testListMyService()");
JmDNS registry = null;
try {
registry = JmDNS.create();
registry.registerService(service);
ServiceInfo[] services = registry.list(service.getType());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
} finally {
if (registry != null) registry.close();
}
}
示例9: 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();
}
}
示例10: testRegisterAndListServiceOnOtherRegistry
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testRegisterAndListServiceOnOtherRegistry() throws IOException, InterruptedException {
System.out.println("Unit Test: testRegisterAndListServiceOnOtherRegistry()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
registry = JmDNS.create("Registry");
registry.registerService(service);
newServiceRegistry = JmDNS.create("Listener");
Thread.sleep(6000);
ServiceInfo[] fetchedServices = newServiceRegistry.list(service.getType());
assertEquals("Did not get the expected services listed:", 1, fetchedServices.length);
assertEquals("Did not get the expected service type:", service.getType(), fetchedServices[0].getType());
assertEquals("Did not get the expected service name:", service.getName(), fetchedServices[0].getName());
assertEquals("Did not get the expected service fully qualified name:", service.getQualifiedName(), fetchedServices[0].getQualifiedName());
newServiceRegistry.getServiceInfo(service.getType(), service.getName());
assertEquals("Did not get the expected service info: ", service, fetchedServices[0]);
registry.close();
registry = null;
// According to the spec the record disappears from the cache 1s after it has been unregistered
// without sleeping for a while, the service would not be unregistered fully
Thread.sleep(1500);
fetchedServices = newServiceRegistry.list(service.getType());
assertEquals("The service was not cancelled after the close:", 0, fetchedServices.length);
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
}
}
示例11: testListMyServiceWithToLowerCase
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testListMyServiceWithToLowerCase() throws IOException, InterruptedException {
System.out.println("Unit Test: testListMyServiceWithToLowerCase()");
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);
JmDNS registry = null;
try {
registry = JmDNS.create();
registry.registerService(service);
// with toLowerCase
ServiceInfo[] services = registry.list(service.getType().toLowerCase());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
// now unregister and make sure it's gone
registry.unregisterService(services[0]);
// According to the spec the record disappears from the cache 1s after it has been unregistered
// without sleeping for a while, the service would not be unregistered fully
Thread.sleep(1500);
services = registry.list(service.getType().toLowerCase());
assertTrue("We should not see the service we just unregistered: ", services == null || services.length == 0);
} finally {
if (registry != null) registry.close();
}
}
示例12: testListMyServiceWithoutLowerCase
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testListMyServiceWithoutLowerCase() throws IOException, InterruptedException {
System.out.println("Unit Test: testListMyServiceWithoutLowerCase()");
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);
JmDNS registry = null;
try {
registry = JmDNS.create();
registry.registerService(service);
// without toLowerCase
ServiceInfo[] services = registry.list(service.getType());
assertEquals("We should see the service we just registered: ", 1, services.length);
assertEquals(service, services[0]);
// now unregister and make sure it's gone
registry.unregisterService(services[0]);
// According to the spec the record disappears from the cache 1s after it has been unregistered
// without sleeping for a while, the service would not be unregistered fully
Thread.sleep(1500);
services = registry.list(service.getType());
assertTrue("We should not see the service we just unregistered: ", services == null || services.length == 0);
} finally {
if (registry != null) registry.close();
}
}
示例13: 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();
}
}