Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/group_session - protocol.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 51 51 100.0 %
Date: 2012-04-09 Functions: 14 14 100.0 %
Branches: 19 30 63.3 %

           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 GroupSession messaging protocol.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_GROUP_SESSION_PROTOCOL_H
      23                 :            : #define __SSRC_WSPR_GROUP_SESSION_PROTOCOL_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers/protocol.h>
      26                 :            : #include <ssrc/wispers/utility/Properties.h>
      27                 :            : #include <ssrc/wispers/utility/ToString.h>
      28                 :            : #include <ssrc/wispers/group_session/types.h>
      29                 :            : 
      30                 :            : // Includes serialize function for std::pair.
      31                 :            : #include <boost/serialization/utility.hpp>
      32                 :            : 
      33                 :            : __BEGIN_NS_SSRC_WSPR_PROTOCOL
      34                 :            : 
      35                 :            : WSPR_DEFINE_PROTOCOL(GroupSession,group_session);
      36                 :            : 
      37                 :            : __END_NS_SSRC_WSPR_PROTOCOL
      38                 :            : 
      39                 :            : __BEGIN_NS_SSRC_WSPR_GROUP_SESSION
      40                 :            : 
      41                 :            : using NS_SSRC_WSPR::ByteBuffer;
      42                 :            : using NS_SSRC_WSPR_UTILITY::Properties;
      43                 :            : using NS_SSRC_WSPR_UTILITY::properties_ptr;
      44                 :            : using NS_SSRC_WSPR_UTILITY::ToString;
      45                 :            : using NS_SSRC_WISP_PROTOCOL::wisp_message_protocol;
      46                 :            : 
      47                 :            : typedef std::vector<gsid_type> gsid_container;
      48                 :            : typedef std::vector<uid_type> uid_container;
      49                 :            : typedef std::vector<GroupSession> group_session_container;
      50                 :            : typedef std::vector<Reservation> reservation_container;
      51                 :            : typedef std::vector<Member> member_container;
      52                 :            : 
      53                 :            : struct GroupSessionProtocol :
      54                 :            :   public protocol::ServiceProtocol<protocol::GroupSession>
      55                 :            : {
      56                 :            :   typedef protocol::ServiceProtocol<protocol::GroupSession> super;
      57                 :            : 
      58                 :            :   WISP_IMPORT(super, caller_type);
      59                 :            : 
      60                 :            :   /** The maximum value a gsid can have. */
      61                 :            :   static const gsid_type GSIDMax = boost::integer_traits<gsid_type>::const_max;
      62                 :            : 
      63                 :            :   /** The minimum value a gsid can have. */
      64                 :            :   static const gsid_type GSIDMin = boost::integer_traits<gsid_type>::const_min;
      65                 :            : 
      66                 :            :   /**
      67                 :            :    * The size of a gsid protocol partition.  For example, the first protocol
      68                 :            :    * partition is assigned the range:
      69                 :            :    * @verbatim (GSIDMin, GSIDMin + GSIDSpace] @endverbatim
      70                 :            :    */
      71                 :            :   // Calculation is independent of whether gsid_type is signed or unsigned.
      72                 :            :   // Protocol numbers start at 0, which is why we use
      73                 :            :   // boost::integer_traits<wisp_message_protocol>::const_max without respect
      74                 :            :   // to the distance to const_min.
      75                 :            :   static const gsid_type GSIDSpace =
      76                 :            :       (((boost::integer_traits<gsid_type>::const_max >> 2) + 1)
      77                 :            :        - (boost::integer_traits<gsid_type>::const_min >> 2))  /
      78                 :            :       ((boost::integer_traits<wisp_message_protocol>::const_max >> 2) + 1);
      79                 :            : 
      80                 :            :   static string event_group_expire() {
      81                 :            :     return WSPR_EVENT_GROUP("group_session", "expire");
      82                 :            :   }
      83                 :            : 
      84                 :            :   enum {
      85                 :            :     RelayEvent,
      86                 :            :     RelayEventSelfDiscard,
      87                 :            :     RelayEvents,
      88                 :            :     RelayEventsSelfDiscard,
      89                 :            :     CreateGroupSession,
      90                 :            :     EndGroupSession,
      91                 :            :     ExpireGroupSession,
      92                 :            :     FindGroupSessions,
      93                 :            :     FindGroupSessionsResult,
      94                 :            :     FindGroupSessionsForMember,
      95                 :            :     ReturnGroupSessionsForMember,
      96                 :            :     FindGroupSessionsByType,
      97                 :            :     ReturnGroupSessionsByType,
      98                 :            :     CreateReservation,
      99                 :            :     CancelReservation,
     100                 :            :     StartGroupSession,
     101                 :            :     FindMembers,
     102                 :            :     FindMembersResult,
     103                 :            :     AddMembers,
     104                 :            :     RemoveMembers,
     105                 :            :     AddMember,
     106                 :            :     AddMemberConfirm,
     107                 :            :     RemoveMember,
     108                 :            :     RemoveMemberConfirm
     109                 :            :   };
     110                 :            : 
     111                 :            :   enum { DefaultPayloadCapacity = NS_SSRC_SPREAD::Message::DefaultCapacity };
     112                 :            : 
     113                 :            :   enum MemberType { Participant, Observer };
     114                 :            : 
     115                 :            :   struct MessageCreateGroupSession :
     116                 :            :     public protocol::MessageGroupSession<CreateGroupSession>
     117                 :            :   {
     118                 :            :     string session_name;
     119                 :            :     string session_group;
     120                 :            :     string session_type;
     121                 :            :     sec_type lifetime;
     122                 :            :     uid_container participants;
     123                 :            :     unsigned int max_observers;
     124                 :            :     ByteBuffer payload;
     125                 :            : 
     126                 :            :     MessageCreateGroupSession() : payload(DefaultPayloadCapacity) { }
     127                 :            : 
     128                 :            :     MessageCreateGroupSession(const string & session_name,
     129                 :            :                               const string & session_group,
     130                 :            :                               const string & session_type,
     131                 :            :                               const sec_type lifetime,
     132                 :            :                               const uid_container & participants,
     133                 :            :                               const unsigned int max_observers) :
     134                 :            :       session_name(session_name),
     135                 :            :       session_group(session_group),
     136                 :            :       session_type(session_type),
     137                 :            :       lifetime(lifetime),
     138                 :            :       participants(participants),
     139                 :            :       max_observers(max_observers),
     140                 :            :       payload(DefaultPayloadCapacity)
     141                 :            :     { }
     142                 :            : 
     143                 :            :     template<class Archive>
     144                 :            :     void serialize(Archive & ar, const unsigned int) {
     145                 :            :       ar & session_name & session_group & session_type & lifetime
     146                 :            :          & participants & max_observers & payload;
     147                 :            :     }
     148                 :            :   };
     149                 :            : 
     150                 :            :   struct MessageCreateReservation :
     151                 :            :     public protocol::MessageGroupSession<CreateReservation>
     152                 :            :   {
     153                 :            :     string session_name;
     154                 :            :     string session_group;
     155                 :            :     string session_type;
     156                 :            :     sec_type gs_lifetime;
     157                 :            :     uid_type creator_uid;
     158                 :            :     sec_type reservation_lifetime;
     159                 :            :     unsigned int max_observers;
     160                 :            :     ByteBuffer payload;
     161                 :            : 
     162                 :            :     MessageCreateReservation() : payload(DefaultPayloadCapacity) { }
     163                 :            : 
     164                 :            :     MessageCreateReservation(const string & session_name,
     165                 :            :                              const string & session_group,
     166                 :            :                              const string & session_type,
     167                 :            :                              const sec_type gs_lifetime,
     168                 :            :                              const uid_type creator_uid,
     169                 :            :                              const sec_type reservation_lifetime,
     170                 :            :                              const unsigned int max_observers) :
     171                 :            :       session_name(session_name),
     172                 :            :       session_group(session_group),
     173                 :            :       session_type(session_type),
     174                 :            :       gs_lifetime(gs_lifetime),
     175                 :            :       creator_uid(creator_uid),
     176                 :            :       reservation_lifetime(reservation_lifetime),
     177                 :            :       max_observers(max_observers),
     178                 :            :       payload(DefaultPayloadCapacity)
     179                 :            :     { }
     180                 :            : 
     181                 :            :     template<class Archive>
     182                 :            :     void serialize(Archive & ar, const unsigned int) {
     183                 :            :       ar & session_name & session_group & session_type & gs_lifetime
     184                 :            :         & creator_uid & reservation_lifetime & max_observers & payload;
     185                 :            :     }
     186                 :            :   };
     187                 :            : 
     188                 :            :   WISP_PROTOCOL_MESSAGE(EndGroupSession, protocol::MessageGroupSession,
     189                 :            :                         ((gsid_type, gsid)));
     190                 :            : 
     191                 :            :   WISP_PROTOCOL_MESSAGE(CancelReservation, protocol::MessageGroupSession,
     192                 :            :                         ((gsid_type, gsid))((uid_type, requestor)));
     193                 :            : 
     194                 :            :   WISP_PROTOCOL_MESSAGE(StartGroupSession,
     195                 :            :                         protocol::MessageGroupSession,
     196                 :            :                         ((string, gs_name))
     197                 :            :                         ((GroupSession, session)));
     198                 :            : 
     199                 :            :   WISP_STRUCT(ExpirationNotification,
     200                 :            :               ((gsid_type, gsid))
     201                 :            :               ((uid_container, uids)));
     202                 :            :   typedef
     203                 :            :   std::vector<ExpirationNotification> expiration_notification_container;
     204                 :            :   /**
     205                 :            :    * Expires both active sessions and reservations.
     206                 :            :    */
     207                 :            :   WISP_PROTOCOL_MESSAGE(ExpireGroupSession, protocol::MessageGroupSession,
     208                 :            :                         ((expiration_notification_container, notifications)));
     209                 :            : 
     210                 :            :   WISP_PROTOCOL_MESSAGE_WITH_INIT(RelayEvent, protocol::MessageGroupSession,
     211                 :            :                                   ((gsid_type, gsid))
     212                 :            :                                   ((string, event_queue))
     213                 :            :                                   ((properties_ptr, event)),
     214                 :            :                                   ((gsid_type()))
     215                 :            :                                   ((string()))
     216                 :            :                                   ((properties_ptr(new Properties))));
     217                 :            : 
     218                 :            :   WISP_PROTOCOL_MESSAGE_WITH_INIT(RelayEventSelfDiscard,
     219                 :            :                                   protocol::MessageGroupSession,
     220                 :            :                                   ((uid_type, source))
     221                 :            :                                   ((gsid_type, gsid))
     222                 :            :                                   ((string, event_queue))
     223                 :            :                                   ((properties_ptr, event)),
     224                 :            :                                   ((uid_type()))
     225                 :            :                                   ((gsid_type()))
     226                 :            :                                   ((string()))
     227                 :            :                                   ((properties_ptr(new Properties))));
     228                 :            : 
     229                 :            :   WISP_PROTOCOL_MESSAGE_WITH_INIT(RelayEvents, protocol::MessageGroupSession,
     230                 :            :                                   ((gsid_type, gsid))
     231                 :            :                                   ((string, event_queue))
     232                 :            :                                   ((std::vector<Properties>, events)),
     233                 :            :                                   ((gsid_type()))
     234                 :            :                                   ((string()))
     235                 :            :                                   ((std::vector<Properties>())));
     236                 :            : 
     237                 :            :   WISP_PROTOCOL_MESSAGE_WITH_INIT(RelayEventsSelfDiscard,
     238                 :            :                                   protocol::MessageGroupSession,
     239                 :            :                                   ((uid_type, source))
     240                 :            :                                   ((gsid_type, gsid))
     241                 :            :                                   ((string, event_queue))
     242                 :            :                                   ((std::vector<Properties>, events)),
     243                 :            :                                   ((uid_type()))
     244                 :            :                                   ((gsid_type()))
     245                 :            :                                   ((string()))
     246                 :            :                                   ((std::vector<Properties>())));
     247                 :            : 
     248                 :            :   WISP_PROTOCOL_MESSAGE(FindGroupSessions, protocol::MessageGroupSession,
     249                 :            :                         ((uid_type, requestor))
     250                 :            :                         ((db_limit_type, limit))
     251                 :            :                         ((db_offset_type, offset)));
     252                 :            : 
     253                 :            :   WISP_PROTOCOL_MESSAGE(FindGroupSessionsResult, protocol::MessageGroupSession,
     254                 :            :                         ((uid_type, requestor))
     255                 :            :                         ((unsigned int, total_sessions))
     256                 :            :                         ((db_limit_type, limit))
     257                 :            :                         ((db_offset_type, offset))
     258                 :            :                         ((string, gs_name))
     259                 :            :                         ((group_session_container, sessions)));
     260                 :            : 
     261                 :            :   WISP_PROTOCOL_MESSAGE(FindGroupSessionsForMember,
     262                 :            :                         protocol::MessageGroupSession,
     263                 :            :                         ((uid_type, uid)));
     264                 :            : 
     265                 :            :   WISP_PROTOCOL_MESSAGE(ReturnGroupSessionsForMember,
     266                 :            :                         protocol::MessageGroupSession,
     267                 :            :                         ((uid_type, uid))
     268                 :            :                         ((string, gs_name))
     269                 :            :                         ((group_session_container, participating))
     270                 :            :                         ((group_session_container, observing))
     271                 :            :                         ((reservation_container, awaiting)));
     272                 :            : 
     273                 :            :   typedef std::vector<string> query_list;
     274                 :            :   WISP_PROTOCOL_MESSAGE(FindGroupSessionsByType, protocol::MessageGroupSession,
     275                 :            :                         ((query_list, keys)));
     276                 :            :   WISP_PROTOCOL_MESSAGE(ReturnGroupSessionsByType,
     277                 :            :                         protocol::MessageGroupSession,
     278                 :            :                         ((group_session_container, sessions)));
     279                 :            : 
     280                 :            :   WISP_PROTOCOL_MESSAGE(FindMembers,
     281                 :            :                         protocol::MessageGroupSession,
     282                 :            :                         ((uid_type, requestor))
     283                 :            :                         ((gsid_container, sessions)));
     284                 :            : 
     285                 :            :   WISP_PROTOCOL_MESSAGE(FindMembersResult,
     286                 :            :                         protocol::MessageGroupSession,
     287                 :            :                         ((uid_type, requestor))
     288                 :            :                         ((member_container, participants))
     289                 :            :                         ((member_container, observers)));
     290                 :            : 
     291                 :            :   WISP_PROTOCOL_MESSAGE(AddMembers,
     292                 :            :                         protocol::MessageGroupSession,
     293                 :            :                         ((member_container, participants))
     294                 :            :                         ((member_container, observers)));
     295                 :            : 
     296                 :            :   WISP_PROTOCOL_MESSAGE(RemoveMembers,
     297                 :            :                         protocol::MessageGroupSession,
     298                 :            :                         ((member_container, participants))
     299                 :            :                         ((member_container, observers)));
     300                 :            : 
     301                 :            :   WISP_PROTOCOL_MESSAGE(AddMember,
     302                 :            :                         protocol::MessageGroupSession,
     303                 :            :                         ((MemberType, member_type))
     304                 :            :                         ((Member, member)));
     305                 :            : 
     306                 :            :   enum AddMemberResultCode {
     307                 :            :     AddMemberInternalError,
     308                 :            :     AddMemberNonexistentGroupSession,
     309                 :            :     AddMemberDuplicateEntry,
     310                 :            :     AddMemberMaxObserversReached,
     311                 :            :     AddMemberSuccess
     312                 :            :   };
     313                 :            : 
     314                 :            :   WISP_PROTOCOL_MESSAGE(AddMemberConfirm,
     315                 :            :                         protocol::MessageGroupSession,
     316                 :            :                         ((AddMemberResultCode, result))
     317                 :            :                         ((MemberType, member_type))
     318                 :            :                         ((uid_type, uid))
     319                 :            :                         ((string, gs_name))
     320                 :            :                         ((GroupSession, session)));
     321                 :            : 
     322                 :            :   WISP_PROTOCOL_MESSAGE(RemoveMember,
     323                 :            :                         protocol::MessageGroupSession,
     324                 :            :                         ((MemberType, member_type))
     325                 :            :                         ((Member, member)));
     326                 :            : 
     327                 :            :   WISP_PROTOCOL_MESSAGE(RemoveMemberConfirm,
     328                 :            :                         protocol::MessageGroupSession,
     329                 :            :                         ((MemberType, member_type))
     330                 :            :                         ((Member, member))
     331                 :            :                         ((string, gs_name))
     332                 :            :                         ((GroupSession, session)));
     333                 :            : 
     334                 :            :   WISP_ONE_WAY_CALL(caller_type, CreateGroupSession);
     335                 :            :   WISP_ONE_WAY_CALL(caller_type, EndGroupSession);
     336                 :            :   WISP_ONE_WAY_CALL(caller_type, ExpireGroupSession);
     337                 :            :   WISP_ONE_WAY_CALL(caller_type, RelayEvent);
     338                 :            :   WISP_ONE_WAY_CALL(caller_type, RelayEventSelfDiscard);
     339                 :            :   WISP_ONE_WAY_CALL(caller_type, RelayEvents);
     340                 :            :   WISP_ONE_WAY_CALL(caller_type, RelayEventsSelfDiscard);
     341                 :            : 
     342                 :            :   WISP_ONE_WAY_CALL(caller_type, CreateReservation);
     343                 :            :   WISP_ONE_WAY_CALL(caller_type, CancelReservation);
     344                 :            :   WISP_ONE_WAY_CALL(caller_type, StartGroupSession);
     345                 :            : 
     346                 :            :   WISP_TWO_WAY_CALL(caller_type, FindGroupSessionsForMember,
     347                 :            :                     ReturnGroupSessionsForMember);
     348                 :            :   WISP_ONE_WAY_CALL(caller_type, ReturnGroupSessionsForMember);
     349                 :            : 
     350                 :            :   WISP_TWO_WAY_CALL(caller_type, FindGroupSessionsByType,
     351                 :            :                     ReturnGroupSessionsByType);
     352                 :            :   WISP_ONE_WAY_CALL(caller_type, ReturnGroupSessionsByType);
     353                 :            : 
     354                 :            :   // Conceptually these are two-way calls, but we don't invoke them that way.
     355                 :            :   WISP_ONE_WAY_CALL(caller_type, FindGroupSessions);
     356                 :            :   WISP_ONE_WAY_CALL(caller_type, FindGroupSessionsResult);
     357                 :            :   WISP_ONE_WAY_CALL(caller_type, FindMembers);
     358                 :            :   WISP_ONE_WAY_CALL(caller_type, FindMembersResult);
     359                 :            : 
     360                 :            : 
     361                 :            :   WISP_ONE_WAY_CALL(caller_type, AddMembers);
     362                 :            :   WISP_ONE_WAY_CALL(caller_type, RemoveMembers);
     363                 :            :   WISP_ONE_WAY_CALL(caller_type, AddMember);
     364                 :            :   WISP_ONE_WAY_CALL(caller_type, AddMemberConfirm);
     365                 :            :   WISP_ONE_WAY_CALL(caller_type, RemoveMember);
     366                 :            :   WISP_ONE_WAY_CALL(caller_type, RemoveMemberConfirm);
     367                 :            : };
     368                 :            : 
     369                 :            : /**
     370                 :            :  * Binds the public properties of a GroupSession instance to a Properties
     371                 :            :  * node for use with a WebServiceEvent.  You should use this function instead
     372                 :            :  * of visit(BindProperties(node)) to avoid leaking internal information
     373                 :            :  * such as the session group name.
     374                 :            :  *
     375                 :            :  * @param session The GroupSession to bind.
     376                 :            :  * @param node The Properties node that will store the session properties.
     377                 :            :  */
     378                 :            : template<typename session_type>
     379                 :            : inline void bind_group_session_properties(const session_type & session,
     380                 :            :                                           Properties & node)
     381                 :            : {
     382                 :            :   node.set(session.gsid, "gsid");
     383                 :            :   node.set(session.created, "created");
     384                 :            :   node.set(session.expires, "expires");
     385                 :            :   node.set(session.type, "type");
     386                 :            :   node.set(session.name, "name");
     387                 :            : }
     388                 :            : 
     389                 :            : /**
     390                 :            :  * Returns the minimum gsid value associated with the specified protocol.
     391                 :            :  *
     392                 :            :  * @param protocol A Wispers protocol number.
     393                 :            :  * @return The minimum gsid value associated with the specified protocol.
     394                 :            :  */
     395                 :         49 : inline gsid_type gsid_min(const wisp_message_protocol protocol) {
     396                 :            :   return
     397                 :            :     (GroupSessionProtocol::GSIDMin +
     398                 :         49 :      (static_cast<gsid_type>(protocol) * GroupSessionProtocol::GSIDSpace));
     399                 :            : }
     400                 :            : 
     401                 :            : /**
     402                 :            :  * Returns the maximum gsid value associated with the specified protocol.
     403                 :            :  *
     404                 :            :  * @param protocol A Wispers protocol number.
     405                 :            :  * @return The maximum gsid value associated with the specified protocol.
     406                 :            :  */
     407                 :         29 : inline gsid_type gsid_max(const wisp_message_protocol protocol) {
     408                 :            :   return
     409                 :            :     ((GroupSessionProtocol::GSIDMin +
     410                 :         29 :       (static_cast<gsid_type>(protocol) + 1) * GroupSessionProtocol::GSIDSpace) - 1);
     411                 :            : }
     412                 :            : 
     413                 :            : /**
     414                 :            :  * Returns the minimum gsid value associated with a partition of the
     415                 :            :  * specified protocol.
     416                 :            :  *
     417                 :            :  * @param protocol A Wispers protocol number.
     418                 :            :  * @param partition_id The partiion number (0 to num_partions - 1).
     419                 :            :  * @param num_partitions The number of partitions the protocol has
     420                 :            :  *        been divided into.
     421                 :            :  * @return The minimum gsid value associated with a partition of the
     422                 :            :  *         specified protocol.
     423                 :            :  */
     424                 :         18 : inline gsid_type gsid_min(const wisp_message_protocol protocol,
     425                 :            :                           const unsigned int partition_id,
     426                 :            :                           const unsigned int num_partitions)
     427                 :            : {
     428                 :         18 :   const gsid_type min_gsid = gsid_min(protocol);
     429                 :         18 :   const gsid_type space = GroupSessionProtocol::GSIDSpace / num_partitions;
     430                 :            : 
     431                 :         18 :   return (min_gsid + space * partition_id);
     432                 :            : }
     433                 :            : 
     434                 :            : /**
     435                 :            :  * Returns the maximum gsid value associated with a partition of the
     436                 :            :  * specified protocol.
     437                 :            :  *
     438                 :            :  * @param protocol A Wispers protocol number.
     439                 :            :  * @param partition_id The partiion number (0 to num_partions - 1).
     440                 :            :  * @param num_partitions The number of partitions the protocol has
     441                 :            :  *        been divided into.
     442                 :            :  * @return The maximum gsid value associated with a partition of the
     443                 :            :  *         specified protocol.
     444                 :            :  */
     445                 :         16 : inline gsid_type gsid_max(const wisp_message_protocol protocol,
     446                 :            :                           const unsigned int partition_id,
     447                 :            :                           const unsigned int num_partitions)
     448                 :            : {
     449                 :            :   // Spill over space rounding error to last partition.
     450         [ +  + ]:         16 :   if(partition_id >= (num_partitions - 1))
     451                 :         14 :     return gsid_max(protocol);
     452                 :            : 
     453                 :          2 :   return gsid_min(protocol, partition_id + 1, num_partitions) - 1;
     454                 :            : }
     455                 :            : 
     456                 :            : /**
     457                 :            :  * Returns the protocol number that a specified gsid belongs to.
     458                 :            :  *
     459                 :            :  * @param gsid_type The gsid to convert.
     460                 :            :  * @return The protocol number that a specified gsid belongs to.
     461                 :            :  */
     462                 :         31 : inline wisp_message_protocol gsid_to_protocol(const gsid_type gsid) {
     463                 :            :   // Can't use basic algebra because of overflow.
     464                 :         31 :   gsid_type result(gsid);
     465                 :            : 
     466         [ +  + ]:         31 :   if(gsid < 0)
     467                 :         24 :     result-=GroupSessionProtocol::GSIDMin;
     468                 :            : 
     469                 :         31 :   result /= GroupSessionProtocol::GSIDSpace;
     470                 :            : 
     471         [ +  + ]:         31 :   if(gsid >= 0) {
     472                 :            :     const gsid_type midpoint = 
     473                 :          7 :       (boost::integer_traits<wisp_message_protocol>::const_min / -2);
     474                 :          7 :     result+=midpoint;
     475                 :            :   }
     476                 :            : 
     477                 :         31 :   return static_cast<wisp_message_protocol>(result);
     478                 :            : }
     479                 :            : 
     480                 :            : /**
     481                 :            :  * Returns the protocol partition number associated with a given gsid
     482                 :            :  * for a protocol with a specified number of partitions.
     483                 :            :  *
     484                 :            :  * @param gsid The gsid to convert.
     485                 :            :  * @param num_partitions The number of partitions.
     486                 :            :  * @return The protocol partition number associated with a given gsid.
     487                 :            :  */
     488                 :            : inline
     489                 :         12 : unsigned int gsid_to_protocol_partition(gsid_type gsid,
     490                 :            :                                         const unsigned int num_partitions)
     491                 :            : {
     492                 :         12 :   const wisp_message_protocol protocol = gsid_to_protocol(gsid);
     493                 :         12 :   const gsid_type space = GroupSessionProtocol::GSIDSpace / num_partitions;
     494                 :            : 
     495                 :         12 :   gsid-=gsid_min(protocol);
     496                 :         12 :   gsid /= space;
     497                 :            : 
     498         [ +  + ]:         12 :   if(gsid >= num_partitions)
     499                 :          1 :     gsid = num_partitions - 1;
     500                 :            : 
     501                 :         12 :   return static_cast<unsigned int>(gsid);
     502                 :            : }
     503                 :            : 
     504                 :            : /**
     505                 :            :  * GSToGroup is a utility class for deriving the group name for a
     506                 :            :  * group session service based on the provided gsid or protocol and/or
     507                 :            :  * partition id.  It is more efficient than using the free functions
     508                 :            :  * because it hangs on to a ToString instance and a std::string instance
     509                 :            :  * instead of creating new ones on each call.
     510                 :            :  */
     511   [ +  -  +  - ]:          5 : class GSToGroup {
     512                 :            :   const string _gs_protocol_prefix;
     513                 :            :   string _gs_group;
     514                 :            :   ToString _string_cast;
     515                 :            : 
     516                 :            : public:
     517                 :            : 
     518                 :          5 :   GSToGroup() :
     519                 :            :     _gs_protocol_prefix("wspr.gs.protocol."),
     520                 :            :     _gs_group(_gs_protocol_prefix),
     521   [ +  -  +  -  :          5 :     _string_cast()
                   +  - ]
     522                 :          5 :   { }
     523                 :            : 
     524                 :          6 :   const string & gs_protocol_group(const wisp_message_protocol protocol) {
     525                 :          6 :     _gs_group.erase(_gs_protocol_prefix.size());
     526                 :          6 :     return _gs_group.append(_string_cast(protocol));
     527                 :            :   }
     528                 :            : 
     529                 :            :   const string &
     530                 :          2 :   gs_protocol_partition_group(const wisp_message_protocol protocol,
     531                 :            :                               const unsigned int partition_id)
     532                 :            :   {
     533                 :          2 :     _gs_group.erase(_gs_protocol_prefix.size());
     534                 :          2 :     _gs_group.append(_string_cast(protocol)).append(".");
     535                 :            :     // Can't string together all of the appends because of undefined
     536                 :            :     // execution order of _string_cast, which returns a reference and
     537                 :            :     // can cause the protocol value to be used in each case.
     538                 :          2 :     return _gs_group.append(_string_cast(partition_id));
     539                 :            :   }
     540                 :            : 
     541                 :          4 :   const string & gsid_protocol_group(const gsid_type gsid) {
     542                 :          4 :     return gs_protocol_group(gsid_to_protocol(gsid));
     543                 :            :   }
     544                 :            : };
     545                 :            : 
     546                 :            : /**
     547                 :            :  * Returns the group name reserved for group session services for a
     548                 :            :  * protocol.  The format for the group is wspr.gs.protocol.N where N is
     549                 :            :  * the protocol number.
     550                 :            :  *
     551                 :            :  * @param protocol The protocol number.
     552                 :            :  * @return The group name reserved for group session services for the
     553                 :            :  *         specified protocol.
     554                 :            :  */
     555                 :          1 : inline string gs_protocol_group(const wisp_message_protocol protocol) {
     556                 :          2 :   GSToGroup gsid_to_group;
     557   [ +  -  +  - ]:          2 :   return gsid_to_group.gs_protocol_group(protocol);
     558                 :            : }
     559                 :            : 
     560                 :            : /**
     561                 :            :  * Returns the group name reserved for the single group session
     562                 :            :  * service responsible for a partition of a protocol.  The format for
     563                 :            :  * the group is wspr.gs.protocol.N.M where N is the protocol number and
     564                 :            :  * M is the partition number..
     565                 :            :  *
     566                 :            :  * @param protocol The protocol number.
     567                 :            :  * @param partition_id The parition number.
     568                 :            :  * @return The group name reserved for the single group session service
     569                 :            :  *         responsible for the specified partition of a protocol.
     570                 :            :  */
     571                 :            : inline
     572                 :          1 : string gs_protocol_partition_group(const wisp_message_protocol protocol,
     573                 :            :                                    const unsigned int partition_id)
     574                 :            : {
     575                 :          2 :   GSToGroup gsid_to_group;
     576   [ +  -  +  - ]:          2 :   return gsid_to_group.gs_protocol_partition_group(protocol, partition_id);
     577                 :            : }
     578                 :            : 
     579                 :            : /**
     580                 :            :  * Returns the protocol group name reserved for the group sessions
     581                 :            :  * services for the protocol associated with the specified gsid.
     582                 :            :  *
     583                 :            :  * @param gsid The group session id.
     584                 :            :  * @return The protocol group name reserved for the group sessions
     585                 :            :  *         services for the protocol associated with the specified gsid.
     586                 :            :  */
     587                 :          2 : inline string gsid_protocol_group(const gsid_type gsid) {
     588                 :          4 :   GSToGroup gsid_to_group;
     589   [ +  -  +  - ]:          4 :   return gsid_to_group.gsid_protocol_group(gsid);
     590                 :            : }
     591                 :            : 
     592                 :            : __END_NS_SSRC_WSPR_GROUP_SESSION
     593                 :            : 
     594                 :            : #endif