Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
#include "PeekProcessor.h"
21
 
22
using namespace apache::thrift::transport;
23
using namespace apache::thrift::protocol;
24
using namespace apache::thrift;
25
 
26
namespace apache { namespace thrift { namespace processor {
27
 
28
PeekProcessor::PeekProcessor() {
29
  memoryBuffer_.reset(new TMemoryBuffer());
30
  targetTransport_ = memoryBuffer_;
31
}
32
PeekProcessor::~PeekProcessor() {}
33
 
34
void PeekProcessor::initialize(boost::shared_ptr<TProcessor> actualProcessor,
35
                               boost::shared_ptr<TProtocolFactory> protocolFactory,
36
                               boost::shared_ptr<TPipedTransportFactory> transportFactory) {
37
  actualProcessor_ = actualProcessor;
38
  pipedProtocol_ = protocolFactory->getProtocol(targetTransport_);
39
  transportFactory_ = transportFactory;
40
  transportFactory_->initializeTargetTransport(targetTransport_);
41
}
42
 
43
boost::shared_ptr<TTransport> PeekProcessor::getPipedTransport(boost::shared_ptr<TTransport> in) {
44
  return transportFactory_->getTransport(in);
45
}
46
 
47
void PeekProcessor::setTargetTransport(boost::shared_ptr<TTransport> targetTransport) {
48
  targetTransport_ = targetTransport;
49
  if (boost::dynamic_pointer_cast<TMemoryBuffer>(targetTransport_)) {
50
    memoryBuffer_ = boost::dynamic_pointer_cast<TMemoryBuffer>(targetTransport);
51
  } else if (boost::dynamic_pointer_cast<TPipedTransport>(targetTransport_)) {
52
    memoryBuffer_ = boost::dynamic_pointer_cast<TMemoryBuffer>(boost::dynamic_pointer_cast<TPipedTransport>(targetTransport_)->getTargetTransport());
53
  }
54
 
55
  if (!memoryBuffer_) {
56
    throw TException("Target transport must be a TMemoryBuffer or a TPipedTransport with TMemoryBuffer");
57
  }
58
}
59
 
60
bool PeekProcessor::process(boost::shared_ptr<TProtocol> in,
61
                            boost::shared_ptr<TProtocol> out) {
62
 
63
  std::string fname;
64
  TMessageType mtype;
65
  int32_t seqid;
66
  in->readMessageBegin(fname, mtype, seqid);
67
 
68
  if (mtype != T_CALL) {
69
    throw TException("Unexpected message type");
70
  }
71
 
72
  // Peek at the name
73
  peekName(fname);
74
 
75
  TType ftype;
76
  int16_t fid;
77
  while (true) {
78
    in->readFieldBegin(fname, ftype, fid);
79
    if (ftype == T_STOP) {
80
      break;
81
    }
82
 
83
    // Peek at the variable
84
    peek(in, ftype, fid);
85
    in->readFieldEnd();
86
  }
87
  in->readMessageEnd();
88
  in->getTransport()->readEnd();
89
 
90
  //
91
  // All the data is now in memoryBuffer_ and ready to be processed
92
  //
93
 
94
  // Let's first take a peek at the full data in memory
95
  uint8_t* buffer;
96
  uint32_t size;
97
  memoryBuffer_->getBuffer(&buffer, &size);
98
  peekBuffer(buffer, size);
99
 
100
  // Done peeking at variables
101
  peekEnd();
102
 
103
  bool ret = actualProcessor_->process(pipedProtocol_, out);
104
  memoryBuffer_->resetBuffer();
105
  return ret;
106
}
107
 
108
void PeekProcessor::peekName(const std::string& fname) {
109
}
110
 
111
void PeekProcessor::peekBuffer(uint8_t* buffer, uint32_t size) {
112
}
113
 
114
void PeekProcessor::peek(boost::shared_ptr<TProtocol> in,
115
                         TType ftype,
116
                         int16_t fid) {
117
  in->skip(ftype);
118
}
119
 
120
void PeekProcessor::peekEnd() {}
121
 
122
}}}