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 _THRIFT_THRIFT_H_
21
#define _THRIFT_THRIFT_H_ 1
22
 
23
#ifdef HAVE_CONFIG_H
24
#include "config.h"
25
#endif
26
#include <stdio.h>
27
 
28
#include <sys/types.h>
29
#include <netinet/in.h>
30
#ifdef HAVE_INTTYPES_H
31
#include <inttypes.h>
32
#endif
33
#include <string>
34
#include <map>
35
#include <list>
36
#include <set>
37
#include <vector>
38
#include <exception>
39
 
40
#include "TLogging.h"
41
 
42
namespace apache { namespace thrift {
43
 
44
class TOutput {
45
 public:
46
  TOutput() : f_(&errorTimeWrapper) {}
47
 
48
  inline void setOutputFunction(void (*function)(const char *)){
49
    f_ = function;
50
  }
51
 
52
  inline void operator()(const char *message){
53
    f_(message);
54
  }
55
 
56
  // It is important to have a const char* overload here instead of
57
  // just the string version, otherwise errno could be corrupted
58
  // if there is some problem allocating memory when constructing
59
  // the string.
60
  void perror(const char *message, int errno_copy);
61
  inline void perror(const std::string &message, int errno_copy) {
62
    perror(message.c_str(), errno_copy);
63
  }
64
 
65
  void printf(const char *message, ...);
66
 
67
  inline static void errorTimeWrapper(const char* msg) {
68
    time_t now;
69
    char dbgtime[26];
70
    time(&now);
71
    ctime_r(&now, dbgtime);
72
    dbgtime[24] = 0;
73
    fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
74
  }
75
 
76
  /** Just like strerror_r but returns a C++ string object. */
77
  static std::string strerror_s(int errno_copy);
78
 
79
 private:
80
  void (*f_)(const char *);
81
};
82
 
83
extern TOutput GlobalOutput;
84
 
85
namespace protocol {
86
  class TProtocol;
87
}
88
 
89
class TException : public std::exception {
90
 public:
91
  TException() {}
92
 
93
  TException(const std::string& message) :
94
    message_(message) {}
95
 
96
  virtual ~TException() throw() {}
97
 
98
  virtual const char* what() const throw() {
99
    if (message_.empty()) {
100
      return "Default TException.";
101
    } else {
102
      return message_.c_str();
103
    }
104
  }
105
 
106
 protected:
107
  std::string message_;
108
 
109
};
110
 
111
class TApplicationException : public TException {
112
 public:
113
 
114
  /**
115
   * Error codes for the various types of exceptions.
116
   */
117
  enum TApplicationExceptionType
118
  { UNKNOWN = 0
119
  , UNKNOWN_METHOD = 1
120
  , INVALID_MESSAGE_TYPE = 2
121
  , WRONG_METHOD_NAME = 3
122
  , BAD_SEQUENCE_ID = 4
123
  , MISSING_RESULT = 5
124
  };
125
 
126
  TApplicationException() :
127
    TException(),
128
    type_(UNKNOWN) {}
129
 
130
  TApplicationException(TApplicationExceptionType type) :
131
    TException(),
132
    type_(type) {}
133
 
134
  TApplicationException(const std::string& message) :
135
    TException(message),
136
    type_(UNKNOWN) {}
137
 
138
  TApplicationException(TApplicationExceptionType type,
139
                        const std::string& message) :
140
    TException(message),
141
    type_(type) {}
142
 
143
  virtual ~TApplicationException() throw() {}
144
 
145
  /**
146
   * Returns an error code that provides information about the type of error
147
   * that has occurred.
148
   *
149
   * @return Error code
150
   */
151
  TApplicationExceptionType getType() {
152
    return type_;
153
  }
154
 
155
  virtual const char* what() const throw() {
156
    if (message_.empty()) {
157
      switch (type_) {
158
        case UNKNOWN              : return "TApplicationException: Unknown application exception";
159
        case UNKNOWN_METHOD       : return "TApplicationException: Unknown method";
160
        case INVALID_MESSAGE_TYPE : return "TApplicationException: Invalid message type";
161
        case WRONG_METHOD_NAME    : return "TApplicationException: Wrong method name";
162
        case BAD_SEQUENCE_ID      : return "TApplicationException: Bad sequence identifier";
163
        case MISSING_RESULT       : return "TApplicationException: Missing result";
164
        default                   : return "TApplicationException: (Invalid exception type)";
165
      };
166
    } else {
167
      return message_.c_str();
168
    }
169
  }
170
 
171
  uint32_t read(protocol::TProtocol* iprot);
172
  uint32_t write(protocol::TProtocol* oprot) const;
173
 
174
 protected:
175
  /**
176
   * Error code
177
   */
178
  TApplicationExceptionType type_;
179
 
180
};
181
 
182
 
183
// Forward declare this structure used by TDenseProtocol
184
namespace reflection { namespace local {
185
struct TypeSpec;
186
}}
187
 
188
 
189
}} // apache::thrift
190
 
191
#endif // #ifndef _THRIFT_THRIFT_H_