Skip to content

Commit

Permalink
Make CoapServerBuilder reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Aug 26, 2024
1 parent 55f3966 commit 47adb08
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
22 changes: 10 additions & 12 deletions coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java
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,11 +137,11 @@ public CoapServerBuilder maxIncomingBlockTransferSize(int size) {
return this;
}

private PutOnlyMap<CoapRequestId, CoapPacket> getOrCreateDuplicateDetectorCache() {
if (duplicateDetectionCache == null) {
duplicateDetectionCache = new DefaultDuplicateDetectorCache("Default cache", duplicationMaxSize, scheduledExecutorService);
private PutOnlyMap<CoapRequestId, CoapPacket> getOrCreateDuplicateDetectorCache(ScheduledExecutorService scheduledExecutorService) {
if (duplicateDetectionCache != null) {
return duplicateDetectionCache;

Check warning on line 142 in coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java

View check run for this annotation

Codecov / codecov/patch

coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java#L142

Added line #L142 was not covered by tests
}
return duplicateDetectionCache;
return new DefaultDuplicateDetectorCache("Default cache", duplicationMaxSize, scheduledExecutorService);
}

private CapabilitiesResolver capabilities() {
Expand Down Expand Up @@ -208,13 +208,11 @@ public CoapServerBuilder requestTagSupplier(RequestTagSupplier requestTagSupplie
}

public CoapServer build() {
requireNonNull(coapTransport, "Missing transport");
CoapTransport coapTransport = requireNonNull(this.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,12 @@
*/
package com.mbed.coap.server;

import static com.mbed.coap.transport.InMemoryCoapTransport.create;
import static com.mbed.coap.utils.Networks.localhost;
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 java.time.Duration;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -55,4 +59,23 @@ public void shouldFail_whenIllegalTimeoutValue() throws Exception {
CoapServer.builder().responseTimeout(Duration.ofMillis(-1))
);
}

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

// when, builder is created
CoapServerBuilder builder = CoapServer.builder();

// then, it can be reused multiple times
CoapClient client1 = builder.transport(create()).buildClient(localhost(5683));
CoapClient client2 = builder.transport(create()).buildClient(localhost(5683));
assertNotNull(client1.ping().get());
assertNotNull(client2.ping().get());

client1.close();
server.stop();
}
}

0 comments on commit 47adb08

Please sign in to comment.