org.eclipse.californium.core.network.CoapEndpoint Java Examples

The following examples show how to use org.eclipse.californium.core.network.CoapEndpoint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExternalCommandsDriver.java    From SoftwarePilot with MIT License 6 votes vote down vote up
public ExternalCommandsDriver() {
		//ecdLogger.log(Level.FINEST, "In Constructor");
		try {
				NetworkConfig nc = new NetworkConfig();
				cs = new CoapServer(nc); //create a new coapserver
				InetSocketAddress bindToAddress = new InetSocketAddress( LISTEN_PORT);
				cs.addEndpoint(new CoapEndpoint(bindToAddress));//Adds an Endpoint to the server.
				driverPort = bindToAddress.getPort();

				cs.add(new ecdResource());//Add a resource to the server
		}
		catch(Exception e) {
		}



}
 
Example #2
Source File: ManagerTradfri.java    From helloiot with GNU General Public License v3.0 6 votes vote down vote up
void connectBridge() {

        DtlsConnectorConfig.Builder builder = new DtlsConnectorConfig.Builder();
        builder.setAddress(new InetSocketAddress(0));
        builder.setPskStore(new StaticPskStore(identity, psk.getBytes()));
        DTLSConnector dtlsConnector = new DTLSConnector(builder.build());
        CoapEndpoint.CoapEndpointBuilder coapbuilder = new CoapEndpoint.CoapEndpointBuilder();
        coapbuilder.setConnector(dtlsConnector);
        coapbuilder.setNetworkConfig(NetworkConfig.getStandard());
        coapEndPoint = coapbuilder.build();
    }
 
Example #3
Source File: CoapTransportService.java    From Groza with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws UnknownHostException {
    log.info("Starting CoAP transport...");
    log.info("Lookup CoAP transport adaptor {}", adaptorName);
    this.adaptor = (CoapTransportAdaptor) appContext.getBean(adaptorName);
    log.info("Starting CoAP transport server");
    this.server = new CoapServer();
    createResources();
    InetAddress addr = InetAddress.getByName(host);
    InetSocketAddress sockAddr = new InetSocketAddress(addr, port);
    server.addEndpoint(new CoapEndpoint(sockAddr));
    server.start();
    log.info("CoAP transport started!");
}
 
