Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Bug fix for pub sub creation (awslabs#214)
Browse files Browse the repository at this point in the history
* bug fix for pubsub creation

* fixed clang format

* clang-format fix

* Added comments for adding default message in created file and changed dynamic buffer to a static one
  • Loading branch information
xlcheng1 authored Feb 4, 2022
1 parent 2790c41 commit 817cd3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
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

0 comments on commit 817cd3a

Please sign in to comment.