diff --git a/services/backup/src/Constants.h b/services/backup/src/Constants.h --- a/services/backup/src/Constants.h +++ b/services/backup/src/Constants.h @@ -8,9 +8,10 @@ namespace comm { namespace network { -const std::string LOG_TABLE_NAME = decorateTableName("backup-service-log"); +const std::string LOG_TABLE_NAME = + tools::decorateTableName("backup-service-log"); const std::string BACKUP_TABLE_NAME = - decorateTableName("backup-service-backup"); + tools::decorateTableName("backup-service-backup"); // This has to be smaller than GRPC_CHUNK_SIZE_LIMIT because we need to // recognize if we may receive multiple chunks or just one. If it was larger diff --git a/services/backup/src/DatabaseManager.cpp b/services/backup/src/DatabaseManager.cpp --- a/services/backup/src/DatabaseManager.cpp +++ b/services/backup/src/DatabaseManager.cpp @@ -28,7 +28,7 @@ request.AddItem( BackupItem::FIELD_CREATED, Aws::DynamoDB::Model::AttributeValue( - std::to_string(getCurrentTimestamp()))); + std::to_string(tools::getCurrentTimestamp()))); request.AddItem( BackupItem::FIELD_BACKUP_ID, Aws::DynamoDB::Model::AttributeValue(item.getBackupID())); diff --git a/services/backup/src/Reactors/server/CreateNewBackupReactor.cpp b/services/backup/src/Reactors/server/CreateNewBackupReactor.cpp --- a/services/backup/src/Reactors/server/CreateNewBackupReactor.cpp +++ b/services/backup/src/Reactors/server/CreateNewBackupReactor.cpp @@ -10,7 +10,7 @@ std::string CreateNewBackupReactor::generateBackupID() { - return this->deviceID + getCurrentTimestamp(); + return this->deviceID + tools::getCurrentTimestamp(); } std::unique_ptr CreateNewBackupReactor::handleRequest( @@ -61,7 +61,7 @@ this->userID + "] already exists, creation aborted"); } response->set_backupid(this->backupID); - this->holder = generateHolder(this->dataHash, this->backupID); + this->holder = tools::generateHolder(this->dataHash, this->backupID); this->putReactor = std::make_shared( this->holder, this->dataHash, &this->blobPutDoneCV); this->blobClient.put(this->putReactor); diff --git a/services/backup/src/Reactors/server/SendLogReactor.cpp b/services/backup/src/Reactors/server/SendLogReactor.cpp --- a/services/backup/src/Reactors/server/SendLogReactor.cpp +++ b/services/backup/src/Reactors/server/SendLogReactor.cpp @@ -23,7 +23,8 @@ } std::string SendLogReactor::generateLogID(const std::string &backupID) { - return backupID + ID_SEPARATOR + std::to_string(getCurrentTimestamp()); + return backupID + tools::ID_SEPARATOR + + std::to_string(tools::getCurrentTimestamp()); } void SendLogReactor::initializePutReactor() { @@ -108,7 +109,7 @@ this->persistenceMethod = PersistenceMethod::BLOB; } if (this->value.empty()) { - this->value = generateHolder(this->hash, this->backupID, this->logID); + this->value = tools::generateHolder(this->hash, this->backupID, this->logID); } this->initializePutReactor(); this->putReactor->scheduleSendingDataChunk(std::move(chunk)); diff --git a/services/backup/src/Tools.h b/services/backup/src/Tools.h --- a/services/backup/src/Tools.h +++ b/services/backup/src/Tools.h @@ -4,6 +4,7 @@ namespace comm { namespace network { +namespace tools { std::string generateRandomString(std::size_t length = 20); @@ -12,5 +13,6 @@ const std::string &backupID, const std::string &resourceID = ""); +} // namespace tools } // namespace network } // namespace comm diff --git a/services/backup/src/Tools.cpp b/services/backup/src/Tools.cpp --- a/services/backup/src/Tools.cpp +++ b/services/backup/src/Tools.cpp @@ -8,6 +8,7 @@ namespace comm { namespace network { +namespace tools { std::string generateRandomString(std::size_t length) { const std::string CHARACTERS = @@ -26,8 +27,9 @@ const std::string &backupID, const std::string &resourceID) { return backupID + ID_SEPARATOR + resourceID + ID_SEPARATOR + blobHash + - ID_SEPARATOR + generateUUID(); + ID_SEPARATOR + tools::generateUUID(); } +} // namespace tools } // namespace network } // namespace comm diff --git a/services/backup/test/DatabaseManagerTest.cpp b/services/backup/test/DatabaseManagerTest.cpp --- a/services/backup/test/DatabaseManagerTest.cpp +++ b/services/backup/test/DatabaseManagerTest.cpp @@ -24,7 +24,8 @@ }; std::string generateName(const std::string prefix = "") { - return prefix + "-" + std::to_string(comm::network::getCurrentTimestamp()); + return prefix + "-" + + std::to_string(comm::network::tools::getCurrentTimestamp()); } BackupItem @@ -32,7 +33,7 @@ return BackupItem( userID, backupID, - comm::network::getCurrentTimestamp(), + comm::network::tools::getCurrentTimestamp(), "xxx", "xxx", {""}); diff --git a/services/blob/src/AwsS3Bucket.cpp b/services/blob/src/AwsS3Bucket.cpp --- a/services/blob/src/AwsS3Bucket.cpp +++ b/services/blob/src/AwsS3Bucket.cpp @@ -105,7 +105,7 @@ const size_t size = this->getObjectSize(objectName); if (size > GRPC_CHUNK_SIZE_LIMIT) { - throw invalid_argument_error(std::string( + throw tools::invalid_argument_error(std::string( "The file is too big(" + std::to_string(size) + " bytes, max is " + std::to_string(GRPC_CHUNK_SIZE_LIMIT) + "bytes), please, use getObjectDataChunks")); diff --git a/services/blob/src/BlobServiceImpl.cpp b/services/blob/src/BlobServiceImpl.cpp --- a/services/blob/src/BlobServiceImpl.cpp +++ b/services/blob/src/BlobServiceImpl.cpp @@ -30,7 +30,7 @@ void BlobServiceImpl::verifyBlobHash( const std::string &expectedBlobHash, const database::S3Path &s3Path) { - const std::string computedBlobHash = computeHashForFile(s3Path); + const std::string computedBlobHash = tools::computeHashForFile(s3Path); if (expectedBlobHash != computedBlobHash) { throw std::runtime_error( "blob hash mismatch, expected: [" + expectedBlobHash + @@ -83,7 +83,7 @@ if (database::DatabaseManager::getInstance() .findReverseIndexItemsByHash(reverseIndexItem->getBlobHash()) .size() == 0) { - database::S3Path s3Path = findS3Path(*reverseIndexItem); + database::S3Path s3Path = tools::findS3Path(*reverseIndexItem); AwsS3Bucket bucket = getBucket(s3Path.getBucketName()); bucket.removeObject(s3Path.getObjectName()); diff --git a/services/blob/src/Constants.h b/services/blob/src/Constants.h --- a/services/blob/src/Constants.h +++ b/services/blob/src/Constants.h @@ -13,9 +13,10 @@ const std::string BLOB_BUCKET_NAME = "commapp-blob"; -const std::string BLOB_TABLE_NAME = decorateTableName("blob-service-blob"); +const std::string BLOB_TABLE_NAME = + tools::decorateTableName("blob-service-blob"); const std::string REVERSE_INDEX_TABLE_NAME = - decorateTableName("blob-service-reverse-index"); + tools::decorateTableName("blob-service-reverse-index"); } // namespace network } // namespace comm diff --git a/services/blob/src/DatabaseManager.cpp b/services/blob/src/DatabaseManager.cpp --- a/services/blob/src/DatabaseManager.cpp +++ b/services/blob/src/DatabaseManager.cpp @@ -30,7 +30,7 @@ request.AddItem( BlobItem::FIELD_CREATED, Aws::DynamoDB::Model::AttributeValue( - std::to_string(getCurrentTimestamp()))); + std::to_string(tools::getCurrentTimestamp()))); this->innerPutItem(std::make_shared(item), request); } diff --git a/services/blob/src/Reactors/server/GetReactor.h b/services/blob/src/Reactors/server/GetReactor.h --- a/services/blob/src/Reactors/server/GetReactor.h +++ b/services/blob/src/Reactors/server/GetReactor.h @@ -63,7 +63,7 @@ } void initialize() override { - this->s3Path = findS3Path(this->request.holder()); + this->s3Path = tools::findS3Path(this->request.holder()); this->fileSize = getBucket(s3Path.getBucketName()).getObjectSize(s3Path.getObjectName()); diff --git a/services/blob/src/Reactors/server/PutReactor.h b/services/blob/src/Reactors/server/PutReactor.h --- a/services/blob/src/Reactors/server/PutReactor.h +++ b/services/blob/src/Reactors/server/PutReactor.h @@ -49,7 +49,7 @@ grpc::Status::OK, true); } this->s3Path = std::make_unique( - generateS3Path(BLOB_BUCKET_NAME, this->blobHash)); + tools::generateS3Path(BLOB_BUCKET_NAME, this->blobHash)); this->blobItem = std::make_shared(this->blobHash, *s3Path); response->set_dataexists(false); diff --git a/services/blob/src/S3Tools.cpp b/services/blob/src/S3Tools.cpp --- a/services/blob/src/S3Tools.cpp +++ b/services/blob/src/S3Tools.cpp @@ -32,7 +32,7 @@ std::unique_ptr getS3Client() { Aws::Client::ClientConfiguration config; config.region = AWS_REGION; - if (isDevMode()) { + if (tools::isDevMode()) { config.endpointOverride = Aws::String("localstack:4566"); config.scheme = Aws::Http::Scheme::HTTP; return std::make_unique( diff --git a/services/blob/src/Tools.h b/services/blob/src/Tools.h --- a/services/blob/src/Tools.h +++ b/services/blob/src/Tools.h @@ -5,6 +5,7 @@ namespace comm { namespace network { +namespace tools { database::S3Path generateS3Path(const std::string &bucketName, const std::string &blobHash); @@ -22,5 +23,6 @@ } }; +} // namespace tools } // namespace network } // namespace comm diff --git a/services/blob/src/Tools.cpp b/services/blob/src/Tools.cpp --- a/services/blob/src/Tools.cpp +++ b/services/blob/src/Tools.cpp @@ -15,6 +15,7 @@ namespace comm { namespace network { +namespace tools { database::S3Path generateS3Path(const std::string &bucketName, const std::string &blobHash) { @@ -69,5 +70,6 @@ return result; } +} // namespace tools } // namespace network } // namespace comm diff --git a/services/blob/test/MultiPartUploadTest.cpp b/services/blob/test/MultiPartUploadTest.cpp --- a/services/blob/test/MultiPartUploadTest.cpp +++ b/services/blob/test/MultiPartUploadTest.cpp @@ -59,7 +59,8 @@ mpu.addPart(generateNByes(AWS_MULTIPART_UPLOAD_MINIMUM_CHUNK_SIZE)); mpu.addPart("xxx"); mpu.finishUpload(); - EXPECT_THROW(bucket->getObjectData(objectName), invalid_argument_error); + EXPECT_THROW( + bucket->getObjectData(objectName), tools::invalid_argument_error); EXPECT_EQ( bucket->getObjectSize(objectName), AWS_MULTIPART_UPLOAD_MINIMUM_CHUNK_SIZE + 3); diff --git a/services/lib/src/DynamoDBTools.cpp b/services/lib/src/DynamoDBTools.cpp --- a/services/lib/src/DynamoDBTools.cpp +++ b/services/lib/src/DynamoDBTools.cpp @@ -9,7 +9,7 @@ std::unique_ptr getDynamoDBClient() { Aws::Client::ClientConfiguration config; config.region = AWS_REGION; - if (isDevMode()) { + if (tools::isDevMode()) { config.endpointOverride = Aws::String("localstack:4566"); config.scheme = Aws::Http::Scheme::HTTP; } diff --git a/services/lib/src/GlobalTools.h b/services/lib/src/GlobalTools.h --- a/services/lib/src/GlobalTools.h +++ b/services/lib/src/GlobalTools.h @@ -5,6 +5,7 @@ namespace comm { namespace network { +namespace tools { const std::string ID_SEPARATOR = ":"; @@ -18,5 +19,6 @@ std::string generateUUID(); +} // namespace tools } // namespace network } // namespace comm diff --git a/services/lib/src/GlobalTools.cpp b/services/lib/src/GlobalTools.cpp --- a/services/lib/src/GlobalTools.cpp +++ b/services/lib/src/GlobalTools.cpp @@ -11,6 +11,7 @@ namespace comm { namespace network { +namespace tools { uint64_t getCurrentTimestamp() { using namespace std::chrono; @@ -42,5 +43,6 @@ return boost::uuids::to_string(random_generator()); } +} // namespace tools } // namespace network } // namespace comm diff --git a/services/tunnelbroker/src/Amqp/AmqpManager.cpp b/services/tunnelbroker/src/Amqp/AmqpManager.cpp --- a/services/tunnelbroker/src/Amqp/AmqpManager.cpp +++ b/services/tunnelbroker/src/Amqp/AmqpManager.cpp @@ -2,6 +2,7 @@ #include "ConfigManager.h" #include "Constants.h" #include "DeliveryBroker.h" +#include "GlobalTools.h" #include "Tools.h" #include diff --git a/services/tunnelbroker/src/Database/DatabaseManager.cpp b/services/tunnelbroker/src/Database/DatabaseManager.cpp --- a/services/tunnelbroker/src/Database/DatabaseManager.cpp +++ b/services/tunnelbroker/src/Database/DatabaseManager.cpp @@ -1,5 +1,6 @@ #include "DatabaseManager.h" #include "DynamoDBTools.h" +#include "GlobalTools.h" namespace comm { namespace network { diff --git a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp --- a/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp +++ b/services/tunnelbroker/src/Service/TunnelbrokerServiceImpl.cpp @@ -77,7 +77,7 @@ } const std::string signature = request->signature(); const std::string publicKey = request->publickey(); - const std::string newSessionID = generateUUID(); + const std::string newSessionID = tools::generateUUID(); try { sessionSignItem = database::DatabaseManager::getInstance().findSessionSignItem(deviceID); @@ -153,7 +153,7 @@ "No such session found. SessionID: " + sessionID); } const std::string clientDeviceID = sessionItem->getDeviceID(); - const std::string messageID = generateUUID(); + const std::string messageID = tools::generateUUID(); if (!AmqpManager::getInstance().send( messageID, clientDeviceID, diff --git a/services/tunnelbroker/src/Tools/Tools.h b/services/tunnelbroker/src/Tools/Tools.h --- a/services/tunnelbroker/src/Tools/Tools.h +++ b/services/tunnelbroker/src/Tools/Tools.h @@ -8,7 +8,6 @@ namespace tools { std::string generateRandomString(std::size_t length); -int64_t getCurrentTimestamp(); bool validateDeviceID(std::string deviceID); bool validateSessionID(std::string sessionID); void checkIfNotEmpty(std::string fieldName, std::string stringToCheck); diff --git a/services/tunnelbroker/src/Tools/Tools.cpp b/services/tunnelbroker/src/Tools/Tools.cpp --- a/services/tunnelbroker/src/Tools/Tools.cpp +++ b/services/tunnelbroker/src/Tools/Tools.cpp @@ -26,12 +26,6 @@ return random_string; } -int64_t getCurrentTimestamp() { - using namespace std::chrono; - return duration_cast(system_clock::now().time_since_epoch()) - .count(); -} - bool validateDeviceID(std::string deviceID) { try { static const std::regex deviceIDKeyserverRegexp("^ks:.*"); diff --git a/services/tunnelbroker/test/AmqpManagerTest.cpp b/services/tunnelbroker/test/AmqpManagerTest.cpp --- a/services/tunnelbroker/test/AmqpManagerTest.cpp +++ b/services/tunnelbroker/test/AmqpManagerTest.cpp @@ -48,7 +48,7 @@ } TEST_F(AmqpManagerTest, SentAndPopedMessagesAreSameOnGeneratedData) { - const std::string messageID = generateUUID(); + const std::string messageID = tools::generateUUID(); const std::string toDeviceID = "mobile:" + tools::generateRandomString(DEVICEID_CHAR_LENGTH); const std::string fromDeviceID = diff --git a/services/tunnelbroker/test/DatabaseManagerTest.cpp b/services/tunnelbroker/test/DatabaseManagerTest.cpp --- a/services/tunnelbroker/test/DatabaseManagerTest.cpp +++ b/services/tunnelbroker/test/DatabaseManagerTest.cpp @@ -67,7 +67,7 @@ TEST_F(DatabaseManagerTest, PutAndFoundMessageItemsGeneratedDataIsSame) { const database::MessageItem item( - generateUUID(), + tools::generateUUID(), "mobile:" + tools::generateRandomString(DEVICEID_CHAR_LENGTH), "web:" + tools::generateRandomString(DEVICEID_CHAR_LENGTH), tools::generateRandomString(256), @@ -134,7 +134,7 @@ TEST_F(DatabaseManagerTest, PutAndFoundDeviceSessionItemGeneratedDataIsSame) { const database::DeviceSessionItem item( - tools::generateUUID(), + tools::tools::generateUUID(), "mobile:" + tools::generateRandomString(DEVICEID_CHAR_LENGTH), tools::generateRandomString(451), tools::generateRandomString(64), diff --git a/services/tunnelbroker/test/DeliveryBrokerTest.cpp b/services/tunnelbroker/test/DeliveryBrokerTest.cpp --- a/services/tunnelbroker/test/DeliveryBrokerTest.cpp +++ b/services/tunnelbroker/test/DeliveryBrokerTest.cpp @@ -47,7 +47,7 @@ const std::string toDeviceID = "mobile:" + tools::generateRandomString(DEVICEID_CHAR_LENGTH); const DeliveryBrokerMessage message{ - .messageID = generateUUID(), + .messageID = tools::generateUUID(), .deliveryTag = static_cast(std::time(0)), .fromDeviceID = "mobile:" + tools::generateRandomString(DEVICEID_CHAR_LENGTH), diff --git a/services/tunnelbroker/test/ToolsTest.cpp b/services/tunnelbroker/test/ToolsTest.cpp --- a/services/tunnelbroker/test/ToolsTest.cpp +++ b/services/tunnelbroker/test/ToolsTest.cpp @@ -59,7 +59,7 @@ } TEST(ToolsTest, ValidateSessionIDReturnsTrueOnValidGeneratedSessionID) { - const std::string validSessionID = generateUUID(); + const std::string validSessionID = tools::generateUUID(); EXPECT_EQ(tools::validateSessionID(validSessionID), true) << "Valid generated sessionID \"" << validSessionID << "\" is invalid by the function";