<commit_before>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2016 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME>
////////////////////////////////////////////////////////////////////////////////

#include "FuerteLogger.h"
#include "VstConnection.h"
#include <condition_variable>
#include <fuerte/loop.h>
#include <fuerte/vst.h>
#include <fuerte/message.h>

#include <boost/asio/connect.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <fuerte/vst.h>

namespace arangodb { namespace fuerte { inline namespace v1 { namespace vst {

using namespace arangodb::fuerte::detail;

namespace ba = ::boost::asio;
namespace bs = ::boost::asio::ssl;
using bt = ::boost::asio::ip::tcp;
using be = ::boost::asio::ip::tcp::endpoint;
using BoostEC = ::boost::system::error_code;
using RequestItemSP = std::shared_ptr<RequestItem>;
using Lock = std::lock_guard<std::mutex>;
typedef std::unique_ptr<Request> RequestUP;
typedef std::unique_ptr<Response> ResponseUP;

MessageID VstConnection::sendRequest(std::unique_ptr<Request> request
                                    ,OnErrorCallback onError
                                    ,OnSuccessCallback onSuccess){

  //check if id is already used and fail
  request->messageid = ++_messageId;
  auto item = std::make_shared<RequestItem>();

  item->_messageId = _messageId;
  item->_onError = onError;
  item->_onSuccess = onSuccess;
  item->_requestBuffer = vst::toNetwork(*request);
  item->_request = std::move(request);

  //start Write may be only entered once!
  bool doWrite;
  {
    Lock lockQueue(_sendQueueMutex);
    doWrite = _sendQueue.empty();
    _sendQueue.push_back(item);
    FUERTE_LOG_DEBUG << "request queued" << std::endl;
  }

  if(doWrite){
    // this allows sendRequest to return immediately and
    // not to block until all writing is done
    if(_connected){
      FUERTE_LOG_VSTTRACE << "queue write" << std::endl;
      auto self = shared_from_this();
      //_ioService->dispatch( [this,self](){ startWrite(); } );
      startWrite();

      bool alreadyReading = _reading.exchange(true);
      if (!alreadyReading){
        FUERTE_LOG_TRACE << "starting new read" << std::endl;
        startRead();
      } else {
        FUERTE_LOG_TRACE << "NOT starting new read" << std::endl;
      }
    }
  }
  return item->_messageId;
}

std::unique_ptr<Response> VstConnection::sendRequest(RequestUP request){<commit_msg>add extra logging for alan<commit_after>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2016 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author <NAME> Uhde
////////////////////////////////////////////////////////////////////////////////

#include "FuerteLogger.h"
#include "VstConnection.h"
#include <condition_variable>
#include <fuerte/loop.h>
#include <fuerte/vst.h>
#include <fuerte/message.h>

#include <boost/asio/connect.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <fuerte/vst.h>

namespace arangodb { namespace fuerte { inline namespace v1 { namespace vst {

using namespace arangodb::fuerte::detail;

namespace ba = ::boost::asio;
namespace bs = ::boost::asio::ssl;
using bt = ::boost::asio::ip::tcp;
using be = ::boost::asio::ip::tcp::endpoint;
using BoostEC = ::boost::system::error_code;
using RequestItemSP = std::shared_ptr<RequestItem>;
using Lock = std::lock_guard<std::mutex>;
typedef std::unique_ptr<Request> RequestUP;
typedef std::unique_ptr<Response> ResponseUP;

MessageID VstConnection::sendRequest(std::unique_ptr<Request> request
                                    ,OnErrorCallback onError
                                    ,OnSuccessCallback onSuccess){

  //check if id is already used and fail
  request->messageid = ++_messageId;
  auto item = std::make_shared<RequestItem>();

  item->_messageId = _messageId;
  item->_onError = onError;
  item->_onSuccess = onSuccess;
  item->_requestBuffer = vst::toNetwork(*request);
  item->_request = std::move(request);

  //start Write may be only entered once!
  bool doWrite;
  {
    Lock lockQueue(_sendQueueMutex);
    doWrite = _sendQueue.empty();
    _sendQueue.push_back(item);
    FUERTE_LOG_DEBUG << "request queued" << std::endl;
  }

  if(doWrite){
    // this allows sendRequest to return immediately and
    // not to block until all writing is done
    if(_connected){
      FUERTE_LOG_VSTTRACE << "queue write" << std::endl;
      FUERTE_LOG_VSTTRACE << "messageid: " << item->_request->messageid << " path: " << item->_request->header.path.get() << std::endl;
      auto self = shared_from_this();
      //_ioService->dispatch( [this,self](){ startWrite(); } );
      startWrite();

      bool alreadyReading = _reading.exchange(true);
      if (!alreadyReading){
        FUERTE_LOG_TRACE << "starting new read" << std::endl;
        startRead();
      } else {
        FUERTE_LOG_TRACE << "NOT starting new read" << std::endl;
      }
    }
  }
  return item->_messageId;
}

std::unique_ptr<Response> VstConnection::sendRequest(RequestUP request){
{'fraction_non_alphanumeric': 0.1777533730781299, 'fraction_numerical': 0.0028239723878255413, 'mean_word_length': 2.8871951219512195, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6068961', 'n_tokens_mistral': 1869, 'n_tokens_neox': 1792, 'n_words': 516}