【发布时间】:2022-09-25 15:01:42
【问题描述】:
我正在尝试构建一个系统,在该系统中我可以通过互联网将一些设备连接到服务器。 我想通过 CoAP (10-30FPS) 传输一些数据,帧大小 = 3KB。 首先,我使用了 Aiocoap,它可以发送高达 100FPS 但占用过多 CPU, 请求是 NON,在 Aiocoap 中的丢失率很低, 而 Eclipse/Californium 不能发送超过 3FPS, 当我使用更高的 FPS 时,我要么只收到每条消息的第一块,要么什么也没收到,大多数时候也没有订购。
我想知道这是否是 Californium 的真实性能,还是我以错误的方式使用它?
我将分享一些代码:
服务器.java
static class CoapObserverServer extends CoapResource {
int i = -1;
public CoapObserverServer() {
super(\"alarm\");
setObservable(true); // enable observing
setObserveType(Type.NON); // configure the notification type to CONs
getAttributes().setObservable(); // mark observable in the Link-Format
System.out.println(this);
// schedule a periodic update task, otherwise let events call changed()
//new Timer().schedule(new UpdateTask(), 0, 1000/2);
}
private class UpdateTask extends TimerTask {
@Override
public void run() {
changed(); // notify all observers
}
}
@Override
public void handleGET(CoapExchange exchange) {
// the Max-Age value should match the update interval
exchange.setMaxAge(1);
//++i;
int leng = 2000;
String s = \"\" + i + \"-\" + fillString(\'X\', leng - 1 - Integer.toString(i).len>
exchange.respond(s);
}
public static String fillString(char fillChar, int count){
// creates a string of \'x\' repeating characters
char[] chars = new char[count];
while (count>0) chars[--count] = fillChar;
return new String(chars);
}
@Override
public void handleDELETE(CoapExchange exchange) {
delete(); // will also call clearAndNotifyObserveRelations(ResponseCode.NOT_>
exchange.respond(ResponseCode.DELETED);
}
@Override
public void handlePUT(CoapExchange exchange) {
exchange.accept();
int format = exchange.getRequestOptions().getContentFormat();
if (format == MediaTypeRegistry.TEXT_PLAIN) {
// ...
String plain = exchange.getRequestText();
try{
i = Integer.valueOf(plain);
} catch(NumberFormatException ex){
System.out.println(\"error converting string\"+ plain);
}
exchange.respond(ResponseCode.CHANGED);
changed(); // notify all observers
}
}
观察者.java
private static final File CONFIG_FILE = new File(\"Californium3.properties\");
private static final String CONFIG_HEADER = \"Californium CoAP Properties file for client\";
private static final int DEFAULT_MAX_RESOURCE_SIZE = 2 * 1024 * 1024; // 2 MB
private static final int DEFAULT_BLOCK_SIZE = 512;
static {
CoapConfig.register();
UdpConfig.register();
}
private static DefinitionsProvider DEFAULTS = new DefinitionsProvider() {
@Override
public void applyDefinitions(Configuration config) {
config.set(CoapConfig.MAX_RESOURCE_BODY_SIZE, DEFAULT_MAX_RESOURCE_SIZE);
config.set(CoapConfig.MAX_MESSAGE_SIZE, DEFAULT_BLOCK_SIZE);
config.set(CoapConfig.PREFERRED_BLOCK_SIZE, DEFAULT_BLOCK_SIZE);
}
};
private static class AsynchListener implements CoapHandler {
@Override
public void onLoad(CoapResponse response) {
System.out.println( response.getResponseText() );
}
@Override
public void onError() {
System.err.println(\"Error\");
}
}
/*
* Application entry point.
*/
public static void main(String args[]) {
Configuration config = Configuration.createWithFile(CONFIG_FILE, CONFIG_HEADER, DEFAULTS);
Configuration.setStandard(config);
URI uri = null; // URI parameter of the request
if (args.length > 0) {
// input URI from command line arguments
try {
uri = new URI(args[0]);
} catch (URISyntaxException e) {
System.err.println(\"Invalid URI: \" + e.getMessage());
System.exit(-1);
}
CoapClient client = new CoapClient(uri);
client.useNONs();
// observe
AsynchListener asynchListener = new AsynchListener();
CoapObserveRelation observation = client.observe(asynchListener);
// User presses ENTER to exit
System.out.println(\"Press ENTER to exit...\");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try { br.readLine(); } catch (IOException e) { }
System.out.println(\"Exiting...\");
observation.proactiveCancel();
}
因此,我通过向具有计数器 0-50 的服务器发送 PUT 请求来控制 FPS。
标签: java udp network-protocols coap californium