Savarese Software Research Corporation
BaseMessage.h
Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright 2006 Savarese Software Research Corporation
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.savarese.com/software/ApacheLicense-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00023 #ifndef __SSRC_SPREAD_BASE_MESSAGE_H
00024 #define __SSRC_SPREAD_BASE_MESSAGE_H
00025 
00026 #include <string>
00027 
00028 #include <ssrc/libssrcspread-packages.h>
00029 
00030 __BEGIN_NS_SPREAD_INCLUDE
00031 # include <sp.h>
00032 
00033   // Convert define to typedef.
00034 # if defined(int16)
00035   typedef int16 foo_int16;
00036 # undef int16
00037   typedef foo_int16 int16;
00038 # endif
00039 __END_NS_SPREAD_INCLUDE
00040 
00041 #include <ssrc/spread/Error.h>
00042 #include <ssrc/spread/GroupList.h>
00043 
00044 __BEGIN_NS_SSRC_SPREAD
00045 
00046 #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
00047 
00048 // Forward declaration
00049 class MembershipInfo;
00050 
00051 #endif
00052 
00058 class BaseMessage {
00059 public:
00061   typedef Spread::int16 message_type;
00062 
00064   typedef Spread::service service_type;
00065 
00079   enum Service {
00080     Unreliable  = UNRELIABLE_MESS,
00081     Reliable    = RELIABLE_MESS,
00082     FIFO        = FIFO_MESS,
00083     Causal      = CAUSAL_MESS,
00084     Agreed      = AGREED_MESS,
00085     Safe        = SAFE_MESS,
00086     SelfDiscard = SELF_DISCARD,
00087     DropReceive = DROP_RECV,
00088 #define SERVICE_TYPE_DISCARD(s) s ## SelfDiscard = s | SelfDiscard
00089 
00090     SERVICE_TYPE_DISCARD(Unreliable),
00091     SERVICE_TYPE_DISCARD(Reliable),
00092     SERVICE_TYPE_DISCARD(FIFO),
00093     SERVICE_TYPE_DISCARD(Causal),
00094     SERVICE_TYPE_DISCARD(Agreed),
00095     SERVICE_TYPE_DISCARD(Safe)
00096 
00097 #undef SERVICE_TYPE_DISCARD
00098   };
00099 
00100 private:
00101 
00102 #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
00103 
00104   void get_vs_set_members(const Spread::vs_set_info *vs_set,
00105                           GroupList *members, unsigned int offset = 0) const;
00106 
00107 #endif
00108 
00109 protected:
00110 
00111   message_type _type;
00112   service_type _service_type;
00113   bool _endian_mismatch;
00114   string _sender;
00115 
00121   BaseMessage() :
00122     _type(0), _service_type(Safe), _endian_mismatch(false), _sender("")
00123   { }
00124 
00125 #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
00126 
00127   virtual int sp_get_membership_info(Spread::membership_info *info) const = 0;
00128 
00129   virtual int sp_get_vs_set_members(const Spread::vs_set_info *vs_set,
00130                                     Spread::group_type member_names[],
00131                                     unsigned int member_names_count) const = 0;
00132 
00133   virtual int sp_get_vs_sets_info(Spread::vs_set_info *vs_sets,
00134                                   unsigned int num_vs_sets,
00135                                   unsigned int *index) const = 0;
00136 
00137 #endif
00138 
00139 public:
00140 
00142   virtual ~BaseMessage() { }
00143 
00149   virtual unsigned int size() const = 0;
00150 
00152   virtual void clear() = 0;
00153 
00154 #ifdef LIBSSRCSPREAD_ENABLE_MEMBERSHIP_INFO
00155 
00156   void get_membership_info(MembershipInfo & info) const SSRC_DECL_THROW(Error);
00157 
00158 #endif
00159 
00165   void set_service(const service_type service) {
00166     _service_type = service;
00167   }
00168 
00175   service_type service() const {
00176     return _service_type;
00177   }
00178 
00183   void set_type(const message_type type) {
00184     _type = type;
00185   }
00186 
00191   message_type type() const {
00192     return _type;
00193   }
00194 
00199   void set_sender(const string & sender) {
00200     _sender = sender;
00201   }
00202 
00207   const string & sender() const {
00208     return _sender;
00209   }
00210 
00215   void set_endian_mismatch(const bool mismatch = true) {
00216     _endian_mismatch = mismatch;
00217   }
00218 
00225   bool endian_mismatch() const {
00226     return _endian_mismatch;
00227   }
00228 
00230   void set_agreed() {
00231     set_service(Agreed);
00232   }
00233 
00238   bool is_agreed() const {
00239     return Is_agreed_mess(service());
00240   }
00241 
00243   void set_causal() {
00244     set_service(Causal);
00245   }
00246 
00251   bool is_causal() const {
00252     return Is_causal_mess(service());
00253   }
00254 
00256   void set_fifo() {
00257     set_service(FIFO);
00258   }
00259 
00264   bool is_fifo() const {
00265     return Is_fifo_mess(service());
00266   }
00267 
00269   void set_reliable() {
00270     set_service(Reliable);
00271   }
00272 
00277   bool is_reliable() const {
00278     return Is_reliable_mess(service());
00279   }
00280 
00282   void set_unreliable() {
00283     set_service(Unreliable);
00284   }
00285 
00290   bool is_unreliable() const {
00291     return Is_unreliable_mess(service());
00292   }
00293 
00295   void set_safe() {
00296     set_service(Safe);
00297   }
00298 
00303   bool is_safe() const {
00304     return Is_safe_mess(service());
00305   }
00306 
00314   void set_self_discard(const bool discard = true) {
00315     _service_type |= SelfDiscard;
00316     if(!discard)
00317       _service_type ^= SelfDiscard;
00318   }
00319 
00326   bool is_self_discard() const {
00327     return Is_self_discard(service());
00328   }
00329 
00335   bool is_regular() const {
00336     return Is_regular_mess(service());
00337   }
00338 
00343   bool is_membership() const {
00344     return Is_membership_mess(service());
00345   }
00346 };
00347 
00348 __END_NS_SSRC_SPREAD
00349 
00350 #endif

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