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.

211 lines
8.4KB

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