-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCheckinSender.h
101 lines (83 loc) · 2.84 KB
/
CheckinSender.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "Clock.h"
#include "ClockHandler.h"
#include "SchemaListener.h"
#include "Signal.h"
#include "SignalTypes.h"
#include "Thread.h"
#include "TimeTypes.h"
#include <atomic>
#include <boost/optional/optional.hpp>
#include <cstdint>
#include <memory>
#include <mutex>
#include <vector>
namespace Aws
{
namespace IoTFleetWise
{
/**
* @brief This thread sends the check-in messages to the Cloud
*
* For that to happen it needs to be notified whenever any of the documents relevant to check-in
* changes.
*/
class CheckinSender
{
public:
CheckinSender( std::shared_ptr<SchemaListener> schemaListener, uint32_t checkinIntervalMs = 0 );
~CheckinSender();
CheckinSender( const CheckinSender & ) = delete;
CheckinSender &operator=( const CheckinSender & ) = delete;
CheckinSender( CheckinSender && ) = delete;
CheckinSender &operator=( CheckinSender && ) = delete;
/**
* @brief Callback from CollectionSchemeManager to notify that the new scheme is available
*
* This needs to be called at least once, otherwise no checkin message will be sent.
*
* @param documents the list of documents that will be sent in the next checkin message
* */
void onCheckinDocumentsChanged( const std::vector<SyncID> &documents );
/**
* @brief stops the internal thread if started and wait until it finishes
*
* @return true if the stop was successful
*/
bool stop();
/**
* @brief starts the internal thread
*
* @return true if the start was successful
*/
bool start();
/**
* @brief Checks that the worker thread is healthy.
*/
bool isAlive();
private:
// default checkin interval set to 5 mins
static constexpr uint32_t DEFAULT_CHECKIN_INTERVAL_IN_MILLISECOND = 300000;
bool shouldStop() const;
void doWork();
boost::optional<Timestamp> getTimeToSendNextCheckin();
void setTimeToSendNextCheckin( boost::optional<Timestamp> timeToSendNextCheckin );
// Time interval in ms to send checkin message
uint32_t mCheckinIntervalMs{ DEFAULT_CHECKIN_INTERVAL_IN_MILLISECOND };
boost::optional<Timestamp> mTimeToSendNextCheckin;
std::mutex mTimeToSendNextCheckinMutex;
Thread mThread;
std::atomic<bool> mShouldStop{ false };
std::mutex mThreadMutex;
Signal mWait;
std::shared_ptr<const Clock> mClock = ClockHandler::getClock();
// The list of checkin documents that will be sent in the next checkin message. If this optional
// doesn't contain a value, then no checkin message will be sent.
boost::optional<std::vector<SyncID>> mCheckinDocuments;
std::mutex mCheckinDocumentsMutex;
std::shared_ptr<SchemaListener> mSchemaListener;
};
} // namespace IoTFleetWise
} // namespace Aws