Skip to content

Commit

Permalink
CoapServerGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Aug 29, 2024
1 parent d5746bd commit f2fd1ab
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.mbed.coap.utils.Timer.toTimer;
import static com.mbed.coap.utils.Validations.require;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import com.mbed.coap.client.CoapClient;
import com.mbed.coap.packet.BlockSize;
import com.mbed.coap.packet.CoapPacket;
Expand Down Expand Up @@ -55,9 +56,11 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Supplier;
import java.util.stream.Stream;

public final class CoapServerBuilder {
private static final long DELAYED_TRANSACTION_TIMEOUT_MS = 120000; //2 minutes
Expand Down Expand Up @@ -286,4 +289,12 @@ public CoapClient buildClient(InetSocketAddress target) throws IOException {
return CoapClient.create(target, build().start());
}

public CoapServerGroup buildGroup(int size) {
List<CoapServer> servers = Stream.generate(this::build)
.limit(size)
.collect(toList());

return new CoapServerGroup(servers);
}

}
51 changes: 51 additions & 0 deletions coap-core/src/main/java/com/mbed/coap/server/CoapServerGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.server;

import static com.mbed.coap.utils.Validations.require;
import static java.util.stream.Collectors.toList;
import java.io.IOException;
import java.util.List;

public class CoapServerGroup {
private final List<CoapServer> servers;

CoapServerGroup(List<CoapServer> servers) {
require(!servers.isEmpty(), "At least one server required");
this.servers = servers;
}

public CoapServerGroup start() throws IOException {
for (CoapServer server : servers) {
server.start();
}
return this;
}

public void stop() {
for (CoapServer server : servers) {
server.stop();
}
}

public boolean isRunning() {
return servers.stream().allMatch(CoapServer::isRunning);
}

public List<Integer> getLocalPorts() {
return servers.stream().map(server -> server.getLocalSocketAddress().getPort()).collect(toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.server;

import static com.mbed.coap.packet.CoapRequest.get;
import static com.mbed.coap.utils.Networks.localhost;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import com.mbed.coap.client.CoapClient;
import com.mbed.coap.packet.CoapResponse;
import com.mbed.coap.transport.InMemoryCoapTransport;
import org.junit.jupiter.api.Test;

class CoapServerGroupTest {

private final CoapServerBuilder builder = CoapServer.builder()
.transport(InMemoryCoapTransport::create)
.route(RouterService.builder()
.get("/test", (req) -> CoapResponse.ok("test").toFuture())
);


@Test
void test() throws Exception {
// given
CoapServerGroup servers = builder.buildGroup(3).start();
CoapClient client = builder.buildClient(localhost(servers.getLocalPorts().get(0)));
CoapClient client2 = builder.buildClient(localhost(servers.getLocalPorts().get(2)));

// when
assertEquals("test", client.sendSync(get("/test")).getPayloadString());
assertEquals("test", client2.sendSync(get("/test")).getPayloadString());

// then
client.close();
client2.close();
servers.stop();
assertFalse(servers.isRunning());
}

}

0 comments on commit f2fd1ab

Please sign in to comment.