org.eclipse.californium.core.CoapResource Java Examples

The following examples show how to use org.eclipse.californium.core.CoapResource. 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: HCoapServer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public HCoapServer(List<String> resourceList, int port, int sec_port) throws SocketException {
	this.port = port;
	this.sec_port = sec_port;

	for(String res : resourceList) {
		
		String[] split = res.split("\\^");
		if (split == null) continue;
		
		log.debug("resource[0]: {}", split[0] );
		add(new HCoapResource(split[0]));
		if(split.length >= 2) {
			log.debug("resource[1]: {}", split[1] );
			add(new CoapResource(split[0]).add(new HCoapResource(split[1])));
		}
	}
	add(new HCoapResource("~"));	// for SP-relative Resource ID
	add(new HCoapResource("_"));	// Absolute Resource ID
	
	addEndpoints();
}
 
Example #2
Source File: TestCoapClientTarget.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
  int port =  TestHttpClientTarget.getFreePort();
  coapServer = new CoapServer(NetworkConfig.createStandardWithoutFile(), port);
  coapServer.add(new CoapResource("test") {
    @Override
    public void handlePOST(CoapExchange exchange) {
      serverRequested = true;
      if (returnErrorResponse) {
        exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR);
        return;
      }
      requestPayload = new String(exchange.getRequestPayload());
      exchange.respond(CoAP.ResponseCode.VALID);
    }
  });
  resourceURl = "coap://localhost:" + port + "/test";
  coapServer.start();
}
 
Example #3
Source File: HCoapServer.java    From SI with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public HCoapServer(List<String> resourceList, int port, int sec_port) throws SocketException {
	this.port = port;
	this.sec_port = sec_port;

	for(String res : resourceList) {
		
		String[] split = res.split("\\^");
		if (split == null) continue;
		
		log.debug("resource[0]: {}", split[0] );
		add(new HCoapResource(split[0]));
		if(split.length >= 2) {
			log.debug("resource[1]: {}", split[1] );
			add(new CoapResource(split[0]).add(new HCoapResource(split[1])));
		}
	}
	add(new HCoapResource("~"));	// for SP-relative Resource ID
	add(new HCoapResource("_"));	// Absolute Resource ID
	
	addEndpoints();
}
 
Example #4
Source File: HelloWorld2.java    From hands-on-coap with Eclipse Public License 1.0 6 votes vote down vote up
public static void main(String[] args) {

        // binds on UDP port 5683
        CoapServer server = new CoapServer();

        // "hello"
        server.add(new HelloResource());

        // "subpath/Another"
        CoapResource path = new CoapResource("subpath");
        path.add(new AnotherResource());
        server.add(path);

        // "removeme!, "time", "writeme!"
        server.add(new RemovableResource(), new TimeResource(), new WritableResource());

        server.start();
    }
 
Example #5
Source File: AbstractVertxBasedCoapAdapterTest.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Verifies that the resources registered with the adapter are always
 * executed on the adapter's vert.x context.
 *
 * @param ctx The helper to use for running async tests on vertx.
 */
@Test
public void testResourcesAreRunOnVertxContext(final VertxTestContext ctx) {

    // GIVEN an adapter
    final Context context = vertx.getOrCreateContext();
    final CoapServer server = getCoapServer(false);
    // with a resource
    final Promise<Void> resourceInvocation = Promise.promise();
    final Resource resource = new CoapResource("test") {

        @Override
        public void handleGET(final CoapExchange exchange) {
            ctx.verify(() -> assertThat(Vertx.currentContext()).isEqualTo(context));
            resourceInvocation.complete();
        }
    };

    final AbstractVertxBasedCoapAdapter<CoapAdapterProperties> adapter = getAdapter(server, true, s -> {});
    adapter.setResources(Collections.singleton(resource));

    final Promise<Void> startupTracker = Promise.promise();
    adapter.init(vertx, context);
    adapter.start(startupTracker);

    startupTracker.future()
        .compose(ok -> {
            // WHEN the resource receives a GET request
            final Request request = new Request(Code.GET);
            final Exchange getExchange = new Exchange(request, Origin.REMOTE, mock(Executor.class));
            final ArgumentCaptor<VertxCoapResource> resourceCaptor = ArgumentCaptor.forClass(VertxCoapResource.class);
            verify(server).add(resourceCaptor.capture());
            resourceCaptor.getValue().handleRequest(getExchange);
            // THEN the resource's handler has been run on the adapter's vert.x event loop
            return resourceInvocation.future();
        })
        .onComplete(ctx.completing());
}
 
Example #6
Source File: IOTCoapServer.java    From IOT-Technical-Guide with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws UnknownHostException {

        CoapServer server = new CoapServer();


        server.add(new HelloResource());

        CoapResource path = new CoapResource("subpath");
        path.add(new AnotherResource());
        server.add(path);

        server.add(new RemovableResource(), new TimeResource(), new WritableResource());


        server.start();
    }
 
Example #7
Source File: CoapTransportService.java    From Groza with Apache License 2.0 4 votes vote down vote up
private void createResources() {
    CoapResource api = new CoapResource(API);
    api.add(new CoapTransportResource(processor,authService,adaptor,V1,timeout, quotaService));
    server.add(api);
}
 
Example #8
Source File: CoapTransportService.java    From iotplatform with Apache License 2.0 4 votes vote down vote up
private void createResources() {
  CoapResource api = new CoapResource(API);
  api.add(new CoapTransportResource(msgProducer, attributesService, authService, V1, timeout));
  server.add(api);
}
 
Example #9
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Constructs a new CoAP Exchange object representing the specified exchange
 * and Resource.
 * 
 * @param exchange the exchange
 * @param resource the resource
 */
public CoapExchange(Exchange exchange, CoapResource resource) {
	if (exchange == null) throw new NullPointerException();
	if (resource == null) throw new NullPointerException();
	this.exchange = exchange;
	this.resource = resource;
}
 
Example #10
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Constructs a new CoAP Exchange object representing the specified exchange
 * and Resource.
 * 
 * @param exchange the exchange
 * @param resource the resource
 */
public CoapExchange(Exchange exchange, CoapResource resource) {
	if (exchange == null) throw new NullPointerException();
	if (resource == null) throw new NullPointerException();
	this.exchange = exchange;
	this.resource = resource;
}