Ssrc C++ Binding for Spread 1.0.15 Unit Test Coverage
Current view: top level - ssrc/spread - BaseMessage.cc (source / functions) Hit Total Coverage
Test: Ssrc C++/Lua/Perl/Python/Ruby Bindings for Spread 1.0.15 Unit Tests Lines: 25 35 71.4 %
Date: 2017-11-28 00:28:17 Functions: 2 2 100.0 %
Branches: 15 32 46.9 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  *
       3                 :            :  * Copyright 2006 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/MembershipInfo.h>
      19                 :            : 
      20                 :            : __BEGIN_NS_SSRC_SPREAD
      21                 :            : 
      22                 :            : #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
      23                 :            : 
      24                 :            : /**
      25                 :            :  * Gets the vs_set members from a vs_set using the virtual
      26                 :            :  * sp_get_vs_set_members method, so the proper SP_ call is used based
      27                 :            :  * on whether or not the message is a scatter message.  On an
      28                 :            :  * Error::BufferTooShort error, automatically resizes the member group
      29                 :            :  * list and tries again
      30                 :            :  */
      31                 :          3 : void BaseMessage::get_vs_set_members(const Spread::vs_set_info *vs_set,
      32                 :            :                                      GroupList *members,
      33                 :            :                                      unsigned int offset) const
      34                 :            : {
      35                 :            :   int result;
      36                 :            : 
      37                 :          3 :   members->resize(members->capacity());
      38                 :            : 
      39                 :            :  try_again:
      40                 :            :   result =
      41                 :          3 :     sp_get_vs_set_members(vs_set, members->groups() + offset,
      42                 :          6 :                           members->size() - offset);
      43                 :            : 
      44         [ -  + ]:          3 :   if(result == Error::BufferTooShort) {
      45                 :          0 :     result = vs_set->num_members;
      46         [ #  # ]:          0 :     if(result > 0) {
      47                 :          0 :       members->resize(offset + result);
      48                 :          0 :       goto try_again;
      49                 :            :     } else
      50                 :          0 :       members->resize(offset);
      51         [ +  + ]:          3 :   } else if(result > 0)
      52                 :          2 :     members->resize(offset + result);
      53                 :            :   else
      54                 :          1 :     members->resize(offset);
      55                 :          3 : }
      56                 :            : 
      57                 :            : /**
      58                 :            :  * Extracts the membership information contained in this message and
      59                 :            :  * stores it in the supplied MembershipInfo object.  Only call this
      60                 :            :  * method if the message is a membership message.
      61                 :            :  *
      62                 :            :  * <b>Warning:</b> This method is available only when compiled against
      63                 :            :  * %Spread 4.x and greater.
      64                 :            :  *
      65                 :            :  * @param info A reference to a MembershipInfo that will store the membership
      66                 :            :  * information.
      67                 :            :  * @throw Error If the structure of the message or its service type does
      68                 :            :  * not comprise a valid membership message.
      69                 :            :  */
      70                 :          3 : void BaseMessage::get_membership_info(MembershipInfo & info) const
      71                 :            :   SSRC_DECL_THROW(Error)
      72                 :            : {
      73                 :          3 :   int result = sp_get_membership_info(&info._info);
      74                 :            : 
      75         [ -  + ]:          3 :   if(result <= 0)
      76                 :          0 :     throw Error(result);
      77                 :            : 
      78                 :          3 :   info.set_service(service());
      79                 :            : 
      80                 :          3 :   get_vs_set_members(&info._info.my_vs_set, &info._local_members);
      81                 :            : 
      82         [ +  + ]:          3 :   if(info._info.num_vs_sets > 0) {
      83         [ +  - ]:          4 :     detail::Buffer<Spread::vs_set_info> vs_sets(info._info.num_vs_sets);
      84                 :          2 :     unsigned int index = 0;
      85                 :            : 
      86         [ +  - ]:          2 :     vs_sets.resize(info._info.num_vs_sets);
      87         [ +  - ]:          2 :     result = sp_get_vs_sets_info(&vs_sets[0], vs_sets.size(), &index);
      88                 :            : 
      89         [ +  - ]:          2 :     if(result > 0) {
      90                 :            : 
      91                 :            :       // Just in case.
      92         [ -  + ]:          2 :       if(static_cast<unsigned int>(result) != vs_sets.size())
      93         [ #  # ]:          0 :         vs_sets.resize(result);
      94                 :            : 
      95         [ +  - ]:          2 :       info._non_local_members.clear();
      96                 :            : 
      97         [ +  + ]:          4 :       for(int i = 0; i < result; ++i) {
      98         [ -  + ]:          2 :         if(static_cast<unsigned int>(i) != index)
      99         [ #  # ]:          0 :           get_vs_set_members(&vs_sets[i], &info._non_local_members,
     100         [ #  # ]:          0 :                              info._non_local_members.size());
     101                 :            :       }
     102                 :            : 
     103                 :            :     } else
     104                 :          0 :       throw Error(result);
     105                 :            :   }
     106                 :          3 : }
     107                 :            : 
     108                 :            : #endif
     109                 :            : 
     110                 :            : __END_NS_SSRC_SPREAD