SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
uid.cpp
Go to the documentation of this file.
1 // Copyright (C) 2017 F4EXB //
3 // written by Edouard Griffiths //
4 // //
5 // Object unique id calculator loosely inspired by MongoDB object id //
6 // //
7 // This program is free software; you can redistribute it and/or modify //
8 // it under the terms of the GNU General Public License as published by //
9 // the Free Software Foundation as version 3 of the License, or //
10 // (at your option) any later version. //
11 // //
12 // This program is distributed in the hope that it will be useful, //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
15 // GNU General Public License V3 for more details. //
16 // //
17 // You should have received a copy of the GNU General Public License //
18 // along with this program. If not, see <http://www.gnu.org/licenses/>. //
20 
21 #include <QDateTime>
22 #include <QTime>
23 #include <QCoreApplication>
24 #include <QHostInfo>
25 #include <QCryptographicHash>
26 
27 #if (defined _WIN32_) || (defined _MSC_VER)
28 #include <windows.h>
29 #else
30 #include <sys/time.h>
31 #endif
32 
33 #include "uid.h"
34 
36 {
37  QDateTime currentDateTime = QDateTime::currentDateTime();
38  uint64_t uid = currentDateTime.toTime_t();
39  uid *= 1000000UL; // make room for microseconds
40 
41 // Fallback to milliseconds:
42 // QTime timeNow = QTime::currentTime();
43 // uint64_t usecs = timeNow.msec() * 1000UL;
44 // uid += usecs;
45  uid += getCurrentMiroseconds();
46 
47  return uid;
48 }
49 
51 {
52  uint32_t uid = (QCoreApplication::applicationPid() % (1<<16));
53 
54  QString hostname = QHostInfo::localHostName();
55  QByteArray hashKey = QCryptographicHash::hash(hostname.toUtf8(), QCryptographicHash::Sha1);
56  uint32_t hashHost = 0;
57 
58  for (int i = 0; i < hashKey.size(); i++) {
59  char c = hashKey.at(i);
60  hashHost += (uint32_t) c;
61  }
62 
63  hashHost %= (1<<16);
64  uid += (hashHost<<16);
65 
66  return uid;
67 }
68 
70 {
71 #if (defined _WIN32_) || (defined _MSC_VER)
72  LARGE_INTEGER tickPerSecond;
73  LARGE_INTEGER tick; // a point in time
74 
75  // get the high resolution counter's accuracy
76  QueryPerformanceFrequency(&tickPerSecond);
77 
78  // what time is it ?
79  QueryPerformanceCounter(&tick);
80 
81  // and here we get the current microsecond! \o/
82  return (tick.QuadPart % tickPerSecond.QuadPart);
83 #else
84  struct timeval tv;
85  gettimeofday(&tv, 0);
86  return tv.tv_usec;
87 #endif
88 }
89 
static uint64_t getCurrentMiroseconds()
Definition: uid.cpp:69
unsigned int uint32_t
Definition: rtptypes_win.h:46
static uint64_t getNewObjectId()
Definition: uid.cpp:35
int32_t i
Definition: decimators.h:244
static uint32_t getNewInstanceId()
Definition: uid.cpp:50
unsigned __int64 uint64_t
Definition: rtptypes_win.h:48