00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef __ZTSWatcher_AutoReconnect__
00026 #define __ZTSWatcher_AutoReconnect__
00027 #include "zconfig.h"
00028
00029 #include "ZCompat_NonCopyable.h"
00030 #include "ZTSWatcher.h"
00031 #include "ZTSWatcherFactory.h"
00032
00033 #ifndef ZCONFIG_ZTSWatcher_AutoReconnect_Debug
00034 # define ZCONFIG_ZTSWatcher_AutoReconnect_Debug 1
00035 #endif
00036
00037
00038 #pragma mark -
00039 #pragma mark * ZTSWatcher_AutoReconnect
00040
00041 class ZTSWatcher_AutoReconnect : public ZTSWatcher
00042 {
00043 public:
00044 enum { kDebug = ZCONFIG_ZTSWatcher_AutoReconnect_Debug };
00045
00046 ZTSWatcher_AutoReconnect(ZRef<ZTSWatcherFactory> iFactory);
00047 virtual ~ZTSWatcher_AutoReconnect();
00048
00049
00050 virtual bool AllocateIDs(size_t iCount, uint64& oBaseID, size_t& oCountIssued);
00051
00052 virtual bool Sync(
00053 const uint64* iRemovedIDs, size_t iRemovedIDsCount,
00054 const uint64* iAddedIDs, size_t iAddedIDsCount,
00055 const int64* iRemovedQueries, size_t iRemovedQueriesCount,
00056 const AddedQueryCombo* iAddedQueries, size_t iAddedQueriesCount,
00057 std::vector<uint64>& oAddedIDs,
00058 std::vector<uint64>& oChangedTupleIDs, std::vector<ZTuple>& oChangedTuples,
00059 const uint64* iWrittenTupleIDs, const ZTuple* iWrittenTuples, size_t iWrittenTuplesCount,
00060 std::map<int64, std::vector<uint64> >& oChangedQueries);
00061
00062 virtual void SetCallback(Callback_t iCallback, void* iRefcon);
00063
00064
00065 enum EStatus
00066 {
00067 eStatus_Dead,
00068 eStatus_Failed,
00069 eStatus_Established
00070 };
00071
00072 virtual void NotifyStatusChange(EStatus iStatus);
00073
00074 private:
00075 bool pEnsureConnection();
00076 bool pReestablishConnection();
00077 bool pEstablishConnection();
00078
00079 void pUpdateRegistrations(
00080 const uint64* iRemovedIDs, size_t iRemovedIDsCount,
00081 const uint64* iAddedIDs, size_t iAddedIDsCount,
00082 const int64* iRemovedQueries, size_t iRemovedQueriesCount,
00083 const AddedQueryCombo* iAddedQueries, size_t iAddedQueriesCount);
00084
00085 bool pAllocateIDs(size_t iCount, uint64& oBaseID, size_t& oCountIssued);
00086
00087 bool pSync(
00088 const uint64* iRemovedIDs, size_t iRemovedIDsCount,
00089 const uint64* iAddedIDs, size_t iAddedIDsCount,
00090 const int64* iRemovedQueries, size_t iRemovedQueriesCount,
00091 const AddedQueryCombo* iAddedQueries, size_t iAddedQueriesCount,
00092 std::vector<uint64>& oAddedIDs,
00093 std::vector<uint64>& oChangedTupleIDs, std::vector<ZTuple>& oChangedTuples,
00094 const uint64* iWrittenTupleIDs, const ZTuple* iWrittenTuples, size_t iWrittenTuplesCount,
00095 std::map<int64, std::vector<uint64> >& oChangedQueries);
00096
00097 void Callback();
00098 static void sCallback(void* iRefcon);
00099
00100 private:
00101 ZMutex fMutex;
00102 ZRef<ZTSWatcherFactory> fFactory;
00103 ZRef<ZTSWatcher> fTSWatcher;
00104 EStatus fStatus;
00105 bool fConnectionFailed;
00106 Callback_t fCallback;
00107 void* fRefcon;
00108 set<uint64> fTupleIDs;
00109 set<AddedQueryCombo> fQueries;
00110 };
00111
00112 #endif // __ZTSWatcher_AutoReconnect__