364a194a5cba93fc681299efc6cd5869aefef9df
[libt2n] / src / t2n_exception.hxx
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 // a generic exception that can be handeled with libt2n
44 class t2n_exception : public std::exception
45 {
46     private:
47         std::string message;
48
49         friend class boost::serialization::access;
50         template<class Archive>
51         void serialize(Archive & ar, const unsigned int version)
52         {
53             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(exception);
54             ar & BOOST_SERIALIZATION_NVP(message);
55         }
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 BOOST_CLASS_EXPORT(t2n_exception)
77
78 // a (unspecified) problem with libt2n communication
79 class t2n_communication_error : public t2n_exception
80 {
81     private:
82         friend class boost::serialization::access;
83         template<class Archive>
84         void serialize(Archive & ar, const unsigned int version)
85         {
86             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
87         }
88
89     public:
90         t2n_communication_error(const std::string& _message)
91             : t2n_exception(_message)
92             { }
93
94         t2n_communication_error()
95             { }
96
97         t2n_exception* clone() const
98             { return new t2n_communication_error(*this); }
99
100         void do_throw()
101             { throw *this; }
102 };
103 BOOST_CLASS_EXPORT(t2n_communication_error)
104
105 // can't connect to libt2n server
106 class t2n_connect_error : public t2n_communication_error
107 {
108     private:
109         friend class boost::serialization::access;
110         template<class Archive>
111         void serialize(Archive & ar, const unsigned int version)
112         {
113             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
114         }
115
116     public:
117         t2n_connect_error(const std::string& _message)
118             : t2n_communication_error(_message)
119             { }
120
121         t2n_connect_error()
122             { }
123
124         t2n_exception* clone() const
125             { return new t2n_connect_error(*this); }
126
127         void do_throw()
128             { throw *this; }
129 };
130 BOOST_CLASS_EXPORT(t2n_connect_error)
131
132 // error establishing a socket on the server (only thrown on the server-side)
133 class t2n_server_error : public t2n_communication_error
134 {
135     private:
136         friend class boost::serialization::access;
137         template<class Archive>
138         void serialize(Archive & ar, const unsigned int version)
139         {
140             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
141         }
142
143     public:
144         t2n_server_error(const std::string& _message)
145             : t2n_communication_error(_message)
146             { }
147
148         t2n_server_error()
149             { }
150
151         t2n_exception* clone() const
152             { return new t2n_server_error(*this); }
153
154         void do_throw()
155             { throw *this; }
156 };
157 BOOST_CLASS_EXPORT(t2n_server_error)
158
159 // error transmitting or receiving libt2n messages
160 class t2n_transfer_error : public t2n_communication_error
161 {
162     private:
163         friend class boost::serialization::access;
164         template<class Archive>
165         void serialize(Archive & ar, const unsigned int version)
166         {
167             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
168         }
169
170     public:
171         t2n_transfer_error(const std::string& _message)
172             : t2n_communication_error(_message)
173             { }
174
175         t2n_transfer_error()
176             { }
177
178         t2n_exception* clone() const
179             { return new t2n_transfer_error(*this); }
180
181         void do_throw()
182             { throw *this; }
183 };
184 BOOST_CLASS_EXPORT(t2n_transfer_error)
185
186 // tried to talk to an incompatible libt2n version
187 class t2n_version_mismatch : public t2n_communication_error
188 {
189     private:
190         friend class boost::serialization::access;
191         template<class Archive>
192         void serialize(Archive & ar, const unsigned int version)
193         {
194             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_communication_error);
195         }
196
197     public:
198         t2n_version_mismatch(const std::string& _message)
199             : t2n_communication_error(_message)
200             { }
201
202         t2n_version_mismatch()
203             { }
204
205         t2n_exception* clone() const
206             { return new t2n_version_mismatch(*this); }
207
208         void do_throw()
209             { throw *this; }
210 };
211 BOOST_CLASS_EXPORT(t2n_version_mismatch)
212
213 // illegal libt2n command received
214 class t2n_command_error : public t2n_exception
215 {
216     private:
217         friend class boost::serialization::access;
218         template<class Archive>
219         void serialize(Archive & ar, const unsigned int version)
220         {
221             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
222         }
223
224     public:
225         t2n_command_error(const std::string& _message)
226             : t2n_exception(_message)
227             { }
228
229         t2n_command_error()
230             { }
231
232         t2n_exception* clone() const
233             { return new t2n_command_error(*this); }
234
235         void do_throw()
236             { throw *this; }
237 };
238 BOOST_CLASS_EXPORT(t2n_command_error)
239
240 // error serializing or deserializing a libt2n command packet
241 class t2n_serialization_error : public t2n_exception
242 {
243     private:
244         friend class boost::serialization::access;
245         template<class Archive>
246         void serialize(Archive & ar, const unsigned int version)
247         {
248             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
249         }
250
251     public:
252         t2n_serialization_error(const std::string& _message)
253             : t2n_exception(_message)
254             { }
255
256         t2n_serialization_error()
257             { }
258
259         t2n_exception* clone() const
260             { return new t2n_serialization_error(*this); }
261
262         void do_throw()
263             { throw *this; }
264 };
265 BOOST_CLASS_EXPORT(t2n_serialization_error)
266
267 // a runtime error within the remote function
268 // derive your own custom exceptions from this one
269 class t2n_runtime_error : public t2n_exception
270 {
271     private:
272         friend class boost::serialization::access;
273         template<class Archive>
274         void serialize(Archive & ar, const unsigned int version)
275         {
276             ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(t2n_exception);
277         }
278
279     public:
280         t2n_runtime_error(const std::string& _message)
281             : t2n_exception(_message)
282             { }
283
284         t2n_runtime_error()
285             { }
286
287         t2n_exception* clone() const
288             { return new t2n_runtime_error(*this); }
289
290         void do_throw()
291             { throw *this; }
292 };
293 BOOST_CLASS_EXPORT(t2n_runtime_error)
294
295 }
296
297 #endif