-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MultiCoapServer, helper class to support multiple Netty channels
- Loading branch information
Showing
5 changed files
with
219 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
coap-netty/src/main/java/org/opencoap/coap/netty/MultiCoapServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 org.opencoap.coap.netty; | ||
|
||
import static com.mbed.coap.utils.Validations.require; | ||
import static java.util.stream.Collectors.toList; | ||
import static org.opencoap.coap.netty.CoapCodec.EMPTY_RESOLVER; | ||
import com.mbed.coap.server.CoapServer; | ||
import com.mbed.coap.server.CoapServerBuilder; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.EventLoopGroup; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class MultiCoapServer { | ||
|
||
private final List<CoapServer> servers; | ||
|
||
private MultiCoapServer(List<CoapServer> servers) { | ||
require(!servers.isEmpty(), "At least one server required"); | ||
this.servers = servers; | ||
} | ||
|
||
public static MultiCoapServer create(CoapServerBuilder builder, Bootstrap bootstrap) { | ||
return create(builder, bootstrap, Integer.MAX_VALUE); | ||
} | ||
|
||
public static MultiCoapServer create(CoapServerBuilder builder, Bootstrap bootstrap, int limitInstances) { | ||
EventLoopGroup eventLoopGroup = bootstrap.config().group(); | ||
|
||
// create as many servers as there are executors in the event loop group | ||
List<CoapServer> servers = StreamSupport.stream(eventLoopGroup.spliterator(), false) | ||
.limit(limitInstances) | ||
.map(executor -> builder | ||
.transport(new NettyCoapTransport(bootstrap, EMPTY_RESOLVER)) | ||
.executor(executor) | ||
.build() | ||
) | ||
.collect(toList()); | ||
|
||
return new MultiCoapServer(servers); | ||
} | ||
|
||
public MultiCoapServer start() throws IOException { | ||
for (CoapServer server : servers) { | ||
server.start(); | ||
} | ||
return this; | ||
} | ||
|
||
public void stop() { | ||
for (CoapServer server : servers) { | ||
server.stop(); | ||
} | ||
} | ||
|
||
public List<Integer> getLocalPorts() { | ||
return servers.stream().map(server -> server.getLocalSocketAddress().getPort()).collect(toList()); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
coap-netty/src/test/java/org/opencoap/coap/netty/MultiChannelNettyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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 org.opencoap.coap.netty; | ||
|
||
import static com.mbed.coap.packet.CoapRequest.get; | ||
import static com.mbed.coap.packet.CoapResponse.ok; | ||
import static com.mbed.coap.transport.udp.DatagramSocketTransport.udp; | ||
import static com.mbed.coap.utils.Assertions.assertEquals; | ||
import static com.mbed.coap.utils.Networks.localhost; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import com.mbed.coap.client.CoapClient; | ||
import com.mbed.coap.server.CoapServer; | ||
import com.mbed.coap.server.CoapServerBuilder; | ||
import com.mbed.coap.server.RouterService; | ||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.epoll.Epoll; | ||
import io.netty.channel.epoll.EpollDatagramChannel; | ||
import io.netty.channel.epoll.EpollEventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.DatagramChannel; | ||
import io.netty.channel.socket.nio.NioDatagramChannel; | ||
import io.netty.channel.unix.UnixChannelOption; | ||
import io.netty.util.concurrent.DefaultThreadFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
public class MultiChannelNettyTest { | ||
private static final int THREADS = 3; | ||
private final Bootstrap bootstrap = createBootstrap(); | ||
|
||
@Test | ||
void test() throws Exception { | ||
CoapServerBuilder builder = CoapServer.builder() | ||
.route(RouterService.builder() | ||
.get("/test", __ -> ok("OK").toFuture()) | ||
); | ||
MultiCoapServer coapServer = MultiCoapServer.create(builder, bootstrap).start(); | ||
|
||
// verify that all channels are working | ||
for (int i = 0; i < THREADS; i++) { | ||
CoapClient client = CoapServer.builder() | ||
.transport(udp()) | ||
.buildClient(localhost(coapServer.getLocalPorts().get(i))); | ||
|
||
assertTrue(client.ping().get()); | ||
assertEquals(ok("OK"), client.sendSync(get("/test"))); | ||
} | ||
|
||
coapServer.stop(); | ||
} | ||
|
||
private Bootstrap createBootstrap() { | ||
if (Epoll.isAvailable()) { | ||
return createEpollBootstrap(); | ||
} | ||
|
||
return createNioBootstrap(); | ||
} | ||
|
||
private Bootstrap createNioBootstrap() { | ||
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(THREADS, new DefaultThreadFactory("udp", true)); | ||
return new Bootstrap() | ||
.group(eventLoopGroup) | ||
.localAddress(0) // this will cause server binding to multiple ports | ||
.channel(NioDatagramChannel.class) | ||
.handler(new ChannelInitializer<DatagramChannel>() { | ||
@Override | ||
protected void initChannel(DatagramChannel ch) { | ||
// do nothing | ||
} | ||
}); | ||
} | ||
|
||
private Bootstrap createEpollBootstrap() { | ||
EventLoopGroup eventLoopGroup = new EpollEventLoopGroup(3, new DefaultThreadFactory("udp", true)); | ||
|
||
return new Bootstrap() | ||
.group(eventLoopGroup) | ||
.option(UnixChannelOption.SO_REUSEPORT, true) | ||
.localAddress(65001) // bind multiple times on single port | ||
.channel(EpollDatagramChannel.class) | ||
.handler(new ChannelInitializer<DatagramChannel>() { | ||
@Override | ||
protected void initChannel(DatagramChannel ch) { | ||
// do nothing | ||
} | ||
}); | ||
} | ||
} |