Search in sources :

Example 1 with PersistentConnector

use of org.eclipse.californium.elements.PersistentConnector in project californium by eclipse.

the class CoapServer method loadConnector.

/**
 * Read connections for the connector of the provided uri.
 *
 * @param identifier connector's identifier
 * @param in input stream
 * @param delta adjust-delta for nano-uptime. In nanoseconds. The stream
 *            contains timestamps based on nano-uptime. On loading, this
 *            requires to adjust these timestamps according the current nano
 *            uptime and the passed real time.
 * @return number of read connections, {@code -1}, if no persistent
 *         connector is available for the provided uri.
 * @throws IOException if an i/o-error occurred
 * @see #readConnectorIdentifier(InputStream)
 * @see ServersSerializationUtil#loadServers(InputStream, List)
 * @see PersistentConnector#loadConnections(InputStream, long)
 * @since 3.0
 */
public int loadConnector(ConnectorIdentifier identifier, InputStream in, long delta) throws IOException {
    Endpoint endpoint = getEndpoint(identifier.uri);
    if (endpoint == null && identifier.wildcard != null) {
        // starting the connector
        for (Endpoint ep : endpoints) {
            if (identifier.matchWildcard(ep.getUri())) {
                endpoint = ep;
                break;
            }
        }
    }
    if (endpoint == null) {
        LOGGER.warn("{}connector {} not available!", getTag(), identifier.uri.toASCIIString());
        return -1;
    }
    PersistentConnector persistentConnector = null;
    if (endpoint instanceof CoapEndpoint) {
        Connector connector = ((CoapEndpoint) endpoint).getConnector();
        if (connector instanceof PersistentConnector) {
            persistentConnector = (PersistentConnector) connector;
        }
    }
    if (persistentConnector != null) {
        try {
            return persistentConnector.loadConnections(in, delta);
        } catch (IllegalArgumentException e) {
            LOGGER.warn("{}loading failed:", getTag(), e);
            return 0;
        }
    } else {
        LOGGER.warn("{}connector {} doesn't support persistence!", getTag(), identifier.uri.toASCIIString());
    }
    return -1;
}
Also used : PersistentConnector(org.eclipse.californium.elements.PersistentConnector) Connector(org.eclipse.californium.elements.Connector) PersistentConnector(org.eclipse.californium.elements.PersistentConnector) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Example 2 with PersistentConnector

use of org.eclipse.californium.elements.PersistentConnector in project californium by eclipse.

the class CoapServer method saveAllConnectors.

/**
 * Save all connector's connections.
 *
 * {@link #stop()}s before saving. Each entry contains the {@link #tag},
 * followed by the {@link Endpoint#getUri()} as ASCII string.
 *
 * Note: the stream will contain not encrypted critical credentials. It is
 * required to protect this data before exporting it.
 *
 * @param out output stream to write to
 * @param maxQuietPeriodInSeconds maximum quiet period of the connections in
 *            seconds. Connections without traffic for that time are skipped
 *            during serialization.
 * @return number of saved connections.
 * @throws IOException if an i/o-error occurred
 * @see ServersSerializationUtil#saveServers(OutputStream, long, List)
 * @see PersistentConnector#saveConnections(OutputStream, long)
 * @since 3.0
 */
public int saveAllConnectors(OutputStream out, long maxQuietPeriodInSeconds) throws IOException {
    stop();
    int count = 0;
    DatagramWriter writer = new DatagramWriter();
    for (Endpoint endpoint : getEndpoints()) {
        if (endpoint instanceof CoapEndpoint) {
            Connector connector = ((CoapEndpoint) endpoint).getConnector();
            if (connector instanceof PersistentConnector) {
                SerializationUtil.write(writer, MARK, Byte.SIZE);
                SerializationUtil.write(writer, getTag(), Byte.SIZE);
                SerializationUtil.write(writer, endpoint.getUri().toASCIIString(), Byte.SIZE);
                writer.writeTo(out);
                int saved = ((PersistentConnector) connector).saveConnections(out, maxQuietPeriodInSeconds);
                count += saved;
            }
        }
    }
    return count;
}
Also used : PersistentConnector(org.eclipse.californium.elements.PersistentConnector) Connector(org.eclipse.californium.elements.Connector) PersistentConnector(org.eclipse.californium.elements.PersistentConnector) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) DatagramWriter(org.eclipse.californium.elements.util.DatagramWriter) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint)

Example 3 with PersistentConnector

use of org.eclipse.californium.elements.PersistentConnector in project californium by eclipse.

