javax.jmdns.JmDNS Java Examples

The following examples show how to use javax.jmdns.JmDNS. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public ServiceInfo[] getServiceInfos(final String type, final String name, final boolean persistent, final long timeout) {
    // We need to run this in parallel to respect the timeout.
    final Set<ServiceInfo> result = Collections.synchronizedSet(new HashSet<ServiceInfo>(_knownMDNS.size()));
    ExecutorService executor = Executors.newCachedThreadPool();
    for (final JmDNS mDNS : _knownMDNS.values()) {
        executor.submit(new Runnable() {
            /**
             * {@inheritDoc}
             */
            @Override
            public void run() {
                result.add(mDNS.getServiceInfo(type, name, persistent, timeout));
            }
        });
    }
    executor.shutdown();
    try {
        executor.awaitTermination(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException exception) {
        logger.log(Level.WARNING, "Exception ", exception);
    }
    return result.toArray(new ServiceInfo[result.size()]);
}
 
Example #2
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public ServiceInfo[] list(final String type, final long timeout) {
    // We need to run this in parallel to respect the timeout.
    final Set<ServiceInfo> result = Collections.synchronizedSet(new HashSet<ServiceInfo>(_knownMDNS.size() * 5));
    ExecutorService executor = Executors.newCachedThreadPool();
    for (final JmDNS mDNS : _knownMDNS.values()) {
        executor.submit(new Runnable() {
            /**
             * {@inheritDoc}
             */
            @Override
            public void run() {
                result.addAll(Arrays.asList(mDNS.list(type, timeout)));
            }
        });
    }
    executor.shutdown();
    try {
        executor.awaitTermination(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException exception) {
        logger.log(Level.WARNING, "Exception ", exception);
    }
    return result.toArray(new ServiceInfo[result.size()]);
}
 
Example #3
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@Override
public void inetAddressAdded(NetworkTopologyEvent event) {
    InetAddress address = event.getInetAddress();
    try {
        synchronized (this) {
            if (!_knownMDNS.containsKey(address)) {
                _knownMDNS.put(address, JmDNS.create(address));
                final NetworkTopologyEvent jmdnsEvent = new NetworkTopologyEventImpl(_knownMDNS.get(address), address);
                for (final NetworkTopologyListener listener : this.networkListeners()) {
                    _ListenerExecutor.submit(new Runnable() {
                        /**
                         * {@inheritDoc}
                         */
                        @Override
                        public void run() {
                            listener.inetAddressAdded(jmdnsEvent);
                        }
                    });
                }
            }
        }
    } catch (Exception e) {
        logger.warning("Unexpected unhandled exception: " + e);
    }
}
 
Example #4
Source File: ZeroConf.java    From cordova-plugin-zeroconf with MIT License 6 votes vote down vote up
public ServiceInfo register(String type, String domain, String name, int port, JSONObject props) throws JSONException, IOException {

            HashMap<String, String> txtRecord = new HashMap<String, String>();
            if (props != null) {
                Iterator<String> iter = props.keys();
                while (iter.hasNext()) {
                    String key = iter.next();
                    txtRecord.put(key, props.getString(key));
                }
            }

            ServiceInfo aService = null;
            for (JmDNS publisher : publishers) {
                ServiceInfo service = ServiceInfo.create(type + domain, name, port, 0, 0, txtRecord);
                try {
                    publisher.registerService(service);
                    aService = service;
                } catch (IOException e) {
                    Log.e(TAG, e.getMessage(), e);
                }
            }
            // returns only one of the ServiceInfo instances!
            return aService;
        }
 
Example #5
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example #6
Source File: ZeroConf.java    From cordova-plugin-zeroconf with MIT License 5 votes vote down vote up
public RegistrationManager(List<InetAddress> addresses, String hostname) throws IOException {

            if (addresses == null || addresses.size() == 0) {
                publishers.add(JmDNS.create(null, hostname));
            } else {
                for (InetAddress addr : addresses) {
                    publishers.add(JmDNS.create(addr, hostname));
                }
            }

        }
 
Example #7
Source File: MDNSClientImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ServiceInfo[] list(String type) {
    ServiceInfo[] services = new ServiceInfo[0];
    for (JmDNS instance : jmdnsInstances.values()) {
        services = concatenate(services, instance.list(type));
    }
    return services;
}
 
Example #8
Source File: MDNSClientImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void close() {
    for (JmDNS jmdns : jmdnsInstances.values()) {
        closeQuietly(jmdns);
        logger.debug("mDNS service has been stopped ({})", jmdns.getName());
    }
    jmdnsInstances.clear();
}
 
Example #9
Source File: MDNSClientImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private void registerServiceInternal(ServiceDescription description) throws IOException {
    for (JmDNS instance : jmdnsInstances.values()) {
        logger.debug("Registering new service {} at {}:{} ({})", description.serviceType,
                instance.getInetAddress().getHostAddress(), description.servicePort, instance.getName());
        // Create one ServiceInfo object for each JmDNS instance
        ServiceInfo serviceInfo = ServiceInfo.create(description.serviceType, description.serviceName,
                description.servicePort, 0, 0, description.serviceProperties);
        instance.registerService(serviceInfo);
    }
}
 
Example #10
Source File: MDNSClientImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private void createJmDNSByAddress(InetAddress address) {
    try {
        JmDNS jmdns = JmDNS.create(address, null);
        jmdnsInstances.put(address, jmdns);
        logger.debug("mDNS service has been started ({} for IP {})", jmdns.getName(), address.getHostAddress());
    } catch (IOException e) {
        logger.debug("JmDNS instantiation failed ({})!", address.getHostAddress());
    }
}
 
Example #11
Source File: AirPlayServer.java    From Android-Airplay-Server with MIT License 5 votes vote down vote up
private AirPlayServer(){
	//create executor service
	executorService = Executors.newCachedThreadPool();
	
	//create channel execution handler
	channelExecutionHandler = new ExecutionHandler(new OrderedMemoryAwareThreadPoolExecutor(4, 0, 0));

	//channel group
	channelGroup = new DefaultChannelGroup();
	
	//list of mDNS services
	jmDNSInstances = new java.util.LinkedList<JmDNS>();
}
 
Example #12
Source File: MDNSClientImpl.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void unregisterService(ServiceDescription description) {
    activeServices.remove(description);
    for (JmDNS instance : jmdnsInstances.values()) {
        try {
            logger.debug("Unregistering service {} at {}:{} ({})", description.serviceType,
                    instance.getInetAddress().getHostAddress(), description.servicePort, instance.getName());
        } catch (IOException e) {
            logger.debug("Unregistering service {} ({})", description.serviceType, instance.getName());
        }
        ServiceInfo serviceInfo = ServiceInfo.create(description.serviceType, description.serviceName,
                description.servicePort, 0, 0, description.serviceProperties);
        instance.unregisterService(serviceInfo);
    }
}
 
Example #13
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void textValueUpdated(ServiceInfo target, byte[] value) {
    synchronized (_services) {
        for (JmDNS mDNS : _knownMDNS.values()) {
            ServiceInfo info = ((JmDNSImpl) mDNS).getServices().get(target.getQualifiedName());
            if (info != null) {
                info.setText(value);
            } else {
                logger.warning("We have a mDNS that does not know about the service info being updated.");
            }
        }
    }
}
 
Example #14
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void registerService(ServiceInfo info) throws IOException {
    // This is really complex. We need to clone the service info for each DNS but then we loose the ability to update it.
    synchronized (_services) {
        for (JmDNS mDNS : _knownMDNS.values()) {
            mDNS.registerService(info.clone());
        }
        ((ServiceInfoImpl) info).setDelegate(this);
        _services.put(info.getQualifiedName(), info);
    }
}
 
Example #15
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void unregisterService(ServiceInfo info) {
    synchronized (_services) {
        for (JmDNS mDNS : _knownMDNS.values()) {
            mDNS.unregisterService(info);
        }
        ((ServiceInfoImpl) info).setDelegate(null);
        _services.remove(info.getQualifiedName());
    }
}
 
Example #16
Source File: JmmDNSImpl.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void unregisterAllServices() {
    synchronized (_services) {
        for (JmDNS mDNS : _knownMDNS.values()) {
            mDNS.unregisterAllServices();
        }
        _services.clear();
    }
}
 
Example #17
Source File: ConnectTradfri.java    From helloiot with GNU General Public License v3.0 5 votes vote down vote up
private ListenableFuture<String[]> findBridge() { 
    return CompletableAsync.supplyAsync(() -> {
        try (JmDNS jmdns = JmDNS.create()) {
            ServiceInfo[] services  = jmdns.list("_coap._udp.local.", 5000); 
            String [] servers = new String[services.length];

            for (int i = 0; i < services.length; i++) {    
                servers[i] = services[i].getHostAddress() + " " + services[i].getServer();
            }
            return servers;
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }             
    });        
}
 
Example #18
Source File: ZeroConf.java    From cordova-plugin-zeroconf with MIT License 5 votes vote down vote up
public void unregister(String type, String domain, String name) {

            for (JmDNS publisher : publishers) {
                ServiceInfo serviceInfo = publisher.getServiceInfo(type + domain, name, 5000);
                if (serviceInfo != null) {
                    publisher.unregisterService(serviceInfo);
                }
            }

        }
 
Example #19
Source File: ZeroConf.java    From cordova-plugin-zeroconf with MIT License 5 votes vote down vote up
public BrowserManager(List<InetAddress> addresses, String hostname) throws IOException {

            lock.acquire();

            if (addresses == null || addresses.size() == 0) {
                browsers.add(JmDNS.create(null, hostname));
            } else {
                for (InetAddress addr : addresses) {
                    browsers.add(JmDNS.create(addr, hostname));
                }
            }
        }
 
Example #20
Source File: WlanBrowser.java    From jReto with MIT License 5 votes vote down vote up
@Override
public void startBrowsing() {
	String serviceType = "_" + this.networkType + "wlan._tcp.local.";
	
	try {
		this.bonjourBrowser = JmDNS.create("RetoWlanBrowser");
		this.bonjourBrowser.addServiceListener(serviceType, this);
		this.browsing = true;		
		this.handler.onBrowsingStarted(this);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
 
Example #21
Source File: ChromeCasts.java    From chromecast-java-api-v2 with Apache License 2.0 5 votes vote down vote up
private void doStartDiscovery(InetAddress addr) throws IOException {
    if (mDNS == null) {
        chromeCasts.clear();

        if (addr != null) {
            mDNS = JmDNS.create(addr);
        } else {
            mDNS = JmDNS.create();
        }
        mDNS.addServiceListener(ChromeCast.SERVICE_TYPE, listener);
    }
}
 
Example #22
Source File: ChromeCast.java    From chromecast-java-api-v2 with Apache License 2.0 5 votes vote down vote up
ChromeCast(JmDNS mDNS, String name) {
    this.name = name;
    ServiceInfo serviceInfo = mDNS.getServiceInfo(SERVICE_TYPE, name);
    this.address = serviceInfo.getInet4Addresses()[0].getHostAddress();
    this.port = serviceInfo.getPort();
    this.appsURL = serviceInfo.getURLs().length == 0 ? null : serviceInfo.getURLs()[0];
    this.application = serviceInfo.getApplication();

    this.title = serviceInfo.getPropertyString("fn");
    this.appTitle = serviceInfo.getPropertyString("rs");
    this.model = serviceInfo.getPropertyString("md");
}
 
Example #23
Source File: Core.java    From screenstandby with GNU General Public License v2.0 5 votes vote down vote up
private JmDNS setUpServer() {
     JmDNS jmdns = null;
     android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(android.content.Context.WIFI_SERVICE);
     lock = wifi.createMulticastLock("mylock");
     WifiInfo info = wifi.getConnectionInfo();
     _WifiName = info.getSSID();
     lock.setReferenceCounted(true);
     lock.acquire();
     try {
     	int intaddr = info.getIpAddress();
     	byte[] byteaddr = new byte[] { (byte) (intaddr & 0xff), (byte) (intaddr >> 8 & 0xff), (byte) (intaddr >> 16 & 0xff), (byte) (intaddr >> 24 & 0xff) };
     	InetAddress addr=InetAddress.getByAddress(byteaddr); //Need to process UnknownHostException
     	_IP = intToIp(intaddr);
     	jmdns=JmDNS.create(addr);
 		serviceInfo = ServiceInfo.create(type, getDeviceName(), SERVICE_PORT, "SS Remote");
java.util.HashMap<String,String> mProp = new java.util.HashMap<String,String>();

//if device has a big screen and cannot make call, it is a tablet
mProp.put("bTab", (this.context.getResources().getBoolean(R.bool.isTablet) && !canDeviceMakeCall())+"");
mProp.put("sHost", android.os.Build.HOST);
serviceInfo.setText(mProp);
     	if (isServerMode) jmdns.registerService(serviceInfo);
     	else jmdns.close();
     } catch (IOException e) {
         Logger.Log(context, e);
     }
     return jmdns;
 }
 
Example #24
Source File: MDNSClientImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void registerServiceInternal(ServiceDescription description) throws IOException {
    for (JmDNS instance : jmdnsInstances.values()) {
        logger.debug("Registering new service {} at {}:{} ({})", description.serviceType,
                instance.getInetAddress().getHostAddress(), description.servicePort, instance.getName());
        // Create one ServiceInfo object for each JmDNS instance
        ServiceInfo serviceInfo = ServiceInfo.create(description.serviceType, description.serviceName,
                description.servicePort, 0, 0, description.serviceProperties);
        instance.registerService(serviceInfo);
    }
}
 
Example #25
Source File: MDNSClientImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void unregisterService(ServiceDescription description) {
    activeServices.remove(description);
    for (JmDNS instance : jmdnsInstances.values()) {
        try {
            logger.debug("Unregistering service {} at {}:{} ({})", description.serviceType,
                    instance.getInetAddress().getHostAddress(), description.servicePort, instance.getName());
        } catch (IOException e) {
            logger.debug("Unregistering service {} ({})", description.serviceType, instance.getName());
        }
        ServiceInfo serviceInfo = ServiceInfo.create(description.serviceType, description.serviceName,
                description.servicePort, 0, 0, description.serviceProperties);
        instance.unregisterService(serviceInfo);
    }
}
 
Example #26
Source File: MDNSClientImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void unregisterAllServices() {
    activeServices.clear();
    for (JmDNS instance : jmdnsInstances.values()) {
        instance.unregisterAllServices();
    }
}
 
Example #27
Source File: MDNSClientImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ServiceInfo[] list(String type) {
    ServiceInfo[] services = new ServiceInfo[0];
    for (JmDNS instance : jmdnsInstances.values()) {
        services = concatenate(services, instance.list(type));
    }
    return services;
}
 
Example #28
Source File: MDNSClientImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ServiceInfo[] list(String type, Duration timeout) {
    ServiceInfo[] services = new ServiceInfo[0];
    for (JmDNS instance : jmdnsInstances.values()) {
        services = concatenate(services, instance.list(type, timeout.toMillis()));
    }
    return services;
}
 
Example #29
Source File: MDNSClientImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void close() {
    for (JmDNS jmdns : jmdnsInstances.values()) {
        closeQuietly(jmdns);
        logger.debug("mDNS service has been stopped ({})", jmdns.getName());
    }
    jmdnsInstances.clear();
}
 
Example #30
Source File: MDNSClientImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void createJmDNSByAddress(InetAddress address) {
    try {
        JmDNS jmdns = JmDNS.create(address, "JmDNS-" + address.toString());
        jmdnsInstances.put(address, jmdns);
        logger.debug("mDNS service has been started ({} for IP {})", jmdns.getName(), address.getHostAddress());
    } catch (IOException e) {
        logger.debug("JmDNS instantiation failed ({})!", address.getHostAddress());
    }
}