Skip to content

Commit 248764b

Browse files
Minor sample adjustments (#432)
Adjust the GG basic discovery sample to use the message argument and adds a port override to the PubSub sample. Commit log: * Added port override and fixed message argument in GG basic discovery * Fix Windows conversion error * Another fix for Windows warnings in CI
1 parent 4157d4b commit 248764b

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

samples/greengrass/basic_discovery/main.cpp

+25-9
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ int main(int argc, char *argv[])
4242
cmdUtils.RegisterCommand(
4343
"mode", "<str>", "Mode options: 'both', 'publish', or 'subscribe' (optional, default='both').");
4444
const char **const_argv = (const char **)argv;
45+
cmdUtils.UpdateCommandHelp(
46+
"message",
47+
"The message to send. If no message is provided, you will be prompted to input one (optional, default='')");
4548
cmdUtils.SendArguments(const_argv, const_argv + argc);
4649

4750
String certificatePath = cmdUtils.GetCommandRequired("cert");
@@ -51,7 +54,7 @@ int main(int argc, char *argv[])
5154
String region = cmdUtils.GetCommandRequired("region");
5255
String topic = cmdUtils.GetCommandOrDefault("topic", "test/topic");
5356
String mode = cmdUtils.GetCommandOrDefault("mode", "both");
54-
String message = cmdUtils.GetCommandOrDefault("message", "Hello World");
57+
String message = cmdUtils.GetCommandOrDefault("message", "");
5558
String proxyHost = cmdUtils.GetCommandOrDefault("proxy_host", "");
5659
if (cmdUtils.HasCommand("proxy_port"))
5760
{
@@ -256,22 +259,35 @@ int main(int argc, char *argv[])
256259
connectionFinishedPromise.get_future().wait();
257260
}
258261

262+
bool first_input = true;
259263
while (true)
260264
{
261-
String input;
265+
String input = "";
262266
if (mode == "both" || mode == "publish")
263267
{
264-
fprintf(
265-
stdout,
266-
"Enter the message you want to publish to topic %s and press enter. Enter 'exit' to exit this "
267-
"program.\n",
268-
topic.c_str());
268+
if (message == "")
269+
{
270+
fprintf(
271+
stdout,
272+
"Enter the message you want to publish to topic %s and press enter. Enter 'exit' to exit this "
273+
"program.\n",
274+
topic.c_str());
275+
std::getline(std::cin, input);
276+
message = input;
277+
}
278+
else if (first_input == false)
279+
{
280+
fprintf(stdout, "Enter a new message or enter 'exit' or 'quit' to exit the program.\n");
281+
std::getline(std::cin, input);
282+
message = input;
283+
}
284+
first_input = false;
269285
}
270286
else
271287
{
272-
fprintf(stdout, "Enter exit or quit to exit the program.\n");
288+
fprintf(stdout, "Enter 'exit' or 'quit' to exit the program.\n");
289+
std::getline(std::cin, input);
273290
}
274-
std::getline(std::cin, input);
275291

276292
if (input == "exit" || input == "quit")
277293
{

samples/pub_sub/basic_pub_sub/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ int main(int argc, char *argv[])
3838
cmdUtils.AddCommonTopicMessageCommands();
3939
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*')");
4040
cmdUtils.RegisterCommand("count", "<int>", "The number of messages to send (optional, default='10')");
41+
cmdUtils.RegisterCommand("port_override", "<int>", "The port override to use when connecting (optional)");
4142
const char **const_argv = (const char **)argv;
4243
cmdUtils.SendArguments(const_argv, const_argv + argc);
4344

samples/utils/CommandLineUtils.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ namespace Utils
222222
{
223223
clientConfigBuilder.WithCertificateAuthority(GetCommand(m_cmd_ca_file).c_str());
224224
}
225+
if (HasCommand(m_cmd_port_override))
226+
{
227+
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
228+
if (tmp_port > 0 && tmp_port < UINT16_MAX)
229+
{
230+
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
231+
}
232+
}
225233

226234
clientConfigBuilder.WithEndpoint(GetCommandRequired(m_cmd_endpoint));
227235
return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
@@ -301,6 +309,14 @@ namespace Utils
301309
{
302310
clientConfigBuilder.WithHttpProxyOptions(proxyOptions);
303311
}
312+
if (HasCommand(m_cmd_port_override))
313+
{
314+
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
315+
if (tmp_port > 0 && tmp_port < UINT16_MAX)
316+
{
317+
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
318+
}
319+
}
304320

305321
clientConfigBuilder.WithEndpoint(GetCommandRequired(m_cmd_endpoint));
306322
return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
@@ -333,6 +349,14 @@ namespace Utils
333349
{
334350
clientConfigBuilder.WithHttpProxyOptions(GetProxyOptionsForMQTTConnection());
335351
}
352+
if (HasCommand(m_cmd_port_override))
353+
{
354+
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
355+
if (tmp_port > 0 && tmp_port < UINT16_MAX)
356+
{
357+
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
358+
}
359+
}
336360

337361
clientConfigBuilder.WithEndpoint(GetCommandRequired(m_cmd_endpoint));
338362
return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
@@ -356,6 +380,14 @@ namespace Utils
356380
{
357381
clientConfigBuilder.WithHttpProxyOptions(GetProxyOptionsForMQTTConnection());
358382
}
383+
if (HasCommand(m_cmd_port_override))
384+
{
385+
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
386+
if (tmp_port > 0 && tmp_port < UINT16_MAX)
387+
{
388+
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
389+
}
390+
}
359391

360392
return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
361393
}

samples/utils/CommandLineUtils.h

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ namespace Utils
245245
const Aws::Crt::String m_cmd_pkcs11_key = "key_label";
246246
const Aws::Crt::String m_cmd_message = "message";
247247
const Aws::Crt::String m_cmd_topic = "topic";
248+
const Aws::Crt::String m_cmd_port_override = "port_override";
248249
const Aws::Crt::String m_cmd_help = "help";
249250
};
250251
} // namespace Utils

0 commit comments

Comments
 (0)