|
@@ -1,53 +1,44 @@
|
|
|
package org.jetlinks.community.network.coap.server;
|
|
package org.jetlinks.community.network.coap.server;
|
|
|
|
|
|
|
|
|
|
|
|
|
-import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
import lombok.Getter;
|
|
import lombok.Getter;
|
|
|
import org.eclipse.californium.core.CoapServer;
|
|
import org.eclipse.californium.core.CoapServer;
|
|
|
|
|
+import org.eclipse.californium.core.network.CoapEndpoint;
|
|
|
import org.jetlinks.community.network.DefaultNetworkType;
|
|
import org.jetlinks.community.network.DefaultNetworkType;
|
|
|
import org.jetlinks.community.network.Network;
|
|
import org.jetlinks.community.network.Network;
|
|
|
import org.jetlinks.community.network.NetworkType;
|
|
import org.jetlinks.community.network.NetworkType;
|
|
|
|
|
+import org.jetlinks.community.network.coap.core.DefaultCoapServer;
|
|
|
import org.jetlinks.community.network.coap.resources.*;
|
|
import org.jetlinks.community.network.coap.resources.*;
|
|
|
import org.jetlinks.core.message.codec.CoapExchangeMessage;
|
|
import org.jetlinks.core.message.codec.CoapExchangeMessage;
|
|
|
import reactor.core.publisher.EmitterProcessor;
|
|
import reactor.core.publisher.EmitterProcessor;
|
|
|
import reactor.core.publisher.Flux;
|
|
import reactor.core.publisher.Flux;
|
|
|
import reactor.core.publisher.FluxSink;
|
|
import reactor.core.publisher.FluxSink;
|
|
|
-
|
|
|
|
|
-import java.util.Collection;
|
|
|
|
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
import java.util.function.Function;
|
|
import java.util.function.Function;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author lifang
|
|
* @author lifang
|
|
|
* @version 1.0.0
|
|
* @version 1.0.0
|
|
|
- * @ClassName MultiCoapServer.java
|
|
|
|
|
|
|
+ * @ClassName NetWorkCoapServer.java
|
|
|
* @Description TODO
|
|
* @Description TODO
|
|
|
* @createTime 2021年12月16日 08:57:00
|
|
* @createTime 2021年12月16日 08:57:00
|
|
|
*/
|
|
*/
|
|
|
-public class MultiCoapServer extends CoapServer implements Network {
|
|
|
|
|
|
|
+public class NetWorkCoapServer extends CoapServer implements Network {
|
|
|
|
|
|
|
|
- private Collection<CoapServer> coapServers;
|
|
|
|
|
|
|
+ private DefaultCoapServer coapServer;
|
|
|
|
|
+ private AtomicBoolean start=new AtomicBoolean(false);
|
|
|
|
|
+ private CoapServerProperties properties;
|
|
|
@Getter
|
|
@Getter
|
|
|
private String id;
|
|
private String id;
|
|
|
- private final String topicFormat="/{productId}/{deviceId}/%s";
|
|
|
|
|
private final EmitterProcessor<CoapExchangeMessage> processor=EmitterProcessor.create();
|
|
private final EmitterProcessor<CoapExchangeMessage> processor=EmitterProcessor.create();
|
|
|
private final FluxSink<CoapExchangeMessage> sink=processor.sink(FluxSink.OverflowStrategy.BUFFER);
|
|
private final FluxSink<CoapExchangeMessage> sink=processor.sink(FluxSink.OverflowStrategy.BUFFER);
|
|
|
|
|
|
|
|
- public MultiCoapServer(String id){
|
|
|
|
|
|
|
+ public NetWorkCoapServer( CoapServerProperties properties,String id){
|
|
|
super();
|
|
super();
|
|
|
|
|
+ this.properties=properties;
|
|
|
this.id=id;
|
|
this.id=id;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void setCoapServer( Collection<CoapServer> servers){
|
|
|
|
|
- if (CollectionUtil.isNotEmpty(coapServers)) {
|
|
|
|
|
- shutdown();
|
|
|
|
|
- }
|
|
|
|
|
- this.coapServers=servers;
|
|
|
|
|
- for (org.eclipse.californium.core.CoapServer coapServer : this.coapServers) {
|
|
|
|
|
- coapServer.start();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public NetworkType getType() {
|
|
public NetworkType getType() {
|
|
|
return DefaultNetworkType.COAP_SERVER;
|
|
return DefaultNetworkType.COAP_SERVER;
|
|
@@ -55,17 +46,15 @@ public class MultiCoapServer extends CoapServer implements Network {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void shutdown() {
|
|
public void shutdown() {
|
|
|
- if (CollectionUtil.isNotEmpty(coapServers)) {
|
|
|
|
|
- for (org.eclipse.californium.core.CoapServer coapServer : coapServers) {
|
|
|
|
|
- coapServer.destroy();
|
|
|
|
|
- }
|
|
|
|
|
- coapServers.clear();
|
|
|
|
|
|
|
+ if (coapServer!=null) {
|
|
|
|
|
+ coapServer.destroy();
|
|
|
|
|
+ coapServer=null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean isAlive() {
|
|
public boolean isAlive() {
|
|
|
- return CollectionUtil.isNotEmpty(coapServers);
|
|
|
|
|
|
|
+ return coapServer!=null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -73,17 +62,27 @@ public class MultiCoapServer extends CoapServer implements Network {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public void reload(CoapServerProperties properties){
|
|
|
|
|
+ this.properties=properties;
|
|
|
|
|
+ this.start.set(false);
|
|
|
|
|
+ coapServer.destroy();
|
|
|
|
|
+ initResoruces();
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public void initResoruces(){
|
|
public void initResoruces(){
|
|
|
- coapServers.forEach(this::initResource);
|
|
|
|
|
|
|
+ if(start.compareAndSet(false,true)){
|
|
|
|
|
+ coapServer=new DefaultCoapServer();
|
|
|
|
|
+ CoapEndpoint endpoint = new CoapEndpoint.Builder()
|
|
|
|
|
+ .setPort(properties.getPort())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ coapServer.addEndpoint(endpoint);
|
|
|
|
|
+ coapServer.add(new MatchAllResource(processor));
|
|
|
|
|
+ coapServer.start();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
public Flux<CoapExchangeMessage> handleAuthRequest(){
|
|
public Flux<CoapExchangeMessage> handleAuthRequest(){
|
|
|
return processor.map(Function.identity());
|
|
return processor.map(Function.identity());
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private void initResource(CoapServer coapServer){
|
|
|
|
|
- coapServer.add(new MatchAllResource(processor));
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|