blob: e0779233de924a0cbfc2a9812520cd0cfdaa8ef3 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_THRIFT_H_
21#define _THRIFT_THRIFT_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +000022
Mark Slee4f261c52007-04-13 00:33:24 +000023#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
David Reiss7247b8c2009-04-02 23:05:40 +000026#include <stdio.h>
Mark Slee4f261c52007-04-13 00:33:24 +000027
Mark Slee8d7e1f62006-06-07 06:48:56 +000028#include <netinet/in.h>
Mark Slee4f261c52007-04-13 00:33:24 +000029#ifdef HAVE_INTTYPES_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000030#include <inttypes.h>
Mark Slee4f261c52007-04-13 00:33:24 +000031#endif
Mark Sleee8540632006-05-30 09:24:40 +000032#include <string>
33#include <map>
34#include <list>
35#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000036#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000037#include <exception>
38
Aditya Agarwald622e962006-10-11 02:42:49 +000039#include "TLogging.h"
40
T Jake Lucianib5e62212009-01-31 22:36:20 +000041namespace apache { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000042
David Reissfaebedd2007-09-17 23:20:38 +000043class TOutput {
44 public:
David Reiss9b209552008-04-08 06:26:05 +000045 TOutput() : f_(&errorTimeWrapper) {}
boz6ded7752007-06-05 22:41:18 +000046
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
David Reiss01e55c12008-07-13 22:18:51 +000055 // 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
David Reiss9b209552008-04-08 06:26:05 +000066 inline static void errorTimeWrapper(const char* msg) {
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000067 time_t now;
David Reissd28ce102009-05-21 02:28:14 +000068 char dbgtime[26];
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000069 time(&now);
70 ctime_r(&now, dbgtime);
71 dbgtime[24] = 0;
David Reiss9b209552008-04-08 06:26:05 +000072 fprintf(stderr, "Thrift: %s %s\n", dbgtime, msg);
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000073 }
David Reiss9b209552008-04-08 06:26:05 +000074
75 /** Just like strerror_r but returns a C++ string object. */
76 static std::string strerror_s(int errno_copy);
77
David Reissfaebedd2007-09-17 23:20:38 +000078 private:
boz6ded7752007-06-05 22:41:18 +000079 void (*f_)(const char *);
80};
81
82extern TOutput GlobalOutput;
83
Mark Sleef9831082007-02-20 20:59:21 +000084namespace protocol {
85 class TProtocol;
86}
87
Mark Sleeb9ff32a2006-11-16 01:00:24 +000088class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +000089 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000090 TException() {}
91
Mark Slee82a6c0f2007-04-04 21:08:21 +000092 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000093 message_(message) {}
94
Martin Kraemer8196a612006-12-09 01:57:58 +000095 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000096
Martin Kraemer92a2eac2007-02-05 20:58:41 +000097 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000098 if (message_.empty()) {
99 return "Default TException.";
100 } else {
101 return message_.c_str();
102 }
Mark Slee2f6404d2006-10-10 01:37:40 +0000103 }
104
David Reissfaebedd2007-09-17 23:20:38 +0000105 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +0000106 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000107
Marc Slemko5b126d62006-08-11 23:03:42 +0000108};
109
Mark Sleef9831082007-02-20 20:59:21 +0000110class TApplicationException : public TException {
David Reissfaebedd2007-09-17 23:20:38 +0000111 public:
Mark Sleef9831082007-02-20 20:59:21 +0000112
113 /**
114 * Error codes for the various types of exceptions.
115 */
David Reiss322e5952008-12-05 02:54:09 +0000116 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
Mark Sleef9831082007-02-20 20:59:21 +0000123 };
124
125 TApplicationException() :
126 TException(),
127 type_(UNKNOWN) {}
128
129 TApplicationException(TApplicationExceptionType type) :
David Reiss0c90f6f2008-02-06 22:18:40 +0000130 TException(),
Mark Sleef9831082007-02-20 20:59:21 +0000131 type_(type) {}
132
Mark Slee82a6c0f2007-04-04 21:08:21 +0000133 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000134 TException(message),
135 type_(UNKNOWN) {}
136
137 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +0000138 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000139 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()) {
dweatherford2323cf62007-12-05 03:40:19 +0000156 switch (type_) {
David Reissadad4ab2007-12-14 20:56:04 +0000157 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)";
dweatherford2323cf62007-12-05 03:40:19 +0000164 };
Mark Sleef9831082007-02-20 20:59:21 +0000165 } else {
166 return message_.c_str();
167 }
168 }
169
Mark Sleeba8f8d72007-04-03 00:34:00 +0000170 uint32_t read(protocol::TProtocol* iprot);
171 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000172
David Reissfaebedd2007-09-17 23:20:38 +0000173 protected:
Mark Sleef9831082007-02-20 20:59:21 +0000174 /**
175 * Error code
176 */
177 TApplicationExceptionType type_;
178
179};
180
181
David Reissd779cbe2007-08-31 01:42:55 +0000182// Forward declare this structure used by TDenseProtocol
183namespace reflection { namespace local {
184struct TypeSpec;
185}}
186
187
T Jake Lucianib5e62212009-01-31 22:36:20 +0000188}} // apache::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000189
Mark Sleef5f2be42006-09-05 21:05:31 +0000190#endif // #ifndef _THRIFT_THRIFT_H_