[英]Adding new resources dynamically - CoAP
Once CoAP server is started I need to add new resources dynamically. CoAP服务器启动后,我需要动态添加新资源。 But I have to stop and start the server again in order to access new resources.
但是我必须停止并重新启动服务器才能访问新资源。 I suppose adding new resources same as adding a new HTTP servlet into already started HTTP server.
我想添加新资源与将新HTTP Servlet添加到已启动的HTTP服务器中相同。
Here I added source code which is used for adding dynamic resources. 在这里,我添加了用于添加动态资源的源代码。 If I am missing anything here let me know.
如果我在这里想念任何东西,请告诉我。
private static CoapServer server;
public CoAPEventAdapter(InputEventAdapterConfiguration eventAdapterConfiguration,
Map<String, String> globalProperties) {
this.eventAdapterConfiguration = eventAdapterConfiguration;
this.globalProperties = globalProperties;
if(server == null){
server = new CoapServer();
server.start();
}
}
@Override
public void connect() {
registerDynamicEndpoint(eventAdapterConfiguration.getName());
isConnected = true;
}
private void registerDynamicEndpoint(String adapterName) {
server.stop();
server.add(new HelloWorldResource(adapterName));
server.start();
}
class HelloWorldResource extends CoapResource {
public HelloWorldResource(String resourceName) {
// set resource identifier
super(resourceName);
// set display name
getAttributes().setTitle("Hello-World Resource");
}
@Override
public void handleGET(CoapExchange exchange) {
// respond to the request
exchange.respond("Hello World!");
}
}
I've just tried to add a CoapResource dynamically: 我刚刚尝试动态添加CoapResource:
@Override
public void handleGET(CoapExchange exchange) {
server.getRoot().add(new CoapResource("dynstatus") {
@Override
public void handleGET(CoapExchange exchange) {
System.err.println("dynstatus!!!");
}
});
And was able to call it. 并能够调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.