CoAP协议开源代码之FreeCoap试炼

1. FreeCoAP

2. 源代码目录

├── build-aux
├── certs
├── docs
├── lib
│   ├── include
│   └── src
├── proxy
│   ├── common
│   │   ├── include
│   │   └── src
│   └── http_coap
│       ├── include
│       └── src
├── sample
│   ├── reg_client
│   ├── reg_server
│   ├── time_client
│   └── time_server
└── test
    ├── test_coap_client
    ├── test_coap_mem
    ├── test_coap_msg
    ├── test_coap_server
    ├── test_config
    ├── test_cross
    ├── test_data_buf
    ├── test_http_client
    ├── test_http_msg
    ├── test_lock
    ├── test_proxy_http_coap
    ├── test_thread
    ├── test_tls_client
    ├── test_tls_server
    └── test_uri

3. 编译

  • 创建安装脚本

    cd /home/xxx/work/coap/FreeCoAP-master
    autoreconf --install
    
  • 编译

    • 在CentOs7中安装gnutls

      yum install gnutls*  # 注意: 编译时,源代码中有include <gnutls/x509.h>,因此,还需要安装gnutls-develop
      # 或安装
      yum install gnutls-develop
      # 查看头文件
      ls /usr/include/gnutls/x509.h
      /usr/include/gnutls/x509.h 
      # 创建libgnutls.so的软链接
      cd /lib
      ln -s /usr/lib64/libgnutls.so.28.43.3 libgnutls.so
      
    • 编译libfreecoap.so

      cd /home/xxx/work/coap/FreeCoAP-master
      mkdir bin
      ./configure --prefix=/home/xxx/work/coap/FreeCoAP-master/bin
      make
      make install
      # 查看输出
      ll /home/xxx/work/coap/FreeCoAP-master/bin/lib/
      -rw-r--r--. 1 root root 268930 4月  22 11:52 libfreecoap.a
      -rwxr-xr-x. 1 root root    988 4月  22 11:52 libfreecoap.la
      lrwxrwxrwx. 1 root root     20 4月  22 11:52 libfreecoap.so -> libfreecoap.so.0.0.5
      lrwxrwxrwx. 1 root root     20 4月  22 11:52 libfreecoap.so.0 -> libfreecoap.so.0.0.5
      -rwxr-xr-x. 1 root root 181304 4月  22 11:52 libfreecoap.so.0.0.5
      

      注意: The FreeCoAP library built using this process requires GnuTLS and cannot be used without DTLS (编译依赖需要gnutls库,生成的freecoap库没有dtls不能使用)

  • 例程

    • 基于CoAP/IPv4的时间服务器和客户端
      $ cd FreeCoAP/sample/time_server
      $ make dtls=n
      $ ./time_server 0.0.0.0 10000
      (In a different terminal)
      $ cd FreeCoAP/sample/time_client
      $ make dtls=n
      $ ./time_client 127.0.0.1 10000
      
    • 基于CoAP/DTLS/IPv4的时间服务器和客户端
      $ cd FreeCoAP/sample/time_server
      $ make
      $ ./time_server 0.0.0.0 10000
      (In a different terminal)
      $ cd FreeCoAP/sample/time_client
      $ make
      $ ./time_client 127.0.0.1 10000
      

4. 测试(time server/client)

  • 测试得到的服务器、客户端的吞吐率

    环境: CentOS 7 64bit, PC, Intel® Core™ i3-6100 CPU @ 3.70GHz

    • 连续请求 100000 次, 41254.12 times / s
    • 连续请求 500000 次, 36922.16 times / s
  • 测试中发现的问题

    • 在time_client的main.c中注释掉

      ret = time_client_get_time(&client, buf, sizeof(buf));
      if (ret < 0)
      {
              
              
        ...
      }
      /* sleep(1); */
      

      通信中断,报告"Bad message"错误。调试,错误报告出处在coap_client.c的coap_client_exchange_con函数中

      static int coap_client_exchange_con(coap_client_t *client, coap_msg_t *req, coap_msg_t *resp)
      {
              
              
       ssize_t num = 0;
       int ret = 0;
      
      /*  wait for piggy-backed response in ack message
       *  or ack message and separate response message
      */
      coap_log_info("Expecting acknowledgement from host %s and port %s", client->server_host, client->server_port);
      coap_client_start_ack_timer(client);
      while (1)
      {
              
              
          ret = coap_client_listen_ack(client, req);
          ...
          if (coap_msg_get_msg_id(resp) == coap_msg_get_msg_id(req))
          {
              
              
              if (coap_msg_get_type(resp) == COAP_MSG_ACK)
              {
              
              
                  if (coap_msg_is_empty(resp))
                  {
              
              
                      /* received ack message, wait for separate response message */
                      coap_log_info("Received acknowledgement from host %s and port %s", client->server_host, client->server_port);
                      return coap_client_exchange_sep(client, req, resp);
                  }
                  else if (coap_client_match_token(req, resp)) /* 错误发生地: token不等 */
                  {
              
              
                      return coap_client_handle_piggybacked_response(client, resp);
                  }
              }
              else if (coap_msg_get_type(resp) == COAP_MSG_RST)
              {
              
              
                  return -ECONNRESET;
              }
              coap_client_reject(client, resp);
              /* 错误报告地: Bad message */
              return -EBADMSG;
          }
          else if (coap_client_match_token(req, resp))
      

      如上所述,Root cause 应当是server响应ack时,未更新 token 所致,正在调试中……

猜你喜欢

转载自blog.csdn.net/hylaking/article/details/89473172