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.

213 lines
8.6KB

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