| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Gerd v. Egidy * |
| 3 | * gve@intra2net.com * |
| 4 | * * |
| 5 | * This library is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU Lesser General Public License version * |
| 7 | * 2.1 as published by the Free Software Foundation. * |
| 8 | * * |
| 9 | * This library is distributed in the hope that it will be useful, * |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 12 | * GNU Lesser General Public License for more details. * |
| 13 | * * |
| 14 | * You should have received a copy of the GNU Lesser General Public * |
| 15 | * License along with this program; if not, write to the * |
| 16 | * Free Software Foundation, Inc., * |
| 17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 18 | ***************************************************************************/ |
| 19 | #ifndef __LIBT2N_T2N_EXCEPTION |
| 20 | #define __LIBT2N_T2N_EXCEPTION |
| 21 | |
| 22 | #include <stdexcept> |
| 23 | #include <string> |
| 24 | |
| 25 | #include <boost/serialization/serialization.hpp> |
| 26 | #include <boost/serialization/export.hpp> |
| 27 | |
| 28 | // serialization for std::exception |
| 29 | namespace boost { |
| 30 | namespace serialization { |
| 31 | |
| 32 | template<class Archive> |
| 33 | void serialize(Archive & ar, std::exception & g, const unsigned int version) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | } // namespace serialization |
| 38 | } // namespace boost |
| 39 | |
| 40 | namespace libt2n |
| 41 | { |
| 42 | |
| 43 | /** |
| 44 | generic libt2n exception |
| 45 | */ |
| 46 | // a generic exception that can be handeled with libt2n |
| 47 | class t2n_exception : public std::exception |
| 48 | { |
| 49 | private: |
| 50 | std::string message; |
| 51 | |
| 52 | friend class boost::serialization::access; |
| 53 | template<class Archive> |
| 54 | void serialize(Archive & ar, const unsigned int version) |
| 55 | { |
| 56 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(exception); |
| 57 | ar & BOOST_SERIALIZATION_NVP(message); |
| 58 | } |
| 59 | |
| 60 | public: |
| 61 | t2n_exception(const std::string& _message) |
| 62 | { message=_message; } |
| 63 | |
| 64 | t2n_exception() |
| 65 | { } |
| 66 | |
| 67 | virtual const char* what() const throw() |
| 68 | { return message.c_str(); } |
| 69 | |
| 70 | virtual t2n_exception* clone() const |
| 71 | { return new t2n_exception(*this); } |
| 72 | |
| 73 | virtual ~t2n_exception() throw() |
| 74 | {} |
| 75 | |
| 76 | virtual void do_throw() |
| 77 | { throw *this; } |
| 78 | }; |
| 79 | BOOST_CLASS_EXPORT(t2n_exception) |
| 80 | |
| 81 | // a (unspecified) problem with libt2n communication |
| 82 | class t2n_communication_error : public t2n_exception |
| 83 | { |
| 84 | private: |
| 85 | friend class boost::serialization::access; |
| 86 | template<class Archive> |
| 87 | void serialize(Archive & ar, const unsigned int version) |
| 88 | { |
| 89 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); |
| 90 | } |
| 91 | |
| 92 | public: |
| 93 | t2n_communication_error(const std::string& _message) |
| 94 | : t2n_exception(_message) |
| 95 | { } |
| 96 | |
| 97 | t2n_communication_error() |
| 98 | { } |
| 99 | |
| 100 | t2n_exception* clone() const |
| 101 | { return new t2n_communication_error(*this); } |
| 102 | |
| 103 | void do_throw() |
| 104 | { throw *this; } |
| 105 | }; |
| 106 | BOOST_CLASS_EXPORT(t2n_communication_error) |
| 107 | |
| 108 | // can't connect to libt2n server |
| 109 | class t2n_connect_error : public t2n_communication_error |
| 110 | { |
| 111 | private: |
| 112 | friend class boost::serialization::access; |
| 113 | template<class Archive> |
| 114 | void serialize(Archive & ar, const unsigned int version) |
| 115 | { |
| 116 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); |
| 117 | } |
| 118 | |
| 119 | public: |
| 120 | t2n_connect_error(const std::string& _message) |
| 121 | : t2n_communication_error(_message) |
| 122 | { } |
| 123 | |
| 124 | t2n_connect_error() |
| 125 | { } |
| 126 | |
| 127 | t2n_exception* clone() const |
| 128 | { return new t2n_connect_error(*this); } |
| 129 | |
| 130 | void do_throw() |
| 131 | { throw *this; } |
| 132 | }; |
| 133 | BOOST_CLASS_EXPORT(t2n_connect_error) |
| 134 | |
| 135 | // error establishing a socket on the server (only thrown on the server-side) |
| 136 | class t2n_server_error : public t2n_communication_error |
| 137 | { |
| 138 | private: |
| 139 | friend class boost::serialization::access; |
| 140 | template<class Archive> |
| 141 | void serialize(Archive & ar, const unsigned int version) |
| 142 | { |
| 143 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); |
| 144 | } |
| 145 | |
| 146 | public: |
| 147 | t2n_server_error(const std::string& _message) |
| 148 | : t2n_communication_error(_message) |
| 149 | { } |
| 150 | |
| 151 | t2n_server_error() |
| 152 | { } |
| 153 | |
| 154 | t2n_exception* clone() const |
| 155 | { return new t2n_server_error(*this); } |
| 156 | |
| 157 | void do_throw() |
| 158 | { throw *this; } |
| 159 | }; |
| 160 | BOOST_CLASS_EXPORT(t2n_server_error) |
| 161 | |
| 162 | // error transmitting or receiving libt2n messages |
| 163 | class t2n_transfer_error : public t2n_communication_error |
| 164 | { |
| 165 | private: |
| 166 | friend class boost::serialization::access; |
| 167 | template<class Archive> |
| 168 | void serialize(Archive & ar, const unsigned int version) |
| 169 | { |
| 170 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); |
| 171 | } |
| 172 | |
| 173 | public: |
| 174 | t2n_transfer_error(const std::string& _message) |
| 175 | : t2n_communication_error(_message) |
| 176 | { } |
| 177 | |
| 178 | t2n_transfer_error() |
| 179 | { } |
| 180 | |
| 181 | t2n_exception* clone() const |
| 182 | { return new t2n_transfer_error(*this); } |
| 183 | |
| 184 | void do_throw() |
| 185 | { throw *this; } |
| 186 | }; |
| 187 | BOOST_CLASS_EXPORT(t2n_transfer_error) |
| 188 | |
| 189 | // tried to talk to an incompatible libt2n version |
| 190 | class t2n_version_mismatch : public t2n_communication_error |
| 191 | { |
| 192 | private: |
| 193 | friend class boost::serialization::access; |
| 194 | template<class Archive> |
| 195 | void serialize(Archive & ar, const unsigned int version) |
| 196 | { |
| 197 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error); |
| 198 | } |
| 199 | |
| 200 | public: |
| 201 | t2n_version_mismatch(const std::string& _message) |
| 202 | : t2n_communication_error(_message) |
| 203 | { } |
| 204 | |
| 205 | t2n_version_mismatch() |
| 206 | { } |
| 207 | |
| 208 | t2n_exception* clone() const |
| 209 | { return new t2n_version_mismatch(*this); } |
| 210 | |
| 211 | void do_throw() |
| 212 | { throw *this; } |
| 213 | }; |
| 214 | BOOST_CLASS_EXPORT(t2n_version_mismatch) |
| 215 | |
| 216 | // illegal libt2n command received |
| 217 | class t2n_command_error : public t2n_exception |
| 218 | { |
| 219 | private: |
| 220 | friend class boost::serialization::access; |
| 221 | template<class Archive> |
| 222 | void serialize(Archive & ar, const unsigned int version) |
| 223 | { |
| 224 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); |
| 225 | } |
| 226 | |
| 227 | public: |
| 228 | t2n_command_error(const std::string& _message) |
| 229 | : t2n_exception(_message) |
| 230 | { } |
| 231 | |
| 232 | t2n_command_error() |
| 233 | { } |
| 234 | |
| 235 | t2n_exception* clone() const |
| 236 | { return new t2n_command_error(*this); } |
| 237 | |
| 238 | void do_throw() |
| 239 | { throw *this; } |
| 240 | }; |
| 241 | BOOST_CLASS_EXPORT(t2n_command_error) |
| 242 | |
| 243 | // error serializing or deserializing a libt2n command packet |
| 244 | class t2n_serialization_error : public t2n_exception |
| 245 | { |
| 246 | private: |
| 247 | friend class boost::serialization::access; |
| 248 | template<class Archive> |
| 249 | void serialize(Archive & ar, const unsigned int version) |
| 250 | { |
| 251 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); |
| 252 | } |
| 253 | |
| 254 | public: |
| 255 | t2n_serialization_error(const std::string& _message) |
| 256 | : t2n_exception(_message) |
| 257 | { } |
| 258 | |
| 259 | t2n_serialization_error() |
| 260 | { } |
| 261 | |
| 262 | t2n_exception* clone() const |
| 263 | { return new t2n_serialization_error(*this); } |
| 264 | |
| 265 | void do_throw() |
| 266 | { throw *this; } |
| 267 | }; |
| 268 | BOOST_CLASS_EXPORT(t2n_serialization_error) |
| 269 | |
| 270 | // a runtime error within the remote function |
| 271 | // derive your own custom exceptions from this one |
| 272 | class t2n_runtime_error : public t2n_exception |
| 273 | { |
| 274 | private: |
| 275 | friend class boost::serialization::access; |
| 276 | template<class Archive> |
| 277 | void serialize(Archive & ar, const unsigned int version) |
| 278 | { |
| 279 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception); |
| 280 | } |
| 281 | |
| 282 | public: |
| 283 | t2n_runtime_error(const std::string& _message) |
| 284 | : t2n_exception(_message) |
| 285 | { } |
| 286 | |
| 287 | t2n_runtime_error() |
| 288 | { } |
| 289 | |
| 290 | t2n_exception* clone() const |
| 291 | { return new t2n_runtime_error(*this); } |
| 292 | |
| 293 | void do_throw() |
| 294 | { throw *this; } |
| 295 | }; |
| 296 | BOOST_CLASS_EXPORT(t2n_runtime_error) |
| 297 | |
| 298 | } |
| 299 | |
| 300 | #endif |