Skip to content

Commit

Permalink
Minor sample adjustments (#432)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
TwistedTwigleg authored May 23, 2022
1 parent 4157d4b commit 248764b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
34 changes: 25 additions & 9 deletions samples/greengrass/basic_discovery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ int main(int argc, char *argv[])
cmdUtils.RegisterCommand(
"mode", "<str>", "Mode options: 'both', 'publish', or 'subscribe' (optional, default='both').");
const char **const_argv = (const char **)argv;
cmdUtils.UpdateCommandHelp(
"message",
"The message to send. If no message is provided, you will be prompted to input one (optional, default='')");
cmdUtils.SendArguments(const_argv, const_argv + argc);

String certificatePath = cmdUtils.GetCommandRequired("cert");
Expand All @@ -51,7 +54,7 @@ int main(int argc, char *argv[])
String region = cmdUtils.GetCommandRequired("region");
String topic = cmdUtils.GetCommandOrDefault("topic", "test/topic");
String mode = cmdUtils.GetCommandOrDefault("mode", "both");
String message = cmdUtils.GetCommandOrDefault("message", "Hello World");
String message = cmdUtils.GetCommandOrDefault("message", "");
String proxyHost = cmdUtils.GetCommandOrDefault("proxy_host", "");
if (cmdUtils.HasCommand("proxy_port"))
{
Expand Down Expand Up @@ -256,22 +259,35 @@ int main(int argc, char *argv[])
connectionFinishedPromise.get_future().wait();
}

bool first_input = true;
while (true)
{
String input;
String input = "";
if (mode == "both" || mode == "publish")
{
fprintf(
stdout,
"Enter the message you want to publish to topic %s and press enter. Enter 'exit' to exit this "
"program.\n",
topic.c_str());
if (message == "")
{
fprintf(
stdout,
"Enter the message you want to publish to topic %s and press enter. Enter 'exit' to exit this "
"program.\n",
topic.c_str());
std::getline(std::cin, input);
message = input;
}
else if (first_input == false)
{
fprintf(stdout, "Enter a new message or enter 'exit' or 'quit' to exit the program.\n");
std::getline(std::cin, input);
message = input;
}
first_input = false;
}
else
{
fprintf(stdout, "Enter exit or quit to exit the program.\n");
fprintf(stdout, "Enter 'exit' or 'quit' to exit the program.\n");
std::getline(std::cin, input);
}
std::getline(std::cin, input);

if (input == "exit" || input == "quit")
{
Expand Down
1 change: 1 addition & 0 deletions samples/pub_sub/basic_pub_sub/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ int main(int argc, char *argv[])
cmdUtils.AddCommonTopicMessageCommands();
cmdUtils.RegisterCommand("client_id", "<str>", "Client id to use (optional, default='test-*')");
cmdUtils.RegisterCommand("count", "<int>", "The number of messages to send (optional, default='10')");
cmdUtils.RegisterCommand("port_override", "<int>", "The port override to use when connecting (optional)");
const char **const_argv = (const char **)argv;
cmdUtils.SendArguments(const_argv, const_argv + argc);

Expand Down
32 changes: 32 additions & 0 deletions samples/utils/CommandLineUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ namespace Utils
{
clientConfigBuilder.WithCertificateAuthority(GetCommand(m_cmd_ca_file).c_str());
}
if (HasCommand(m_cmd_port_override))
{
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
if (tmp_port > 0 && tmp_port < UINT16_MAX)
{
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
}
}

clientConfigBuilder.WithEndpoint(GetCommandRequired(m_cmd_endpoint));
return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
Expand Down Expand Up @@ -301,6 +309,14 @@ namespace Utils
{
clientConfigBuilder.WithHttpProxyOptions(proxyOptions);
}
if (HasCommand(m_cmd_port_override))
{
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
if (tmp_port > 0 && tmp_port < UINT16_MAX)
{
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
}
}

clientConfigBuilder.WithEndpoint(GetCommandRequired(m_cmd_endpoint));
return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
Expand Down Expand Up @@ -333,6 +349,14 @@ namespace Utils
{
clientConfigBuilder.WithHttpProxyOptions(GetProxyOptionsForMQTTConnection());
}
if (HasCommand(m_cmd_port_override))
{
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
if (tmp_port > 0 && tmp_port < UINT16_MAX)
{
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
}
}

clientConfigBuilder.WithEndpoint(GetCommandRequired(m_cmd_endpoint));
return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
Expand All @@ -356,6 +380,14 @@ namespace Utils
{
clientConfigBuilder.WithHttpProxyOptions(GetProxyOptionsForMQTTConnection());
}
if (HasCommand(m_cmd_port_override))
{
int tmp_port = atoi(GetCommand(m_cmd_port_override).c_str());
if (tmp_port > 0 && tmp_port < UINT16_MAX)
{
clientConfigBuilder.WithPortOverride(static_cast<uint16_t>(tmp_port));
}
}

return GetClientConnectionForMQTTConnection(client, &clientConfigBuilder);
}
Expand Down
1 change: 1 addition & 0 deletions samples/utils/CommandLineUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ namespace Utils
const Aws::Crt::String m_cmd_pkcs11_key = "key_label";
const Aws::Crt::String m_cmd_message = "message";
const Aws::Crt::String m_cmd_topic = "topic";
const Aws::Crt::String m_cmd_port_override = "port_override";
const Aws::Crt::String m_cmd_help = "help";
};
} // namespace Utils

0 comments on commit 248764b

Please sign in to comment.