Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/registry - protocol.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 19 19 100.0 %
Date: 2012-04-09 Functions: 43 43 100.0 %
Branches: 18 40 45.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                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the Registry messaging protocol.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_REGISTRY_PROTOCOL_H
      23                 :            : #define __SSRC_WSPR_REGISTRY_PROTOCOL_H
      24                 :            : 
      25                 :            : #include <boost/serialization/vector.hpp>
      26                 :            : 
      27                 :            : #include <ssrc/wispers/protocol.h>
      28                 :            : 
      29                 :            : #if !defined(SWIG)
      30                 :            : 
      31                 :            : __BEGIN_NS_SSRC_WSPR_PROTOCOL
      32                 :            : 
      33   [ +  -  +  - ]:         52 : WSPR_DEFINE_PROTOCOL(Registry,registry);
      34                 :            : 
      35                 :            : __END_NS_SSRC_WSPR_PROTOCOL
      36                 :            : 
      37                 :            : #endif
      38                 :            : 
      39                 :            : __BEGIN_NS_SSRC_WSPR_REGISTRY
      40                 :            : 
      41                 :            : using std::string;
      42                 :            : 
      43   [ +  -  +  - ]:         73 : struct RegistryEntry {
      44                 :            :   string service_name;
      45                 :            :   string service_type;
      46                 :            : 
      47         [ +  - ]:          5 :   RegistryEntry() : service_name(), service_type() { }
      48                 :            : 
      49                 :         16 :   RegistryEntry(const string & name, const string & type) :
      50         [ +  - ]:         16 :     service_name(name), service_type(type)
      51                 :         16 :   { }
      52                 :            : 
      53                 :            :   template<class Archive>
      54                 :         10 :   void serialize(Archive & ar, const unsigned int) {
      55                 :         10 :     ar & service_name & service_type;
      56                 :         10 :   }
      57                 :            : };
      58                 :            : 
      59                 :            : enum RegistryMessageType {
      60                 :            :   Register, Reregister, Unregister,
      61                 :            :   QueryResult, QueryByName, QueryByType, QueryAll
      62                 :            : };
      63                 :            : 
      64                 :            : enum IndexScheme { ByName, ByType, ByComposite};
      65                 :            : 
      66                 :            : #if !defined(SWIG)
      67                 :            : typedef boost::multi_index_container<
      68                 :            :   RegistryEntry,
      69                 :            :   boost::multi_index::indexed_by<
      70                 :            :     boost::multi_index::hashed_non_unique<
      71                 :            :       boost::multi_index::member<RegistryEntry, string,
      72                 :            :                                  &RegistryEntry::service_name> >,
      73                 :            :     boost::multi_index::hashed_non_unique<
      74                 :            :       boost::multi_index::member<RegistryEntry, string,
      75                 :            :                                  &RegistryEntry::service_type> >,
      76                 :            :     boost::multi_index::hashed_unique<
      77                 :            :       boost::multi_index::composite_key<
      78                 :            :         RegistryEntry,
      79                 :            :         boost::multi_index::member<RegistryEntry, string,
      80                 :            :                                    &RegistryEntry::service_name>,
      81                 :            :         boost::multi_index::member<RegistryEntry, string,
      82                 :            :                                    &RegistryEntry::service_type> >
      83                 :            :       >
      84                 :            :     > > service_map;
      85                 :            : #endif
      86                 :            : 
      87                 :            : typedef service_map::nth_index<ByName>::type index_by_name;
      88                 :            : typedef service_map::nth_index<ByType>::type index_by_type;
      89                 :            : typedef service_map::nth_index<ByComposite>::type index_by_composite;
      90                 :            : 
      91                 :            : /**
      92                 :            :  * Returns a string identifying the group to which all Registry
      93                 :            :  * instances belong.  This is the group where clients should direct
      94                 :            :  * their queries.
      95                 :            :  * @return A string identifying the group to which all Registry
      96                 :            :  * instances belong.
      97                 :            :  */
      98                 :         17 : inline string service_group_registry() {
      99                 :         17 :   return protocol::service_type_registry();
     100                 :            : }
     101                 :            : 
     102                 :         16 : inline string event_group_register() {
     103         [ +  - ]:         16 :   return WSPR_EVENT_GROUP("registry", "register");
     104                 :            : }
     105                 :            : 
     106                 :            : typedef std::vector<string> query_list;
     107                 :            : typedef std::vector<string> service_type_list;
     108                 :            : 
     109                 :            : typedef protocol::MessageRegistry<QueryAll> MessageQueryAll;
     110                 :            : 
     111                 :            : #define WSPR_MESSAGE_REGISTRATION(message_id)                    \
     112                 :            :   WISP_PROTOCOL_MESSAGE(message_id, protocol::MessageRegistry,   \
     113                 :            :                        ((string, service_name))                 \
     114                 :            :                        ((service_type_list, service_types)))
     115                 :            : 
     116   [ +  -  +  -  :          6 : WSPR_MESSAGE_REGISTRATION(Register);
                   #  # ]
     117   [ +  -  +  -  :         30 : WSPR_MESSAGE_REGISTRATION(Reregister);
          +  -  +  -  +  
                      - ]
     118   [ +  -  +  -  :          6 : WSPR_MESSAGE_REGISTRATION(Unregister);
                   #  # ]
     119                 :            : 
     120                 :            : #undef WSPR_MESSAGE_REGISTRATION
     121                 :            : 
     122                 :            : WISP_PROTOCOL_MESSAGE(QueryResult, protocol::MessageRegistry,
     123   [ +  -  +  - ]:         22 :                      ((service_map, result)));
     124                 :            : WISP_PROTOCOL_MESSAGE(QueryByName, protocol::MessageRegistry,
     125                 :         14 :                      ((query_list, keys)));
     126                 :            : WISP_PROTOCOL_MESSAGE(QueryByType, protocol::MessageRegistry,
     127                 :          6 :                      ((query_list, keys)));
     128                 :            : 
     129                 :            : typedef BinaryPackingTraits RegistryPackingTraits;
     130                 :            : typedef NS_SSRC_WISP_PROTOCOL::Caller<RegistryPackingTraits> RegistryCaller;
     131                 :            : 
     132                 :            : WISP_ONE_WAY_CALL(RegistryCaller, Register);
     133                 :            : WISP_ONE_WAY_CALL(RegistryCaller, Reregister);
     134                 :            : WISP_ONE_WAY_CALL(RegistryCaller, Unregister);
     135                 :            : WISP_ONE_WAY_CALL(RegistryCaller, QueryResult);
     136                 :            : 
     137                 :            : WISP_TWO_WAY_CALL(RegistryCaller, QueryByName, QueryResult);
     138                 :            : WISP_TWO_WAY_CALL(RegistryCaller, QueryByType, QueryResult);
     139                 :            : WISP_TWO_WAY_CALL(RegistryCaller, QueryAll, QueryResult);
     140                 :            : 
     141                 :            : /* Utility functions. */
     142                 :            : 
     143                 :            : /**
     144                 :            :  * Synchronously looks up a service by its service type.
     145                 :            :  *
     146                 :            :  * @param caller The Caller to use to perform the lookup.
     147                 :            :  * @param service_type A string identifying the service type to look up.
     148                 :            :  * @return The private group name of the first service found matching
     149                 :            :  * the specified type.  If no service is found, returns an empty string.
     150                 :            :  */
     151                 :            : template<typename caller_type>
     152                 :            : string get_service_by_type(caller_type & caller, string service_type) {
     153                 :            :   CallQueryByType::parameter_type query;
     154                 :            :   CallQueryByType::return_type result;
     155                 :            : 
     156                 :            :   query.keys.push_back(service_type);
     157                 :            : 
     158                 :            :   caller(CallQueryByType(), service_group_registry(), query, &result);
     159                 :            : 
     160                 :            :   if(result.result.size() > 0)
     161                 :            :     return result.result.begin()->service_name;
     162                 :            : 
     163                 :            :   return string();
     164                 :            : }
     165                 :            : 
     166                 :            : __END_NS_SSRC_WSPR_REGISTRY
     167                 :            : 
     168                 :            : #endif