blob: 0134d48d309cdaa70d6b5e1385f6630b46c7e83d [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_THRIFT_H_
8#define _THRIFT_THRIFT_H_ 1
Mark Sleee8540632006-05-30 09:24:40 +00009
Mark Slee4f261c52007-04-13 00:33:24 +000010#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
Mark Slee8d7e1f62006-06-07 06:48:56 +000014#include <netinet/in.h>
Mark Slee4f261c52007-04-13 00:33:24 +000015#ifdef HAVE_INTTYPES_H
Mark Slee8d7e1f62006-06-07 06:48:56 +000016#include <inttypes.h>
Mark Slee4f261c52007-04-13 00:33:24 +000017#endif
Mark Sleee8540632006-05-30 09:24:40 +000018#include <string>
19#include <map>
20#include <list>
21#include <set>
Mark Slee4ecbebc2006-09-05 00:14:21 +000022#include <vector>
Marc Slemko5b126d62006-08-11 23:03:42 +000023#include <exception>
24
Aditya Agarwald622e962006-10-11 02:42:49 +000025#include "TLogging.h"
26
Mark Sleecfc01932006-09-01 22:18:16 +000027namespace facebook { namespace thrift {
Marc Slemko5b126d62006-08-11 23:03:42 +000028
David Reissfaebedd2007-09-17 23:20:38 +000029class TOutput {
30 public:
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000031 TOutput() : f_(&perrorTimeWrapper) {}
boz6ded7752007-06-05 22:41:18 +000032
33 inline void setOutputFunction(void (*function)(const char *)){
34 f_ = function;
35 }
36
37 inline void operator()(const char *message){
38 f_(message);
39 }
40
Aditya Agarwal4529c4b2007-09-05 01:01:15 +000041 inline static void perrorTimeWrapper(const char* msg) {
42 time_t now;
43 char dbgtime[25];
44 time(&now);
45 ctime_r(&now, dbgtime);
46 dbgtime[24] = 0;
47 fprintf(stderr, "%s ", dbgtime);
48 perror(msg);
49 }
David Reissfaebedd2007-09-17 23:20:38 +000050 private:
boz6ded7752007-06-05 22:41:18 +000051 void (*f_)(const char *);
52};
53
54extern TOutput GlobalOutput;
55
Mark Sleef9831082007-02-20 20:59:21 +000056namespace protocol {
57 class TProtocol;
58}
59
Mark Sleeb9ff32a2006-11-16 01:00:24 +000060class TException : public std::exception {
David Reissfaebedd2007-09-17 23:20:38 +000061 public:
Mark Sleeb9ff32a2006-11-16 01:00:24 +000062 TException() {}
63
Mark Slee82a6c0f2007-04-04 21:08:21 +000064 TException(const std::string& message) :
Mark Slee2f6404d2006-10-10 01:37:40 +000065 message_(message) {}
66
Martin Kraemer8196a612006-12-09 01:57:58 +000067 virtual ~TException() throw() {}
Mark Slee2f6404d2006-10-10 01:37:40 +000068
Martin Kraemer92a2eac2007-02-05 20:58:41 +000069 virtual const char* what() const throw() {
Mark Sleeb9ff32a2006-11-16 01:00:24 +000070 if (message_.empty()) {
71 return "Default TException.";
72 } else {
73 return message_.c_str();
74 }
Mark Slee2f6404d2006-10-10 01:37:40 +000075 }
76
David Reissfaebedd2007-09-17 23:20:38 +000077 protected:
Mark Slee2abc9df2006-12-16 01:06:49 +000078 std::string message_;
Mark Slee2f6404d2006-10-10 01:37:40 +000079
Marc Slemko5b126d62006-08-11 23:03:42 +000080};
81
Mark Sleef9831082007-02-20 20:59:21 +000082class TApplicationException : public TException {
David Reissfaebedd2007-09-17 23:20:38 +000083 public:
Mark Sleef9831082007-02-20 20:59:21 +000084
85 /**
86 * Error codes for the various types of exceptions.
87 */
88 enum TApplicationExceptionType {
89 UNKNOWN = 0,
Mark Sleef3d33632007-02-21 01:39:23 +000090 UNKNOWN_METHOD = 1,
91 INVALID_MESSAGE_TYPE = 2,
92 WRONG_METHOD_NAME = 3,
93 BAD_SEQUENCE_ID = 4,
94 MISSING_RESULT = 5,
Mark Sleef9831082007-02-20 20:59:21 +000095 };
96
97 TApplicationException() :
98 TException(),
99 type_(UNKNOWN) {}
100
101 TApplicationException(TApplicationExceptionType type) :
102 TException(),
103 type_(type) {}
104
Mark Slee82a6c0f2007-04-04 21:08:21 +0000105 TApplicationException(const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000106 TException(message),
107 type_(UNKNOWN) {}
108
109 TApplicationException(TApplicationExceptionType type,
Mark Slee82a6c0f2007-04-04 21:08:21 +0000110 const std::string& message) :
Mark Sleef9831082007-02-20 20:59:21 +0000111 TException(message),
112 type_(type) {}
113
114 virtual ~TApplicationException() throw() {}
115
116 /**
117 * Returns an error code that provides information about the type of error
118 * that has occurred.
119 *
120 * @return Error code
121 */
122 TApplicationExceptionType getType() {
123 return type_;
124 }
125
126 virtual const char* what() const throw() {
127 if (message_.empty()) {
dweatherford2323cf62007-12-05 03:40:19 +0000128 switch (type_) {
129 case UNKNOWN_METHOD: return "Unknown method";
130 case INVALID_MESSAGE_TYPE: return "Invalid message type";
131 case WRONG_METHOD_NAME: return "Wrong method name";
132 case BAD_SEQUENCE_ID: return "Bad sequence identifier";
133 case MISSING_RESULT: return "Missing result";
134
135 default: return "Default TApplicationException.";
136 };
Mark Sleef9831082007-02-20 20:59:21 +0000137 } else {
138 return message_.c_str();
139 }
140 }
141
Mark Sleeba8f8d72007-04-03 00:34:00 +0000142 uint32_t read(protocol::TProtocol* iprot);
143 uint32_t write(protocol::TProtocol* oprot) const;
Mark Sleef9831082007-02-20 20:59:21 +0000144
David Reissfaebedd2007-09-17 23:20:38 +0000145 protected:
Mark Sleef9831082007-02-20 20:59:21 +0000146 /**
147 * Error code
148 */
149 TApplicationExceptionType type_;
150
151};
152
153
David Reissd779cbe2007-08-31 01:42:55 +0000154// Forward declare this structure used by TDenseProtocol
155namespace reflection { namespace local {
156struct TypeSpec;
157}}
158
159
Marc Slemko5b126d62006-08-11 23:03:42 +0000160}} // facebook::thrift
Mark Sleee8540632006-05-30 09:24:40 +0000161
Mark Sleef5f2be42006-09-05 21:05:31 +0000162#endif // #ifndef _THRIFT_THRIFT_H_