Search in sources :

Example 1 with ZeroConfService

use of jmri.util.zeroconf.ZeroConfService in project JMRI by JMRI.

the class ReportContext method addNetworkInfo.

/**
     * Add network connection and running service information
     */
private void addNetworkInfo() {
    try {
        // This code is based on that in jmri.jmrit.withrottle.UserInterface which,
        // itself, was adapted from http://www.rgagnon.com/javadetails/java-0390.html
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();
                String hostAddress = inetAddress.getHostAddress();
                if (!hostAddress.equals("0.0.0.0") && !hostAddress.regionMatches(0, "127", 0, 3) && !hostAddress.contains(":")) {
                    addString("Network Interface: " + networkInterface.getName());
                    addString(" Long Name: " + networkInterface.getDisplayName());
                    addString(" Host Name: " + inetAddress.getHostName());
                    addString(" IP address: " + hostAddress);
                }
            }
        }
    } catch (SocketException ex) {
        addString("Unable to enumerate Network Interfaces");
    }
    Collection<ZeroConfService> services = ZeroConfService.allServices();
    for (InetAddress address : ZeroConfService.netServices().keySet()) {
        addString("ZeroConfService host: " + ZeroConfService.hostName(address) + " running " + services.size() + " service(s)");
    }
    if (services.size() > 0) {
        for (ZeroConfService service : services) {
            addString("ZeroConfService: " + service.serviceInfo().getQualifiedName() + "  ");
            addString(" Name: " + service.name() + "   ");
            try {
                for (String address : service.serviceInfo().getHostAddresses()) {
                    addString(" Address:" + address + "   ");
                }
            } catch (NullPointerException ex) {
                addString(" Address: [unknown due to NPE]");
            }
            addString(" Port: " + service.serviceInfo().getPort() + "   ");
            addString(" Server: " + service.serviceInfo().getServer() + "   ");
            addString(" Type: " + service.type() + "   ");
            try {
                for (String url : service.serviceInfo().getURLs()) {
                    addString(" URL: " + url + "   ");
                }
            } catch (NullPointerException ex) {
                addString(" URL: [unknown due to NPE]");
            }
            addString(" Published: " + (service.isPublished() ? "yes" : "no"));
        }
    }
}
Also used : SocketException(java.net.SocketException) NetworkInterface(java.net.NetworkInterface) ZeroConfService(jmri.util.zeroconf.ZeroConfService) InetAddress(java.net.InetAddress)

Example 2 with ZeroConfService

use of jmri.util.zeroconf.ZeroConfService in project JMRI by JMRI.

the class JsonUtilHttpServiceTest method testGetNetworkService.

/**
     * Test of getNetworkService method, of class JsonUtilHttpService.
     *
     * @throws jmri.server.json.JsonException if test fails in an unexpected
     *                                        manner
     */
