Branch data Line data Source code
1 : : /*
2 : : *
3 : : * Copyright 2006 Savarese Software Research Corporation
4 : : *
5 : : * Licensed under the Apache License, Version 2.0 (the "License");
6 : : * you may not use this file except in compliance with the License.
7 : : * You may obtain a copy of the License at
8 : : *
9 : : * http://www.savarese.com/software/ApacheLicense-2.0
10 : : *
11 : : * Unless required by applicable law or agreed to in writing, software
12 : : * distributed under the License is distributed on an "AS IS" BASIS,
13 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : : * See the License for the specific language governing permissions and
15 : : * limitations under the License.
16 : : */
17 : :
18 : : /**
19 : : * @file
20 : : * This header defines the BaseMessage class.
21 : : */
22 : :
23 : : #ifndef __SSRC_SPREAD_BASE_MESSAGE_H
24 : : #define __SSRC_SPREAD_BASE_MESSAGE_H
25 : :
26 : : #include <string>
27 : :
28 : : #include <ssrc/libssrcspread-packages.h>
29 : :
30 : : __BEGIN_NS_SPREAD_INCLUDE
31 : : # include <sp.h>
32 : :
33 : : // Convert define to typedef.
34 : : # if defined(int16)
35 : : typedef int16 foo_int16;
36 : : # undef int16
37 : : typedef foo_int16 int16;
38 : : # endif
39 : : __END_NS_SPREAD_INCLUDE
40 : :
41 : : #include <ssrc/spread/Error.h>
42 : : #include <ssrc/spread/GroupList.h>
43 : :
44 : : __BEGIN_NS_SSRC_SPREAD
45 : :
46 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
47 : :
48 : : // Forward declaration
49 : : class MembershipInfo;
50 : :
51 : : #endif
52 : :
53 : : /**
54 : : * BaseMessage is an abstract class defining the operations shared in
55 : : * common by Message and ScatterMessage. We do not document its
56 : : * protected members because they are intended only for internal library use.
57 : : */
58 : : class BaseMessage {
59 : : public:
60 : : /** Defines the type for 16-bit message type identifiers. */
61 : : typedef Spread::int16 message_type;
62 : :
63 : : /** Defines the type for specifying service types. */
64 : : typedef Spread::service service_type;
65 : :
66 : : /**
67 : : * Service is not a proper enumeration, but rather a specification
68 : : * of constants corresponding to the %Spread service type flags. We
69 : : * do not document the meaning of these flags here. See the %Spread
70 : : * C API documentation to understand their meaning. We will note,
71 : : * however, that DropReceive is not a service type, but rather a
72 : : * flag instructing the %Spread receive functions to truncate
73 : : * messages and group lists if the buffers are too small. There
74 : : * should be no need to use this constant in the API as it is
75 : : * handled by Mailbox::set_drop_receive.
76 : : *
77 : : *
78 : : */
79 : : enum Service {
80 : : Unreliable = UNRELIABLE_MESS,
81 : : Reliable = RELIABLE_MESS,
82 : : FIFO = FIFO_MESS,
83 : : Causal = CAUSAL_MESS,
84 : : Agreed = AGREED_MESS,
85 : : Safe = SAFE_MESS,
86 : : SelfDiscard = SELF_DISCARD,
87 : : DropReceive = DROP_RECV,
88 : : #define SERVICE_TYPE_DISCARD(s) s ## SelfDiscard = s | SelfDiscard
89 : :
90 : : SERVICE_TYPE_DISCARD(Unreliable),
91 : : SERVICE_TYPE_DISCARD(Reliable),
92 : : SERVICE_TYPE_DISCARD(FIFO),
93 : : SERVICE_TYPE_DISCARD(Causal),
94 : : SERVICE_TYPE_DISCARD(Agreed),
95 : : SERVICE_TYPE_DISCARD(Safe)
96 : :
97 : : #undef SERVICE_TYPE_DISCARD
98 : : };
99 : :
100 : : private:
101 : :
102 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
103 : :
104 : : void get_vs_set_members(const Spread::vs_set_info *vs_set,
105 : : GroupList *members, unsigned int offset = 0) const;
106 : :
107 : : #endif
108 : :
109 : : protected:
110 : :
111 : : message_type _type;
112 : : service_type _service_type;
113 : : bool _endian_mismatch;
114 : : string _sender;
115 : :
116 : : /**
117 : : * Initializes the class with a message type equal to 0, service
118 : : * type equal to Safe, endian mismatch equal to false, and a
119 : : * zero-length string for the sender.
120 : : */
121 : 40 : BaseMessage() :
122 [ + - ]: 40 : _type(0), _service_type(Safe), _endian_mismatch(false), _sender("")
123 : 40 : { }
124 : :
125 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
126 : :
127 : : virtual int sp_get_membership_info(Spread::membership_info *info) const = 0;
128 : :
129 : : virtual int sp_get_vs_set_members(const Spread::vs_set_info *vs_set,
130 : : Spread::group_type member_names[],
131 : : unsigned int member_names_count) const = 0;
132 : :
133 : : virtual int sp_get_vs_sets_info(Spread::vs_set_info *vs_sets,
134 : : unsigned int num_vs_sets,
135 : : unsigned int *index) const = 0;
136 : :
137 : : #endif
138 : :
139 : : public:
140 : :
141 : : /** Virtual destructor. */
142 [ - + ]: 40 : virtual ~BaseMessage() { }
143 : :
144 : : /**
145 : : * Returns the number of bytes in the message.
146 : : *
147 : : * @return The number of bytes in the message.
148 : : */
149 : : virtual unsigned int size() const = 0;
150 : :
151 : : /** Clears the message for reuse, resetting its size to zero. */
152 : : virtual void clear() = 0;
153 : :
154 : : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
155 : :
156 : : void get_membership_info(MembershipInfo & info) const SSRC_DECL_THROW(Error);
157 : :
158 : : #endif
159 : :
160 : : /**
161 : : * Sets the service type of the message.
162 : : *
163 : : * @param service The service type of the message.
164 : : */
165 : 25 : void set_service(const service_type service) {
166 : 25 : _service_type = service;
167 : 25 : }
168 : :
169 : : /**
170 : : * Returns the service type requested (for sends) or sent under (for
171 : : * receives) of the message.
172 : : *
173 : : * @return The service type of the message.
174 : : */
175 : 40 : service_type service() const {
176 : 40 : return _service_type;
177 : : }
178 : :
179 : : /**
180 : : * Sets the message type identifier.
181 : : * @param type The new message type.
182 : : */
183 : 24 : void set_type(const message_type type) {
184 : 24 : _type = type;
185 : 24 : }
186 : :
187 : : /**
188 : : * Returns the type of the message.
189 : : * @return The type of the message.
190 : : */
191 : 22 : message_type type() const {
192 : 22 : return _type;
193 : : }
194 : :
195 : : /**
196 : : * Sets the message sender.
197 : : * @param sender The message sender.
198 : : */
199 : 17 : void set_sender(const string & sender) {
200 : 17 : _sender = sender;
201 : 17 : }
202 : :
203 : : /**
204 : : * Returns the message sender. This only has meaning for received messages.
205 : : * @return The message sender.
206 : : */
207 : 6 : const string & sender() const {
208 : 6 : return _sender;
209 : : }
210 : :
211 : : /**
212 : : * Sets the endian mismatch flag to the specified value.
213 : : * @param mismatch The mismatch value.
214 : : */
215 : 17 : void set_endian_mismatch(const bool mismatch = true) {
216 : 17 : _endian_mismatch = mismatch;
217 : 17 : }
218 : :
219 : : /**
220 : : * Returns true if there is an endian mismatch between sender and receiver,
221 : : * false otherwise. This only has meaning for received messages.
222 : : * @return true if there is an endian mismatch between sender and receiver,
223 : : * false otherwise.
224 : : */
225 : 9 : bool endian_mismatch() const {
226 : 9 : return _endian_mismatch;
227 : : }
228 : :
229 : : /** Sets the service type to BaseMessage::Agreed. */
230 : : void set_agreed() {
231 : : set_service(Agreed);
232 : : }
233 : :
234 : : /**
235 : : * Returns true if service type is BaseMessage::Agreed, false otherwise.
236 : : * @return true if service type is BaseMessage::Agreed, false otherwise.
237 : : */
238 : : bool is_agreed() const {
239 : : return Is_agreed_mess(service());
240 : : }
241 : :
242 : : /** Sets the service type to BaseMessage::Causal. */
243 : : void set_causal() {
244 : : set_service(Causal);
245 : : }
246 : :
247 : : /**
248 : : * Returns true if service type is BaseMessage::Causal, false otherwise.
249 : : * @return true if service type is BaseMessage::Causal, false otherwise.
250 : : */
251 : : bool is_causal() const {
252 : : return Is_causal_mess(service());
253 : : }
254 : :
255 : : /** Sets the service type to BaseMessage::FIFO. */
256 : : void set_fifo() {
257 : : set_service(FIFO);
258 : : }
259 : :
260 : : /**
261 : : * Returns true if service type is BaseMessage::FIFO, false otherwise.
262 : : * @return true if service type is BaseMessage::FIFO, false otherwise.
263 : : */
264 : : bool is_fifo() const {
265 : : return Is_fifo_mess(service());
266 : : }
267 : :
268 : : /** Sets the service type to BaseMessage::Reliable. */
269 : : void set_reliable() {
270 : : set_service(Reliable);
271 : : }
272 : :
273 : : /**
274 : : * Returns true if service type is BaseMessage::Reliable, false otherwise.
275 : : * @return true if service type is BaseMessage::Reliable, false otherwise.
276 : : */
277 : : bool is_reliable() const {
278 : : return Is_reliable_mess(service());
279 : : }
280 : :
281 : : /** Sets the service type to BaseMessage::Unreliable. */
282 : : void set_unreliable() {
283 : : set_service(Unreliable);
284 : : }
285 : :
286 : : /**
287 : : * Returns true if service type is BaseMessage::Unreliable, false otherwise.
288 : : * @return true if service type is BaseMessage::Unreliable, false otherwise.
289 : : */
290 : : bool is_unreliable() const {
291 : : return Is_unreliable_mess(service());
292 : : }
293 : :
294 : : /** Sets the service type to BaseMessage::Safe. */
295 : : void set_safe() {
296 : : set_service(Safe);
297 : : }
298 : :
299 : : /**
300 : : * Returns true if service type is BaseMessage::Safe, false otherwise.
301 : : * @return true if service type is BaseMessage::Safe, false otherwise.
302 : : */
303 : : bool is_safe() const {
304 : : return Is_safe_mess(service());
305 : : }
306 : :
307 : : /**
308 : : * Adds or removes the BaseMessage::SelfDiscard flag to or from the
309 : : * service type.
310 : : *
311 : : * @param discard true to set the BaseMessage::SelfDiscard flag, false
312 : : * to remove it.
313 : : */
314 : 2 : void set_self_discard(const bool discard = true) {
315 : 2 : _service_type |= SelfDiscard;
316 [ + + ]: 2 : if(!discard)
317 : 1 : _service_type ^= SelfDiscard;
318 : 2 : }
319 : :
320 : : /**
321 : : * Returns true if service type has the BaseMessage::SelfDiscard flag set,
322 : : * false otherwise.
323 : : * @return true if service type has the BaseMessage::SelfDiscard flag set,
324 : : * false otherwise.
325 : : */
326 : 3 : bool is_self_discard() const {
327 : 3 : return Is_self_discard(service());
328 : : }
329 : :
330 : : /**
331 : : * Returns true if this is a regular data (as opposed to membership)
332 : : * message, false otherwise.
333 : : * @return true if this is a regular message, false otherwise.
334 : : */
335 : : bool is_regular() const {
336 : : return Is_regular_mess(service());
337 : : }
338 : :
339 : : /**
340 : : * Returns true if this is a membership message, false otherwise.
341 : : * @return true if this is a membership message, false otherwise.
342 : : */
343 : 4 : bool is_membership() const {
344 [ + - ][ + - ]: 4 : return Is_membership_mess(service());
345 : : }
346 : : };
347 : :
348 : : __END_NS_SSRC_SPREAD
349 : :
350 : : #endif
|