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

DCDT_Member.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 dEVICE cOMMUNITIES dEVELOPMENT tOOLKIT 
00003 
00004 TCPSocket.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 
00011 This library is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU Lesser General Public
00013 License as published by the Free Software Foundation; either
00014 version 2 of the License, or (at your option) any later version.
00015 
00016 This library is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 Lesser General Public License for more details.
00020 
00021 You should have received a copy of the GNU Lesser General Public
00022 License along with this library; if not, write to the Free Software
00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00024 
00025 ****************************************************************************/
00026 
00027 #ifndef DCDT_MEMBER_H
00028 #define DCDT_MEMBER_H
00029 
00030 #include <string.h>
00031 #include <pthread.h>
00032 #include <DCDT_Time.h>
00033 #include <DCDT_Defs.h>
00034 #include <iostream>
00035 #include <DCDT_Msg.h>
00036 #include <DCDT_PostOffice.h>
00037 #include <DCDT_ctrace.h>
00038 
00039 class DCDT_Agora;
00040 class DCDT_Finder;
00041 
00044 class DCDT_Member {
00045   friend class DCDT_Agora;
00046 
00047  public:
00048   DCDT_Member( DCDT_Agora *, DCDT_TIME period); // Periodical members prototype
00049   DCDT_Member( DCDT_Agora *);  // Cyclic members prototype
00050   
00051   virtual ~DCDT_Member();
00052   
00053   inline DCDT_Member*  GetMember(int myID);
00054   
00055   inline void *LifeCycle();
00056   
00057   inline void SetID( int ID);
00058   inline int ReadID();
00059   inline bool CheckProfile(unsigned int mask);
00060 
00061   inline void Shutdown( int num_sec);
00062   
00063   unsigned int Profile;
00064 
00065   // Virtual functions that has to be defined for each new member
00066   virtual void Init(){};
00067   virtual void Close(){};
00068   virtual void DoYourJob(int par = 0) = 0;  //pure virtual
00069 
00071   int Activate( const char *thdName=NULL );
00072 
00073   // Inner Methods
00074   inline void Run();
00075   inline void Suspend();
00076   inline void WakeUp();
00077   inline void Exit() { Status = TERMINATING; };
00078 
00079   inline void SubscribeMsgType(int type, DCDT_RequestType ReqType=DCDT_ALL_MSG);
00080   inline void UnSubscribeMsgType(int type, DCDT_RequestType ReqType=DCDT_ALL_MSG);
00081   inline void UnSubscribeAll();
00082 
00083   inline const DCDT_Msg* ReceiveMsg( bool wait );
00084   inline const DCDT_Msg* ReceiveMsgType( int type, bool wait );
00085   inline const DCDT_Msg* ReceiveLastMsg( int type, bool wait );
00086   inline DCDT_Msg* CreateMsg(int type, int delivery_warranty); // method to set the warranty
00087   
00088   // TODO: remove the following outdated method, now we have PostOffice
00089   inline DCDT_Msg* CreateMsg(int type);
00090 
00091   inline void ShareMsg(DCDT_Msg *);
00092   inline DCDT_Msg * DupMsg(DCDT_Msg *);
00093   inline void Wait( DCDT_TIME sleep_time);
00094   inline void Wait( struct timeval deadline );
00095 
00096  protected:
00097 
00098   DCDT_PostOffice* myPostOffice;
00099   char myIPAddr[INET_ADDRSTRLEN];
00100 
00102   DCDT_Agora* myAgora;
00103 
00105   DCDT_Finder* myFinder;
00106 
00108   int myID;
00109 
00111   int myAgoraID;
00112 
00113   bool EndOfSession;
00114   DCDT_TIME 
00116     Period, 
00118     ExecTime,
00120     CycleTime,
00122     StartTime;
00123 
00124   struct timeval deadline;
00125 
00126   int Status;
00127 
00128 #ifdef SEQUENCER_VERSION
00129   DCDT_Sequencer * mySequencer;
00130 #endif
00131  
00133   int SubsMsgMask[NUMINT_MSGTYPE];
00134   
00135   // Thread management code
00136   pthread_t      myThread;
00137   pthread_attr_t myThreadAttr;
00138 };
00139 
00140 inline void DCDT_Member::SetID( int ID){
00141   myID = ID;
00142 };
00143 
00144 inline int DCDT_Member::ReadID(){
00145   return myID;
00146 };
00147 
00148 inline bool DCDT_Member::CheckProfile(unsigned int mask){
00149   return ((Profile & mask) == mask);
00150 };
00151 
00152 inline void DCDT_Member::SubscribeMsgType(int type, DCDT_RequestType ReqType) {
00153   myPostOffice->SubscribeMsgTypeID( myID, type, ReqType );
00154 }
00155 
00156 inline void DCDT_Member::UnSubscribeMsgType(int type, DCDT_RequestType ReqType) {
00157   myPostOffice->UnSubscribeMsgTypeID( myID, type, ReqType);
00158 }
00159 
00160 inline void DCDT_Member::UnSubscribeAll() {
00161   myPostOffice->UnSubscribeAll( myID );
00162 }
00163 
00164 inline const DCDT_Msg * DCDT_Member::ReceiveMsg( bool wait ) {
00165   return( myPostOffice->ReadNextMsg( myID, wait ));
00166 }
00167 
00168 inline const DCDT_Msg * DCDT_Member::ReceiveMsgType( int type, bool wait ) {
00169   return( myPostOffice->ReadNextMsg( myID, type, wait ) );
00170 }
00171 
00172 inline const DCDT_Msg * DCDT_Member::ReceiveLastMsg( int type, bool wait ) {
00173   return( myPostOffice->ReadLastMsg( myID, type, wait ) );
00174 }
00175 
00176 inline DCDT_Msg* DCDT_Member::CreateMsg(int type, int delivery_warranty){
00177 
00178   TRC_PRINT( DCDT_TRC_MEMBER, TRC1, ("CreateMsg()"));
00179 
00180   DCDT_Msg * newMsg = new DCDT_Msg(type) ;
00181   newMsg->SetMemberID( myID );
00182   newMsg->SetAgoraID( myAgoraID );
00183   newMsg->SetDeliveryWarranty( delivery_warranty ); 
00184 
00185   return newMsg;
00186 };
00187 
00188 inline DCDT_Msg* DCDT_Member::CreateMsg(int type ){
00189 
00190   TRC_PRINT( DCDT_TRC_MEMBER, TRC1, ("CreateMsg()"));
00191 
00192   DCDT_Msg * newMsg = new DCDT_Msg(type) ;
00193   newMsg->SetMemberID( myID );
00194   newMsg->SetAgoraID( myAgoraID );
00195   newMsg->SetDeliveryWarranty( 0 ); 
00196 
00197   return newMsg;
00198 };
00199 
00200 inline void DCDT_Member::ShareMsg(DCDT_Msg * Msg) {
00201 
00202   TRC_PRINT( DCDT_TRC_MEMBER, TRC1, ("ShareMsg()"));
00203 
00204   // If we are an MPM_LINK and MPM_BRIDGE all the messages that we share
00205   // are LOCAL messages; if we are only an MPM_LINK the messages are REMOTE
00206   if ( myAgora->CheckMemberProfile( myID, MPM_LINK ) ) {
00207      if ( myAgora->CheckMemberProfile( myID, MPM_BRIDGE ) )
00208         myPostOffice->AddMsg( Msg, myID, DCDT_LOCAL_MSG );
00209      else
00210         myPostOffice->AddMsg( Msg, myID, DCDT_REMOTE_MSG );
00211   }
00212   else
00213      myPostOffice->AddMsg( Msg, myID, DCDT_LOCAL_MSG );
00214 
00215 };
00216 
00217 inline DCDT_Msg *DCDT_Member::DupMsg(DCDT_Msg * Msg) {
00218   int payloadlen;
00219 
00220   TRC_PRINT( DCDT_TRC_MEMBER, TRC1, ("DupMsg()"));
00221 
00222   DCDT_Msg * newMsg = new DCDT_Msg( Msg->ReadType(), Msg->ReadPriority());
00223 
00224   if( (payloadlen = Msg->ReadPayloadLen()) ) {
00225     void * newpayload = malloc( payloadlen );
00226     if ( newpayload != NULL ) {
00227       memmove(newpayload, Msg->GetPayload(), payloadlen);
00228       newMsg->SetPayload( newpayload, payloadlen);
00229     }
00230     else {
00231       delete newMsg;
00232       return ( NULL );
00233     }
00234   }
00235   return ( newMsg );
00236 };
00237 
00238 inline void DCDT_Member::Shutdown(int Sec_Delay) {
00239   
00240   // terminate the session within Sec_Delay seconds
00241   if( Sec_Delay)
00242     Delay ( Sec_Delay*1000000 );
00243   
00244   myAgora->SetStatus ( DCDT_TERMINATING );
00245 };
00246 
00247 #endif  //define

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