@Test
public void testGetNetworkService() throws JsonException {
    Locale locale = Locale.ENGLISH;
    ObjectMapper mapper = new ObjectMapper();
    JsonUtilHttpService instance = new JsonUtilHttpService(mapper);
    JsonNode result = null;
    // non-existant service
    JsonException exception = null;
    try {
        // NOI18N
        result = instance.getNetworkService(locale, "non-existant-service");
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNull(result);
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, exception.getCode());
    // published service
    ZeroConfService service = ZeroConfService.create(JSON.ZEROCONF_SERVICE_TYPE, 9999);
    service.publish();
    Assume.assumeTrue("Published ZeroConf Service", JUnitUtil.waitFor(() -> {
        return service.isPublished() == true;
    }));
    result = instance.getNetworkService(locale, JSON.ZEROCONF_SERVICE_TYPE);
    Assert.assertEquals(JSON.NETWORK_SERVICE, result.path(JSON.TYPE).asText());
    JsonNode data = result.path(JSON.DATA);
    Assert.assertFalse(data.isMissingNode());
    Assert.assertEquals(WebServerPreferences.getDefault().getRailRoadName(), data.path(JSON.NAME).asText());
    Assert.assertEquals(9999, data.path(JSON.PORT).asInt());
    Assert.assertEquals(JSON.ZEROCONF_SERVICE_TYPE, data.path(JSON.TYPE).asText());
    Assert.assertEquals(NodeIdentity.identity(), data.path(JSON.NODE).asText());
    Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERCANON), data.path("jmri").asText());
    Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERSION), data.path("version").asText());
}
Also used : Locale(java.util.Locale) JsonException(jmri.server.json.JsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ZeroConfService(jmri.util.zeroconf.ZeroConfService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with ZeroConfService

use of jmri.util.zeroconf.ZeroConfService in project JMRI by JMRI.

the class JsonUtil method getNetworkServices.

/**
     *
     * @param locale the client's Locale.
     * @return the JSON networkServices message.
     * @deprecated since 4.5.2; use
     * {@link jmri.server.json.util.JsonUtilHttpService#getNetworkServices(java.util.Locale)}.
     */
@Deprecated
public static JsonNode getNetworkServices(Locale locale) {
    ArrayNode root = mapper.createArrayNode();
    for (ZeroConfService service : ZeroConfService.allServices()) {
        ObjectNode ns = mapper.createObjectNode().put(TYPE, NETWORK_SERVICE);
        ObjectNode data = ns.putObject(DATA);
        data.put(NAME, service.name());
        data.put(PORT, service.serviceInfo().getPort());
        data.put(TYPE, service.type());
        Enumeration<String> pe = service.serviceInfo().getPropertyNames();
        while (pe.hasMoreElements()) {
            String pn = pe.nextElement();
            data.put(pn, service.serviceInfo().getPropertyString(pn));
        }
        root.add(ns);
    }
    return root;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ZeroConfService(jmri.util.zeroconf.ZeroConfService) TrainCommon.splitString(jmri.jmrit.operations.trains.TrainCommon.splitString)

Example 4 with ZeroConfService

use of jmri.util.zeroconf.ZeroConfService in project JMRI by JMRI.

the class JsonUtilHttpServiceTest method testGetNetworkServices.

/**
     * Test of getNetworkServices method, of class JsonUtilHttpService.
     */
@Test
public void testGetNetworkServices() {
    Locale locale = Locale.ENGLISH;
    ObjectMapper mapper = new ObjectMapper();
    JsonUtilHttpService instance = new JsonUtilHttpService(mapper);
    // no services published
    JsonNode result = instance.getNetworkServices(locale);
    Assert.assertEquals(0, result.size());
    // publish a service
    ZeroConfService service = ZeroConfService.create(JSON.ZEROCONF_SERVICE_TYPE, 9999);
    service.publish();
    Assume.assumeTrue("Published ZeroConf Service", JUnitUtil.waitFor(() -> {
        return service.isPublished() == true;
    }));
    result = instance.getNetworkServices(locale);
    Assert.assertEquals(1, result.size());
    Assert.assertEquals(JSON.NETWORK_SERVICE, result.get(0).path(JSON.TYPE).asText());
    JsonNode data = result.get(0).path(JSON.DATA);
    Assert.assertFalse(data.isMissingNode());
    Assert.assertEquals(WebServerPreferences.getDefault().getRailRoadName(), data.path(JSON.NAME).asText());
    Assert.assertEquals(9999, data.path(JSON.PORT).asInt());
    Assert.assertEquals(JSON.ZEROCONF_SERVICE_TYPE, data.path(JSON.TYPE).asText());
    Assert.assertEquals(NodeIdentity.identity(), data.path(JSON.NODE).asText());
    Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERCANON), data.path("jmri").asText());
    Assert.assertEquals(Metadata.getBySystemName(Metadata.JMRIVERSION), data.path("version").asText());
}
Also used : Locale(java.util.Locale) JsonNode(com.fasterxml.jackson.databind.JsonNode) ZeroConfService(jmri.util.zeroconf.ZeroConfService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with ZeroConfService

use of jmri.util.zeroconf.ZeroConfService in project JMRI by JMRI.

the class JsonUtilHttpServiceTest method testDoGet.

/**
     * Test of doGet method, of class JsonUtilHttpService.
     *
     * @throws jmri.server.json.JsonException if test fails in an unexpected
     *                                        manner
     */
@Test
public void testDoGet() throws JsonException {
    Locale locale = Locale.ENGLISH;
    ObjectMapper mapper = new ObjectMapper();
    JsonUtilHttpService instance = new JsonUtilHttpService(mapper);
    JsonServerPreferences.getDefault().setHeartbeatInterval(10);
    Assert.assertEquals(instance.getHello(locale, 10), instance.doGet(JSON.HELLO, null, locale));
    Assert.assertEquals(instance.getMetadata(locale, Metadata.JMRIVERCANON), instance.doGet(JSON.METADATA, Metadata.JMRIVERCANON, locale));
    Assert.assertEquals(instance.getMetadata(locale), instance.doGet(JSON.METADATA, null, locale));
    Assert.assertEquals(instance.getNode(locale), instance.doGet(JSON.NODE, null, locale));
    Assert.assertEquals(instance.getNetworkServices(locale), instance.doGet(JSON.NETWORK_SERVICE, null, locale));
    Assert.assertEquals(instance.getNetworkServices(locale), instance.doGet(JSON.NETWORK_SERVICES, null, locale));
    JsonException exception = null;
    try {
        instance.doGet(JSON.NETWORK_SERVICE, JSON.ZEROCONF_SERVICE_TYPE, locale);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, exception.getCode());
    exception = null;
    try {
        instance.doGet("INVALID TYPE TOKEN", null, locale);
    } catch (JsonException ex) {
        exception = ex;
    }
    Assert.assertNotNull(exception);
    Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, exception.getCode());
    ZeroConfService service = ZeroConfService.create(JSON.ZEROCONF_SERVICE_TYPE, 9999);
    service.publish();
    Assume.assumeTrue("Publishing ZeroConf Service", JUnitUtil.waitFor(() -> {
        return service.isPublished() == true;
    }));
    Assert.assertEquals(instance.getNetworkService(locale, JSON.ZEROCONF_SERVICE_TYPE), instance.doGet(JSON.NETWORK_SERVICE, JSON.ZEROCONF_SERVICE_TYPE, locale));
}
Also used : Locale(java.util.Locale) JsonException(jmri.server.json.JsonException) ZeroConfService(jmri.util.zeroconf.ZeroConfService) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

ZeroConfService (jmri.util.zeroconf.ZeroConfService)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Locale (java.util.Locale)3 Test (org.junit.Test)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JsonException (jmri.server.json.JsonException)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 InetAddress (java.net.InetAddress)1 NetworkInterface (java.net.NetworkInterface)1 SocketException (java.net.SocketException)1 TrainCommon.splitString (jmri.jmrit.operations.trains.TrainCommon.splitString)1