51becbde
Peter M. Groen
Committed the ent...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
/* 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);
}
|