#include "PeekProcessor.h"
+using namespace facebook::thrift::transport;
+using namespace facebook::thrift::protocol;
+using namespace facebook::thrift;
+
namespace facebook { namespace thrift { namespace processor {
PeekProcessor::PeekProcessor() {
- memoryBuffer_.reset(new facebook::thrift::transport::TMemoryBuffer());
+ memoryBuffer_.reset(new TMemoryBuffer());
+ targetTransport_ = memoryBuffer_;
}
PeekProcessor::~PeekProcessor() {}
-void PeekProcessor::initialize(boost::shared_ptr<facebook::thrift::TProcessor> actualProcessor,
- boost::shared_ptr<facebook::thrift::protocol::TProtocolFactory> protocolFactory,
- boost::shared_ptr<facebook::thrift::transport::TPipedTransportFactory> transportFactory) {
+void PeekProcessor::initialize(boost::shared_ptr<TProcessor> actualProcessor,
+ boost::shared_ptr<TProtocolFactory> protocolFactory,
+ boost::shared_ptr<TPipedTransportFactory> transportFactory) {
actualProcessor_ = actualProcessor;
- pipedProtocol_ = protocolFactory->getProtocol(memoryBuffer_);
+ pipedProtocol_ = protocolFactory->getProtocol(targetTransport_);
transportFactory_ = transportFactory;
- transportFactory_->initializeTargetTransport(memoryBuffer_);
+ transportFactory_->initializeTargetTransport(targetTransport_);
}
-boost::shared_ptr<facebook::thrift::transport::TTransport> PeekProcessor::getPipedTransport(boost::shared_ptr<facebook::thrift::transport::TTransport> in) {
+boost::shared_ptr<TTransport> PeekProcessor::getPipedTransport(boost::shared_ptr<TTransport> in) {
return transportFactory_->getTransport(in);
}
-bool PeekProcessor::process(boost::shared_ptr<facebook::thrift::protocol::TProtocol> in,
- boost::shared_ptr<facebook::thrift::protocol::TProtocol> out) {
+void PeekProcessor::setTargetTransport(boost::shared_ptr<TTransport> targetTransport) {
+ targetTransport_ = targetTransport;
+ if (boost::dynamic_pointer_cast<TMemoryBuffer>(targetTransport_)) {
+ memoryBuffer_ = boost::dynamic_pointer_cast<TMemoryBuffer>(targetTransport);
+ } else if (boost::dynamic_pointer_cast<TPipedTransport>(targetTransport_)) {
+ memoryBuffer_ = boost::dynamic_pointer_cast<TMemoryBuffer>(boost::dynamic_pointer_cast<TPipedTransport>(targetTransport_)->getTargetTransport());
+ }
+
+ if (!memoryBuffer_) {
+ throw TException("Target transport must be a TMemoryBuffer or a TPipedTransport with TMemoryBuffer");
+ }
+}
+
+bool PeekProcessor::process(boost::shared_ptr<TProtocol> in,
+ boost::shared_ptr<TProtocol> out) {
std::string fname;
- facebook::thrift::protocol::TMessageType mtype;
+ TMessageType mtype;
int32_t seqid;
in->readMessageBegin(fname, mtype, seqid);
- if (mtype != facebook::thrift::protocol::T_CALL) {
- throw facebook::thrift::TException("Unexpected message type");
+ if (mtype != T_CALL) {
+ throw TException("Unexpected message type");
}
// Peek at the name
peekName(fname);
- facebook::thrift::protocol::TType ftype;
+ TType ftype;
int16_t fid;
while (true) {
in->readFieldBegin(fname, ftype, fid);
- if (ftype == facebook::thrift::protocol::T_STOP) {
+ if (ftype == T_STOP) {
break;
}
void PeekProcessor::peekBuffer(uint8_t* buffer, uint32_t size) {
}
-void PeekProcessor::peek(boost::shared_ptr<facebook::thrift::protocol::TProtocol> in,
- facebook::thrift::protocol::TType ftype,
- int16_t fid) {
+void PeekProcessor::peek(boost::shared_ptr<TProtocol> in,
+ TType ftype,
+ int16_t fid) {
in->skip(ftype);
}
boost::shared_ptr<facebook::thrift::transport::TTransport> getPipedTransport(boost::shared_ptr<facebook::thrift::transport::TTransport> in);
+ void setTargetTransport(boost::shared_ptr<facebook::thrift::transport::TTransport> targetTransport);
+
virtual bool process(boost::shared_ptr<facebook::thrift::protocol::TProtocol> in,
boost::shared_ptr<facebook::thrift::protocol::TProtocol> out);
boost::shared_ptr<facebook::thrift::protocol::TProtocol> pipedProtocol_;
boost::shared_ptr<facebook::thrift::transport::TPipedTransportFactory> transportFactory_;
boost::shared_ptr<facebook::thrift::transport::TMemoryBuffer> memoryBuffer_;
+ boost::shared_ptr<facebook::thrift::transport::TTransport> targetTransport_;
};
}}} // facebook::thrift::processor