The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A base class for dispatching analytics events on a dedicated thread.
  23. This class is particularly useful for sending analytics events to a web
  24. server without blocking the message thread. It can also save (and restore)
  25. events that were not dispatched so no information is lost when an internet
  26. connection is absent or something else prevents successful logging.
  27. Once startAnalyticsThread is called the logBatchedEvents method is
  28. periodically invoked on an analytics thread, with the period determined by
  29. calls to setBatchPeriod. Here events are grouped together into batches, with
  30. the maximum batch size set by the implementation of getMaximumBatchSize.
  31. It's important to call stopAnalyticsThread in the destructor of your
  32. subclass (or before then) to give the analytics thread time to shut down.
  33. Calling stopAnalyticsThread will, in turn, call stopLoggingEvents, which
  34. you should use to terminate the currently running logBatchedEvents call.
  35. @see Analytics, AnalyticsDestination, AnalyticsDestination::AnalyticsEvent
  36. @tags{Analytics}
  37. */
  38. class JUCE_API ThreadedAnalyticsDestination : public AnalyticsDestination
  39. {
  40. public:
  41. //==============================================================================
  42. /**
  43. Creates a ThreadedAnalyticsDestination.
  44. @param threadName used to identify the analytics
  45. thread in debug builds
  46. */
  47. ThreadedAnalyticsDestination (const String& threadName = "Analytics thread");
  48. /** Destructor. */
  49. ~ThreadedAnalyticsDestination() override;
  50. //==============================================================================
  51. /**
  52. Override this method to provide the maximum batch size you can handle in
  53. your subclass.
  54. Calls to logBatchedEvents will contain no more than this number of events.
  55. */
  56. virtual int getMaximumBatchSize() = 0;
  57. /**
  58. This method will be called periodically on the analytics thread.
  59. If this method returns false then the subsequent call of this function will
  60. contain the same events as previous call, plus any new events that have been
  61. generated in the period between calls. The order of events will not be
  62. changed. This allows you to retry logging events until they are logged
  63. successfully.
  64. @param events a list of events to be logged
  65. @returns if the events were successfully logged
  66. */
  67. virtual bool logBatchedEvents (const Array<AnalyticsEvent>& events) = 0;
  68. /**
  69. You must always call stopAnalyticsThread in the destructor of your subclass
  70. (or before then) to give the analytics thread time to shut down.
  71. Calling stopAnalyticsThread triggers a call to this method. At this point
  72. you are guaranteed that logBatchedEvents has been called for the last time
  73. and you should make sure that the current call to logBatchedEvents finishes
  74. as quickly as possible. This method and a subsequent call to
  75. saveUnloggedEvents must both complete before the timeout supplied to
  76. stopAnalyticsThread.
  77. In a normal use case stopLoggingEvents will be called on the message thread
  78. from the destructor of your ThreadedAnalyticsDestination subclass, and must
  79. stop the logBatchedEvents method which is running on the analytics thread.
  80. @see stopAnalyticsThread
  81. */
  82. virtual void stopLoggingEvents() = 0;
  83. //==============================================================================
  84. /**
  85. Call this to set the period between logBatchedEvents invocations.
  86. This method is thread safe and can be used to implements things like
  87. exponential backoff in logBatchedEvents calls.
  88. @param newSubmissionPeriodMilliseconds the new submission period to
  89. use in milliseconds
  90. */
  91. void setBatchPeriod (int newSubmissionPeriodMilliseconds);
  92. /**
  93. Adds an event to the queue, which will ultimately be submitted to
  94. logBatchedEvents.
  95. This method is thread safe.
  96. @param event the analytics event to add to the queue
  97. */
  98. void logEvent (const AnalyticsEvent& event) override final;
  99. protected:
  100. //==============================================================================
  101. /**
  102. Starts the analytics thread, with an initial event batching period.
  103. @param initialBatchPeriodMilliseconds the initial event batching period
  104. in milliseconds
  105. */
  106. void startAnalyticsThread (int initialBatchPeriodMilliseconds);
  107. //==============================================================================
  108. /**
  109. Triggers the shutdown of the analytics thread.
  110. You must call this method in the destructor of your subclass (or before
  111. then) to give the analytics thread time to shut down.
  112. This method invokes stopLoggingEvents and you should ensure that both the
  113. analytics thread and a call to saveUnloggedEvents are able to finish before
  114. the supplied timeout. This timeout is important because on platforms like
  115. iOS an app is killed if it takes too long to shut down.
  116. @param timeoutMilliseconds the number of milliseconds before
  117. the analytics thread is forcibly
  118. terminated
  119. */
  120. void stopAnalyticsThread (int timeoutMilliseconds);
  121. private:
  122. //==============================================================================
  123. /**
  124. This method will be called when the analytics thread is shut down,
  125. giving you the chance to save any analytics events that could not be
  126. logged. Once saved these events can be put back into the queue of events
  127. when the ThreadedAnalyticsDestination is recreated via
  128. restoreUnloggedEvents.
  129. This method should return as quickly as possible, as both
  130. stopLoggingEvents and this method need to complete inside the timeout
  131. set in stopAnalyticsThread.
  132. @param eventsToSave the events that could not be logged
  133. @see stopAnalyticsThread, stopLoggingEvents, restoreUnloggedEvents
  134. */
  135. virtual void saveUnloggedEvents (const std::deque<AnalyticsEvent>& eventsToSave) = 0;
  136. /**
  137. The counterpart to saveUnloggedEvents.
  138. Events added to the event queue provided by this method will be the
  139. first events supplied to logBatchedEvents calls. Use this method to
  140. restore any unlogged events previously stored in a call to
  141. saveUnloggedEvents.
  142. This method is called on the analytics thread.
  143. @param restoredEventQueue place restored events into this queue
  144. @see saveUnloggedEvents
  145. */
  146. virtual void restoreUnloggedEvents (std::deque<AnalyticsEvent>& restoredEventQueue) = 0;
  147. struct EventDispatcher : public Thread
  148. {
  149. EventDispatcher (const String& threadName, ThreadedAnalyticsDestination&);
  150. void run() override;
  151. void addToQueue (const AnalyticsEvent&);
  152. ThreadedAnalyticsDestination& parent;
  153. std::deque<AnalyticsEvent> eventQueue;
  154. CriticalSection queueAccess;
  155. Atomic<int> batchPeriodMilliseconds { 1000 };
  156. Array<AnalyticsEvent> eventsToSend;
  157. };
  158. const String destinationName;
  159. EventDispatcher dispatcher;
  160. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadedAnalyticsDestination)
  161. };
  162. } // namespace juce