-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathAwsIotReceiver.h
104 lines (88 loc) · 3.15 KB
/
AwsIotReceiver.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
102
103
104
// 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 "IConnectionTypes.h"
#include "IConnectivityModule.h"
#include "IReceiver.h"
#include "Listener.h"
#include "MqttClientWrapper.h"
#include <atomic>
#include <aws/crt/mqtt/Mqtt5Client.h>
#include <future>
#include <memory>
#include <mutex>
#include <string>
namespace Aws
{
namespace IoTFleetWise
{
/**
* @brief A receiver to receive messages using IoT Core MQTT connection
*
* There can be multiple AwsIotReceivers from one AwsIotConnectivityModule. The connection of the
* connectivityModule passed in the constructor must be established before anything meaningful
* can be done with this class.
* @see AwsIotConnectivityModule
*/
class AwsIotReceiver : public IReceiver
{
public:
AwsIotReceiver( IConnectivityModule *connectivityModule,
std::shared_ptr<MqttClientWrapper> &mqttClient,
std::string topicName );
~AwsIotReceiver() override;
AwsIotReceiver( const AwsIotReceiver & ) = delete;
AwsIotReceiver &operator=( const AwsIotReceiver & ) = delete;
AwsIotReceiver( AwsIotReceiver && ) = delete;
AwsIotReceiver &operator=( AwsIotReceiver && ) = delete;
/**
* @brief Subscribe to the MQTT topic from setTopic. Necessary if data is received on the topic
*
* This function blocks until subscribe succeeded or failed and should be done in the setup form
* the bootstrap thread. The connection of the connectivityModule passed in the constructor
* must be established otherwise subscribe will fail. No retries are done to try to subscribe
* this needs to be done in the bootstrap during the setup.
* @return Success if subscribe finished correctly
*/
ConnectivityError subscribe();
/**
* @brief After unsubscribe no data will be received over by the receiver
* @return True for success
*/
bool unsubscribe();
/**
* @brief Unsubscribe from the MQTT topic asynchronously
* @return A future that can be used to wait for the unsubscribe to finish. It will return True on success.
*/
std::future<bool> unsubscribeAsync();
bool isAlive() override;
void subscribeToDataReceived( OnDataReceivedCallback callback ) override;
void onDataReceived( const Aws::Crt::Mqtt5::PublishReceivedEventData &eventData );
void
invalidateConnection()
{
std::lock_guard<std::mutex> connectivityLock( mConnectivityMutex );
mConnectivityModule = nullptr;
};
void
resetSubscription()
{
mSubscribed = false;
}
private:
bool isAliveNotThreadSafe();
IConnectivityModule *mConnectivityModule;
ThreadSafeListeners<OnDataReceivedCallback> mListeners;
std::shared_ptr<MqttClientWrapper> &mMqttClient;
std::mutex mConnectivityMutex;
std::string mTopicName;
std::atomic<bool> mSubscribed;
/**
* @brief Clock member variable used to generate the time an MQTT message was received
*/
std::shared_ptr<const Clock> mClock = ClockHandler::getClock();
};
} // namespace IoTFleetWise
} // namespace Aws