Examples of JmDNS


Examples of javax.jmdns.JmDNS

    }

    @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();
        }
    }
View Full Code Here

Examples of javax.jmdns.JmDNS

    }

    @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();
        }
    }
View Full Code Here

Examples of javax.jmdns.JmDNS

    }

    @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);
        }
    }
View Full Code Here

Examples of javax.jmdns.JmDNS

    }

    @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();
        }

    }
View Full Code Here

Examples of javax.jmdns.JmDNS

                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();
        }
    }
View Full Code Here

Examples of javax.jmdns.JmDNS

        }
        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);
View Full Code Here

Examples of javax.jmdns.JmDNS

                logger.addHandler(handler);
                logger.setLevel(Level.FINEST);
            }
        }

        JmDNS jmdns = null;
        try {
            jmdns = JmDNS.create();
            while (true) {
                ServiceInfo[] infos = jmdns.list("_airport._tcp.local.");
                System.out.println("List _airport._tcp.local.");
                for (int i = 0; i < infos.length; i++) {
                    System.out.println(infos[i]);
                }
                System.out.println();

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (jmdns != null) try {
                jmdns.close();
            } catch (IOException exception) {
                //
            }
        }
    }
View Full Code Here

Examples of javax.jmdns.JmDNS

    }

    @Override
    public void run() {
        try {
            final JmDNS jmdns = JmDNS.create();
            jmdns.addServiceListener(TOUCH_ABLE_TYPE, this);
            jmdns.addServiceListener(DACP_TYPE, this);

            final HashMap<String, String> values = new HashMap<String, String>();
            byte[] number = new byte[4];
            random.nextBytes(number);
            values.put("DvNm", "Android-" + toHex(number));
            values.put("RemV", "10000");
            values.put("DvTy", "iPod");
            values.put("RemN", "Remote");
            values.put("txtvers", "1");
            byte[] pair = new byte[8];
            random.nextBytes(pair);
            values.put("Pair", toHex(pair));

            while (_running) {
                ServerSocket server = new ServerSocket(0);

                byte[] name = new byte[20];
                random.nextBytes(name);
                System.out.println("Requesting pairing for " + toHex(name));
                ServiceInfo pairservice = ServiceInfo.create(REMOTE_TYPE, toHex(name), server.getLocalPort(), 0, 0, values);
                jmdns.registerService(pairservice);

                System.out.println("Waiting for pass code");
                final Socket socket = server.accept();
                OutputStream output = null;

                try {
                    output = socket.getOutputStream();

                    // output the contents for debugging
                    final BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    while (br.ready()) {
                        String line = br.readLine();
                        System.out.println(line);
                    }

                    // edit our local PAIRING_RAW to return the correct guid
                    byte[] code = new byte[8];
                    random.nextBytes(code);
                    System.out.println("Device guid: " + toHex(code));
                    System.arraycopy(code, 0, PAIRING_RAW, 16, 8);

                    byte[] header = String.format("HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n", new Integer(PAIRING_RAW.length)).getBytes();
                    byte[] reply = new byte[header.length + PAIRING_RAW.length];

                    System.arraycopy(header, 0, reply, 0, header.length);
                    System.arraycopy(PAIRING_RAW, 0, reply, header.length, PAIRING_RAW.length);

                    System.out.println("Response: " + new String(reply));

                    output.write(reply);
                    output.flush();

                    System.out.println("someone paired with me!");

                    jmdns.unregisterService(pairservice);
                } finally {
                    if (output != null) {
                        output.close();
                    }

                    System.out.println("Closing Socket");
                    if (!server.isClosed()) {
                        server.close();
                    }
                    _running = false;
                }
            }
            Thread.sleep(6000);
            System.out.println("Closing JmDNS");
            jmdns.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
View Full Code Here

Examples of javax.jmdns.JmDNS

         * 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();
        }
    }
View Full Code Here

Examples of javax.jmdns.JmDNS

            }
        }

        try {
            System.out.println("Opening JmDNS...");
            JmDNS jmdns = JmDNS.create();
            System.out.println("Opened JmDNS!");
            Random random = new Random();
            int id = random.nextInt(100000);
            System.out.println("\nPress r and Enter, to register Itunes Remote service 'Android-'" + id);
            int b;
            while ((b = System.in.read()) != -1 && (char) b != 'r') {
                /* Stub */
            }

            final HashMap<String, String> values = new HashMap<String, String>();
            values.put("DvNm", "Android-" + id);
            values.put("RemV", "10000");
            values.put("DvTy", "iPod");
            values.put("RemN", "Remote");
            values.put("txtvers", "1");
            byte[] pair = new byte[8];
            random.nextBytes(pair);
            values.put("Pair", toHex(pair));

            byte[] name = new byte[20];
            random.nextBytes(name);
            System.out.println("Requesting pairing for " + toHex(name));
            ServiceInfo pairservice = ServiceInfo.create(REMOTE_TYPE, toHex(name), 1025, 0, 0, values);
            jmdns.registerService(pairservice);

            System.out.println("\nRegistered Service as " + pairservice);
            System.out.println("Press q and Enter, to quit");
            // int b;
            while ((b = System.in.read()) != -1 && (char) b != 'q') {
                /* Stub */
            }
            System.out.println("Closing JmDNS...");
            jmdns.unregisterService(pairservice);
            jmdns.unregisterAllServices();
            jmdns.close();
            System.out.println("Done!");
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.