<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */

#ifndef THRIFT_ASYNC_REQUESTCHANNEL_H_
#define THRIFT_ASYNC_REQUESTCHANNEL_H_ 1

#include <functional>
#include <memory>
#include "thrift/lib/cpp2/async/MessageChannel.h"
#include "thrift/lib/cpp2/async/Stream.h"
#include "thrift/lib/cpp/Thrift.h"
#include "thrift/lib/cpp/async/TEventBase.h"
#include "thrift/lib/cpp/async/Request.h"
#include "thrift/lib/cpp/EventHandlerBase.h"
#include "folly/String.h"

#include <glog/logging.h>

#include <chrono>

namespace folly {
class IOBuf;
}

namespace apache { namespace thrift {

class ClientReceiveState {
 public:
  ClientReceiveState()
      : protocolId_(-1),
        streamManager_(nullptr) {
  }

  ClientReceiveState(uint16_t protocolId,
                     std::unique_ptr<folly::IOBuf> buf,
                     std::unique_ptr<apache::thrift::ContextStack> ctx,
                     std::unique_ptr<StreamManager> * streamManager)
    : protocolId_(protocolId),
      ctx_(std::move(ctx)),
      buf_(std::move(buf)),
      streamManager_(streamManager) {
  }
  ClientReceiveState(std::exception_ptr exc,
                     std::unique_ptr<apache::thrift::ContextStack> ctx)
    : protocolId_(-1),
      ctx_(std::move(ctx)),
      streamManager_(nullptr),
      exc_(std::move(exc)) {
  }
  std::exception_ptr exception() const {
    return exc_;
  }
  void resetException(std::exception_ptr exc) {
    exc_ = exc;
  }
  uint16_t protocolId() const {
    return protocolId_;
  }
  folly::IOBuf* buf() const {
    return buf_.get();
  }
  std::unique_ptr<folly::IOBuf> extractBuf() {
    return std::move(buf_);
  }
  apache::thrift::ContextStack* ctx() const {
    return ctx_.get();
  }

  void resetCtx(std::unique_ptr<apache::thrift::ContextStack> ctx) {
    ctx_ = std::move(ctx);
  }

  // Used by servicerouter to steal the context back for retrying a request
  std::unique_ptr<apache::thrift::ContextStack> releaseCtx() {
    return std::move(ctx_);
  }

  void setStreamManager(std::unique_ptr<StreamManager>&& manager) {
    assert(streamManager_ != nullptr);
    *streamManager_ = std::move(manager);
  }

 private:
  uint16_t protocolId_;
  std::unique_ptr<apache::thrift::ContextStack> ctx_;
  std::unique_ptr<folly::IOBuf> buf_;
  std::unique_ptr<StreamManager> * streamManager_;
  std::exception_ptr exc_;
};

class RequestCallback {
 public:
  virtual ~RequestCallback() {}
  virtual void requestSent() = 0;
  virtual void replyReceived(ClientReceiveState&&) = 0;
  virtual void requestError(ClientReceiveState&&) = 0;

  std::shared_ptr<apache::thrift::async::RequestContext> context_;
};

class StreamCallback : public RequestCallback {
  private:
    typedef void (*Processor)(std::unique_ptr<StreamManager>&&,
                              ClientReceiveState&);

  public:
    StreamCallback(std::unique_ptr<StreamManager>&& manager,
                   apache::thrift::async::TEventBase* eventBase,
                   Processor processor,
                   bool isSync)
      : streamManager_(std::move(manager)),
        eventBase_(eventBase),
        processor_(processor),
        isSync_(isSync) {
    }

