David Reiss | ea2cba8 | 2009-03-30 21:35:00 +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 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_THRIFT_H_ |
| 21 | #define _THRIFT_THRIFT_H_ 1 |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 22 | |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include "config.h" |
| 25 | #endif |
David Reiss | 7247b8c | 2009-04-02 23:05:40 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 27 | |
David Reiss | 08d2f11 | 2009-05-21 02:28:36 +0000 | [diff] [blame^] | 28 | #include <sys/types.h> |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 29 | #include <netinet/in.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 30 | #ifdef HAVE_INTTYPES_H |
Mark Slee | 8d7e1f6 | 2006-06-07 06:48:56 +0000 | [diff] [blame] | 31 | #include <inttypes.h> |
Mark Slee | 4f261c5 | 2007-04-13 00:33:24 +0000 | [diff] [blame] | 32 | #endif |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 33 | #include <string> |
| 34 | #include <map> |
| 35 | #include <list> |
| 36 | #include <set> |
Mark Slee | 4ecbebc | 2006-09-05 00:14:21 +0000 | [diff] [blame] | 37 | #include <vector> |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 38 | #include <exception> |
| 39 | |
Aditya Agarwal | d622e96 | 2006-10-11 02:42:49 +0000 | [diff] [blame] | 40 | #include "TLogging.h" |
| 41 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 42 | namespace apache { namespace thrift { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 43 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 44 | class TOutput { |
| 45 | public: |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 46 | TOutput() : f_(&errorTimeWrapper) {} |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 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 | |
David Reiss | 01e55c1 | 2008-07-13 22:18:51 +0000 | [diff] [blame] | 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 | |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 67 | inline static void errorTimeWrapper(const char* msg) { |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 68 | time_t now; |
David Reiss | d28ce10 | 2009-05-21 02:28:14 +0000 | [diff] [blame] | 69 | char dbgtime[26]; |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 70 | time(&now); |
| 71 | ctime_r(&now, dbgtime); |
| 72 | dbgtime[24] = 0; |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 73 | fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg); |
Aditya Agarwal | 4529c4b | 2007-09-05 01:01:15 +0000 | [diff] [blame] | 74 | } |
David Reiss | 9b20955 | 2008-04-08 06:26:05 +0000 | [diff] [blame] | 75 | |
| 76 | /** Just like strerror_r but returns a C++ string object. */ |
| 77 | static std::string strerror_s(int errno_copy); |
| 78 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 79 | private: |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 80 | void (*f_)(const char *); |
| 81 | }; |
| 82 | |
| 83 | extern TOutput GlobalOutput; |
| 84 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 85 | namespace protocol { |
| 86 | class TProtocol; |
| 87 | } |
| 88 | |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 89 | class TException : public std::exception { |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 90 | public: |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 91 | TException() {} |
| 92 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 93 | TException(const std::string& message) : |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 94 | message_(message) {} |
| 95 | |
Martin Kraemer | 8196a61 | 2006-12-09 01:57:58 +0000 | [diff] [blame] | 96 | virtual ~TException() throw() {} |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 97 | |
Martin Kraemer | 92a2eac | 2007-02-05 20:58:41 +0000 | [diff] [blame] | 98 | virtual const char* what() const throw() { |
Mark Slee | b9ff32a | 2006-11-16 01:00:24 +0000 | [diff] [blame] | 99 | if (message_.empty()) { |
| 100 | return "Default TException."; |
| 101 | } else { |
| 102 | return message_.c_str(); |
| 103 | } |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 104 | } |
| 105 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 106 | protected: |
Mark Slee | 2abc9df | 2006-12-16 01:06:49 +0000 | [diff] [blame] | 107 | std::string message_; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 108 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 111 | class TApplicationException : public TException { |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 112 | public: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Error codes for the various types of exceptions. |
| 116 | */ |
David Reiss | 322e595 | 2008-12-05 02:54:09 +0000 | [diff] [blame] | 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 |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | TApplicationException() : |
| 127 | TException(), |
| 128 | type_(UNKNOWN) {} |
| 129 | |
| 130 | TApplicationException(TApplicationExceptionType type) : |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 131 | TException(), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 132 | type_(type) {} |
| 133 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 134 | TApplicationException(const std::string& message) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 135 | TException(message), |
| 136 | type_(UNKNOWN) {} |
| 137 | |
| 138 | TApplicationException(TApplicationExceptionType type, |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 139 | const std::string& message) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 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()) { |
dweatherford | 2323cf6 | 2007-12-05 03:40:19 +0000 | [diff] [blame] | 157 | switch (type_) { |
David Reiss | adad4ab | 2007-12-14 20:56:04 +0000 | [diff] [blame] | 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)"; |
dweatherford | 2323cf6 | 2007-12-05 03:40:19 +0000 | [diff] [blame] | 165 | }; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 166 | } else { |
| 167 | return message_.c_str(); |
| 168 | } |
| 169 | } |
| 170 | |
Mark Slee | ba8f8d7 | 2007-04-03 00:34:00 +0000 | [diff] [blame] | 171 | uint32_t read(protocol::TProtocol* iprot); |
| 172 | uint32_t write(protocol::TProtocol* oprot) const; |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 173 | |
David Reiss | faebedd | 2007-09-17 23:20:38 +0000 | [diff] [blame] | 174 | protected: |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 175 | /** |
| 176 | * Error code |
| 177 | */ |
| 178 | TApplicationExceptionType type_; |
| 179 | |
| 180 | }; |
| 181 | |
| 182 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 183 | // Forward declare this structure used by TDenseProtocol |
| 184 | namespace reflection { namespace local { |
| 185 | struct TypeSpec; |
| 186 | }} |
| 187 | |
| 188 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 189 | }} // apache::thrift |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 190 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 191 | #endif // #ifndef _THRIFT_THRIFT_H_ |