mqttidgenerator.cpp 2.97 KB
/* Copyright (C) 2019
 *
 * This file is part of the osdev components suite
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 */
#include "mqttidgenerator.h"

// std
#include <chrono>
#include <mutex>
#include <thread>
#include <unistd.h>

// boost
#include <boost/uuid/uuid_generators.hpp>

#include "lockguard.h"
#include "commondefs.h"

namespace {

boost::uuids::basic_random_generator<boost::mt19937> createGenerator()
{
    static auto timeSeed = static_cast<boost::mt19937::result_type>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
    static auto pidSeed = static_cast<boost::mt19937::result_type>(getpid());
    static std::hash<std::thread::id> hasher;
    static auto threadSeed = static_cast<boost::mt19937::result_type>(hasher(std::this_thread::get_id()));
    static boost::mt19937 randomNumberGenerator;
    randomNumberGenerator.seed(timeSeed ^ pidSeed ^ threadSeed);
    return boost::uuids::basic_random_generator<boost::mt19937>(&randomNumberGenerator);
}

} // namespace

using namespace osdev::components::mqtt;

const MqttId mqttNamespace = boost::lexical_cast<boost::uuids::uuid>("9c0f3730-cc4f-49eb-ab5b-079bc5b0e481");

// static
MqttId MqttIdGenerator::generate()
{
    // From the boost design notes
    // Seeding is unqiue per random generator:
    // The boost::uuids::basic_random_generator class' default constructor seeds the random number generator
    //  with a SHA-1 hash of a number of different values including std::time(0), std::clock(), uninitialized data,
    //  value return from new unsigned int, etc..
    // Functions are reentrant:
    // All functions are re-entrant. Classes are as thread-safe as an int. That is an instance can not be shared
    //  between threads without proper synchronization.
    static auto uuidGenerator = createGenerator();
    static std::mutex generatorMutex;
    OSDEV_COMPONENTS_LOCKGUARD(generatorMutex);
    return uuidGenerator();
}

//static
MqttId MqttIdGenerator::nullId()
{
    return boost::uuids::nil_uuid();
}

// static
MqttId MqttIdGenerator::nameId(MqttId namespaceUuid, const std::string& name)
{
    boost::uuids::name_generator gen(namespaceUuid);
    return gen(name.c_str());
}

// static
MqttId MqttIdGenerator::nameId(const std::string& name)
{
    return MqttIdGenerator::nameId(mqttNamespace, name);
}