Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/registry - service.cc (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 35 55 63.6 %
Date: 2012-04-09 Functions: 7 9 77.8 %
Branches: 57 163 35.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : #include <set>
      18                 :            : 
      19                 :            : #include <ssrc/wispers/registry/service.h>
      20                 :            : 
      21                 :            : __BEGIN_NS_SSRC_WSPR_REGISTRY
      22                 :            : 
      23                 :            : using NS_SSRC_WSPR_SERVICE::ServiceProtocol;
      24                 :            : 
      25                 :            : // TODO: move this into utility package or into ssrcspread
      26                 :          0 : int group_index(const NS_SSRC_SPREAD::GroupList & groups,
      27                 :            :                 const string & group)
      28                 :            : {
      29                 :          0 :   int index(groups.size());
      30                 :            : 
      31         [ #  # ]:          0 :   while(index-- > 0) {
      32   [ #  #  #  # ]:          0 :     if(groups[index] == group)
      33                 :          0 :       return index;
      34                 :            :   }
      35                 :            : 
      36                 :          0 :   return index;
      37                 :            : }
      38                 :            : 
      39                 :            : /**
      40                 :            :  *
      41                 :            :  */
      42                 :          1 : void Registry::request_registrations(const string & registry,
      43                 :            :                                      const bool self_discard)
      44                 :            : {
      45                 :            :   Message::Service service_type =
      46         [ +  - ]:          1 :     (self_discard ? Message::FIFO : Message::FIFOSelfDiscard);
      47                 :            : 
      48                 :            :   _caller.send<ServiceProtocol::CallReregister>
      49         [ +  - ]:          1 :     (service_type, ServiceProtocol::service_group(), registry);
      50                 :          1 : }
      51                 :            : 
      52                 :            : /**
      53                 :            :  *
      54                 :            :  */
      55                 :          4 : void Registry::process_membership_message(const MessageInfo & msginfo,
      56                 :            :                                           const MembershipInfo & meminfo)
      57                 :            : {
      58                 :          8 :   string sender(msginfo.sender());
      59                 :            : 
      60   [ +  -  +  -  :          4 :   if(sender == ServiceProtocol::service_group()) {
             +  -  +  + ]
      61         [ +  + ]:          3 :     if(meminfo.caused_by_join()) {
      62   [ +  -  +  - ]:          4 :       string changed(meminfo.changed_member());
      63                 :            : 
      64   [ +  -  +  -  :          2 :       if(name() == changed) {
             +  -  +  + ]
      65                 :            :         // TODO: Move to separate method.
      66                 :            :         // Register all existing services (includes us) as unknown.
      67         [ +  - ]:          1 :         const unsigned int count_groups = _caller.mbox().count_groups();
      68         [ +  + ]:          2 :         for(unsigned int i = 0; i < count_groups; ++i)
      69   [ +  -  +  -  :          1 :           service_join(msginfo.groups[i]);
                   +  - ]
      70                 :            : 
      71   [ +  -  +  -  :          1 :         request_registrations(name());
                   +  - ]
      72                 :            :       } else
      73         [ +  - ]:          1 :         service_join(changed);
      74   [ -  +  #  #  :          1 :     } else if(meminfo.caused_by_leave() || meminfo.caused_by_disconnect())
                   +  - ]
      75   [ +  -  +  -  :          1 :       service_leave(meminfo.changed_member());
                   +  - ]
      76         [ #  # ]:          0 :     else if(meminfo.caused_by_network()) {
      77                 :            :       // Remove all service entries that aren't in new group.
      78                 :            :       typedef boost::multi_index_container<string> group_set;
      79   [ #  #  #  #  :          0 :       group_set group;
                   #  # ]
      80         [ #  # ]:          0 :       int i = _caller.mbox().count_groups();
      81                 :            : 
      82         [ #  # ]:          0 :       while(i-- > 0)
      83   [ #  #  #  #  :          0 :         group.insert(msginfo.groups[i]);
                   #  # ]
      84                 :            : 
      85         [ #  # ]:          0 :       group_set::iterator group_end(group.end());
      86         [ #  # ]:          0 :       index_by_name::iterator it(begin<ByName>());
      87         [ #  # ]:          0 :       index_by_name::iterator end(end<ByName>());
      88                 :            : 
      89   [ #  #  #  # ]:          0 :       while(it != end) {
      90   [ #  #  #  #  :          0 :         if(group.find(it->service_name) == group_end)
             #  #  #  # ]
      91   [ #  #  #  # ]:          0 :           get_index<ByName>().erase(it);
      92         [ #  # ]:          0 :         ++it;
      93                 :            :       }
      94                 :            :     }
      95   [ +  -  +  -  :          1 :   } else if(sender == service_group_registry()) {
             +  -  +  - ]
      96         [ +  - ]:          1 :     if(meminfo.is_regular_membership()) {
      97   [ +  -  +  - ]:          1 :       if(_caller.mbox().count_groups() > 0)
      98   [ +  -  +  -  :          1 :         _leader_name = msginfo.groups[0];
                   +  - ]
      99                 :            :       else
     100         [ #  # ]:          0 :         _leader_name.clear();
     101                 :            : 
     102   [ +  -  +  -  :          1 :       _is_leader = (_leader_name == name());
                   +  - ]
     103                 :            : 
     104                 :            :       // Request reregistrations to go to registry.
     105   [ -  +  #  #  :          1 :       if(meminfo.caused_by_network() && _is_leader)
                   -  + ]
     106   [ #  #  #  #  :          0 :         request_registrations(service_group_registry(), false);
                   #  # ]
     107                 :            :     }
     108                 :            :   }
     109                 :          4 : }
     110                 :            : 
     111                 :            : /**
     112                 :            :  *
     113                 :            :  */
     114                 :          3 : void Registry::transition(State state) {
     115      [ +  +  + ]:          3 :   switch(state) {
     116                 :            :   case Starting:
     117         [ +  - ]:          1 :     _caller.join(service_group_registry());
     118                 :          1 :     state = Started;
     119                 :          1 :     break;
     120                 :            :   case Stopping:
     121         [ +  - ]:          1 :     _caller.leave(service_group_registry());
     122                 :          1 :     break;
     123                 :            :   default:
     124                 :          1 :     break;
     125                 :            :   }
     126                 :            : 
     127                 :            :   // Transition at end so we've joined registry group before
     128                 :            :   // super::transition registers us.
     129                 :          3 :   super::transition(state);
     130                 :          3 : }
     131                 :            : 
     132   [ +  -  +  -  :          7 : __END_NS_SSRC_WSPR_REGISTRY
          +  -  +  -  +  
                -  +  - ]