Commit cc592966 authored by Yann Garcia's avatar Yann Garcia
Browse files

Enahnce params && add timer class for oneM2M ATS

parent b6218a70
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
/*!
 * \file      base_timer.hh
 * \brief     Header file for base_time functionality.
 * \author    ETSI STF525
 * \copyright ETSI Copyright Notification
 *            No part may be reproduced except as authorized by written permission.
 *            The copyright and the foregoing restriction extend to reproduction in all media.
 *            All rights reserved.
 * \version   0.1
 */
#pragma once

#include <iostream>
#include <thread>
#include <chrono>

/**
 * \class base_timer
 * \brief This class provides timer feature based on threading
 */
class base_timer {
  std::thread _thread;
  bool _running = false;

public:
  base_timer(): _thread(), _running{false} {};
  virtual ~base_timer() {};

  inline void start(const std::chrono::milliseconds& interval, const std::function<void(void*)>& fn, void* arg = nullptr) {
    _running = true;
    _thread = std::thread([=]() {
      while (_running == true) {
        std::this_thread::sleep_for(interval);
        fn(arg);
      } // End of 'while' statement
    });
  }

  inline void stop() {
    if (_running) {
      _running = false;
      _thread.join();
    }
  }
};
+11 −0
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ public: //! \publicsection
  static const std::string& ip_dst;      //! Destination IP address parameter name
  static const std::string& ip_proto;    //! IP proto parameter name

  static const std::string& device_mode;  //! To indicate to the lower layer to act as a standalone device

  static const std::string& server;       //! Remote server address (e.g. www.etsi.org)
  static const std::string& port;         //! Remote server port. Default: 80
  static const std::string& use_ssl;      //! Set to 1 to use SSL to communicate with the HTTP server. Default: false
@@ -54,8 +56,10 @@ public: //! \publicsection
  static const std::string& uri;          //! HTTP URI value. Default: /
  static const std::string& host;         //! HTTP Host value. Default: 127.0.0.1
  static const std::string& content_type; //! HTTP Content-type value. Default: application/text
  static const std::string& headers;      //! HTTP headers list to add in the request. ';' will be replaced by '\r\n'. Default: omitted. E.g. "X-M2M-RI:8787;X-M2M-Origin:CAdmin;X-M2M-RVI:5"

  static const std::string& codecs; //! List of codecs to use for HTTP application layers
  static const std::string& serialization; //! Message serialization format

  /*!
   * \brief Default constructor
@@ -84,6 +88,13 @@ public: //! \publicsection
   * \brief Provides a dump of the content of this instance
   */
  void log();
  /*!
   * \fn void add(const params &p_params, bool p_overwrite = false);
   * \brief Add the content of p_params to this instance
   * \param[in] p_params An existing instance of a params object to add to this
   * \param[in] p_overwrite If set to true, existing parameters are overwritten
   */
  void add(const params &p_params, bool p_overwrite = false);
  /*!
   * \fn void reset();
   * \brief Reset the content of this instance
+53 −26
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ const std::string& params::ip_src = std::string("ip_src");
const std::string& params::ip_dst          = std::string("ip_dst");
const std::string& params::ip_proto        = std::string("ip_proto");

const std::string& params::device_mode     = std::string("device_mode");

const std::string& params::server          = std::string("server");
const std::string& params::port            = std::string("port");
const std::string& params::use_ssl         = std::string("use_ssl");
@@ -48,8 +50,10 @@ const std::string& params::method = std::string("method");
const std::string& params::uri             = std::string("uri");
const std::string& params::host            = std::string("host");
const std::string& params::content_type    = std::string("content_type");
const std::string& params::headers         = std::string("headers");

const std::string& params::codecs          = std::string("codecs");
const std::string& params::serialization   = std::string("serialization");

void params::convert(params &p_param, const std::string p_parameters) {
  // Sanity checks
@@ -74,6 +78,29 @@ void params::convert(params &p_param, const std::string p_parameters) {
  loggers::get_instance().log("<<< params::convert");
}

void params::add(const params &p_params, bool p_overwrite) {
  loggers::get_instance().log(">>> params::add");
  p_params.log();
  loggers::get_instance().log("params::add: current params before add:");
  this->log();
  
  for (const_iterator it = p_params.cbegin(); it != p_params.cend(); ++it) {
    loggers::get_instance().log("params::add: processing %s", it->first.c_str());
    const_iterator n = this->find(it->first);
    if (n == this->cend()) {
      loggers::get_instance().log("params::add: Insert %s", it->first.c_str());
      this->insert(std::pair<std::string, std::string>(it->first, it->second));
    }  else if (p_overwrite) {
      loggers::get_instance().log("params::add: Overwrite %s", it->first.c_str());
      (*this)[it->first] = it->second;
    } else {
      loggers::get_instance().log("params::add: Skip %s", it->first.c_str());
    }
  } // End of 'for' statement 
  loggers::get_instance().log("<<< params::add");
  this->log();
}

void params::log() const {
  loggers::get_instance().log("params::log");
  if (size() == 0) {