/* 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 #include #include #include // boost #include #include "lockguard.h" #include "commondefs.h" namespace { boost::uuids::basic_random_generator createGenerator() { static auto timeSeed = static_cast(std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); static auto pidSeed = static_cast(getpid()); static std::hash hasher; static auto threadSeed = static_cast(hasher(std::this_thread::get_id())); static boost::mt19937 randomNumberGenerator; randomNumberGenerator.seed(timeSeed ^ pidSeed ^ threadSeed); return boost::uuids::basic_random_generator(&randomNumberGenerator); } } // namespace using namespace osdev::components::mqtt; const MqttId mqttNamespace = boost::lexical_cast("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); }