Savarese Software Research Corporation
ScatterMessage.cc
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 
19 
21 
28 void ScatterMessage::init_pre_receive() {
29  int size = _messages.size();
30 
31  for(int i = 0; i < size; ++i) {
32  value_type & value = _messages[i];
33  Message *m = value.first;
34 
35  if(value.second >= _scatter.num_elements)
36  break;
37 
38  _size-=m->size();
39  m->resize(m->capacity());
40  _size+=m->size();
41  _scatter.elements[value.second].len = m->size();
42  }
43 }
44 
55 void ScatterMessage::init_post_receive(int bytes_received) {
56  int index = 0, i, len;
57  int size = _messages.size();
58  Message *m;
59 
60  if(bytes_received > 0)
61  _size = bytes_received;
62  else
63  _size = 0;
64 
65  for(i = 0; i < size && bytes_received > 0; ++i, ++index) {
66  value_type & value = _messages[i];
67  int mindex = value.second;
68 
69  if(mindex >= _scatter.num_elements)
70  break;
71 
72  while(index < mindex)
73  bytes_received-=_scatter.elements[index++].len;
74 
75  if(bytes_received <= 0)
76  break;
77 
78  len = _scatter.elements[mindex].len;
79  m = value.first;
80 
81  m->set_type(type());
82  m->set_service(service());
84  m->set_sender(_sender);
85 
86  if(len <= bytes_received)
87  m->resize(len);
88  else
89  m->resize(bytes_received);
90 
91  bytes_received-=len;
92  }
93 
94  // Anything left over gets a size of zero.
95  for(; i < size; ++i) {
96  m = _messages[i].first;
97  m->resize(0);
98  m->set_type(type());
99  m->set_service(service());
101  m->set_sender(_sender);
102  }
103 }
104 
116 bool ScatterMessage::add(const void *data, const unsigned int size) {
117  int i = _scatter.num_elements;
118 
119  if(i >= MaxScatterElements)
120  return false;
121 
122  ++_scatter.num_elements;
123  _scatter.elements[i].buf =
124  const_cast<char *>(static_cast<const char *>(data));
125  _scatter.elements[i].len = size;
126  _size+=size;
127 
128  return true;
129 }
130 

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