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) {
66  value_type & value = _messages[i];
67  int mindex = value.second;
68  if(mindex >= _scatter.num_elements)
69  break;
70 
71  while(index < mindex)
72  bytes_received-=_scatter.elements[index++].len;
73 
74  if(bytes_received <= 0)
75  break;
76 
77  len = _scatter.elements[mindex].len;
78  m = value.first;
79 
80  m->set_type(type());
81  m->set_service(service());
83  m->set_sender(_sender);
84 
85  if(len <= bytes_received)
86  m->resize(len);
87  else
88  m->resize(bytes_received);
89 
90  bytes_received-=len;
91  }
92 
93  // Anything left over gets a size of zero.
94  for(; i < size; ++i) {
95  m = _messages[i].first;
96  m->resize(0);
97  m->set_type(type());
98  m->set_service(service());
100  m->set_sender(_sender);
101  }
102 }
103 
115 bool ScatterMessage::add(const void *data, const unsigned int size) {
116  int i = _scatter.num_elements;
117 
118  if(i >= MaxScatterElements)
119  return false;
120 
121  ++_scatter.num_elements;
122  _scatter.elements[i].buf =
123  const_cast<char *>(static_cast<const char *>(data));
124  _scatter.elements[i].len = size;
125  _size+=size;
126 
127  return true;
128 }
129 

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