| 1 | /* |
| 2 | Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy |
| 3 | |
| 4 | The software in this package is distributed under the GNU General |
| 5 | Public License version 2 (with a special exception described below). |
| 6 | |
| 7 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 8 | in the file COPYING.GPL. |
| 9 | |
| 10 | As a special exception, if other files instantiate templates or use macros |
| 11 | or inline functions from this file, or you compile this file and link it |
| 12 | with other works to produce a work based on this file, this file |
| 13 | does not by itself cause the resulting work to be covered |
| 14 | by the GNU General Public License. |
| 15 | |
| 16 | However the source code for this file must still be made available |
| 17 | in accordance with section (3) of the GNU General Public License. |
| 18 | |
| 19 | This exception does not invalidate any other reasons why a work based |
| 20 | on this file might be covered by the GNU General Public License. |
| 21 | */ |
| 22 | #ifndef __LIBT2N_T2N_EXCEPTION |
| 23 | #define __LIBT2N_T2N_EXCEPTION |
| 24 | |
| 25 | #include <stdexcept> |
| 26 | #include <string> |
| 27 | |
| 28 | #include <boost/serialization/serialization.hpp> |
| 29 | |
| 30 | namespace boost { |
| 31 | namespace serialization { |
| 32 | |
| 33 | // make std::exception serializable |
| 34 | template<class Archive> |
| 35 | void serialize(Archive & ar, std::exception & g, const unsigned int version) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | } // namespace serialization |
| 40 | } // namespace boost |
| 41 | |
| 42 | namespace libt2n |
| 43 | { |
| 44 | /** @brief a generic exception that can be handeled with libt2n |
| 45 | @note don't derive the exceptions your application generates directly from this one |
| 46 | but use libt2n::t2n_runtime_error for this |
| 47 | */ |
| 48 | class t2n_exception : public std::exception |
| 49 | { |
| 50 | private: |
| 51 | std::string message; |
| 52 | |
| 53 | friend class boost::serialization::access; |
| 54 | template<class Archive> |
| 55 | void serialize(Archive & ar, const unsigned int version); |
| 56 | |
| 57 | public: |
| 58 | t2n_exception(const std::string& _message) |
| 59 | { message=_message; } |
| 60 | |
| 61 | t2n_exception() |
| 62 | { } |
| 63 | |
| 64 | virtual const char* what() const throw() |
| 65 | { return message.c_str(); } |
| 66 | |
| 67 | virtual t2n_exception* clone() const |
| 68 | { return new t2n_exception(*this); } |
| 69 | |
| 70 | virtual ~t2n_exception() throw() |
| 71 | {} |
| 72 | |
| 73 | virtual void do_throw() |
| 74 | { throw *this; } |
| 75 | }; |
| 76 | |
| 77 | /// a (unspecified) problem with libt2n communication |
| 78 | class t2n_communication_error : public t2n_exception |
| 79 | { |
| 80 | private: |
| 81 | friend class boost::serialization::access; |
| 82 | template<class Archive> |
| 83 | void serialize(Archive & ar, const unsigned int version); |
| 84 | |
| 85 | public: |
| 86 | t2n_communication_error(const std::string& _message) |
| 87 | : t2n_exception(_message) |
| 88 | { } |
| 89 | |
| 90 | t2n_communication_error() |
| 91 | { } |
| 92 | |
| 93 | t2n_exception* clone() const |
| 94 | { return new t2n_communication_error(*this); } |
| 95 | |
| 96 | void do_throw() |
| 97 | { throw *this; } |
| 98 | }; |
| 99 | |
| 100 | /// can't connect to libt2n server |
| 101 | class t2n_connect_error : public t2n_communication_error |
| 102 | { |
| 103 | private: |
| 104 | friend class boost::serialization::access; |
| 105 | template<class Archive> |
| 106 | void serialize(Archive & ar, const unsigned int version); |
| 107 | |
| 108 | public: |
| 109 | t2n_connect_error(const std::string& _message) |
| 110 | : t2n_communication_error(_message) |
| 111 | { } |
| 112 | |
| 113 | t2n_connect_error() |
| 114 | { } |
| 115 | |
| 116 | t2n_exception* clone() const |
| 117 | { return new t2n_connect_error(*this); } |
| 118 | |
| 119 | void do_throw() |
| 120 | { throw *this; } |
| 121 | }; |
| 122 | |
| 123 | /// error establishing a socket on the server (only thrown on the server-side) |
| 124 | class t2n_server_error : public t2n_communication_error |
| 125 | { |
| 126 | private: |
| 127 | friend class boost::serialization::access; |
| 128 | template<class Archive> |
| 129 | void serialize(Archive & ar, const unsigned int version); |
| 130 | |
| 131 | public: |
| 132 | t2n_server_error(const std::string& _message) |
| 133 | : t2n_communication_error(_message) |
| 134 | { } |
| 135 | |
| 136 | t2n_server_error() |
| 137 | { } |
| 138 | |
| 139 | t2n_exception* clone() const |
| 140 | { return new t2n_server_error(*this); } |
| 141 | |
| 142 | void do_throw() |
| 143 | { throw *this; } |
| 144 | }; |
| 145 | |
| 146 | /// error transmitting or receiving libt2n messages |
| 147 | class t2n_transfer_error : public t2n_communication_error |
| 148 | { |
| 149 | private: |
| 150 | friend class boost::serialization::access; |
| 151 | template<class Archive> |
| 152 | void serialize(Archive & ar, const unsigned int version); |
| 153 | |
| 154 | public: |
| 155 | t2n_transfer_error(const std::string& _message) |
| 156 | : t2n_communication_error(_message) |
| 157 | { } |
| 158 | |
| 159 | t2n_transfer_error() |
| 160 | { } |
| 161 | |
| 162 | t2n_exception* clone() const |
| 163 | { return new t2n_transfer_error(*this); } |
| 164 | |
| 165 | void do_throw() |
| 166 | { throw *this; } |
| 167 | }; |
| 168 | |
| 169 | /// tried to talk to an incompatible libt2n version |
| 170 | class t2n_version_mismatch : public t2n_communication_error |
| 171 | { |
| 172 | private: |
| 173 | friend class boost::serialization::access; |
| 174 | template<class Archive> |
| 175 | void serialize(Archive & ar, const unsigned int version); |
| 176 | |
| 177 | public: |
| 178 | t2n_version_mismatch(const std::string& _message) |
| 179 | : t2n_communication_error(_message) |
| 180 | { } |
| 181 | |
| 182 | t2n_version_mismatch() |
| 183 | { } |
| 184 | |
| 185 | t2n_exception* clone() const |
| 186 | { return new t2n_version_mismatch(*this); } |
| 187 | |
| 188 | void do_throw() |
| 189 | { throw *this; } |
| 190 | }; |
| 191 | |
| 192 | /// illegal libt2n command received |
| 193 | class t2n_command_error : public t2n_exception |
| 194 | { |
| 195 | private: |
| 196 | friend class boost::serialization::access; |
| 197 | template<class Archive> |
| 198 | void serialize(Archive & ar, const unsigned int version); |
| 199 | |
| 200 | public: |
| 201 | t2n_command_error(const std::string& _message) |
| 202 | : t2n_exception(_message) |
| 203 | { } |
| 204 | |
| 205 | t2n_command_error() |
| 206 | { } |
| 207 | |
| 208 | t2n_exception* clone() const |
| 209 | { return new t2n_command_error(*this); } |
| 210 | |
| 211 | void do_throw() |
| 212 | { throw *this; } |
| 213 | }; |
| 214 | |
| 215 | /// error serializing or deserializing a libt2n command packet |
| 216 | class t2n_serialization_error : public t2n_exception |
| 217 | { |
| 218 | private: |
| 219 | friend class boost::serialization::access; |
| 220 | template<class Archive> |
| 221 | void serialize(Archive & ar, const unsigned int version); |
| 222 | |
| 223 | public: |
| 224 | t2n_serialization_error(const std::string& _message) |
| 225 | : t2n_exception(_message) |
| 226 | { } |
| 227 | |
| 228 | t2n_serialization_error() |
| 229 | { } |
| 230 | |
| 231 | t2n_exception* clone() const |
| 232 | { return new t2n_serialization_error(*this); } |
| 233 | |
| 234 | void do_throw() |
| 235 | { throw *this; } |
| 236 | }; |
| 237 | |
| 238 | /** @brief a runtime error within the remote function. |
| 239 | derive your own custom exceptions from this one |
| 240 | |
| 241 | @note you must override the virtual clone method if you do so (used by libt2n::command_server::handle()) |
| 242 | */ |
| 243 | class t2n_runtime_error : public t2n_exception |
| 244 | { |
| 245 | private: |
| 246 | friend class boost::serialization::access; |
| 247 | template<class Archive> |
| 248 | void serialize(Archive & ar, const unsigned int version); |
| 249 | |
| 250 | public: |
| 251 | t2n_runtime_error(const std::string& _message) |
| 252 | : t2n_exception(_message) |
| 253 | { } |
| 254 | |
| 255 | t2n_runtime_error() |
| 256 | { } |
| 257 | |
| 258 | t2n_exception* clone() const |
| 259 | { return new t2n_runtime_error(*this); } |
| 260 | |
| 261 | void do_throw() |
| 262 | { throw *this; } |
| 263 | }; |
| 264 | |
| 265 | } // namespace libt2n |
| 266 | |
| 267 | #include "t2n_exception.tcc" |
| 268 | |
| 269 | #endif |