#ifndef __CRefTable_h__
#define __CRefTable_h__ 1
#include "DirServicesTypes.h"
#include "PrivateTypes.h"
#include "DSMutexSemaphore.h"
#include <CoreFoundation/CoreFoundation.h>
#include <map>
#include <set>
class CRefTable;
class CServerPlugin;
#define kMaxTableItems 512
#define kMaxTables 0xFF
typedef struct sListInfo *sListInfoPtr;
typedef struct sListInfo {
UInt32 fRefNum;
UInt32 fType;
SInt32 fPID;
UInt32 fIPAddress;
sListInfoPtr fNext;
} sListInfo;
typedef struct sPIDInfo {
SInt32 fPID;
UInt32 fIPAddress;
sPIDInfo *fNext;
} sPIDInfo;
typedef struct sRefEntry {
UInt32 fRefNum;
UInt32 fType;
UInt32 fParentID;
SInt32 fPID;
UInt32 fIPAddress;
CServerPlugin *fPlugin;
sListInfo *fChildren;
sPIDInfo *fChildPID;
char *fNodeName; } sRefEntry;
typedef struct sRefCleanUpEntry {
UInt32 fRefNum;
UInt32 fType;
CServerPlugin *fPlugin;
sRefCleanUpEntry *fNext;
} sRefCleanUpEntry;
typedef SInt32 RefDeallocateProc ( UInt32 inRefNum, UInt32 inRefType, CServerPlugin *inPluginPtr );
typedef struct sRefTable *sRefTablePtr;
typedef struct sRefTable {
UInt32 fTableNum;
UInt32 fCurRefNum;
UInt32 fItemCnt;
sRefEntry *fTableData[ kMaxTableItems ];
} sRefTable;
typedef std::set<UInt32> tDirRefSet;
typedef std::map<UInt32, tDirRefSet> tPIDDirRefMap;
typedef std::map<UInt32, tPIDDirRefMap> tIPPIDDirRefMap;
typedef std::map<UInt32, UInt32> tPIDRefCountMap;
typedef std::map<UInt32, tPIDRefCountMap> tIPPIDRefCountMap;
class CRefTable {
public:
CRefTable ( RefDeallocateProc *deallocProc );
virtual ~CRefTable ( void );
void Lock ( void );
void Unlock ( void );
static tDirStatus VerifyDirRef ( tDirReference inDirRef, CServerPlugin **outPlugin, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus VerifyNodeRef ( tDirNodeReference inDirNodeRef, CServerPlugin **outPlugin, SInt32 inPID, UInt32 inIPAddress );
static char* GetNodeRefName ( tDirNodeReference inDirNodeRef );
static tDirStatus VerifyRecordRef ( tRecordReference inRecordRef, CServerPlugin **outPlugin, SInt32 inPID, UInt32 inIPAddress, bool inDaemonPID_OK = false );
static tDirStatus VerifyAttrListRef ( tAttributeListRef inAttributeListRef, CServerPlugin **outPlugin, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus VerifyAttrValueRef ( tAttributeValueListRef inAttributeValueListRef, CServerPlugin **outPlugin, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus NewDirRef ( UInt32 *outNewRef, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus NewNodeRef ( UInt32 *outNewRef, CServerPlugin *inPlugin, UInt32 inParentID, SInt32 inPID, UInt32 inIPAddress, const char *inNodeName );
static tDirStatus NewRecordRef ( UInt32 *outNewRef, CServerPlugin *inPlugin, UInt32 inParentID, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus NewAttrListRef ( UInt32 *outNewRef, CServerPlugin *inPlugin, UInt32 inParentID, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus NewAttrValueRef ( UInt32 *outNewRef, CServerPlugin *inPlugin, UInt32 inParentID, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus RemoveDirRef ( UInt32 inDirRef, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus RemoveNodeRef ( UInt32 inNodeRef, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus RemoveRecordRef ( UInt32 inRecRef, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus RemoveAttrListRef ( UInt32 inAttrListRef, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus RemoveAttrValueRef ( UInt32 InAttrValueRef, SInt32 inPID, UInt32 inIPAddress );
static tDirStatus SetNodePluginPtr ( tDirNodeReference inNodeRef, CServerPlugin *inPlugin );
static tDirStatus AddChildPIDToRef ( UInt32 inRefNum, UInt32 inParentPID, SInt32 inChildPID, UInt32 inIPAddress );
static void CleanClientRefs ( UInt32 inIPAddress, UInt32 inPIDorPort );
void LockClientPIDList ( void );
void UnlockClientPIDList ( void );
char* CreateNextClientPIDListString
( bool inStart, tIPPIDDirRefMap::iterator &inOutIPEntry, tPIDDirRefMap::iterator &inOutPIDEntry );
char* CreateNextClientPIDListRecordName
( bool inStart,
tIPPIDDirRefMap::iterator &inOutIPEntry, tPIDDirRefMap::iterator &inOutPIDEntry,
char** outIPAddress, char** outPIDValue,
SInt32 &outIP, UInt32 &outPID,
UInt32 &outTotalRefCount,
UInt32 &outDirRefCount, char** &outDirRefs );
void RetrieveRefDataPerClientPIDAndIP
( SInt32 inIP, UInt32 inPID, char** inDirRefs,
UInt32 &outNodeRefCount, char** &outNodeRefs,
UInt32 &outRecRefCount, char** &outRecRefs,
UInt32 &outAttrListRefCount, char** &outAttrListRefs,
UInt32 &outAttrListValueRefCount, char** &outAttrListValueRefs );
private:
tDirStatus VerifyReference ( tDirReference inDirRef, UInt32 inType, CServerPlugin **outPlugin, SInt32 inPID, UInt32 inIPAddress, bool inDaemonPID_OK = false );
char* GetNodeName ( tDirReference inDirRef );
tDirStatus GetNewRef ( UInt32 *outRef, UInt32 inParentID, eRefTypes inType, CServerPlugin *inPlugin, SInt32 inPID, UInt32 inIPAddress, const char *inNodeName = NULL );
tDirStatus RemoveRef ( UInt32 inRefNum, UInt32 inType, SInt32 inPID, UInt32 inIPAddress, bool inbAtTop = false);
tDirStatus SetPluginPtr ( UInt32 inRefNum, UInt32 inType, CServerPlugin *inPlugin );
tDirStatus GetReference ( UInt32 inRefNum, sRefEntry **outRefData );
tDirStatus LinkToParent ( UInt32 inRefNum, UInt32 inType, UInt32 inParentID, SInt32 inPID, UInt32 inIPAddress );
tDirStatus UnlinkFromParent ( UInt32 inRefNum );
void RemoveChildren ( sListInfo *inChildList, SInt32 inPID, UInt32 inIPAddress );
sRefTable* GetNextTable ( sRefTable *inCurTable );
sRefTable* GetThisTable ( UInt32 inTableNum );
sRefEntry* GetTableRef ( UInt32 inRefNum );
UInt32 UpdateClientPIDRefCount
( SInt32 inClientPID, UInt32 inIPAddress, bool inUpRefCount, UInt32 inDirRef=0 );
void DoCleanClientRefs ( UInt32 inIPAddress, UInt32 inPIDorPort );
UInt32 fTableCount;
sRefTable *fRefTables[ kMaxTables + 1 ]; RefDeallocateProc *fDeallocProc;
tIPPIDDirRefMap fClientDirRefMap;
tIPPIDRefCountMap fClientRefCountMap;
DSMutexSemaphore *fClientPIDListLock; DSMutexSemaphore fTableMutex;
sRefCleanUpEntry *fRefCleanUpEntriesHead;
sRefCleanUpEntry *fRefCleanUpEntriesTail;
};
void GrowList(char** &inOutRefs, UInt32 &inOutRefListSize);
#endif