本文整理匯總了Java中javax.jmdns.JmDNS.close方法的典型用法代碼示例。如果您正苦於以下問題:Java JmDNS.close方法的具體用法?Java JmDNS.close怎麽用?Java JmDNS.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jmdns.JmDNS
的用法示例。
在下文中一共展示了JmDNS.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: inetAddressRemoved
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Override
public void inetAddressRemoved(NetworkTopologyEvent event) {
InetAddress address = event.getInetAddress();
try {
synchronized (this) {
if (_knownMDNS.containsKey(address)) {
JmDNS mDNS = _knownMDNS.remove(address);
mDNS.close();
final NetworkTopologyEvent jmdnsEvent = new NetworkTopologyEventImpl(mDNS, address);
for (final NetworkTopologyListener listener : this.networkListeners()) {
_ListenerExecutor.submit(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
listener.inetAddressRemoved(jmdnsEvent);
}
});
}
}
}
} catch (Exception e) {
logger.warning("Unexpected unhandled exception: " + e);
}
}
示例2: testRegisterServiceTwice
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testRegisterServiceTwice() throws IOException {
System.out.println("Unit Test: testRegisterService()");
JmDNS registry = null;
try {
registry = JmDNS.create();
registry.registerService(service);
// This should cause an exception
registry.registerService(service);
fail("Registering the same service info should fail.");
} catch (IllegalStateException exception) {
// Expected exception.
} finally {
if (registry != null) registry.close();
}
}
示例3: 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;
}
示例4: main
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
/**
* @param args
* the command line arguments
*/
public static void main(String[] args) {
/*
* Activate these lines to see log messages of JmDNS Logger logger = Logger.getLogger(JmDNS.class.getName()); ConsoleHandler handler = new ConsoleHandler(); logger.addHandler(handler); logger.setLevel(Level.FINER);
* handler.setLevel(Level.FINER);
*/
try {
JmDNS jmdns = JmDNS.create();
jmdns.addServiceTypeListener(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();
}
}
示例5: testWaitAndQueryForServiceOnOtherRegistry
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Test
public void testWaitAndQueryForServiceOnOtherRegistry() throws IOException {
System.out.println("Unit Test: testWaitAndQueryForServiceOnOtherRegistry()");
JmDNS registry = null;
JmDNS newServiceRegistry = null;
try {
newServiceRegistry = JmDNS.create();
registry = JmDNS.create();
registry.registerService(service);
ServiceInfo fetchedService = newServiceRegistry.getServiceInfo(service.getType(), service.getName());
assertEquals("Did not get the expected service info: ", service, fetchedService);
} finally {
if (registry != null) registry.close();
if (newServiceRegistry != null) newServiceRegistry.close();
}
}
示例6: 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();
}
}
示例7: inetAddressRemoved
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Override
public void inetAddressRemoved(NetworkTopologyEvent event) {
InetAddress address = event.getInetAddress();
try {
if (_knownMDNS.containsKey(address)) {
synchronized (_knownMDNS) {
if (_knownMDNS.containsKey(address)) {
JmDNS mDNS = _knownMDNS.remove(address);
mDNS.close();
final NetworkTopologyEvent jmdnsEvent = new NetworkTopologyEventImpl(mDNS, address);
for (final NetworkTopologyListener listener : this.networkListeners()) {
_listenerExecutor.submit(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run() {
listener.inetAddressRemoved(jmdnsEvent);
}
});
}
}
}
}
} catch (Exception e) {
logger.warning("Unexpected unhandled exception: " + e);
}
}
示例8: inetAddressRemoved
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
@Override
public void inetAddressRemoved(NetworkTopologyEvent event)
{
InetAddress address = event.getInetAddress();
try
{
synchronized (this)
{
if (_knownMDNS.containsKey(address))
{
JmDNS mDNS = JmDNS.create(address);
_knownMDNS.remove(address);
mDNS.close();
final NetworkTopologyEvent jmdnsEvent = new NetworkTopologyEventImpl(mDNS, address);
for (final NetworkTopologyListener listener : this.networkListeners())
{
_ListenerExecutor.submit(new Runnable() {
/**
* {@inheritDoc}
*/
@Override
public void run()
{
listener.inetAddressRemoved(jmdnsEvent);
}
});
}
}
}
}
catch (Exception e)
{
logger.warning("Unexpected unhandled exception: " + e);
}
}
示例9: stop
import javax.jmdns.JmDNS; //導入方法依賴的package包/類
public void stop() {
if (jmdns != null) {
for (Iterator<ServiceInfo> iter = serviceInfos.iterator(); iter.hasNext();) {
ServiceInfo si = iter.next();
jmdns.unregisterService(si);
}
// Close it down async since this could block for a while.
final JmDNS closeTarget = jmdns;
Thread thread = new Thread() {
public void run() {
try {
if (JmDNSFactory.onClose(getLocalAddress())) {
closeTarget.close();
};
} catch (IOException e) {
LOG.debug("Error closing JmDNS " + getLocalhost() + ". This exception will be ignored.", e);
}
}
};
thread.setDaemon(true);
thread.start();
jmdns = null;
}
}
示例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: 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();
}
}
示例12: 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();
}
}
示例13: 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);
} finally {
if (registry != null) registry.close();
}
}
示例14: 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();
}
}
示例15: 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();
}
}