Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

DCDT_Msg.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 dEVICE cOMMUNITIES dEVELOPMENT tOOLKIT 
00003 
00004 DCDT_Msg.h
00005 
00006 COPYRIGHT (C) 2002  Paolo Meriggi (meriggi@ing.unibs.it)
00007                     Alessandro Mazzini (mazzini@airlab.elet.polimi.it)
00008                     Cristian Giussani (cgiussani@fastflow.it)
00009 
00010 This library is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU Lesser General Public
00012 License as published by the Free Software Foundation; either
00013 version 2 of the License, or (at your option) any later version.
00014 
00015 This library is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public
00021 License along with this library; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00023 
00024 ****************************************************************************/
00025 
00026 #ifndef DCDT_MSG_H
00027 #define DCDT_MSG_H
00028 
00029 #include <DCDT_Defs.h>
00030 #include <DCDT_Time.h>
00031 
00032 // HandShake messages types
00034 #define HS_NOTIFY    1
00035 
00036 #define HS_ANSWER    2
00037 
00038 // msg types
00039 #define MT_SUBSUPD        1001
00040 #define MT_KEEPALIVE      1002
00041 #define MT_CONNECT        1003
00042 
00043 // msg delivery warranties
00044 #define NO_WARRANTY    0
00045 #define MID_WARRANTY   5
00046 #define MAX_WARRANTY  10
00047 
00048 #define UDP_WARRANTY          0
00049 #define UDP_RETRY_WARRANTY    5
00050 #define TCP_WARRANTY         10
00051 
00053 typedef struct _HSMsgHeader {
00054   int type;
00055   int AgoraID;
00056   int channel;
00057   int payload_len;
00058 } HSMsgHeader;
00059 
00060 #define HSMSGHEADER_LEN sizeof(HSMsgHeader)
00061 #define MAX_HSPAYLOAD_LEN 16
00062 
00064 typedef struct _DCDT_MsgHeader {
00065 
00067   int type;
00068   
00070   int MemberID;
00071 
00073   int AgoraID;
00074 
00076   int Readers_Counter; 
00077   DCDT_Mutex mtx;
00078 
00079   DCDT_TIME creation_time;
00080   int priority;
00081   int payload_len;
00082   int delivery_warranty;
00083 } DCDT_MsgHeader;
00084 
00085 #define MSGHEADER_LEN sizeof(DCDT_MsgHeader)
00086 
00091 class DCDT_Msg {
00092 
00093  public:
00094   // Constructor with type and priority
00095   DCDT_Msg(int type, int priority) {
00096     header = new DCDT_MsgHeader ;
00097     memset ( header, 0, sizeof( DCDT_MsgHeader ));
00098     payload = NULL; //SetPayload( NULL, 0);
00099 
00100     SetType( type);
00101 
00102     header->priority = priority;
00103     header->creation_time = GetTime();
00104     header->payload_len = 0;
00105     header->delivery_warranty = 0;
00106     header->Readers_Counter = 0;
00107    };
00108 
00109   // Constructor with type
00110   DCDT_Msg(int type ) {
00111     header = new DCDT_MsgHeader ;
00112     memset ( header, 0, sizeof( DCDT_MsgHeader ) );
00113     SetType( type );
00114     payload = NULL; //SetPayload( NULL, 0);
00115     header->creation_time = GetTime();
00116     header->priority = 0;
00117     header->payload_len = 0;
00118     header->delivery_warranty = 0;
00119     header->Readers_Counter = 0;
00120   }
00121 
00122   // Constructor from header
00123   DCDT_Msg(DCDT_MsgHeader* h) {
00124     header = h;
00125     payload = NULL; //SetPayload( NULL, 0);
00126     header->Readers_Counter = 0;
00127  };
00128 
00129   ~DCDT_Msg() {
00130     if(payload) {
00131       free(payload);
00132     }
00133     delete header;
00134   };
00135 
00136   inline void SetType(int);
00137   inline int AllocPayload(size_t size, void *buffer );
00138   inline int SetPayload(void *p, int len);
00139   inline int ReadType() const;
00140   inline int ReadAgoraID() const;
00141   inline void SetAgoraID(int ID);
00142 
00143   inline int ReadMemberID() const;
00144   inline void SetMemberID(int ID);
00145 
00146   inline int ReadPriority() const;
00147   inline int ReadPayloadLen() const;
00148 
00149   inline void * GetPayload() const;
00150   inline DCDT_MsgHeader * GetHeader() const;
00151 
00152   inline DCDT_TIME ReadCreationTime() const;
00153 
00154   inline void SetDeliveryWarranty( int new_del_warran ); 
00155   inline int ReadDeliveryWarranty() const;
00156 
00157   // functions to manage Readers_Count 
00158   inline void Increment_Readers_Counter();
00159   inline void Decrement_Readers_Counter();
00160   inline int Get_Readers_Counter() const;
00161 
00162  private:
00163 
00164   DCDT_MsgHeader * header;
00165   void * payload;
00166 
00167 };
00168 
00169 inline void DCDT_Msg::SetType(int num_type) {
00170   header->type = num_type;
00171 };
00172 
00173 inline int DCDT_Msg::AllocPayload(size_t size, void *buffer ) {
00174    if ( payload ) {
00175       free( payload );
00176    }
00177 
00178    if ( payload = malloc( size ) ) {
00179       if ( buffer ) {
00180          memcpy ( payload, buffer, size );
00181          header->payload_len = size;
00182       }
00183 
00184       return SUCCESS;
00185    }
00186 
00187    return FAILURE;
00188 };
00189 
00190 inline int DCDT_Msg::SetPayload(void *p, int len) {
00191   header->payload_len = len;
00192   if( payload ) {
00193     free( payload );
00194   }
00195   payload = p;
00196   return SUCCESS;
00197 };
00198 
00199 inline int DCDT_Msg::ReadType() const {
00200   return ( header->type );
00201 };
00202 
00203 inline int DCDT_Msg::ReadAgoraID() const {
00204   return ( header->AgoraID );
00205 };
00206 
00207 inline void DCDT_Msg::SetAgoraID(int ID ){
00208   header->AgoraID = ID;
00209 };
00210 
00211 inline int DCDT_Msg::ReadMemberID() const {
00212   return( header->MemberID );
00213 };
00214 
00215 inline void DCDT_Msg::SetMemberID(int ID){
00216   header->MemberID = ID;
00217 };
00218 
00219 inline int DCDT_Msg::ReadDeliveryWarranty() const {
00220   return ( header->delivery_warranty );
00221 };
00222 
00223 inline void DCDT_Msg::SetDeliveryWarranty(int new_del_warran){
00224   header->delivery_warranty = new_del_warran;
00225 };
00226 
00227 inline int DCDT_Msg::ReadPriority() const {
00228   return ( header->priority );
00229 };
00230 
00231 inline int DCDT_Msg::ReadPayloadLen() const {
00232   return (header->payload_len);
00233 }
00234 
00235 inline void * DCDT_Msg::GetPayload() const {
00236   return(payload);
00237 };
00238 
00239 inline DCDT_MsgHeader * DCDT_Msg::GetHeader() const {
00240   return( header );
00241 };
00242 
00243 inline DCDT_TIME DCDT_Msg::ReadCreationTime() const {
00244   return( header->creation_time);
00245 };
00246 
00247 inline void DCDT_Msg::Increment_Readers_Counter()
00248 {
00249   header->mtx.lock();
00250   header->Readers_Counter ++;
00251   header->mtx.unlock();
00252 }
00253 
00254 inline void DCDT_Msg::Decrement_Readers_Counter()
00255 {
00256   header->mtx.lock();
00257   header->Readers_Counter --;
00258   header->mtx.unlock();
00259 }
00260 
00261 inline int DCDT_Msg::Get_Readers_Counter() const
00262 {
00263   int tmp;
00264   header->mtx.lock();
00265   tmp = header->Readers_Counter;
00266   header->mtx.unlock();
00267   return tmp;
00268 }
00269 
00270 #endif //define

Generated on Sun Jun 19 10:35:50 2005 for dcdt by  doxygen 1.3.9.1