-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathCheckinSender.cpp
195 lines (169 loc) · 5.48 KB
/
CheckinSender.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "CheckinSender.h"
#include "LoggingModule.h"
#include "SignalTypes.h"
#include <algorithm>
#include <boost/none.hpp>
#include <cstddef>
#include <string>
#include <utility>
#include <vector>
namespace Aws
{
namespace IoTFleetWise
{
// Checkin retry interval. Used issue checkins to the cloud as soon as possible, set to 5 seconds
static constexpr uint32_t RETRY_CHECKIN_INTERVAL_IN_MILLISECOND = 5000;
CheckinSender::CheckinSender( std::shared_ptr<SchemaListener> schemaListener, uint32_t checkinIntervalMs )
: mSchemaListener( std::move( schemaListener ) )
{
if ( checkinIntervalMs > 0 )
{
mCheckinIntervalMs = checkinIntervalMs;
}
}
CheckinSender::~CheckinSender()
{
if ( isAlive() )
{
stop();
}
}
void
CheckinSender::onCheckinDocumentsChanged( const std::vector<SyncID> &documents )
{
std::lock_guard<std::mutex> lock( mCheckinDocumentsMutex );
mCheckinDocuments = documents;
mWait.notify();
}
bool
CheckinSender::start()
{
// Prevent concurrent stop/init
std::lock_guard<std::mutex> lock( mThreadMutex );
// On multi core systems the shared variable mShouldStop must be updated for
// all cores before starting the thread otherwise thread will directly end
mShouldStop.store( false );
if ( !mThread.create(
[]( void *data ) {
CheckinSender *checkinSender = static_cast<CheckinSender *>( data );
checkinSender->doWork();
},
this ) )
{
FWE_LOG_TRACE( "Checkin Thread failed to start" );
}
else
{
FWE_LOG_TRACE( "Checkin Thread started" );
mThread.setThreadName( "fwCheckin" );
}
return mThread.isActive() && mThread.isValid();
}
void
CheckinSender::doWork()
{
setTimeToSendNextCheckin( mClock->monotonicTimeSinceEpochMs() );
while ( true )
{
if ( shouldStop() )
{
break;
}
auto timeToSendNextCheckinMs = getTimeToSendNextCheckin();
if ( !timeToSendNextCheckinMs.has_value() )
{
auto timeToWaitMs = mCheckinIntervalMs;
FWE_LOG_TRACE( "Spurious wake up. Still waiting for the previous checkin response.Sleeping again for: " +
std::to_string( timeToWaitMs ) + " ms" );
mWait.wait( timeToWaitMs );
continue;
}
auto currentTimeMs = mClock->monotonicTimeSinceEpochMs();
if ( timeToSendNextCheckinMs.get() > currentTimeMs )
{
auto timeToWaitMs = static_cast<uint32_t>( timeToSendNextCheckinMs.get() - currentTimeMs );
FWE_LOG_TRACE( "Spurious wake up. Time for next checkin not reached yet. Sleeping again for: " +
std::to_string( timeToWaitMs ) + " ms" );
mWait.wait( timeToWaitMs );
continue;
}
{
std::unique_lock<std::mutex> lock( mCheckinDocumentsMutex );
if ( !mCheckinDocuments.has_value() )
{
lock.unlock();
FWE_LOG_TRACE( "List of checkin documents not available yet. Sleeping until it is available" );
mWait.wait( Signal::WaitWithPredicate );
continue;
}
auto &checkinDocuments = mCheckinDocuments.get();
std::string checkinLogStr;
for ( size_t i = 0; i < checkinDocuments.size(); i++ )
{
if ( i > 0 )
{
checkinLogStr += ", ";
}
checkinLogStr += checkinDocuments[i];
}
FWE_LOG_TRACE( "CHECKIN: " + checkinLogStr );
setTimeToSendNextCheckin( boost::none );
mSchemaListener->sendCheckin( checkinDocuments, [this, currentTimeMs]( bool success ) {
if ( success )
{
setTimeToSendNextCheckin( currentTimeMs + mCheckinIntervalMs );
}
else
{
uint32_t minimumCheckinInterval =
std::min( RETRY_CHECKIN_INTERVAL_IN_MILLISECOND, mCheckinIntervalMs );
setTimeToSendNextCheckin( mClock->monotonicTimeSinceEpochMs() + minimumCheckinInterval );
mWait.notify();
}
} );
}
mWait.wait( mCheckinIntervalMs );
}
}
boost::optional<Timestamp>
CheckinSender::getTimeToSendNextCheckin()
{
std::lock_guard<std::mutex> lock( mTimeToSendNextCheckinMutex );
return mTimeToSendNextCheckin;
}
void
CheckinSender::setTimeToSendNextCheckin( boost::optional<Timestamp> timeToSendNextCheckin )
{
std::lock_guard<std::mutex> lock( mTimeToSendNextCheckinMutex );
mTimeToSendNextCheckin = timeToSendNextCheckin;
}
bool
CheckinSender::stop()
{
if ( ( !mThread.isValid() ) || ( !mThread.isActive() ) )
{
return true;
}
std::lock_guard<std::mutex> lock( mThreadMutex );
mShouldStop.store( true, std::memory_order_relaxed );
FWE_LOG_TRACE( "Request stop" );
mWait.notify();
mThread.release();
FWE_LOG_TRACE( "Stop finished" );
mShouldStop.store( false, std::memory_order_relaxed );
return !mThread.isActive();
}
bool
CheckinSender::shouldStop() const
{
return mShouldStop.load( std::memory_order_relaxed );
}
bool
CheckinSender::isAlive()
{
return mThread.isValid() && mThread.isActive();
}
} // namespace IoTFleetWise
} // namespace Aws