the class ServersSerializationUtil method loadServers.

/**
 * Load coap servers from input stream.
 *
 * The coap-servers must not be {@link CoapServer#start()}ed before loading.
 * Reads a
 * {@link SerializationUtil#readNanotimeSynchronizationMark(DataStreamReader)}
 * ahead in order to synchronize the nano-uptimes.
 *
 * @param in input stream to load from
 * @param servers servers to load
 * @return number of loaded connections.
 * @see #loadServers(InputStream, CoapServer...)
 * @see CoapServer#loadConnector(org.eclipse.californium.core.CoapServer.ConnectorIdentifier,
 *      InputStream, long)
 * @see CoapServer#readConnectorIdentifier(InputStream)
 * @see PersistentConnector#loadConnections(InputStream, long)
 */
public static int loadServers(InputStream in, List<CoapServer> servers) {
    int count = 0;
    long time = System.nanoTime();
    List<CoapServer.ConnectorIdentifier> failed = new ArrayList<>();
    try {
        DataStreamReader reader = new DataStreamReader(in);
        long delta = SerializationUtil.readNanotimeSynchronizationMark(reader);
        CoapServer.ConnectorIdentifier id;
        while ((id = CoapServer.readConnectorIdentifier(in)) != null) {
            boolean foundTag = false;
            int loaded = -1;
            for (CoapServer server : servers) {
                if (id.tag.equals(server.getTag())) {
                    foundTag = true;
                    loaded = server.loadConnector(id, in, delta);
                    if (loaded >= 0) {
                        count += loaded;
                        break;
                    }
                }
            }
            if (foundTag) {
                if (loaded < 0) {
                    int skip = SerializationUtil.skipItems(new DataStreamReader(in), Short.SIZE);
                    LOGGER.warn("{}loading {} failed, {} connections skipped, no connector in {} servers!", id.tag, id.uri, skip, servers.size());
                    failed.add(id);
                } else {
                    LOGGER.info("{}loading {}, {} connections, {} servers.", id.tag, id.uri, loaded, servers.size());
                }
            } else {
                int skip = SerializationUtil.skipItems(new DataStreamReader(in), Short.SIZE);
                LOGGER.warn("{}loading {} failed, {} connections skipped, no server in {} servers!", id.tag, id.uri, skip, servers.size());
                failed.add(id);
            }
        }
    } catch (IllegalArgumentException e) {
        LOGGER.warn("loading failed:", e);
    } catch (IOException e) {
        LOGGER.warn("loading failed:", e);
    }
    if (!failed.isEmpty()) {
        LOGGER.warn("Loading failures:");
        for (int index = 0; index < failed.size(); ++index) {
            LOGGER.warn("[CON {}] {}", index, failed.get(index));
        }
        int index2 = 0;
        for (CoapServer server : servers) {
            List<Endpoint> endpoints = server.getEndpoints();
            for (Endpoint endpoint : endpoints) {
                if (endpoint instanceof CoapEndpoint) {
                    Connector connector = ((CoapEndpoint) endpoint).getConnector();
                    if (connector instanceof PersistentConnector) {
                        LOGGER.warn("[SRV {}] {}{}", index2, server.getTag(), endpoint.getUri().toASCIIString());
                        ++index2;
                    }
                }
            }
        }
    }
    time = System.nanoTime() - time;
    LOGGER.info("load: {} ms, {} connections", TimeUnit.NANOSECONDS.toMillis(time), count);
    return count;
}
Also used : PersistentConnector(org.eclipse.californium.elements.PersistentConnector) Connector(org.eclipse.californium.elements.Connector) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CoapServer(org.eclipse.californium.core.CoapServer) IOException(java.io.IOException) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) PersistentConnector(org.eclipse.californium.elements.PersistentConnector) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) Endpoint(org.eclipse.californium.core.network.Endpoint) DataStreamReader(org.eclipse.californium.elements.util.DataStreamReader) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint)

Aggregations

CoapEndpoint (org.eclipse.californium.core.network.CoapEndpoint)3 Endpoint (org.eclipse.californium.core.network.Endpoint)3 Connector (org.eclipse.californium.elements.Connector)3 PersistentConnector (org.eclipse.californium.elements.PersistentConnector)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CoapServer (org.eclipse.californium.core.CoapServer)1 DataStreamReader (org.eclipse.californium.elements.util.DataStreamReader)1 DatagramWriter (org.eclipse.californium.elements.util.DatagramWriter)1