Savarese Software Research Corporation
ScatterMessage.h
Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright 2006,2007 Savarese Software Research Corporation
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.savarese.com/software/ApacheLicense-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00023 #ifndef __SSRC_SPREAD_SCATTER_MESSAGE_H
00024 #define __SSRC_SPREAD_SCATTER_MESSAGE_H
00025 
00026 #include <ssrc/spread/Message.h>
00027 
00028 #include <utility>
00029 #include <vector>
00030 
00031 __BEGIN_NS_SSRC_SPREAD
00032 
00050 class ScatterMessage : public BaseMessage {
00051 public:
00052 
00053   enum {
00058     MaxScatterElements = MAX_CLIENT_SCATTER_ELEMENTS
00059   };
00060 
00061 private:
00062   friend class Mailbox;
00063 
00064   typedef std::pair<Message *, int> value_type;
00065 
00066   Spread::scatter _scatter;
00067   std::vector<value_type> _messages;
00068   unsigned int _size;
00069 
00070   const Spread::scatter *scatter() const {
00071     return &_scatter;
00072   }
00073 
00074   Spread::scatter *scatter() {
00075     return &_scatter;
00076   }
00077 
00078   void init_pre_receive();
00079 
00080   void init_post_receive(int bytes_received);
00081 
00082   Message * message(const unsigned int index) {
00083     return _messages[index].first;
00084   }
00085 
00086   void resize_message(const unsigned int message_index,
00087                       const unsigned int size)
00088   {
00089     value_type & v = _messages[message_index];
00090     Message *m = v.first;
00091 
00092     _size-=m->size();
00093     m->resize(size);
00094     _size+=m->size();
00095     _scatter.elements[v.second].buf = &((*m)[0]);
00096     _scatter.elements[v.second].len = m->size();
00097   }
00098 
00099 protected:
00100 
00101 #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
00102 
00103   virtual int sp_get_membership_info(Spread::membership_info *info) const {
00104     return Spread::SP_scat_get_memb_info(&_scatter, service(), info);
00105   }
00106 
00107   virtual int sp_get_vs_set_members(const Spread::vs_set_info *vs_set,
00108                                         Spread::group_type member_names[],
00109                                         unsigned int member_names_count)
00110     const
00111   {
00112     return
00113       Spread::SP_scat_get_vs_set_members(&_scatter, vs_set, member_names,
00114                                          member_names_count);
00115   }
00116 
00117   virtual int sp_get_vs_sets_info(Spread::vs_set_info *vs_sets,
00118                                       unsigned int num_vs_sets,
00119                                       unsigned int *index)
00120     const
00121   {
00122     return
00123       Spread::SP_scat_get_vs_sets_info(&_scatter, vs_sets, num_vs_sets, index);
00124   }
00125 
00126 #endif
00127 
00128 public:
00129 
00135   // The initial vector capacity of 8 is an arbitrary choice.  We don't expect
00136   // a typical ScatterMessage to have too many parts.
00137   ScatterMessage() : _messages(8), _size(0) {
00138     _scatter.num_elements = 0;
00139   }
00140 
00146   virtual unsigned int size() const {
00147     return _size;
00148   }
00149 
00154   virtual void clear() {
00155     _scatter.num_elements = 0;
00156     _size = 0;
00157     _messages.clear();
00158   }
00159 
00164   unsigned int count_message_parts() const {
00165     return _scatter.num_elements;
00166   }
00167 
00176   unsigned int count_message_objects() const {
00177     return _messages.size();
00178   }
00179 
00180   bool add(const void *data, const unsigned int size);
00181 
00201   bool add(const Message & message) {
00202     if(add(&message[0], message.size())) {
00203       // We cheat and cast away constness!
00204       _messages.push_back(value_type(const_cast<Message*>(&message),
00205                                      _scatter.num_elements - 1));
00206       return true;
00207     } else
00208       return false;
00209   }
00210 };
00211 
00212 
00213 __END_NS_SSRC_SPREAD
00214 
00215 #endif

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