<commit_before>﻿#include "DpaMessage.h"
#include "DpaHandler.h"
#include "IqrfLogging.h"

DpaHandler::DpaHandler(IChannel* dpa_interface) {
  if (dpa_interface == nullptr) {
    throw std::invalid_argument("dpa_interface argument can not be nullptr.");
  }
  default_timeout_ms_ = kDefaultTimeout;

  dpa_interface_ = dpa_interface;

  dpa_interface_->registerReceiveFromHandler([&](const std::basic_string<unsigned char>& msg) -> int {
    ResponseHandler(msg);
    return 0;
  });

  current_request_ = new DpaRequest();
}

DpaHandler::~DpaHandler() {
  delete current_request_;
}

void DpaHandler::ResponseHandler(const std::basic_string<unsigned char>& message) {
  if (message.length() == 0)
    return;

  DpaMessage received_message;
  try {
    received_message.FillFromResponse(message.data(), message.length());
  }
  catch (std::exception& e) {
    CATCH_EX("in processing msg", std::exception, e);
    return;
  }
  if (!ProcessMessage(received_message)) {
    ProcessUnexpectedMessage(received_message);
  }
  {
    std::unique_lock<std::mutex> lck(condition_variable_mutex_);
    condition_variable_.notify_one();
  }
}

void DpaHandler::SendDpaMessage(const DpaMessage& message, DpaTransaction* responseHndl) {
  if (IsDpaMessageInProgress()) {
    throw std::logic_error("Other Dpa Message is in progress.");
  }

  try {
    dpa_interface_->sendTo(std::basic_string<unsigned char>(message.DpaPacketData(), message.Length()));
  }
  catch (std::exception& e) {
    throw std::logic_error("Message was not send.");
  }

  delete current_request_;
  if (!responseHndl)
    current_request_ = new DpaRequest();
  else
    current_request_ = new DpaRequest(responseHndl);
  current_request_->DefaultTimeout(default_timeout_ms_);
  current_request_->ProcessSentMessage(message);
}

void DpaHandler::ExecuteDpaTransaction(DpaTransaction& dpaTransaction)
{
  const DpaMessage& message = dpaTransaction.getMessage();

  int32_t remains(0);
  int32_t defaultTimeout = Timeout();
  int32_t requiredTimeout = dpaTransaction.getTimeout();

  if (requiredTimeout > 0)
    Timeout(requiredTimeout);

  DpaRequest::DpaRequestStatus status(DpaRequest::kCreated);<commit_msg>Added tracing of both sent & received traffic on DPA interface<commit_after>﻿#include "DpaMessage.h"
#include "DpaHandler.h"
#include "IqrfLogging.h"

DpaHandler::DpaHandler(IChannel* dpa_interface) {
  if (dpa_interface == nullptr) {
    throw std::invalid_argument("dpa_interface argument can not be nullptr.");
  }
  default_timeout_ms_ = kDefaultTimeout;

  dpa_interface_ = dpa_interface;

  dpa_interface_->registerReceiveFromHandler([&](const std::basic_string<unsigned char>& msg) -> int {
    ResponseHandler(msg);
    return 0;
  });

  current_request_ = new DpaRequest();
}

DpaHandler::~DpaHandler() {
  delete current_request_;
}

void DpaHandler::ResponseHandler(const std::basic_string<unsigned char>& message) {
  if (message.length() == 0)
    return;

  TRC_DBG(">>>>>>>>>>>>>>>>>>" << std::endl <<
      "Received from DPA interface: " << std::endl << FORM_HEX(message.data(), message.length()));

  DpaMessage received_message;
  try {
    received_message.FillFromResponse(message.data(), message.length());
  }
  catch (std::exception& e) {
    CATCH_EX("in processing msg", std::exception, e);
    return;
  }
  if (!ProcessMessage(received_message)) {
    ProcessUnexpectedMessage(received_message);
  }
  {
    std::unique_lock<std::mutex> lck(condition_variable_mutex_);
    condition_variable_.notify_one();
  }
}

void DpaHandler::SendDpaMessage(const DpaMessage& message, DpaTransaction* responseHndl) {
  if (IsDpaMessageInProgress()) {
    throw std::logic_error("Other Dpa Message is in progress.");
  }

  try {
    TRC_DBG("<<<<<<<<<<<<<<<<<<" << std::endl <<
      "Sent to DPA interface: " << std::endl << FORM_HEX(message.DpaPacketData(), message.Length()));

    dpa_interface_->sendTo(std::basic_string<unsigned char>(message.DpaPacketData(), message.Length()));
  }
  catch (std::exception& e) {
    throw std::logic_error("Message was not send.");
  }

  delete current_request_;
  if (!responseHndl)
    current_request_ = new DpaRequest();
  else
    current_request_ = new DpaRequest(responseHndl);
  current_request_->DefaultTimeout(default_timeout_ms_);
  current_request_->ProcessSentMessage(message);
}

void DpaHandler::ExecuteDpaTransaction(DpaTransaction& dpaTransaction)
{
  const DpaMessage& message = dpaTransaction.getMessage();

  int32_t remains(0);
  int32_t defaultTimeout = Timeout();
  int32_t requiredTimeout = dpaTransaction.getTimeout();

  if (requiredTimeout > 0)
    Timeout(requiredTimeout);

  DpaRequest::DpaRequestStatus status(DpaRequest::kCreated);
{'fraction_non_alphanumeric': 0.13436967632027258, 'fraction_numerical': 0.004258943781942078, 'mean_word_length': 5.46079779917469, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5151605', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1479, 'n_words': 278}