ECSessionGroup.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. // ECSessionGroup.h: interface for the ECSessionGroup class.
  18. //
  19. //////////////////////////////////////////////////////////////////////
  20. #ifndef ECSESSIONGROUP
  21. #define ECSESSIONGROUP
  22. #include <kopano/zcdefs.h>
  23. #include <condition_variable>
  24. #include <list>
  25. #include <map>
  26. #include <mutex>
  27. #include <set>
  28. #include <kopano/ECKeyTable.h>
  29. #include "ECNotification.h"
  30. #include <kopano/kcodes.h>
  31. #include <kopano/CommonUtil.h>
  32. struct soap;
  33. namespace KC {
  34. class ECSession;
  35. class ECSessionGroup;
  36. class ECSessionManager;
  37. struct sessionInfo {
  38. sessionInfo(ECSession *lpSession) : lpSession(lpSession) {}
  39. ECSession *lpSession;
  40. };
  41. typedef std::map<ECSESSIONID, sessionInfo> SESSIONINFOMAP;
  42. struct subscribeItem {
  43. ECSESSIONID ulSession; // Unique session identifier
  44. unsigned int ulConnection; // Unique client identifier for notification
  45. unsigned int ulKey; // database object id (also storeid) or a tableid
  46. unsigned int ulEventMask;
  47. };
  48. typedef std::list<ECNotification> ECNOTIFICATIONLIST;
  49. typedef std::map<unsigned int, subscribeItem> SUBSCRIBEMAP;
  50. typedef std::multimap<unsigned int, unsigned int> SUBSCRIBESTOREMULTIMAP;
  51. struct changeSubscribeItem {
  52. ECSESSIONID ulSession;
  53. unsigned int ulConnection;
  54. notifySyncState sSyncState;
  55. };
  56. typedef std::multimap<unsigned int, changeSubscribeItem> CHANGESUBSCRIBEMAP; // SyncId -> changeSubscribeItem
  57. class ECSessionGroup _kc_final {
  58. public:
  59. ECSessionGroup(ECSESSIONGROUPID sessionGroupId, ECSessionManager *lpSessionManager);
  60. virtual ~ECSessionGroup();
  61. /*
  62. * Thread safety handlers
  63. */
  64. virtual void Lock();
  65. virtual void Unlock();
  66. virtual bool IsLocked(void) const { return m_ulRefCount > 0; }
  67. /*
  68. * Returns the SessionGroupId
  69. */
  70. virtual ECSESSIONGROUPID GetSessionGroupId(void) const { return m_sessionGroupId; }
  71. /*
  72. * Add/Remove Session from group
  73. */
  74. virtual void AddSession(ECSession *lpSession);
  75. virtual void ShutdownSession(ECSession *lpSession);
  76. virtual void ReleaseSession(ECSession *lpSession);
  77. /*
  78. * Update session time for all attached sessions
  79. */
  80. virtual void UpdateSessionTime();
  81. /*
  82. * Check is SessionGroup has lost all its children
  83. */
  84. virtual bool isOrphan();
  85. /*
  86. * Item subscription
  87. */
  88. virtual ECRESULT AddAdvise(ECSESSIONID ulSessionId, unsigned int ulConnection, unsigned int ulKey, unsigned int ulEventMask);
  89. virtual ECRESULT AddChangeAdvise(ECSESSIONID ulSessionId, unsigned int ulConnection, notifySyncState *lpSyncState);
  90. virtual ECRESULT DelAdvise(ECSESSIONID ulSessionId, unsigned int ulConnection);
  91. /*
  92. * Notifications
  93. */
  94. virtual ECRESULT AddNotification(notification *notifyItem, unsigned int ulKey, unsigned int ulStore, ECSESSIONID ulSessionId = 0);
  95. virtual ECRESULT AddNotificationTable(ECSESSIONID ulSessionId, unsigned int ulType, unsigned int ulObjType, unsigned int ulTableId,
  96. sObjectTableKey* lpsChildRow, sObjectTableKey* lpsPrevRow, struct propValArray *lpRow);
  97. virtual ECRESULT AddChangeNotification(const std::set<unsigned int> &syncIds, unsigned int ulChangeId, unsigned int ulChangeType);
  98. virtual ECRESULT AddChangeNotification(ECSESSIONID ulSessionId, unsigned int ulConnection, unsigned int ulSyncId, unsigned long ulChangeId);
  99. virtual ECRESULT GetNotifyItems(struct soap *soap, ECSESSIONID ulSessionId, struct notifyResponse *notifications);
  100. size_t GetObjectSize(void);
  101. private:
  102. ECRESULT releaseListeners();
  103. /* Personal SessionGroupId */
  104. ECSESSIONGROUPID m_sessionGroupId;
  105. /* All Sessions attached to this group */
  106. SESSIONINFOMAP m_mapSessions;
  107. /* List of all items the group is subscribed to */
  108. SUBSCRIBEMAP m_mapSubscribe;
  109. CHANGESUBSCRIBEMAP m_mapChangeSubscribe;
  110. std::recursive_mutex m_hSessionMapLock;
  111. /* Notifications */
  112. ECNOTIFICATIONLIST m_listNotification;
  113. /* Notifications lock/event */
  114. std::mutex m_hNotificationLock;
  115. std::condition_variable m_hNewNotificationEvent;
  116. ECSESSIONID m_getNotifySession = 0;
  117. /* Thread safety mutex/event */
  118. unsigned int m_ulRefCount = 0;
  119. std::mutex m_hThreadReleasedMutex;
  120. std::condition_variable m_hThreadReleased;
  121. /* Set to TRUE if no more GetNextNotifyItems() should be done on this group since the main
  122. * session has exited
  123. */
  124. bool m_bExit = false;
  125. /* Reference to the session manager needed to notify changes in our queue */
  126. ECSessionManager * m_lpSessionManager;
  127. /* Multimap of subscriptions that we have (key -> store id) */
  128. SUBSCRIBESTOREMULTIMAP m_mapSubscribedStores;
  129. std::mutex m_mutexSubscribedStores;
  130. private:
  131. // Make ECSessionGroup non-copyable
  132. ECSessionGroup(const ECSessionGroup &) = delete;
  133. ECSessionGroup &operator=(const ECSessionGroup &) = delete;
  134. };
  135. } /* namespace */
  136. #endif // #ifndef ECSESSIONGROUP