Ssrc C++ Binding for Spread 1.0.15 Unit Test Coverage
Current view: top level - ssrc/spread - ScatterMessage.cc (source / functions) Hit Total Coverage
Test: Ssrc C++/Lua/Perl/Python/Ruby Bindings for Spread 1.0.15 Unit Tests Lines: 49 54 90.7 %
Date: 2017-11-28 00:28:17 Functions: 3 3 100.0 %
Branches: 17 22 77.3 %

           Branch data     Line data    Source code
       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                 :            : 
      18                 :            : #include <ssrc/spread/ScatterMessage.h>
      19                 :            : 
      20                 :            : __BEGIN_NS_SSRC_SPREAD
      21                 :            : 
      22                 :            : /**
      23                 :            :  * Handles auto-resizing prior to a receive.  Each message part that
      24                 :            :  * is a Message instance is resized to its capacity so that it may be
      25                 :            :  * completely filled if necessary.  The size of the ScatterMessage is
      26                 :            :  * adjusted accordingly.
      27                 :            :  */
      28                 :         10 : void ScatterMessage::init_pre_receive() {
      29                 :         10 :   int size = _messages.size();
      30                 :            : 
      31         [ +  + ]:         23 :   for(int i = 0; i < size; ++i) {
      32                 :         13 :     value_type & value = _messages[i];
      33                 :         13 :     Message *m = value.first;
      34                 :            : 
      35         [ -  + ]:         13 :     if(value.second >= _scatter.num_elements)
      36                 :          0 :       break;
      37                 :            : 
      38                 :         13 :     _size-=m->size();
      39                 :         13 :     m->resize(m->capacity());
      40                 :         13 :     _size+=m->size();
      41                 :         13 :     _scatter.elements[value.second].len = m->size();
      42                 :            :   }
      43                 :         10 : }
      44                 :            : 
      45                 :            : /**
      46                 :            :  * Handles auto-resizing after a receive.  Each message part that is a
      47                 :            :  * Message instance is resized to reflect the number of bytes that was
      48                 :            :  * stored in it by the receive.  The type, service, endian mismatch,
      49                 :            :  * and sender of each Message is set to that of the message (copied
      50                 :            :  * from the ScatterMessage).
      51                 :            :  *
      52                 :            :  * @param bytes_received The total number of bytes received.  May be
      53                 :            :  * negative in case of an error.
      54                 :            :  */
      55                 :         10 : void ScatterMessage::init_post_receive(int bytes_received) {
      56                 :         10 :   int index = 0, i, len;
      57                 :         10 :   int size = _messages.size();
      58                 :            :   Message *m;
      59                 :            : 
      60         [ +  + ]:         10 :   if(bytes_received > 0)
      61                 :          9 :     _size = bytes_received;
      62                 :            :   else
      63                 :          1 :     _size = 0;
      64                 :            : 
      65   [ +  +  +  + ]:         21 :   for(i = 0; i < size && bytes_received > 0; ++i, ++index) {
      66                 :         11 :     value_type & value = _messages[i];
      67                 :         11 :     int mindex = value.second;
      68                 :            : 
      69         [ -  + ]:         11 :     if(mindex >= _scatter.num_elements)
      70                 :          0 :       break;
      71                 :            : 
      72         [ -  + ]:         11 :     while(index < mindex)
      73                 :          0 :       bytes_received-=_scatter.elements[index++].len;
      74                 :            : 
      75         [ -  + ]:         11 :     if(bytes_received <= 0)
      76                 :          0 :       break;
      77                 :            : 
      78                 :         11 :     len = _scatter.elements[mindex].len;
      79                 :         11 :     m   = value.first;
      80                 :            : 
      81                 :         11 :     m->set_type(type());
      82                 :         11 :     m->set_service(service());
      83                 :         11 :     m->set_endian_mismatch(endian_mismatch());
      84                 :         11 :     m->set_sender(_sender);
      85                 :            : 
      86         [ +  + ]:         11 :     if(len <= bytes_received)
      87                 :          5 :       m->resize(len);
      88                 :            :     else
      89                 :          6 :       m->resize(bytes_received);
      90                 :            : 
      91                 :         11 :     bytes_received-=len;
      92                 :            :   }
      93                 :            : 
      94                 :            :   // Anything left over gets a size of zero.
      95         [ +  + ]:         14 :   for(; i < size; ++i) {
      96                 :          2 :     m = _messages[i].first;
      97                 :          2 :     m->resize(0);
      98                 :          2 :     m->set_type(type());
      99                 :          2 :     m->set_service(service());
     100                 :          2 :     m->set_endian_mismatch(endian_mismatch());
     101                 :          2 :     m->set_sender(_sender);
     102                 :            :   }
     103                 :         10 : }
     104                 :            : 
     105                 :            : /**
     106                 :            :  * Adds a message part to the ScatterMessage with a designated number
     107                 :            :  * of bytes indicating either the capacity of the buffer (for
     108                 :            :  * receiving) or the length of its contents (for sending).
     109                 :            :  *
     110                 :            :  * @param data A pointer to the data buffer to add.
     111                 :            :  * @param size The number of bytes to be filled in to or read from the
     112                 :            :  * data buffer.
     113                 :            :  * @return true if the message part was added, false if there's no
     114                 :            :  * more parts can be added.
     115                 :            :  */
     116                 :         27 : bool ScatterMessage::add(const void *data, const unsigned int size) {
     117                 :         27 :   int i = _scatter.num_elements;
     118                 :            : 
     119         [ -  + ]:         27 :   if(i >= MaxScatterElements)
     120                 :          0 :     return false;
     121                 :            : 
     122                 :         27 :   ++_scatter.num_elements;
     123                 :         27 :   _scatter.elements[i].buf = 
     124                 :            :     const_cast<char *>(static_cast<const char *>(data));
     125                 :         27 :   _scatter.elements[i].len = size;
     126                 :         27 :   _size+=size;
     127                 :            : 
     128                 :         27 :   return true;
     129                 :            : }
     130                 :            : 
     131                 :            : __END_NS_SSRC_SPREAD