Savarese Software Research Corporation
Mailbox.h
Go to the documentation of this file.
1 /* Copyright 2006 Savarese Software Research Corporation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.savarese.com/software/ApacheLicense-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
21 #ifndef __SSRC_SPREAD_MAILBOX_H
22 #define __SSRC_SPREAD_MAILBOX_H
23 
25 
27 
36 class Timeout {
37  Spread::sp_time time;
38 
39 public:
46  Timeout(const Spread::sp_time time) : time(time) { }
47 
57  Timeout(const long sec = 0, const long usec = 0) {
58  time.sec = sec, time.usec = usec;
59  }
60 
66  operator Spread::sp_time() const { return time; }
67 };
68 
158 class Mailbox {
159 public:
161  typedef Spread::mailbox descriptor_type;
162 
163  static const Timeout ZeroTimeout;
164 
169  enum Priority {
170  Low = LOW_PRIORITY, Medium = MEDIUM_PRIORITY, High = HIGH_PRIORITY
171  };
172 
173 private:
174  bool _group_membership, _drop_receive, _killed;
175  string _connection, _name, _private_group;
176  descriptor_type _mbox;
177  ScatterMessage _scatter;
178  GroupList _groups;
179 
180 public:
181 
182  explicit Mailbox(const string & connection = "", const string & name = "",
183  const bool group_membership = true,
184  const Timeout & timeout = ZeroTimeout,
185  const Priority priority = Medium) SSRC_DECL_THROW(Error);
186 
191  ~Mailbox() {
192  if(!killed())
193  Spread::SP_disconnect(_mbox);
194  }
195 
200  const string & connection() const {
201  return _connection;
202  }
203 
208  const string & name() const {
209  return _name;
210  }
211 
218  descriptor_type descriptor() const {
219  return _mbox;
220  }
221 
226  const string & private_group() const {
227  return _private_group;
228  }
229 
236  bool group_membership() const {
237  return _group_membership;
238  }
239 
247  void set_drop_receive(const bool drop = true) {
248  _drop_receive = drop;
249  }
250 
255  bool drop_receive() const {
256  return _drop_receive;
257  }
258 
265  void join(const string & group) SSRC_DECL_THROW(Error) {
266  int result = Spread::SP_join(_mbox, group.c_str());
267  if(result != 0)
268  throw Error(result);
269  }
270 
277  void leave(const string & group) SSRC_DECL_THROW(Error) {
278  int result = Spread::SP_leave(_mbox, group.c_str());
279  if(result != 0)
280  throw Error(result);
281  }
282 
283 #ifdef LIBSSRCSPREAD_ENABLE_MAILBOX_KILL
284 
294  void kill() {
295  Spread::SP_kill(_mbox);
296  _killed = true;
297  }
298 #endif
299 
308  bool killed() const {
309  return _killed;
310  }
311 
320  unsigned int poll() const SSRC_DECL_THROW(Error) {
321  int result = Spread::SP_poll(_mbox);
322  if(result < 0)
323  throw Error(result);
324  return result;
325  }
326 
333  bool add_message_part(const void *data, const unsigned int size) {
334  return _scatter.add(data, size);
335  }
336 
346  bool add_message_part(const Message & message) {
347  return _scatter.add(message);
348  }
349 
355  void add_group(const string & group) {
356  _groups.add(group);
357  }
358 
364  void add_groups(const GroupList & groups) {
365  _groups.add(groups);
366  }
367 
376  string group(const unsigned int index) const {
377  return _groups[index];
378  }
379 
385  void copy_groups(GroupList & groups) {
386  groups = _groups;
387  }
388 
393  unsigned int count_groups() const {
394  return _groups.size();
395  }
396 
400  void clear_groups() {
401  _groups.clear();
402  }
403 
408  unsigned int count_message_parts() const {
409  return _scatter.count_message_parts();
410  }
411 
416  _scatter.clear();
417  }
418 
419  int send(const Message & message, const string & group)
420  SSRC_DECL_THROW(Error);
421 
422  int send(const Message & message, const GroupList & groups)
423  SSRC_DECL_THROW(Error);
424 
425  int send(const ScatterMessage & message, const GroupList & groups)
426  SSRC_DECL_THROW(Error);
427 
432  int send(const ScatterMessage & message, const string & group)
433  SSRC_DECL_THROW(Error)
434  {
435  clear_groups();
436  add_group(group);
437  return send(message, _groups);
438  }
439 
447  int send(const GroupList & groups, const BaseMessage::message_type type = 0,
449  SSRC_DECL_THROW(Error)
450  {
451  _scatter.set_type(type);
452  _scatter.set_service(service);
453  return send(_scatter, groups);
454  }
455 
462  int send(const string & group, const BaseMessage::message_type type = 0,
464  SSRC_DECL_THROW(Error)
465  {
466  clear_groups();
467  add_group(group);
468  return send(_groups, type, service);
469  }
470 
479  SSRC_DECL_THROW(Error)
480  {
481  return send(_groups, type, service);
482  }
483 
489  int send(const Message & message) SSRC_DECL_THROW(Error) {
490  return send(message, _groups);
491  }
492 
498  int send(const ScatterMessage & message) SSRC_DECL_THROW(Error) {
499  return send(message, _groups);
500  }
501 
502  int receive(ScatterMessage & message, GroupList & groups)
503  SSRC_DECL_THROW(BufferSizeError, Error);
504 
511  int receive(Message & message, GroupList & groups)
512  SSRC_DECL_THROW(BufferSizeError, Error)
513  {
514  clear_message_parts();
515  add_message_part(message);
516  return receive(_scatter, groups);
517  }
518 
526  int receive(GroupList & groups) SSRC_DECL_THROW(BufferSizeError, Error) {
527  return receive(_scatter, groups);
528  }
529 
537  int receive(Message & message) SSRC_DECL_THROW(BufferSizeError, Error) {
538  return receive(message, _groups);
539  }
540 
548  int receive(ScatterMessage & message)
549  SSRC_DECL_THROW(BufferSizeError, Error)
550  {
551  return receive(message, _groups);
552  }
553 
561  int receive() SSRC_DECL_THROW(BufferSizeError, Error) {
562  return receive(_scatter, _groups);
563  }
564 
565 };
566 
568 
569 #endif

Savarese Software Research Corporation
Copyright © 2006-2015 Savarese Software Research Corporation. All rights reserved.