Search in sources :

Example 1 with LeshanBootstrapServerBuilder

use of org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder 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 LeshanBootstrapServerBuilder

use of org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder in project leshan by eclipse.

the class LeshanBootstrapServerDemo method createAndStartServer.

public static void createAndStartServer(int webPort, String localAddress, int localPort, String secureLocalAddress, int secureLocalPort, String modelsFolderPath, String configFilename) throws Exception {
    // Create Models
    List<ObjectModel> models = ObjectLoader.loadDefault();
    if (modelsFolderPath != null) {
        models.addAll(ObjectLoader.loadObjectsFromDir(new File(modelsFolderPath)));
    }
    // Prepare and start bootstrap server
    LeshanBootstrapServerBuilder builder = new LeshanBootstrapServerBuilder();
    BootstrapStoreImpl bsStore = new BootstrapStoreImpl(configFilename);
    builder.setConfigStore(bsStore);
    builder.setSecurityStore(new BootstrapSecurityStoreImpl(bsStore));
    builder.setLocalAddress(localAddress, localPort);
    builder.setLocalSecureAddress(secureLocalAddress, secureLocalPort);
    builder.setModel(new LwM2mModel(models));
    // Create CoAP Config
    NetworkConfig coapConfig;
    File configFile = new File(NetworkConfig.DEFAULT_FILE_NAME);
    if (configFile.isFile()) {
        coapConfig = new NetworkConfig();
        coapConfig.load(configFile);
    } else {
        coapConfig = LeshanServerBuilder.createDefaultNetworkConfig();
        coapConfig.store(configFile);
    }
    builder.setCoapConfig(coapConfig);
    LeshanBootstrapServer bsServer = builder.build();
    bsServer.start();
    // Now prepare and start jetty
    Server server = new Server(webPort);
    WebAppContext root = new WebAppContext();
    root.setContextPath("/");
    root.setResourceBase(LeshanBootstrapServerDemo.class.getClassLoader().getResource("webapp").toExternalForm());
    root.setParentLoaderPriority(true);
    ServletHolder bsServletHolder = new ServletHolder(new BootstrapServlet(bsStore));
    root.addServlet(bsServletHolder, "/api/bootstrap/*");
    ServletHolder serverServletHolder = new ServletHolder(new ServerServlet(bsServer));
    root.addServlet(serverServletHolder, "/api/server/*");
    server.setHandler(root);
    server.start();
    LOG.info("Web server started at {}.", server.getURI());
}
Also used : LeshanBootstrapServerBuilder(org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) LeshanBootstrapServer(org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer) LeshanBootstrapServer(org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer) Server(org.eclipse.jetty.server.Server) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServerServlet(org.eclipse.leshan.server.bootstrap.demo.servlet.ServerServlet) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) LwM2mModel(org.eclipse.leshan.core.model.LwM2mModel) BootstrapServlet(org.eclipse.leshan.server.bootstrap.demo.servlet.BootstrapServlet) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) File(java.io.File)

Aggregations

LeshanBootstrapServerBuilder (org.eclipse.leshan.server.californium.LeshanBootstrapServerBuilder)2 File (java.io.File)1 InetSocketAddress (java.net.InetSocketAddress)1 NetworkConfig (org.eclipse.californium.core.network.config.NetworkConfig)1 Server (org.eclipse.jetty.server.Server)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)1 LwM2mModel (org.eclipse.leshan.core.model.LwM2mModel)1 ObjectModel (org.eclipse.leshan.core.model.ObjectModel)1 BootstrapConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig)1 ServerConfig (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerConfig)1 ServerSecurity (org.eclipse.leshan.server.bootstrap.BootstrapConfig.ServerSecurity)1 BootstrapStore (org.eclipse.leshan.server.bootstrap.BootstrapStore)1 BootstrapServlet (org.eclipse.leshan.server.bootstrap.demo.servlet.BootstrapServlet)1 ServerServlet (org.eclipse.leshan.server.bootstrap.demo.servlet.ServerServlet)1 LeshanBootstrapServer (org.eclipse.leshan.server.californium.impl.LeshanBootstrapServer)1