Search in sources :

Example 1 with ServerConfig

use of org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig in project leshan by eclipse.

the class BootstrapIntegrationTestHelper method createBootstrapServer.

public void createBootstrapServer(BootstrapSecurityStore securityStore) {
    BootstrapStore bsStore = new BootstrapStore() {

        @Override
        public BootstrapConfig getBootstrap(String endpoint) {
            BootstrapConfig bsConfig = new BootstrapConfig();
            // security for BS server
            ServerSecurity bsSecurity = new ServerSecurity();
            bsSecurity.serverId = 1111;
            bsSecurity.bootstrapServer = true;
            bsSecurity.uri = "coap://" + bootstrapServer.getUnsecuredAddress().getHostString() + ":" + bootstrapServer.getUnsecuredAddress().getPort();
            bsSecurity.securityMode = SecurityMode.NO_SEC;
            bsConfig.security.put(0, bsSecurity);
            // security for DM server
            ServerSecurity dmSecurity = new ServerSecurity();
            dmSecurity.uri = "coap://" + server.getUnsecuredAddress().getHostString() + ":" + server.getUnsecuredAddress().getPort();
            dmSecurity.serverId = 2222;
            dmSecurity.securityMode = SecurityMode.NO_SEC;
            bsConfig.security.put(1, dmSecurity);
            // DM server
            ServerConfig dmConfig = new ServerConfig();
            dmConfig.shortId = 2222;
            bsConfig.servers.put(0, dmConfig);
            return bsConfig;
        }
    };
    if (securityStore == null) {
        securityStore = dummyBsSecurityStore();
    }
    LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
    builder.setConfigStore(bsStore);
    builder.setSecurityStore(securityStore);
    builder.setLocalAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    builder.setLocalSecureAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    bootstrapServer = builder.build();
}
Also used : ServerConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig) LeshanBootstrapServerBuilder(org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder) InetSocketAddress(java.net.InetSocketAddress) BootstrapConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig) ServerSecurity(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity) BootstrapStore(org.eclipse.leshan.server.bootstrap.BootstrapStore)

Example 2 with ServerConfig

use of org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig in project leshan by eclipse.

the class BootstrapHandler method sendServers.

private void sendServers(final BootstrapSession session, final BootstrapConfig cfg, final List<Integer> toSend) {
    if (!toSend.isEmpty()) {
        // get next config
        Integer key = toSend.remove(0);
        ServerConfig serverConfig = cfg.servers.get(key);
        // extract write request parameters
        LwM2mPath path = new LwM2mPath(1, key);
        final LwM2mNode serverInstance = convertToServerInstance(key, serverConfig);
        final BootstrapWriteRequest writeServerRequest = new BootstrapWriteRequest(path, serverInstance, session.getContentFormat());
        send(session, writeServerRequest, new ResponseCallback<BootstrapWriteResponse>() {

            @Override
            public void onResponse(BootstrapWriteResponse response) {
                LOG.trace("Bootstrap write {} return code {}", session.getEndpoint(), response.getCode());
                // recursive call until toSend is empty
                sendServers(session, cfg, toSend);
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.warn(String.format("Error during bootstrap write of server instance %s on %s", serverInstance, session.getEndpoint()), e);
                sessionManager.failed(session, WRITE_SERVER_FAILED, writeServerRequest);
            }
        });
    } else {
        final BootstrapFinishRequest finishBootstrapRequest = new BootstrapFinishRequest();
        send(session, finishBootstrapRequest, new ResponseCallback<BootstrapFinishResponse>() {

            @Override
            public void onResponse(BootstrapFinishResponse response) {
                LOG.trace("Bootstrap Finished {} return code {}", session.getEndpoint(), response.getCode());
                if (response.isSuccess()) {
                    sessionManager.end(session);
                } else {
                    sessionManager.failed(session, FINISHED_WITH_ERROR, finishBootstrapRequest);
                }
            }
        }, new ErrorCallback() {

            @Override
            public void onError(Exception e) {
                LOG.debug(String.format("Error during bootstrap finished on %s", session.getEndpoint()), e);
                sessionManager.failed(session, SEND_FINISH_FAILED, finishBootstrapRequest);
            }
        });
    }
}
Also used : BootstrapWriteResponse(org.eclipse.leshan.core.response.BootstrapWriteResponse) ErrorCallback(org.eclipse.leshan.core.response.ErrorCallback) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) BootstrapFinishRequest(org.eclipse.leshan.core.request.BootstrapFinishRequest) ServerConfig(org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig) LwM2mPath(org.eclipse.leshan.core.node.LwM2mPath) BootstrapFinishResponse(org.eclipse.leshan.core.response.BootstrapFinishResponse) BootstrapWriteRequest(org.eclipse.leshan.core.request.BootstrapWriteRequest)

Aggregations

ServerConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig)2 InetSocketAddress (java.net.InetSocketAddress)1 LwM2mNode (org.eclipse.leshan.core.node.LwM2mNode)1 LwM2mPath (org.eclipse.leshan.core.node.LwM2mPath)1 BootstrapFinishRequest (org.eclipse.leshan.core.request.BootstrapFinishRequest)1 BootstrapWriteRequest (org.eclipse.leshan.core.request.BootstrapWriteRequest)1 BootstrapFinishResponse (org.eclipse.leshan.core.response.BootstrapFinishResponse)1 BootstrapWriteResponse (org.eclipse.leshan.core.response.BootstrapWriteResponse)1 ErrorCallback (org.eclipse.leshan.core.response.ErrorCallback)1 BootstrapConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig)1 ServerSecurity (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)1 BootstrapStore (org.eclipse.leshan.server.bootstrap.BootstrapStore)1 LeshanBootstrapServerBuilder (org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder)1