00001 /**************************************************************************** 00002 dEVICE cOMMUNITIES dEVELOPMENT tOOLKIT 00003 00004 DCDT_Agora.h 00005 00006 COPYRIGHT (C) 2002 Paolo Meriggi (meriggi@ing.unibs.it) 00007 Alessandro Mazzini (mazzini@airlab.elet.polimi.it) 00008 (C) 2003 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 AGORA_H 00027 #define AGORA_H 00028 00029 #include <sys/utsname.h> 00030 #include <arpa/inet.h> 00031 #include <string.h> 00032 #include <sys/types.h> 00033 #include <sys/stat.h> 00034 #include <fcntl.h> 00035 #include <DCDT_Defs.h> 00036 00037 #include <DCDT_Msg.h> 00038 00039 // TODO effort to check if possible to use signalling with C++ 00040 #include <signal.h> 00041 00042 class DCDT_Member; 00043 class DCDT_PostOffice; 00044 class DCDT_Finder; 00045 class DCDT_MsgManager; 00046 class DCDT_InnerLinkManager; 00047 00052 typedef struct _DCDT_MemberTableElem { 00053 00055 bool system; 00056 00058 bool empty; 00059 00061 bool active; 00062 00064 DCDT_Mutex mtx; 00065 00067 bool FreeSlot; 00068 00069 DCDT_Member * MemberPtr; 00070 unsigned int Profile; 00071 int status; 00072 int num_missed_deadlines; // Not actually used 00073 } DCDT_MemberTableElem; 00074 00075 typedef struct _FinderData { 00077 char IfStrAddr[INET_ADDRSTRLEN]; 00079 int IfNumAddr; 00081 char McStrAddr[INET_ADDRSTRLEN]; 00083 int McNumAddr; 00085 short McPort; 00087 FILE *fp; 00088 } FinderData; 00089 00090 // in order to use signal handler 00091 typedef void (*sighandler_t) (int); 00092 00093 00103 class DCDT_Agora { 00104 00105 public: 00106 DCDT_Agora( int PO_Type = 1 ); 00107 DCDT_Agora( char *filename, int PO_TYPE = 1 ); 00108 ~DCDT_Agora(); 00109 00110 void AddMember(DCDT_Member *); 00111 void AddSysMember( DCDT_Member *, unsigned int profile); 00112 void RemoveMember(DCDT_Member *); 00113 00114 int ActivateMember(DCDT_Member *, char *name=NULL ); 00115 int ActivateSysMember(DCDT_Member *, char *name=NULL ); 00116 void LetsWork(); 00117 00118 void SetStatus(int NewStatus); 00119 00120 inline bool CheckMemberProfile(int MemberID, unsigned int mask); 00121 inline int GetStatus(); 00122 int ReadAgoraID() { return AgoraID; }; 00123 void ReadIPStrAddr(char * addr) { strcpy(addr, IPStrAddr); }; 00124 int ReadIPAddr() { return IPAddr; }; 00125 00127 inline int CheckIfAllMemberReady(); 00128 00129 inline int GetFirstFreeMemberTableSlot(); 00130 00131 inline void DeleteMemberTableEntry(int MemberID); 00132 00134 bool FirstAgora; 00135 00137 bool commflag; 00138 00139 inline void SetActive( int MembID) { MemberTable[MembID].active = true; }; 00140 inline void SetInactive( int MembID ) { MemberTable[MembID].active = false; }; 00141 00142 private: 00143 00144 bool CommConfig(char *filename); 00145 char IPStrAddr[INET_ADDRSTRLEN]; 00146 00148 int AgoraID; 00149 int IPAddr; 00150 int Status; 00151 00153 int MaxIDMember; 00154 00155 DCDT_PostOffice * PostOffice; 00156 DCDT_MsgManager * MsgManager; 00157 DCDT_Finder * Finder; 00158 00159 // TODO: Check if we must keep always the following 00160 DCDT_InnerLinkManager *InnerLinkManager; 00161 00162 int LastDCDT_MemberID; 00163 00164 DCDT_MemberTableElem MemberTable[MAX_MEMBERS]; 00165 00166 FinderData *finder_data; 00167 00168 #ifdef SEQUENCER_VERSION 00169 DCDT_Sequencer * Sequencer; 00170 #endif 00171 }; 00172 00173 int DCDT_Agora::GetStatus() { 00174 return Status; 00175 } 00176 00184 inline bool DCDT_Agora::CheckMemberProfile(int MemberID, unsigned int mask) { 00185 return ((MemberTable[MemberID].Profile & mask) == mask); 00186 } 00187 00191 inline int DCDT_Agora::GetFirstFreeMemberTableSlot(){ 00192 for( int i = 1; i < MAX_MEMBERS; i++) { 00193 MemberTable[i].mtx.lock(); 00194 if ( MemberTable[i].FreeSlot) { 00195 MemberTable[i].FreeSlot = false; 00196 MemberTable[i].mtx.unlock(); 00197 return i; 00198 } 00199 MemberTable[i].mtx.unlock(); 00200 } 00201 return 0; 00202 }; 00203 00207 inline void DCDT_Agora::DeleteMemberTableEntry(int MemberID){ 00208 MemberTable[MemberID].mtx.lock(); 00209 00210 MemberTable[MemberID].MemberPtr = NULL; 00211 MemberTable[MemberID].num_missed_deadlines = 0; 00212 MemberTable[MemberID].FreeSlot = true; 00213 MemberTable[MemberID].empty = true; 00214 00215 MemberTable[MemberID].mtx.unlock(); 00216 00217 }; 00218 00219 00220 #endif //define