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