Example #4
Source File: CoapServer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Constructs a server with the specified configuration that listens to the
 * specified ports after method {@link #start()} is called.
 *
 * @param config the configuration, if <code>null</code> the configuration returned by
 * {@link NetworkConfig#getStandard()} is used.
 * @param ports the ports to bind to
 */
public CoapServer(NetworkConfig config, int... ports) {
	
	// global configuration that is passed down (can be observed for changes)
	if (config != null) {
		this.config = config;
	} else {
		this.config = NetworkConfig.getStandard();
	}
	
	// resources
	this.root = createRoot();
	this.deliverer = new ServerMessageDeliverer(root);
	
	CoapResource well_known = new CoapResource(".well-known");
	well_known.setVisible(false);
	well_known.add(new DiscoveryResource(root));
	root.add(well_known);
	
	// endpoints
	this.endpoints = new ArrayList<Endpoint>();
	// sets the central thread pool for the protocol stage over all endpoints
	this.executor = Executors.newScheduledThreadPool( config.getInt(NetworkConfig.Keys.PROTOCOL_STAGE_THREAD_COUNT) );
	// create endpoint for each port
	for (int port:ports)
		addEndpoint(new CoapEndpoint(port, this.config));
}
 
Example #5
Source File: CoapServer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Starts the server by starting all endpoints this server is assigned to.
 * Each endpoint binds to its port. If no endpoint is assigned to the
 * server, an endpoint is started on the port defined in the config.
 */
@Override
public void start() {
	
	LOGGER.info("Starting server");
	
	if (endpoints.isEmpty()) {
		// servers should bind to the configured port (while clients should use an ephemeral port through the default endpoint)
		int port = config.getInt(NetworkConfig.Keys.COAP_PORT);
		LOGGER.info("No endpoints have been defined for server, setting up server endpoint on default port " + port);
		addEndpoint(new CoapEndpoint(port, this.config));
	}
	
	int started = 0;
	for (Endpoint ep:endpoints) {
		try {
			ep.start();
			// only reached on success
			++started;
		} catch (IOException e) {
			LOGGER.severe(e.getMessage() + " at " + ep.getAddress());
		}
	}
	if (started==0) {
		throw new IllegalStateException("None of the server endpoints could be started");
	}
}
 
Example #6
Source File: CoapServer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Constructs a server with the specified configuration that listens to the
 * specified ports after method {@link #start()} is called.
 *
 * @param config the configuration, if <code>null</code> the configuration returned by
 * {@link NetworkConfig#getStandard()} is used.
 * @param ports the ports to bind to
 */
public CoapServer(NetworkConfig config, int... ports) {
	
	// global configuration that is passed down (can be observed for changes)
	if (config != null) {
		this.config = config;
	} else {
		this.config = NetworkConfig.getStandard();
	}
	
	// resources
	this.root = createRoot();
	this.deliverer = new ServerMessageDeliverer(root);
	
	CoapResource well_known = new CoapResource(".well-known");
	well_known.setVisible(false);
	well_known.add(new DiscoveryResource(root));
	root.add(well_known);
	
	// endpoints
	this.endpoints = new ArrayList<Endpoint>();
	// sets the central thread pool for the protocol stage over all endpoints
	this.executor = Executors.newScheduledThreadPool( config.getInt(NetworkConfig.Keys.PROTOCOL_STAGE_THREAD_COUNT) );
	// create endpoint for each port
	for (int port:ports)
		addEndpoint(new CoapEndpoint(port, this.config));
}
 
Example #7
Source File: CoapTestBase.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates the client to use for uploading data to the secure endpoint
 * of the CoAP adapter.
 *
 * @param pskStoreToUse The store to retrieve shared secrets from.
 * @return The client.
 */
protected CoapClient getCoapsClient(final PskStore pskStoreToUse) {

    final DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
    dtlsConfig.setAddress(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    dtlsConfig.setPskStore(pskStoreToUse);
    dtlsConfig.setMaxRetransmissions(1);
    final CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
    builder.setNetworkConfig(NetworkConfig.createStandardWithoutFile());
    builder.setConnector(new DTLSConnector(dtlsConfig.build()));
    return new CoapClient().setEndpoint(builder.build());
}
 
Example #8
Source File: AbstractVertxBasedCoapAdapter.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private Future<Endpoint> createInsecureEndpoint() {

        log.info("creating insecure endpoint");

        return getInsecureNetworkConfig()
                .map(config -> {
                    final CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
                    builder.setNetworkConfig(config);
                    builder.setInetSocketAddress(new InetSocketAddress(
                            getConfig().getInsecurePortBindAddress(),
                            getConfig().getInsecurePort(getInsecurePortDefaultValue())));
                    return builder.build();
                });
    }
 
Example #9
Source File: CoapServer.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Starts the server by starting all endpoints this server is assigned to.
 * Each endpoint binds to its port. If no endpoint is assigned to the
 * server, an endpoint is started on the port defined in the config.
 */
@Override
public void start() {
	
	LOGGER.info("Starting server");
	
	if (endpoints.isEmpty()) {
		// servers should bind to the configured port (while clients should use an ephemeral port through the default endpoint)
		int port = config.getInt(NetworkConfig.Keys.COAP_PORT);
		LOGGER.info("No endpoints have been defined for server, setting up server endpoint on default port " + port);
		addEndpoint(new CoapEndpoint(port, this.config));
	}
	
	int started = 0;
	for (Endpoint ep:endpoints) {
		try {
			ep.start();
			// only reached on success
			++started;
		} catch (IOException e) {
			LOGGER.severe(e.getMessage() + " at " + ep.getAddress());
		}
	}
	if (started==0) {
		throw new IllegalStateException("None of the server endpoints could be started");
	}
}
 
Example #10
Source File: CoapTransportService.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() throws UnknownHostException {
  log.info("Starting CoAP transport...");
  log.info("Lookup CoAP transport adaptor {}", adaptorName);
  // this.adaptor = (CoapTransportAdaptor) appContext.getBean(adaptorName);
  log.info("Starting CoAP transport server");
  this.server = new CoapServer();
  createResources();
  InetAddress addr = InetAddress.getByName(host);
  InetSocketAddress sockAddr = new InetSocketAddress(addr, port);
  server.addEndpoint(new CoapEndpoint(sockAddr));
  server.start();
  log.info("CoAP transport started!");
}
 
Example #11
Source File: CaptureImageV2Driver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public CaptureImageV2Driver() throws Exception {
	//NetworkConfig.getStandard().set(NetworkConfig.Keys.MAX_RESOURCE_BODY_SIZE,10000000);
	bdLogger.log(Level.FINEST, "In Constructor");
	cs = new CoapServer(); //initilize the server
	InetSocketAddress bindToAddress =
			new InetSocketAddress( LISTEN_PORT);//get the address
	CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
	cs.addEndpoint(tmp);//add endpoint to server
	startTime = System.currentTimeMillis();
	tmp.start();//Start this endpoint and all its components.
	driverPort = tmp.getAddress().getPort();
	cs.add(new bdResource());

	try {
		Class.forName("org.h2.Driver");
		Connection conn = DriverManager.getConnection("jdbc:h2:mem:CaptureImageV2Driver;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1",
				"user", "password");
		conn.createStatement().executeUpdate("CREATE TABLE data (" +" key  INTEGER AUTO_INCREMENT,"
				+" time BIGINT, "
				+" X VARCHAR(16), "
				+" Y VARCHAR(16), "
				+" Z VARCHAR(16), "
				+" Filename VARCHAR(1023) )");
		conn.close();
	}
	catch(Exception e) {
		e.printStackTrace();
	}

}
 
Example #12
Source File: BatteryDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public BatteryDriver() throws Exception {
	bdLogger.log(Level.FINEST, "In Constructor");
	cs = new CoapServer(); //initilize the server
	InetSocketAddress bindToAddress =
			new InetSocketAddress("localhost", LISTEN_PORT);//get the address
	CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
	cs.addEndpoint(tmp);//add endpoint to server
	startTime = System.currentTimeMillis();
	tmp.start();//Start this endpoint and all its components.
	driverPort = tmp.getAddress().getPort();
	cs.add(new bdResource());

	try {
			Class.forName("org.h2.Driver");
			Connection conn = DriverManager.getConnection("jdbc:h2:mem:BatteryDriver;DB_CLOSE_DELAY=-1",
																						 "user", "password");
			conn.createStatement().executeUpdate("CREATE TABLE data ("
																					 +" key  INTEGER AUTO_INCREMENT,"
																					 +" time BIGINT, "
																					 +" type VARCHAR(16), "
																					 +" value VARCHAR(1023) )");
			conn.close();
	}
	catch(Exception e) {
									e.printStackTrace();
	}

}
 
Example #13
Source File: TemplateDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public TemplateDriver() throws Exception {
		logger.log(Level.FINEST, "In Constructor");
		cs = new CoapServer(); //initilize the server
		InetSocketAddress bindToAddress =
				new InetSocketAddress("localhost", LISTEN_PORT);//get the address
		CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
		cs.addEndpoint(tmp);//add endpoint to server
		tmp.start();//Start this endpoint and all its components.
		driverPort = tmp.getAddress().getPort();
		cs.add(new Resource());
}
 
Example #14
Source File: VisionDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public VisionDriver() throws Exception {
		fddLogger.log(Level.FINEST, "In Constructor");
		cs = new CoapServer(); //initilize the server
		InetSocketAddress bindToAddress = new InetSocketAddress( LISTEN_PORT);//get the address
		CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
		cs.addEndpoint(tmp);//add endpoint to server
		tmp.start();//Start this endpoint and all its components.
		driverPort = tmp.getAddress().getPort();

		cs.add(new fddResource());
}
 
Example #15
Source File: DroneGimbalDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public DroneGimbalDriver() throws Exception {
		dgdLogger.log(Level.FINEST, "In Constructor");
		cs = new CoapServer(); //initilize the server
		InetSocketAddress bindToAddress = new InetSocketAddress( LISTEN_PORT);//get the address
		CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
		cs.addEndpoint(tmp);//add endpoint to server
		tmp.start();//Start this endpoint and all its components.
		driverPort = tmp.getAddress().getPort();

		cs.add(new dgdResource());

}
 
Example #16
Source File: PicTraceDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public PicTraceDriver() throws Exception {
		bdLogger.log(Level.FINEST, "In Constructor");
		cs = new CoapServer(); //initilize the server
		InetSocketAddress bindToAddress =
				new InetSocketAddress( LISTEN_PORT);//get the address
		CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
		cs.addEndpoint(tmp);//add endpoint to server
		startTime = System.currentTimeMillis();
		tmp.start();//Start this endpoint and all its components.
		driverPort = tmp.getAddress().getPort();
		cs.add(new bdResource());
		makeH2();
}
 
Example #17
Source File: LocationDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public LocationDriver() throws Exception {
		bdLogger.log(Level.FINEST, "In Constructor");
		cs = new CoapServer(); //initilize the server
		InetSocketAddress bindToAddress =
				new InetSocketAddress( LISTEN_PORT);//get the address
		CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
		cs.addEndpoint(tmp);//add endpoint to server
		startTime = System.currentTimeMillis();
		tmp.start();//Start this endpoint and all its components.
		driverPort = tmp.getAddress().getPort();
		cs.add(new bdResource());
		makeH2();
}
 
Example #18
Source File: MissionDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public MissionDriver() throws Exception {
mdLogger.log(Level.FINEST, "In Constructor");
cs = new CoapServer(); //initilize the server
InetSocketAddress bindToAddress =
		new InetSocketAddress("localhost", LISTEN_PORT);//get the address
CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
cs.addEndpoint(tmp);//add endpoint to server
tmp.start();//Start this endpoint and all its components.
driverPort = tmp.getAddress().getPort();
cs.add(new mdResource());
}
 
Example #19
Source File: FlyDroneDriver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public FlyDroneDriver() throws Exception {
		fddLogger.log(Level.FINEST, "In Constructor");
		cs = new CoapServer(); //initilize the server
		InetSocketAddress bindToAddress = new InetSocketAddress( LISTEN_PORT);//get the address
		CoapEndpoint tmp = new CoapEndpoint(bindToAddress); //create endpoint
		cs.addEndpoint(tmp);//add endpoint to server
		tmp.start();//Start this endpoint and all its components.
		driverPort = tmp.getAddress().getPort();

		cs.add(new fddResource());

}
 
Example #20
Source File: TradfriGateway.java    From ThingML-Tradfri with Apache License 2.0 4 votes vote down vote up
protected void initCoap() {
	DtlsConnectorConfig.Builder builder = new DtlsConnectorConfig.Builder(); //new InetSocketAddress(0)
	builder.setPskStore(new StaticPskStore("", security_key.getBytes()));
	coap = new CoapEndpoint(new DTLSConnector(builder.build()), NetworkConfig.getStandard());
}
 
Example #21
Source File: AbstractVertxBasedCoapAdapter.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
private Future<Endpoint> createSecureEndpoint(final NetworkConfig config) {

        final ApplicationLevelInfoSupplier deviceResolver = Optional.ofNullable(honoDeviceResolver)
                .orElse(new DefaultDeviceResolver(context, tracer, getTypeName(), getConfig(), getCredentialsClientFactory()));
        final PskStore store = Optional.ofNullable(pskStore)
                .orElseGet(() -> {
                    if (deviceResolver instanceof PskStore) {
                        return (PskStore) deviceResolver;
                    } else {
                        return new DefaultDeviceResolver(context, tracer, getTypeName(), getConfig(), getCredentialsClientFactory());
                    }
                });

        final DtlsConnectorConfig.Builder dtlsConfig = new DtlsConnectorConfig.Builder();
        dtlsConfig.setServerOnly(true);
        dtlsConfig.setRecommendedCipherSuitesOnly(true);
        dtlsConfig.setClientAuthenticationRequired(getConfig().isAuthenticationRequired());
        dtlsConfig.setAddress(
                new InetSocketAddress(getConfig().getBindAddress(), getConfig().getPort(getPortDefaultValue())));
        dtlsConfig.setApplicationLevelInfoSupplier(deviceResolver);
        dtlsConfig.setPskStore(store);
        dtlsConfig.setRetransmissionTimeout(getConfig().getDtlsRetransmissionTimeout());
        dtlsConfig.setMaxConnections(config.getInt(Keys.MAX_ACTIVE_PEERS));
        addIdentity(dtlsConfig);

        try {
            final DtlsConnectorConfig dtlsConnectorConfig = dtlsConfig.build();
            if (log.isInfoEnabled()) {
                final String ciphers = dtlsConnectorConfig.getSupportedCipherSuites()
                        .stream()
                        .map(cipher -> cipher.name())
                        .collect(Collectors.joining(", "));
                log.info("creating secure endpoint supporting ciphers: {}", ciphers);
            }
            final DTLSConnector dtlsConnector = new DTLSConnector(dtlsConnectorConfig);
            final CoapEndpoint.Builder builder = new CoapEndpoint.Builder();
            builder.setNetworkConfig(config);
            builder.setConnector(dtlsConnector);
            return Future.succeededFuture(builder.build());

        } catch (final IllegalStateException ex) {
            log.warn("failed to create secure endpoint", ex);
            return Future.failedFuture(ex);
        }
    }
 
Example #22
Source File: TradfriGatewayHandler.java    From smarthome with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Returns the coap endpoint that can be used within coap clients.
 *
 * @return the coap endpoint
 */
public CoapEndpoint getEndpoint() {
    return endPoint;
}