-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathAwsGreengrassV2ConnectivityModule.cpp
100 lines (84 loc) · 2.74 KB
/
AwsGreengrassV2ConnectivityModule.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#include "AwsGreengrassV2ConnectivityModule.h"
#include "AwsGreengrassV2Receiver.h"
#include "AwsGreengrassV2Sender.h"
#include "LoggingModule.h"
#include <future>
namespace Aws
{
namespace IoTFleetWise
{
AwsGreengrassV2ConnectivityModule::AwsGreengrassV2ConnectivityModule( Aws::Crt::Io::ClientBootstrap *clientBootstrap,
const TopicConfig &topicConfig )
: mConnected( false )
, mConnectionEstablished( false )
, mClientBootstrap( clientBootstrap )
, mTopicConfig( topicConfig )
{
}
/*
* As first step to enable backend communication this code is mainly oriented on the basic_pub_sub
* example from the Aws Iot C++ SDK
*/
bool
AwsGreengrassV2ConnectivityModule::connect()
{
mConnected = false;
mLifecycleHandler = std::make_unique<IpcLifecycleHandler>();
mGreengrassClient = std::make_shared<Aws::Greengrass::GreengrassCoreIpcClient>( *( mClientBootstrap ) );
auto connectionStatus = mGreengrassClient->Connect( *( mLifecycleHandler.get() ) ).get();
if ( !connectionStatus )
{
FWE_LOG_ERROR( "Failed to establish connection with error " + std::to_string( connectionStatus ) );
return false;
}
mConnected = true;
for ( auto receiver : mReceivers )
{
receiver->subscribe();
}
// subscribe to all topics first before notifying listeners for connection
mConnectionEstablishedListeners.notify();
FWE_LOG_INFO( "Successfully connected" );
return true;
}
std::shared_ptr<ISender>
AwsGreengrassV2ConnectivityModule::createSender()
{
auto sender = std::make_shared<AwsGreengrassV2Sender>( this, mGreengrassClient, mTopicConfig );
mSenders.emplace_back( sender );
return sender;
}
std::shared_ptr<IReceiver>
AwsGreengrassV2ConnectivityModule::createReceiver( const std::string &topicName )
{
auto receiver = std::make_shared<AwsGreengrassV2Receiver>( this, mGreengrassClient, topicName );
mReceivers.emplace_back( receiver );
return receiver;
}
void
AwsGreengrassV2ConnectivityModule::subscribeToConnectionEstablished( OnConnectionEstablishedCallback callback )
{
mConnectionEstablishedListeners.subscribe( callback );
}
bool
AwsGreengrassV2ConnectivityModule::disconnect()
{
for ( auto receiver : mReceivers )
{
receiver->unsubscribe();
receiver->invalidateConnection();
}
for ( auto sender : mSenders )
{
sender->invalidateConnection();
}
return true;
}
AwsGreengrassV2ConnectivityModule::~AwsGreengrassV2ConnectivityModule()
{
AwsGreengrassV2ConnectivityModule::disconnect();
}
} // namespace IoTFleetWise
} // namespace Aws