ECNotificationManager.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef ECNOTIFICATIONMANAGER_H
  18. #define ECNOTIFICATIONMANAGER_H
  19. #include <kopano/zcdefs.h>
  20. #include <condition_variable>
  21. #include <mutex>
  22. #include <pthread.h>
  23. #include "ECSession.h"
  24. #include <kopano/ECLogger.h>
  25. #include <kopano/ECConfig.h>
  26. #include <map>
  27. #include <set>
  28. struct soap;
  29. namespace KC {
  30. /*
  31. * The notification manager runs in a single thread, servicing notifications to ALL clients
  32. * that are waiting for a notification. We simply store all waiting soap connection objects together
  33. * with their getNextNotify() request, and once we are signalled that something has changed for one
  34. * of those queues, we send the reply, and requeue the soap connection for the next request.
  35. *
  36. * So, basically we only handle the SOAP-reply part of the soap request.
  37. */
  38. struct NOTIFREQUEST {
  39. struct soap *soap;
  40. time_t ulRequestTime;
  41. };
  42. class ECNotificationManager _kc_final {
  43. public:
  44. ECNotificationManager();
  45. ~ECNotificationManager();
  46. // Called by the SOAP handler
  47. HRESULT AddRequest(ECSESSIONID ecSessionId, struct soap *soap);
  48. // Called by a session when it has a notification to send
  49. HRESULT NotifyChange(ECSESSIONID ecSessionId);
  50. private:
  51. // Just a wrapper to Work()
  52. static void * Thread(void *lpParam);
  53. void * Work();
  54. bool m_bExit = false;
  55. pthread_t m_thread;
  56. unsigned int m_ulTimeout = 60; /* Currently hardcoded at 60s, see comment in Work() */
  57. // A map of all sessions that are waiting for a SOAP response to be sent (an item can be in here for up to 60 seconds)
  58. std::map<ECSESSIONID, NOTIFREQUEST> m_mapRequests;
  59. // A set of all sessions that have reported notification activity, but are yet to be processed.
  60. // (a session is in here for only very short periods of time, and contains only a few sessions even if the load is high)
  61. std::set<ECSESSIONID> m_setActiveSessions;
  62. std::mutex m_mutexRequests;
  63. std::mutex m_mutexSessions;
  64. std::condition_variable m_condSessions;
  65. };
  66. extern ECSessionManager *g_lpSessionManager;
  67. extern _kc_export void (*kopano_notify_done)(struct soap *);
  68. } /* namespace */
  69. #endif