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