Skip to content

Commit

Permalink
Reuse coap-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Aug 26, 2024
1 parent 55f3966 commit 26150c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -137,7 +137,7 @@ public CoapServerBuilder maxIncomingBlockTransferSize(int size) {
return this;
}

private PutOnlyMap<CoapRequestId, CoapPacket> getOrCreateDuplicateDetectorCache() {
private PutOnlyMap<CoapRequestId, CoapPacket> getOrCreateDuplicateDetectorCache(ScheduledExecutorService scheduledExecutorService) {
if (duplicateDetectionCache == null) {
duplicateDetectionCache = new DefaultDuplicateDetectorCache("Default cache", duplicationMaxSize, scheduledExecutorService);
}
Expand Down Expand Up @@ -210,11 +210,9 @@ public CoapServerBuilder requestTagSupplier(RequestTagSupplier requestTagSupplie
public CoapServer build() {
requireNonNull(coapTransport, "Missing transport");
final boolean stopExecutor = scheduledExecutorService == null;
if (scheduledExecutorService == null) {
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
}
final ScheduledExecutorService effectiveExecutorService = scheduledExecutorService != null ? scheduledExecutorService : Executors.newSingleThreadScheduledExecutor();
Timer timer = toTimer(effectiveExecutorService);

Timer timer = toTimer(scheduledExecutorService);
Service<CoapPacket, Boolean> sender = packet -> coapTransport.sendPacket(packet)
.whenComplete((__, throwable) -> logSent(packet, throwable));

Expand Down Expand Up @@ -248,7 +246,7 @@ public CoapServer build() {
.then(sender);

// INBOUND
PutOnlyMap<CoapRequestId, CoapPacket> duplicateDetectorCache = getOrCreateDuplicateDetectorCache();
PutOnlyMap<CoapRequestId, CoapPacket> duplicateDetectorCache = getOrCreateDuplicateDetectorCache(effectiveExecutorService);
DuplicateDetector duplicateDetector = new DuplicateDetector(duplicateDetectorCache, duplicatedCoapMessageCallback);
Service<CoapPacket, CoapPacket> inboundService = duplicateDetector
.andThen(new CoapRequestConverter(midSupplier))
Expand All @@ -271,7 +269,7 @@ public CoapServer build() {
piggybackedExchangeFilter.stop();
duplicateDetectorCache.stop();
if (stopExecutor) {
scheduledExecutorService.shutdown();
effectiveExecutorService.shutdown();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2011-2021 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,8 +16,11 @@
*/
package com.mbed.coap.server;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import com.mbed.coap.client.CoapClient;
import com.mbed.coap.transport.InMemoryCoapTransport;
import java.time.Duration;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,4 +58,23 @@ public void shouldFail_whenIllegalTimeoutValue() throws Exception {
CoapServer.builder().responseTimeout(Duration.ofMillis(-1))
);
}

@Test
public void shouldReuseBuilder() throws Exception {
CoapServer server = new CoapServerBuilder()
.transport(InMemoryCoapTransport.create(5683))
.build().start();

CoapServerBuilder builder = CoapServer.builder()
.transport(InMemoryCoapTransport.create());


for (int i = 0; i < 3; i++) {
CoapClient client = builder.buildClient(server.getLocalSocketAddress());
assertNotNull(client.ping().get());
client.close();
}

server.stop();
}
}

0 comments on commit 26150c7

Please sign in to comment.