Savarese Software Research Corporation
ScatterMessage.h
Go to the documentation of this file.
1 /* Copyright 2006,2007 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_SCATTER_MESSAGE_H
22 #define __SSRC_SPREAD_SCATTER_MESSAGE_H
23 
24 #include <ssrc/spread/Message.h>
25 
26 #include <utility>
27 #include <vector>
28 
30 
48 class ScatterMessage : public BaseMessage {
49 public:
50 
51  enum {
56  MaxScatterElements = MAX_CLIENT_SCATTER_ELEMENTS
57  };
58 
59 private:
60  friend class Mailbox;
61 
62  typedef std::pair<Message *, int> value_type;
63 
64  Spread::scatter _scatter;
65  std::vector<value_type> _messages;
66  unsigned int _size;
67 
68  const Spread::scatter *scatter() const {
69  return &_scatter;
70  }
71 
72  Spread::scatter *scatter() {
73  return &_scatter;
74  }
75 
76  void init_pre_receive();
77 
78  void init_post_receive(int bytes_received);
79 
80  Message * message(const unsigned int index) {
81  return _messages[index].first;
82  }
83 
84  void resize_message(const unsigned int message_index,
85  const unsigned int size)
86  {
87  value_type & v = _messages[message_index];
88  Message *m = v.first;
89 
90  _size-=m->size();
91  m->resize(size);
92  _size+=m->size();
93  _scatter.elements[v.second].buf = &((*m)[0]);
94  _scatter.elements[v.second].len = m->size();
95  }
96 
97 protected:
98 
99 #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
100 
101  virtual int sp_get_membership_info(Spread::membership_info *info) const {
102  return Spread::SP_scat_get_memb_info(&_scatter, service(), info);
103  }
104 
105  virtual int sp_get_vs_set_members(const Spread::vs_set_info *vs_set,
106  Spread::group_type member_names[],
107  unsigned int member_names_count)
108  const
109  {
110  return
111  Spread::SP_scat_get_vs_set_members(&_scatter, vs_set, member_names,
112  member_names_count);
113  }
114 
115  virtual int sp_get_vs_sets_info(Spread::vs_set_info *vs_sets,
116  unsigned int num_vs_sets,
117  unsigned int *index)
118  const
119  {
120  return
121  Spread::SP_scat_get_vs_sets_info(&_scatter, vs_sets, num_vs_sets, index);
122  }
123 
124 #endif
125 
126 public:
127 
133  ScatterMessage() : _messages(), _size(0) {
134  _scatter.num_elements = 0;
135  }
136 
142  virtual unsigned int size() const {
143  return _size;
144  }
145 
150  virtual void clear() {
151  _scatter.num_elements = 0;
152  _size = 0;
153  _messages.clear();
154  }
155 
160  unsigned int count_message_parts() const {
161  return _scatter.num_elements;
162  }
163 
172  unsigned int count_message_objects() const {
173  return _messages.size();
174  }
175 
176  bool add(const void *data, const unsigned int size);
177 
197  bool add(const Message & message) {
198  if(add(&message[0], message.size())) {
199  // We cheat and cast away constness!
200  _messages.push_back(value_type(const_cast<Message*>(&message),
201  _scatter.num_elements - 1));
202  return true;
203  } else
204  return false;
205  }
206 };
207 
208 
210 
211 #endif

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