    void replyReceived(ClientReceiveState&& state) {
      processor_(std::move(streamManager_), state);
      stopEventBaseIfIsSync();<commit_msg>Allow for folly::exception_wrapper in ClientReceiveState<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */

#ifndef THRIFT_ASYNC_REQUESTCHANNEL_H_
#define THRIFT_ASYNC_REQUESTCHANNEL_H_ 1

#include <functional>
#include <memory>
#include "thrift/lib/cpp2/async/MessageChannel.h"
#include "thrift/lib/cpp2/async/Stream.h"
#include "thrift/lib/cpp/Thrift.h"
#include "thrift/lib/cpp/async/TEventBase.h"
#include "thrift/lib/cpp/async/Request.h"
#include "thrift/lib/cpp/EventHandlerBase.h"
#include "folly/ExceptionWrapper.h"
#include "folly/String.h"

#include <glog/logging.h>

#include <chrono>

namespace folly {
class IOBuf;
}

namespace apache { namespace thrift {

class ClientReceiveState {
 public:
  ClientReceiveState()
      : protocolId_(-1),
        streamManager_(nullptr) {
  }

  ClientReceiveState(uint16_t protocolId,
                     std::unique_ptr<folly::IOBuf> buf,
                     std::unique_ptr<apache::thrift::ContextStack> ctx,
                     std::unique_ptr<StreamManager> * streamManager)
    : protocolId_(protocolId),
      ctx_(std::move(ctx)),
      buf_(std::move(buf)),
      streamManager_(streamManager) {
  }
  ClientReceiveState(std::exception_ptr exc,
                     std::unique_ptr<apache::thrift::ContextStack> ctx)
    : protocolId_(-1),
      ctx_(std::move(ctx)),
      streamManager_(nullptr),
      exc_(std::move(exc)) {
  }
  ClientReceiveState(folly::exception_wrapper excw,
                     std::unique_ptr<apache::thrift::ContextStack> ctx)
    : protocolId_(-1),
      ctx_(std::move(ctx)),
      streamManager_(nullptr),
      excw_(std::move(excw)) {
  }

  bool isException() const {
    return exc_ || excw_.get();
  }

  // TODO: Once everything is using the folly::exception_wrapper constructor,
  // expose a API to get the ptr to that exception without having to throw it.

  std::exception_ptr exception() {
    if (!exc_ && excw_.get()) {
      exc_ = excw_.getExceptionPtr();
    }
    return exc_;
  }

  void resetException(std::exception_ptr exc) {
    excw_ = folly::exception_wrapper();
    exc_ = exc;
  }

  uint16_t protocolId() const {
    return protocolId_;
  }

  folly::IOBuf* buf() const {
    return buf_.get();
  }

  std::unique_ptr<folly::IOBuf> extractBuf() {
    return std::move(buf_);
  }

  apache::thrift::ContextStack* ctx() const {
    return ctx_.get();
  }

  void resetCtx(std::unique_ptr<apache::thrift::ContextStack> ctx) {
    ctx_ = std::move(ctx);
  }

  // Used by servicerouter to steal the context back for retrying a request
  std::unique_ptr<apache::thrift::ContextStack> releaseCtx() {
    return std::move(ctx_);
  }

  void setStreamManager(std::unique_ptr<StreamManager>&& manager) {
    assert(streamManager_ != nullptr);
    *streamManager_ = std::move(manager);
  }

 private:
  uint16_t protocolId_;
  std::unique_ptr<apache::thrift::ContextStack> ctx_;
  std::unique_ptr<folly::IOBuf> buf_;
  std::unique_ptr<StreamManager> * streamManager_;
  std::exception_ptr exc_;
  folly::exception_wrapper excw_;
};

class RequestCallback {
 public:
  virtual ~RequestCallback() {}
  virtual void requestSent() = 0;
  virtual void replyReceived(ClientReceiveState&&) = 0;
  virtual void requestError(ClientReceiveState&&) = 0;

  std::shared_ptr<apache::thrift::async::RequestContext> context_;
};

class StreamCallback : public RequestCallback {
  private:
    typedef void (*Processor)(std::unique_ptr<StreamManager>&&,
                              ClientReceiveState&);

  public:
    StreamCallback(std::unique_ptr<StreamManager>&& manager,
                   apache::thrift::async::TEventBase* eventBase,
                   Processor processor,
                   bool isSync)
      : streamManager_(std::move(manager)),
        eventBase_(eventBase),
        processor_(processor),
        isSync_(isSync) {
    }

    void replyReceived(ClientReceiveState&& state) {
      processor_(std::move(streamManager_), state);
      stopEventBaseIfIsSync();
{'fraction_non_alphanumeric': 0.11673858732682262, 'fraction_numerical': 0.004201680672268907, 'mean_word_length': 3.7222520107238606, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4611726', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2601, 'n_words': 628}