Skip to content

Commit 3f9a465

Browse files
committed
🏗️ add coap service
1 parent 263ae04 commit 3f9a465

File tree

7 files changed

+229
-2
lines changed

7 files changed

+229
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright © 2018-2022 The Groza Authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
server:
18+
# Server bind address
19+
address: "${HTTP_BIND_ADDRESS:0.0.0.0}"
20+
# Server bind port
21+
port: "${HTTP_BIND_PORT:8080}"

groza-transport-api/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>groza</artifactId>
7+
<groupId>iot.technology</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>groza-transport-api</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.google.protobuf</groupId>
22+
<artifactId>protobuf-java</artifactId>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.xolstice.maven.plugins</groupId>
30+
<artifactId>protobuf-maven-plugin</artifactId>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
35+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import lombok.Getter;
2+
import lombok.extern.slf4j.Slf4j;
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* @author mushuwei
8+
*/
9+
@Slf4j
10+
@Component
11+
public class CoapServerContext {
12+
13+
@Getter
14+
@Value("${transport.coap.bind_address}")
15+
private String host;
16+
17+
@Getter
18+
@Value("${transport.coap.bind_port}")
19+
private Integer port;
20+
21+
@Getter
22+
@Value("${transport.coap.timeout}")
23+
private Long timeout;
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import org.eclipse.californium.core.CoapServer;
2+
3+
import java.net.UnknownHostException;
4+
5+
/**
6+
* @author mushuwei
7+
*/
8+
public interface CoapServerService {
9+
10+
CoapServer getCoapServer() throws UnknownHostException;
11+
12+
long getTimeout();
13+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import lombok.extern.slf4j.Slf4j;
2+
import org.eclipse.californium.core.CoapServer;
3+
import org.eclipse.californium.core.config.CoapConfig;
4+
import org.eclipse.californium.core.network.CoapEndpoint;
5+
import org.eclipse.californium.core.server.resources.Resource;
6+
import org.eclipse.californium.elements.config.Configuration;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
import javax.annotation.PostConstruct;
11+
import javax.annotation.PreDestroy;
12+
import java.net.InetAddress;
13+
import java.net.InetSocketAddress;
14+
import java.net.UnknownHostException;
15+
import java.util.concurrent.TimeUnit;
16+
17+
import static org.eclipse.californium.core.config.CoapConfig.DEFAULT_BLOCKWISE_STATUS_LIFETIME_IN_SECONDS;
18+
19+
20+
/**
21+
* @author mushuwei
22+
*/
23+
@Slf4j
24+
@Component
25+
public class DefaultCoapServerService implements CoapServerService {
26+
27+
@Autowired
28+
private CoapServerContext coapServerContext;
29+
30+
private CoapServer server;
31+
32+
@PostConstruct
33+
private void init() throws UnknownHostException {
34+
createCoapServer();
35+
}
36+
37+
@PreDestroy
38+
private void shutdown() {
39+
log.info("Stopping CoAP server!");
40+
server.destroy();
41+
log.info("CoAP server stopped!");
42+
}
43+
44+
@Override
45+
public CoapServer getCoapServer() throws UnknownHostException {
46+
if (server != null) {
47+
return server;
48+
} else {
49+
return createCoapServer();
50+
}
51+
}
52+
53+
@Override
54+
public long getTimeout() {
55+
return coapServerContext.getTimeout();
56+
}
57+
58+
private CoapServer createCoapServer() throws UnknownHostException {
59+
Configuration networkConfig = new Configuration();
60+
networkConfig.set(CoapConfig.BLOCKWISE_STRICT_BLOCK2_OPTION, true);
61+
networkConfig.set(CoapConfig.BLOCKWISE_ENTITY_TOO_LARGE_AUTO_FAILOVER, true);
62+
networkConfig.set(CoapConfig.BLOCKWISE_STATUS_LIFETIME, DEFAULT_BLOCKWISE_STATUS_LIFETIME_IN_SECONDS, TimeUnit.SECONDS);
63+
networkConfig.set(CoapConfig.MAX_RESOURCE_BODY_SIZE, 256 * 1024 * 1024);
64+
networkConfig.set(CoapConfig.RESPONSE_MATCHING, CoapConfig.MatcherMode.RELAXED);
65+
networkConfig.set(CoapConfig.PREFERRED_BLOCK_SIZE, 1024);
66+
networkConfig.set(CoapConfig.MAX_MESSAGE_SIZE, 1024);
67+
networkConfig.set(CoapConfig.MAX_RETRANSMIT, 4);
68+
networkConfig.set(CoapConfig.COAP_PORT, coapServerContext.getPort());
69+
server = new CoapServer(networkConfig);
70+
71+
CoapEndpoint.Builder noSecCoapEndpointBuilder = new CoapEndpoint.Builder();
72+
InetAddress addr = InetAddress.getByName(coapServerContext.getHost());
73+
InetSocketAddress sockAddr = new InetSocketAddress(addr, coapServerContext.getPort());
74+
noSecCoapEndpointBuilder.setInetSocketAddress(sockAddr);
75+
76+
noSecCoapEndpointBuilder.setConfiguration(networkConfig);
77+
CoapEndpoint noSecCoapEndpoint = noSecCoapEndpointBuilder.build();
78+
server.addEndpoint(noSecCoapEndpoint);
79+
Resource root = server.getRoot();
80+
GaCoapServerMessageDeliverer messageDeliverer = new GaCoapServerMessageDeliverer(root);
81+
server.setMessageDeliverer(messageDeliverer);
82+
83+
server.start();
84+
return server;
85+
}
86+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import lombok.extern.slf4j.Slf4j;
2+
import org.eclipse.californium.core.coap.OptionSet;
3+
import org.eclipse.californium.core.network.Exchange;
4+
import org.eclipse.californium.core.server.DelivererException;
5+
import org.eclipse.californium.core.server.ServerMessageDeliverer;
6+
import org.eclipse.californium.core.server.resources.Resource;
7+
import org.springframework.util.CollectionUtils;
8+
9+
import java.util.List;
10+
11+
/**
12+
* @author mushuwei
13+
*/
14+
@Slf4j
15+
public class GaCoapServerMessageDeliverer extends ServerMessageDeliverer {
16+
17+
public GaCoapServerMessageDeliverer(Resource root) {
18+
super(root);
19+
}
20+
21+
@Override
22+
protected Resource findResource(Exchange exchange) throws DelivererException {
23+
return super.findResource(exchange);
24+
}
25+
26+
private void validateUriPath(Exchange exchange) {
27+
OptionSet options = exchange.getRequest().getOptions();
28+
List<String> uriPathList = options.getUriPath();
29+
String path = toPath(uriPathList);
30+
if (path != null) {
31+
options.setUriPath(path);
32+
exchange.getRequest().setOptions(options);
33+
}
34+
}
35+
36+
private String toPath(List<String> list) {
37+
if (!CollectionUtils.isEmpty(list) && list.size() == 1) {
38+
final String slash = "/";
39+
String path = list.get(0);
40+
if (path.startsWith(slash)) {
41+
path = path.substring(slash.length());
42+
}
43+
return path;
44+
}
45+
return null;
46+
}
47+
}

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<module>groza-queue</module>
3232
<module>groza-message</module>
3333
<module>groza-dao</module>
34+
<module>groza-transport-api</module>
3435
</modules>
3536

3637
<name>Groza :: Server</name>
@@ -52,7 +53,7 @@
5253
<logback.version>1.2.3</logback.version>
5354
<junit.version>4.12</junit.version>
5455
<mockito.version>3.3.3</mockito.version>
55-
<guava.version>28.2-jre</guava.version>
56+
<guava.version>30.0-jre</guava.version>
5657
<californium.version>3.2.0</californium.version>
5758
<jackson.version>2.12.1</jackson.version>
5859
<commons-lang3.version>3.4</commons-lang3.version>
@@ -431,7 +432,7 @@
431432
<plugin>
432433
<groupId>org.xolstice.maven.plugins</groupId>
433434
<artifactId>protobuf-maven-plugin</artifactId>
434-
<version>0.5.0</version>
435+
<version>0.6.1</version>
435436
<configuration>
436437
<!--
437438
The version of protoc must match protobuf-java. If you don't depend on

0 commit comments

Comments
 (0)