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 
unsigned int capacity() const
Returns the maximum number of elements that can be written to the buffer without reallocating memory...
Definition: Buffer.h:86
message_type type() const
Returns the type of the message.
Definition: BaseMessage.h:193
void resize(const unsigned int size)
Resizes the buffer to the specified size.
Definition: Buffer.h:214
void set_endian_mismatch(const bool mismatch=true)
Sets the endian mismatch flag to the specified value.
Definition: BaseMessage.h:217
Message is a reusable and resizable data buffer for sending and receiving messages.
Definition: Message.h:47
This header defines the ScatterMessage class.
void set_service(const service_type service)
Sets the service type of the message.
Definition: BaseMessage.h:167
string _sender
Definition: BaseMessage.h:116
#define __BEGIN_NS_SSRC_SPREAD
virtual unsigned int size() const
Returns the number of bytes in the message.
Definition: Message.h:96
The maximum number of elements that can be added to a scatter message.
service_type service() const
Returns the service type requested (for sends) or sent under (for receives) of the message...
Definition: BaseMessage.h:177
#define __END_NS_SSRC_SPREAD
void set_type(const message_type type)
Sets the message type identifier.
Definition: BaseMessage.h:185
void set_sender(const string &sender)
Sets the message sender.
Definition: BaseMessage.h:201
virtual unsigned int size() const
Returns the total size of the message (comprising all of its parts) in bytes.
bool endian_mismatch() const
Returns true if there is an endian mismatch between sender and receiver, false otherwise.
Definition: BaseMessage.h:227
bool add(const void *data, const unsigned int size)
Adds a message part to the ScatterMessage with a designated number of bytes indicating either the cap...

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