Skip to content

Commit

Permalink
feat: wrap and unwrap logging transport
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Sep 9, 2024
1 parent 268b9c8 commit 101ed3e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
5 changes: 3 additions & 2 deletions coap-core/src/main/java/com/mbed/coap/server/CoapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.mbed.coap.packet.CoapResponse;
import com.mbed.coap.packet.SeparateResponse;
import com.mbed.coap.transport.CoapTransport;
import com.mbed.coap.transport.LoggingCoapTransport;
import com.mbed.coap.utils.Service;
import java.io.IOException;
import java.net.InetSocketAddress;
Expand All @@ -33,7 +34,7 @@
public class CoapServer {
private static final Logger LOGGER = LoggerFactory.getLogger(CoapServer.class);
private final AtomicBoolean isRunning = new AtomicBoolean(false);
final CoapTransport transport;
private final CoapTransport transport;
private final Consumer<CoapPacket> dispatcher;
private final Service<CoapRequest, CoapResponse> outboundService;
private final Service<SeparateResponse, Boolean> outboundResponseService;
Expand Down Expand Up @@ -131,6 +132,6 @@ public Service<SeparateResponse, Boolean> outboundResponseService() {
}

CoapTransport getTransport() {
return transport;
return LoggingCoapTransport.unwrap(transport);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public CoapServerBuilder transportLogging(boolean isTransportLoggingEnabled) {

public CoapServer build() {
CoapTransport realTransport = requireNonNull(this.coapTransport.get(), "Missing transport");
CoapTransport coapTransport = isTransportLoggingEnabled ? new LoggingCoapTransport(realTransport) : realTransport;
CoapTransport coapTransport = isTransportLoggingEnabled ? LoggingCoapTransport.wrap(realTransport) : realTransport;
final boolean stopExecutor = scheduledExecutorService == null;
final ScheduledExecutorService effectiveExecutorService = scheduledExecutorService != null ? scheduledExecutorService : Executors.newSingleThreadScheduledExecutor();
Timer timer = toTimer(effectiveExecutorService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,25 @@
import org.slf4j.LoggerFactory;

public class LoggingCoapTransport implements CoapTransport {
private static Logger LOGGER = LoggerFactory.getLogger(LoggingCoapTransport.class);
private static final Logger LOGGER = LoggerFactory.getLogger(LoggingCoapTransport.class);

private final CoapTransport transport;

public LoggingCoapTransport(CoapTransport transport) {
public static LoggingCoapTransport wrap(CoapTransport transport) {
if (transport instanceof LoggingCoapTransport) {
return (LoggingCoapTransport) transport;
}
return new LoggingCoapTransport(transport);
}

public static CoapTransport unwrap(CoapTransport transport) {
if (transport instanceof LoggingCoapTransport) {
return ((LoggingCoapTransport) transport).getTransport();
}
return transport;
}

private LoggingCoapTransport(CoapTransport transport) {
this.transport = transport;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mbed.coap.transport;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class LoggingCoapTransportTest {

@Test
void shouldUnwrapTransport() {
CoapTransport transport = Mockito.mock(CoapTransport.class);
LoggingCoapTransport loggingCoapTransport = LoggingCoapTransport.wrap(transport);

assertEquals(transport, LoggingCoapTransport.unwrap(loggingCoapTransport));
assertEquals(transport, LoggingCoapTransport.unwrap(transport));
}

@Test
void shouldNotWrapTransportWhenAlreadyLoggingCoapTransport() {
CoapTransport transport = Mockito.mock(CoapTransport.class);
LoggingCoapTransport loggingCoapTransport = LoggingCoapTransport.wrap(transport);

// when
LoggingCoapTransport wrapped = LoggingCoapTransport.wrap(loggingCoapTransport);

// then
assertEquals(loggingCoapTransport, wrapped);
assertEquals(transport, loggingCoapTransport.getTransport());
}

}
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 @@ -140,7 +140,7 @@ public CoapClient buildClient(InetSocketAddress target) throws IOException {
}

public CoapServer build() {
Service<CoapPacket, Boolean> sender = (isTransportLoggingEnabled ? new LoggingCoapTransport(coapTransport) : coapTransport)::sendPacket;
Service<CoapPacket, Boolean> sender = (isTransportLoggingEnabled ? LoggingCoapTransport.wrap(coapTransport) : coapTransport)::sendPacket;

// NOTIFICATION
Service<SeparateResponse, Boolean> sendNotification = new NotificationValidator()
Expand Down

0 comments on commit 101ed3e

Please sign in to comment.