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;
}
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;
}
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;
}
Aggregations