Java Code Examples for javax.jmdns.ServiceInfo#create()

The following examples show how to use javax.jmdns.ServiceInfo#create() . 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: 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 2
Source File: ExtractorServer.java    From gtasa-savegame-editor with MIT License 5 votes vote down vote up
private void startServer() throws IOException {
    String hostAddress = getPreferredNetworkAddress();
    log.info("Starting server on '" + hostAddress + "'");
    server = HttpServer.create(new InetSocketAddress(hostAddress, 0), 0);
    server.createContext("/add", addHandler());
    server.createContext("/upload", uploadHandler());
    server.createContext("/list", listHandler());
    server.createContext("/get", downloadHandler());
    server.createContext("/version", httpExchange -> {
        String response = PROTO_VERSION;
        httpExchange.sendResponseHeaders(200, response.length());
        OutputStream os = httpExchange.getResponseBody();
        os.write(response.getBytes());
        os.close();
    });
    server.setExecutor(java.util.concurrent.Executors.newCachedThreadPool());
    server.start();

    String hostName;
    try {
        hostName = InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        JOptionPane.showMessageDialog(MainWindow.getInstance(), e.getMessage(), "Unable to get hostname!", JOptionPane.ERROR_MESSAGE);
        hostName = null;
    }
    if (jmdns == null) {
        jmdns = JmDNS.create(InetAddress.getByName(hostAddress), (hostName != null) ? hostName : "GTA:SA Savegame Editor");
        log.info("Started mDNS as '" + hostName + "' on '" + hostAddress + "'");
    }
    HashMap<String, String> props = new HashMap<>();
    props.put("version", PROTO_VERSION);
    props.put("ip", hostAddress);
    props.put("port", String.valueOf(server.getAddress().getPort()));
    if (hostName != null) {
        props.put("hostname", hostName);
    }
    serviceInfo = ServiceInfo.create("_gtasa-se._tcp.local.", hostAddress.replaceAll("\\.", "-"), server.getAddress().getPort(), 0, 0, props);
    jmdns.registerService(serviceInfo);
}
 
Example 3
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 4
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 5
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 6
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 7
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);
    }
}