Skip to content

Bug fix for pub sub creation #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions source/samples/pubsub/PubSubFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ string PubSubFeature::getName()
return "Pub Sub Sample";
}

bool PubSubFeature::createPubSub(const PlainConfig &config, std::string filePath)
bool PubSubFeature::createPubSub(const PlainConfig &config, std::string filePath, const aws_byte_buf *payload)
{
std::string pubSubFileDir = FileUtils::ExtractParentDirectory(filePath);
LOGM_INFO(TAG, "Creating Pub/Sub file: %s", filePath.c_str());
Expand Down Expand Up @@ -69,6 +69,11 @@ bool PubSubFeature::createPubSub(const PlainConfig &config, std::string filePath
{
return false;
}
// Write payload data in newly created empty file.
if (payload != NULL)
{
FileUtils::WriteToFile(filePath, payload);
}
}
else
{
Expand Down Expand Up @@ -106,7 +111,10 @@ int PubSubFeature::init(
}
pubFile = FileUtils::ExtractExpandedPath(pubFile);

if (!createPubSub(config, pubFile))
ByteBuf payload;
payload = aws_byte_buf_from_c_str(DEFAULT_PUBLISH_PAYLOAD.c_str());

if (!createPubSub(config, pubFile, &payload))
{
LOG_ERROR(TAG, "Failed to create publish directory or file");
}
Expand All @@ -117,7 +125,7 @@ int PubSubFeature::init(
}
subFile = FileUtils::ExtractExpandedPath(subFile);

if (!createPubSub(config, subFile))
if (!createPubSub(config, subFile, NULL))
{
LOG_ERROR(TAG, "Failed to create subscribe directory or file");
}
Expand Down Expand Up @@ -152,12 +160,8 @@ int PubSubFeature::getPublishFileData(aws_byte_buf *buf)
void PubSubFeature::publishFileData()
{
ByteBuf payload;
if (pubFile == "")
{
aws_byte_buf_init(&payload, resourceManager->getAllocator(), DEFAULT_PUBLISH_PAYLOAD.size());
aws_byte_buf_write(&payload, (uint8_t *)DEFAULT_PUBLISH_PAYLOAD.c_str(), DEFAULT_PUBLISH_PAYLOAD.size());
}
else if (getPublishFileData(&payload) != AWS_OP_SUCCESS)

if (getPublishFileData(&payload) != AWS_OP_SUCCESS)
{
LOG_ERROR(TAG, "Failed to read publish file... Skipping publish");
return;
Expand Down
2 changes: 1 addition & 1 deletion source/samples/pubsub/PubSubFeature.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Aws
static constexpr char DEFAULT_PUBLISH_FILE[] = "~/.aws-iot-device-client/pubsub/publish-file.txt";
static constexpr char DEFAULT_SUBSCRIBE_FILE[] =
"~/.aws-iot-device-client/pubsub/subscribe-file.txt";
bool createPubSub(const PlainConfig &config, std::string absFilePath);
bool createPubSub(const PlainConfig &config, std::string absFilePath, const aws_byte_buf *payload);
/**
* \brief Initializes the PubSub feature with all the required setup information, event
* handlers, and the SharedCrtResourceManager
Expand Down