Savarese Software Research Corporation
ScatterMessage.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2006,2007 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 
23 #ifndef __SSRC_SPREAD_SCATTER_MESSAGE_H
24 #define __SSRC_SPREAD_SCATTER_MESSAGE_H
25 
26 #include <ssrc/spread/Message.h>
27 
28 #include <utility>
29 #include <vector>
30 
32 
50 class ScatterMessage : public BaseMessage {
51 public:
52 
53  enum {
58  MaxScatterElements = MAX_CLIENT_SCATTER_ELEMENTS
59  };
60 
61 private:
62  friend class Mailbox;
63 
64  typedef std::pair<Message *, int> value_type;
65 
66  Spread::scatter _scatter;
67  std::vector<value_type> _messages;
68  unsigned int _size;
69 
70  const Spread::scatter *scatter() const {
71  return &_scatter;
72  }
73 
74  Spread::scatter *scatter() {
75  return &_scatter;
76  }
77 
78  void init_pre_receive();
79 
80  void init_post_receive(int bytes_received);
81 
82  Message * message(const unsigned int index) {
83  return _messages[index].first;
84  }
85 
86  void resize_message(const unsigned int message_index,
87  const unsigned int size)
88  {
89  value_type & v = _messages[message_index];
90  Message *m = v.first;
91 
92  _size-=m->size();
93  m->resize(size);
94  _size+=m->size();
95  _scatter.elements[v.second].buf = &((*m)[0]);
96  _scatter.elements[v.second].len = m->size();
97  }
98 
99 protected:
100 
101 #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
102 
103  virtual int sp_get_membership_info(Spread::membership_info *info) const {
104  return Spread::SP_scat_get_memb_info(&_scatter, service(), info);
105  }
106 
107  virtual int sp_get_vs_set_members(const Spread::vs_set_info *vs_set,
108  Spread::group_type member_names[],
109  unsigned int member_names_count)
110  const
111  {
112  return
113  Spread::SP_scat_get_vs_set_members(&_scatter, vs_set, member_names,
114  member_names_count);
115  }
116 
117  virtual int sp_get_vs_sets_info(Spread::vs_set_info *vs_sets,
118  unsigned int num_vs_sets,
119  unsigned int *index)
120  const
121  {
122  return
123  Spread::SP_scat_get_vs_sets_info(&_scatter, vs_sets, num_vs_sets, index);
124  }
125 
126 #endif
127 
128 public:
129 
135  // The initial vector capacity of 8 is an arbitrary choice. We don't expect
136  // a typical ScatterMessage to have too many parts.
137  ScatterMessage() : _messages(8), _size(0) {
138  _scatter.num_elements = 0;
139  }
140 
146  virtual unsigned int size() const {
147  return _size;
148  }
149 
154  virtual void clear() {
155  _scatter.num_elements = 0;
156  _size = 0;
157  _messages.clear();
158  }
159 
164  unsigned int count_message_parts() const {
165  return _scatter.num_elements;
166  }
167 
176  unsigned int count_message_objects() const {
177  return _messages.size();
178  }
179 
180  bool add(const void *data, const unsigned int size);
181 
201  bool add(const Message & message) {
202  if(add(&message[0], message.size())) {
203  // We cheat and cast away constness!
204  _messages.push_back(value_type(const_cast<Message*>(&message),
205  _scatter.num_elements - 1));
206  return true;
207  } else
208  return false;
209  }
210 };
211 
212 
214 
215 #endif

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