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
#ifndef STATSPROCESSOR_H
21
#define STATSPROCESSOR_H
22
 
23
#include <boost/shared_ptr.hpp>
24
#include <transport/TTransport.h>
25
#include <protocol/TProtocol.h>
26
#include <TProcessor.h>
27
 
28
namespace apache { namespace thrift { namespace processor {
29
 
30
/*
31
 * Class for keeping track of function call statistics and printing them if desired
32
 *
33
 */
34
class StatsProcessor : public apache::thrift::TProcessor {
35
public:
36
  StatsProcessor(bool print, bool frequency)
37
    : print_(print),
38
      frequency_(frequency)
39
  {}
40
  virtual ~StatsProcessor() {};
41
 
42
  virtual bool process(boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot, boost::shared_ptr<apache::thrift::protocol::TProtocol> poprot) {
43
 
44
    piprot_ = piprot;
45
 
46
    std::string fname;
47
    apache::thrift::protocol::TMessageType mtype;
48
    int32_t seqid;
49
 
50
    piprot_->readMessageBegin(fname, mtype, seqid);
51
    if (mtype != apache::thrift::protocol::T_CALL) {
52
      if (print_) {
53
        printf("Unknown message type\n");
54
      }
55
      throw apache::thrift::TException("Unexpected message type");
56
    }
57
    if (print_) {
58
      printf("%s (", fname.c_str());
59
    }
60
    if (frequency_) {
61
      if (frequency_map_.find(fname) != frequency_map_.end()) {
62
        frequency_map_[fname]++;
63
      } else {
64
        frequency_map_[fname] = 1;
65
      }
66
    }
67
 
68
    apache::thrift::protocol::TType ftype;
69
    int16_t fid;
70
 
71
    while (true) {
72
      piprot_->readFieldBegin(fname, ftype, fid);
73
      if (ftype == apache::thrift::protocol::T_STOP) {
74
        break;
75
      }
76
 
77
      printAndPassToBuffer(ftype);
78
      if (print_) {
79
        printf(", ");
80
      }
81
    }
82
 
83
    if (print_) {
84
      printf("\b\b)\n");
85
    }
86
    return true;
87
  }
88
 
89
  const std::map<std::string, int64_t>& get_frequency_map() {
90
    return frequency_map_;
91
  }
92
 
93
protected:
94
  void printAndPassToBuffer(apache::thrift::protocol::TType ftype) {
95
    switch (ftype) {
96
      case apache::thrift::protocol::T_BOOL:
97
        {
98
          bool boolv;
99
          piprot_->readBool(boolv);
100
          if (print_) {
101
            printf("%d", boolv);
102
          }
103
        }
104
        break;
105
      case apache::thrift::protocol::T_BYTE:
106
        {
107
          int8_t bytev;
108
          piprot_->readByte(bytev);
109
          if (print_) {
110
            printf("%d", bytev);
111
          }
112
        }
113
        break;
114
      case apache::thrift::protocol::T_I16:
115
        {
116
          int16_t i16;
117
          piprot_->readI16(i16);
118
          if (print_) {
119
            printf("%d", i16);
120
          }
121
        }
122
        break;
123
      case apache::thrift::protocol::T_I32:
124
        {
125
          int32_t i32;
126
          piprot_->readI32(i32);
127
          if (print_) {
128
            printf("%d", i32);
129
          }
130
        }
131
        break;
132
      case apache::thrift::protocol::T_I64:
133
        {
134
          int64_t i64;
135
          piprot_->readI64(i64);
136
          if (print_) {
137
            printf("%ld", i64);
138
          }
139
        }
140
        break;
141
      case apache::thrift::protocol::T_DOUBLE:
142
        {
143
          double dub;
144
          piprot_->readDouble(dub);
145
          if (print_) {
146
            printf("%f", dub);
147
          }
148
        }
149
        break;
150
      case apache::thrift::protocol::T_STRING:
151
        {
152
          std::string str;
153
          piprot_->readString(str);
154
          if (print_) {
155
            printf("%s", str.c_str());
156
          }
157
        }
158
        break;
159
      case apache::thrift::protocol::T_STRUCT:
160
        {
161
          std::string name;
162
          int16_t fid;
163
          apache::thrift::protocol::TType ftype;
164
          piprot_->readStructBegin(name);
165
          if (print_) {
166
            printf("<");
167
          }
168
          while (true) {
169
            piprot_->readFieldBegin(name, ftype, fid);
170
            if (ftype == apache::thrift::protocol::T_STOP) {
171
              break;
172
            }
173
            printAndPassToBuffer(ftype);
174
            if (print_) {
175
              printf(",");
176
            }
177
            piprot_->readFieldEnd();
178
          }
179
          piprot_->readStructEnd();
180
          if (print_) {
181
            printf("\b>");
182
          }
183
        }
184
        break;
185
      case apache::thrift::protocol::T_MAP:
186
        {
187
          apache::thrift::protocol::TType keyType;
188
          apache::thrift::protocol::TType valType;
189
          uint32_t i, size;
190
          piprot_->readMapBegin(keyType, valType, size);
191
          if (print_) {
192
            printf("{");
193
          }
194
          for (i = 0; i < size; i++) {
195
            printAndPassToBuffer(keyType);
196
            if (print_) {
197
              printf("=>");
198
            }
199
            printAndPassToBuffer(valType);
200
            if (print_) {
201
              printf(",");
202
            }
203
          }
204
          piprot_->readMapEnd();
205
          if (print_) {
206
            printf("\b}");
207
          }
208
        }
209
        break;
210
      case apache::thrift::protocol::T_SET:
211
        {
212
          apache::thrift::protocol::TType elemType;
213
          uint32_t i, size;
214
          piprot_->readSetBegin(elemType, size);
215
          if (print_) {
216
            printf("{");
217
          }
218
          for (i = 0; i < size; i++) {
219
            printAndPassToBuffer(elemType);
220
            if (print_) {
221
              printf(",");
222
            }
223
          }
224
          piprot_->readSetEnd();
225
          if (print_) {
226
            printf("\b}");
227
          }
228
        }
229
        break;
230
      case apache::thrift::protocol::T_LIST:
231
        {
232
          apache::thrift::protocol::TType elemType;
233
          uint32_t i, size;
234
          piprot_->readListBegin(elemType, size);
235
          if (print_) {
236
            printf("[");
237
          }
238
          for (i = 0; i < size; i++) {
239
            printAndPassToBuffer(elemType);
240
            if (print_) {
241
              printf(",");
242
            }
243
          }
244
          piprot_->readListEnd();
245
          if (print_) {
246
            printf("\b]");
247
          }
248
        }
249
        break;
250
      default:
251
        break;
252
    }
253
  }
254
 
255
  boost::shared_ptr<apache::thrift::protocol::TProtocol> piprot_;
256
  std::map<std::string, int64_t> frequency_map_;
257
 
258
  bool print_;
259
  bool frequency_;
260
};
261
 
262
}}} // apache::thrift::processor
263
 
264
#endif