20
20
using namespace Aws ::Crt;
21
21
using namespace Aws ::Discovery;
22
22
23
- std::shared_ptr<Mqtt::MqttConnection> getMqttConnection (
23
+ static std::shared_ptr<Mqtt::MqttConnection> getMqttConnection (
24
24
Aws::Iot::MqttClient &mqttClient,
25
- DiscoverResponse *response ,
25
+ const Aws::Crt::Vector<GGGroup> &ggGroups ,
26
26
Utils::cmdData &cmdData,
27
27
std::promise<void > &shutdownCompletedPromise)
28
28
{
29
29
std::shared_ptr<Mqtt::MqttConnection> connection;
30
30
31
- for (const auto &groupToUse : *response-> GGGroups )
31
+ for (const auto &groupToUse : ggGroups )
32
32
{
33
33
for (const auto &connectivityInfo : *groupToUse.Cores ->at (0 ).Connectivity )
34
34
{
@@ -115,6 +115,23 @@ std::shared_ptr<Mqtt::MqttConnection> getMqttConnection(
115
115
return nullptr ;
116
116
}
117
117
118
+ static void printGreengrassResponse (const Aws::Crt::Vector<GGGroup> &ggGroups)
119
+ {
120
+ for (const auto &ggGroup : ggGroups)
121
+ {
122
+ fprintf (stdout, " Group ID: %s\n " , ggGroup.GGGroupId ->c_str ());
123
+ for (const auto &ggCore : *ggGroup.Cores )
124
+ {
125
+ fprintf (stdout, " Thing ARN: %s\n " , ggCore.ThingArn ->c_str ());
126
+ for (const auto &connectivity : *ggCore.Connectivity )
127
+ {
128
+ fprintf (stdout, " Connectivity Host Address: %s\n " , connectivity.HostAddress ->c_str ());
129
+ fprintf (stdout, " Connectivity Host Port: %u\n " , connectivity.Port .value ());
130
+ }
131
+ }
132
+ }
133
+ }
134
+
118
135
int main (int argc, char *argv[])
119
136
{
120
137
/* *********************** Setup ****************************/
@@ -191,63 +208,23 @@ int main(int argc, char *argv[])
191
208
Aws::Iot::MqttClient mqttClient;
192
209
std::shared_ptr<Mqtt::MqttConnection> connection (nullptr );
193
210
194
- std::promise<void > connectionFinishedPromise;
211
+ DiscoverResponse discoverResponse;
212
+
213
+ std::promise<bool > discoveryStatusPromise;
195
214
std::promise<void > shutdownCompletedPromise;
196
215
216
+ // Initiate gathering a list of discoverable Greengrass cores.
217
+ // NOTE: This is an asynchronous operation, so it completes before the results are actually ready. You need to use
218
+ // synchronization techniques to obtain its results. For simplicity, we use promise/future in this sample.
197
219
discoveryClient->Discover (
198
- cmdData.input_thingName , [&](DiscoverResponse *response, int error, int httpResponseCode) {
220
+ cmdData.input_thingName ,
221
+ [&discoverResponse, &discoveryStatusPromise](DiscoverResponse *response, int error, int httpResponseCode) {
199
222
fprintf (stdout, " Discovery completed with error code %d; http code %d\n " , error, httpResponseCode);
200
- if (!error && response->GGGroups )
201
- {
202
- // Print the discovery response information and then exit. Does not use the discovery info.
203
- // (unless in CI, in which case just note it was successful and exit)
204
- if (cmdData.input_PrintDiscoverRespOnly )
205
- {
206
- // Print the discovery response information and then exit (unless in CI, in which case just note it
207
- // was successful)
208
- if (!cmdData.input_isCI )
209
- {
210
- for (size_t a = 0 ; a < response->GGGroups ->size (); a++)
211
- {
212
- auto tmpGroup = std::move (response->GGGroups ->at (a));
213
- fprintf (stdout, " Group ID: %s\n " , tmpGroup.GGGroupId ->c_str ());
214
- for (size_t b = 0 ; b < tmpGroup.Cores ->size (); b++)
215
- {
216
- fprintf (stdout, " Thing ARN: %s\n " , tmpGroup.Cores ->at (b).ThingArn ->c_str ());
217
- for (size_t c = 0 ; c < tmpGroup.Cores ->at (b).Connectivity ->size (); c++)
218
- {
219
- fprintf (
220
- stdout,
221
- " Connectivity Host Address: %s\n " ,
222
- tmpGroup.Cores ->at (b).Connectivity ->at (c).HostAddress ->c_str ());
223
- fprintf (
224
- stdout,
225
- " Connectivity Host Port: %d\n " ,
226
- tmpGroup.Cores ->at (b).Connectivity ->at (c).Port .value ());
227
- }
228
- }
229
- }
230
- }
231
- else
232
- {
233
- fprintf (
234
- stdout,
235
- " Received a greengrass discovery result! Not showing result in CI for possible data "
236
- " sensitivity.\n " );
237
- }
238
- exit (0 );
239
- }
240
223
241
- connection = getMqttConnection (mqttClient, response, cmdData, shutdownCompletedPromise);
242
- if (connection)
243
- {
244
- connectionFinishedPromise.set_value ();
245
- }
246
- else
247
- {
248
- fprintf (stderr, " All connection attempts failed\n " );
249
- exit (-1 );
250
- }
224
+ if (!error && response->GGGroups .has_value ())
225
+ {
226
+ discoverResponse = *response;
227
+ discoveryStatusPromise.set_value (true );
251
228
}
252
229
else
253
230
{
@@ -256,12 +233,42 @@ int main(int argc, char *argv[])
256
233
" Discover failed with error: %s, and http response code %d\n " ,
257
234
aws_error_debug_str (error),
258
235
httpResponseCode);
259
- exit (- 1 );
236
+ discoveryStatusPromise. set_value ( false );
260
237
}
261
238
});
262
239
263
- connectionFinishedPromise.get_future ().wait ();
240
+ // Wait for the discovery process to return actual discovery results, or error if something went wrong.
241
+ auto isDiscoverySucceeded = discoveryStatusPromise.get_future ().get ();
242
+ if (!isDiscoverySucceeded)
243
+ {
244
+ return 1 ;
245
+ }
246
+
247
+ // Print the discovery response information and then exit. Does not use the discovery info.
248
+ if (cmdData.input_PrintDiscoverRespOnly )
249
+ {
250
+ if (!cmdData.input_isCI )
251
+ {
252
+ printGreengrassResponse (*discoverResponse.GGGroups );
253
+ }
254
+ else
255
+ {
256
+ fprintf (
257
+ stdout,
258
+ " Received a greengrass discovery result! Not showing result in CI for possible data sensitivity.\n " );
259
+ }
260
+ return 0 ;
261
+ }
262
+
263
+ // Try to establish a connection with one of the discovered Greengrass cores.
264
+ connection = getMqttConnection (mqttClient, *discoverResponse.GGGroups , cmdData, shutdownCompletedPromise);
265
+ if (!connection)
266
+ {
267
+ fprintf (stderr, " All connection attempts failed\n " );
268
+ exit (-1 );
269
+ }
264
270
271
+ // Now, with the established connection to a Greengrass core, we can perform MQTT-related actions.
265
272
if (cmdData.input_mode == " both" || cmdData.input_mode == " subscribe" )
266
273
{
267
274
auto onMessage = [&](Mqtt::MqttConnection & /* connection*/ ,
0 commit comments