Savarese Software Research Corporation
registry/protocol.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00022 #ifndef __SSRC_WSPR_REGISTRY_PROTOCOL_H
00023 #define __SSRC_WSPR_REGISTRY_PROTOCOL_H
00024 
00025 #include <boost/serialization/vector.hpp>
00026 
00027 #include <ssrc/wispers/protocol.h>
00028 
00029 #if !defined(SWIG)
00030 
00031 __BEGIN_NS_SSRC_WSPR_PROTOCOL
00032 
00033 WSPR_DEFINE_PROTOCOL(Registry,registry);
00034 
00035 __END_NS_SSRC_WSPR_PROTOCOL
00036 
00037 #endif
00038 
00039 __BEGIN_NS_SSRC_WSPR_REGISTRY
00040 
00041 using std::string;
00042 
00043 struct RegistryEntry {
00044   string service_name;
00045   string service_type;
00046 
00047   RegistryEntry() : service_name(), service_type() { }
00048 
00049   RegistryEntry(const string & name, const string & type) :
00050     service_name(name), service_type(type)
00051   { }
00052 
00053   template<class Archive>
00054   void serialize(Archive & ar, const unsigned int) {
00055     ar & service_name & service_type;
00056   }
00057 };
00058 
00059 enum RegistryMessageType {
00060   Register, Reregister, Unregister,
00061   QueryResult, QueryByName, QueryByType, QueryAll
00062 };
00063 
00064 enum IndexScheme { ByName, ByType, ByComposite};
00065 
00066 #if !defined(SWIG)
00067 typedef boost::multi_index_container<
00068   RegistryEntry,
00069   boost::multi_index::indexed_by<
00070     boost::multi_index::hashed_non_unique<
00071       boost::multi_index::member<RegistryEntry, string,
00072                                  &RegistryEntry::service_name> >,
00073     boost::multi_index::hashed_non_unique<
00074       boost::multi_index::member<RegistryEntry, string,
00075                                  &RegistryEntry::service_type> >,
00076     boost::multi_index::hashed_unique<
00077       boost::multi_index::composite_key<
00078         RegistryEntry,
00079         boost::multi_index::member<RegistryEntry, string,
00080                                    &RegistryEntry::service_name>,
00081         boost::multi_index::member<RegistryEntry, string,
00082                                    &RegistryEntry::service_type> >
00083       >
00084     > > service_map;
00085 #endif
00086 
00087 typedef service_map::nth_index<ByName>::type index_by_name;
00088 typedef service_map::nth_index<ByType>::type index_by_type;
00089 typedef service_map::nth_index<ByComposite>::type index_by_composite;
00090 
00098 inline string service_group_registry() {
00099   return protocol::service_type_registry();
00100 }
00101 
00102 inline string event_group_register() {
00103   return WSPR_EVENT_GROUP("registry", "register");
00104 }
00105 
00106 typedef std::vector<string> query_list;
00107 typedef std::vector<string> service_type_list;
00108 
00109 typedef protocol::MessageRegistry<QueryAll> MessageQueryAll;
00110 
00111 #define WSPR_MESSAGE_REGISTRATION(message_id)                    \
00112   WISP_PROTOCOL_MESSAGE(message_id, protocol::MessageRegistry,   \
00113                        ((string, service_name))                 \
00114                        ((service_type_list, service_types)))
00115 
00116 WSPR_MESSAGE_REGISTRATION(Register);
00117 WSPR_MESSAGE_REGISTRATION(Reregister);
00118 WSPR_MESSAGE_REGISTRATION(Unregister);
00119 
00120 #undef WSPR_MESSAGE_REGISTRATION
00121 
00122 WISP_PROTOCOL_MESSAGE(QueryResult, protocol::MessageRegistry,
00123                      ((service_map, result)));
00124 WISP_PROTOCOL_MESSAGE(QueryByName, protocol::MessageRegistry,
00125                      ((query_list, keys)));
00126 WISP_PROTOCOL_MESSAGE(QueryByType, protocol::MessageRegistry,
00127                      ((query_list, keys)));
00128 
00129 typedef BinaryPackingTraits RegistryPackingTraits;
00130 typedef NS_SSRC_WISP_PROTOCOL::Caller<RegistryPackingTraits> RegistryCaller;
00131 
00132 WISP_ONE_WAY_CALL(RegistryCaller, Register);
00133 WISP_ONE_WAY_CALL(RegistryCaller, Reregister);
00134 WISP_ONE_WAY_CALL(RegistryCaller, Unregister);
00135 WISP_ONE_WAY_CALL(RegistryCaller, QueryResult);
00136 
00137 WISP_TWO_WAY_CALL(RegistryCaller, QueryByName, QueryResult);
00138 WISP_TWO_WAY_CALL(RegistryCaller, QueryByType, QueryResult);
00139 WISP_TWO_WAY_CALL(RegistryCaller, QueryAll, QueryResult);
00140 
00141 /* Utility functions. */
00142 
00151 template<typename caller_type>
00152 string get_service_by_type(caller_type & caller, string service_type) {
00153   CallQueryByType::parameter_type query;
00154   CallQueryByType::return_type result;
00155 
00156   query.keys.push_back(service_type);
00157 
00158   caller(CallQueryByType(), service_group_registry(), query, &result);
00159 
00160   if(result.result.size() > 0)
00161     return result.result.begin()->service_name;
00162 
00163   return string();
00164 }
00165 
00166 __END_NS_SSRC_WSPR_REGISTRY
00167 
00168 #endif

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