added UUID object, and updated UniqueId to generate a 64 bit id
based one a UUID
This commit is contained in:
parent
3d3006bdcc
commit
0520d5b516
10 changed files with 957 additions and 18 deletions
|
@ -21,7 +21,7 @@
|
|||
#
|
||||
#
|
||||
# Author : $Author: maroy $
|
||||
# Version : $Revision: 1.28 $
|
||||
# Version : $Revision: 1.29 $
|
||||
# Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/Makefile.in,v $
|
||||
#
|
||||
# @configure_input@
|
||||
|
@ -113,6 +113,7 @@ LDFLAGS = @LDFLAGS@ -pthread \
|
|||
# Dependencies
|
||||
#-------------------------------------------------------------------------------
|
||||
CORE_LIB_OBJS = ${TMP_DIR}/UniqueId.o \
|
||||
${TMP_DIR}/Uuid.o \
|
||||
${TMP_DIR}/Playable.o \
|
||||
${TMP_DIR}/AudioClip.o \
|
||||
${TMP_DIR}/FadeInfo.o \
|
||||
|
@ -132,6 +133,7 @@ CORE_LIB_OBJS = ${TMP_DIR}/UniqueId.o \
|
|||
${TMP_DIR}/MetadataTypeContainer.o
|
||||
|
||||
TEST_RUNNER_OBJS = ${TMP_DIR}/TestRunner.o \
|
||||
${TMP_DIR}/UuidTest.o \
|
||||
${TMP_DIR}/UniqueIdTest.o \
|
||||
${TMP_DIR}/AudioClipTest.o \
|
||||
${TMP_DIR}/FadeInfoTest.o \
|
||||
|
|
|
@ -21,7 +21,7 @@ dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|||
dnl
|
||||
dnl
|
||||
dnl Author : $Author: maroy $
|
||||
dnl Version : $Revision: 1.10 $
|
||||
dnl Version : $Revision: 1.11 $
|
||||
dnl Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/configure.ac,v $
|
||||
dnl-----------------------------------------------------------------------------
|
||||
|
||||
|
@ -35,14 +35,20 @@ dnl-----------------------------------------------------------------------------
|
|||
AC_INIT(Core, 1.0, bugs@campware.org)
|
||||
AC_PREREQ(2.59)
|
||||
AC_COPYRIGHT([Copyright (c) 2004 Media Development Loan Fund under the GNU GPL])
|
||||
AC_REVISION($Revision: 1.10 $)
|
||||
AC_REVISION($Revision: 1.11 $)
|
||||
|
||||
AC_CONFIG_SRCDIR(../src/UniqueId.cxx)
|
||||
|
||||
AC_CONFIG_HEADERS(configure.h)
|
||||
AC_PROG_CXX()
|
||||
|
||||
AC_CHECK_HEADERS(getopt.h sys/time.h time.h sys/types.h pwd.h errno.h)
|
||||
AC_CHECK_TYPES([uint64_t, uint32_t, uint16_t, uint8_t],
|
||||
HAVE_INTEGRAL_TYPES="yes", HAVE_INTEGRAL_TYPES="no")
|
||||
if test "x${HAVE_INTEGRAL_TYPES}" = "xno" ; then
|
||||
AC_MSG_ERROR([a required integral type is not available])
|
||||
fi
|
||||
|
||||
AC_CHECK_HEADERS(getopt.h sys/time.h time.h sys/types.h pwd.h errno.h stdint.h)
|
||||
|
||||
PKG_CHECK_MODULES(LIBXMLPP,[libxml++-2.6 >= 2.6.0])
|
||||
AC_SUBST(LIBXMLPP_CFLAGS)
|
||||
|
|
|
@ -72,8 +72,8 @@ documentation and/or software.
|
|||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
Author : $Author: fgerlits $
|
||||
Version : $Revision: 1.1 $
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.2 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/Md5.h,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
|
@ -87,6 +87,17 @@ documentation and/or software.
|
|||
|
||||
/* ============================================================ include files */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "configure.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_STDINT_H
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#error need stdint.h
|
||||
#endif
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
@ -118,8 +129,8 @@ namespace Core {
|
|||
* on the basis of the original C code by RSA Data Security, Inc. See the
|
||||
* header of the source file for further information.
|
||||
*
|
||||
* @author $Author: fgerlits $
|
||||
* @version $Revision: 1.1 $
|
||||
* @author $Author: maroy $
|
||||
* @version $Revision: 1.2 $
|
||||
*/
|
||||
class Md5
|
||||
{
|
||||
|
@ -144,6 +155,16 @@ class Md5
|
|||
uint1 digest[16];
|
||||
uint1 finalized;
|
||||
|
||||
/**
|
||||
* The low 64 bits of the checksum.
|
||||
*/
|
||||
uint64_t low64;
|
||||
|
||||
/**
|
||||
* The high 64 bits of the checksum.
|
||||
*/
|
||||
uint64_t high64;
|
||||
|
||||
// last, the private methods, mostly static:
|
||||
void init (); // called by all constructors
|
||||
void transform (uint1 *buffer); // does the real update work. Note
|
||||
|
@ -168,6 +189,13 @@ class Md5
|
|||
static inline void II (uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
|
||||
uint4 s, uint4 ac);
|
||||
|
||||
/**
|
||||
* Calculate the lower and higher 64 bit values for the checksum
|
||||
*/
|
||||
void
|
||||
calcNumericRepresentation(void) throw ();
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
|
@ -199,6 +227,27 @@ class Md5
|
|||
*/
|
||||
operator std::string () throw();
|
||||
|
||||
/**
|
||||
* Return the lower 64 bits of the checksum.
|
||||
*
|
||||
* @return the lower 64 bits of the checksum.
|
||||
*/
|
||||
uint64_t
|
||||
low64bits(void) const throw ()
|
||||
{
|
||||
return low64;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the higher 64 bits of the checksum.
|
||||
*
|
||||
* @return the higher 64 bits of the checksum.
|
||||
*/
|
||||
uint64_t
|
||||
high64bits(void) const throw ()
|
||||
{
|
||||
return high64;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
320
livesupport/modules/core/include/LiveSupport/Core/Uuid.h
Normal file
320
livesupport/modules/core/include/LiveSupport/Core/Uuid.h
Normal file
|
@ -0,0 +1,320 @@
|
|||
/*------------------------------------------------------------------------------
|
||||
|
||||
Copyright (c) 2004 Media Development Loan Fund
|
||||
|
||||
This file is part of the LiveSupport project.
|
||||
http://livesupport.campware.org/
|
||||
To report bugs, send an e-mail to bugs@campware.org
|
||||
|
||||
LiveSupport 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 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
LiveSupport 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 LiveSupport; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
This code is based upon the Leach working draft for UUIDs,
|
||||
http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
|
||||
and the sample code therein.
|
||||
The original copyright message of the sample code is the following:
|
||||
|
||||
** Copyright (c) 1990- 1993, 1996 Open Software Foundation, Inc.
|
||||
** Copyright (c) 1989 by Hewlett-Packard Company, Palo Alto, Ca. &
|
||||
** Digital Equipment Corporation, Maynard, Mass.
|
||||
** Copyright (c) 1998 Microsoft.
|
||||
** To anyone who acknowledges that this file is provided "AS IS"
|
||||
** without any express or implied warranty: permission to use, copy,
|
||||
** modify, and distribute this file for any purpose is hereby
|
||||
** granted without fee, provided that the above copyright notices and
|
||||
** this notice appears in all source code copies, and that none of
|
||||
** the names of Open Software Foundation, Inc., Hewlett-Packard
|
||||
** Company, or Digital Equipment Corporation be used in advertising
|
||||
** or publicity pertaining to distribution of the software without
|
||||
** specific, written prior permission. Neither Open Software
|
||||
** Foundation, Inc., Hewlett-Packard Company, Microsoft, nor Digital Equipment
|
||||
** Corporation makes any representations about the suitability of
|
||||
** this software for any purpose.
|
||||
|
||||
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.1 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/Uuid.h,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
#ifndef LiveSupport_Core_Uuid_h
|
||||
#define LiveSupport_Core_Uuid_h
|
||||
|
||||
#ifndef __cplusplus
|
||||
#error This is a C++ include file
|
||||
#endif
|
||||
|
||||
|
||||
/* ============================================================ include files */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "configure.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_STDINT_H
|
||||
#include <stdint.h>
|
||||
#else
|
||||
#error need stdint.h
|
||||
#endif
|
||||
|
||||
|
||||
#include <string>
|
||||
#include "LiveSupport/Core/Ptr.h"
|
||||
|
||||
namespace LiveSupport {
|
||||
namespace Core {
|
||||
|
||||
/* ================================================================ constants */
|
||||
|
||||
|
||||
/* =================================================================== macros */
|
||||
|
||||
|
||||
/* =============================================================== data types */
|
||||
|
||||
/**
|
||||
* A class representing globally unique identifiers.
|
||||
* This implementation is based on the Leach UUID/GUID draft:
|
||||
* http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
|
||||
*
|
||||
* @author $Author: maroy $
|
||||
* @version $Revision: 1.1 $
|
||||
* @see http://www.opengroup.org/dce/info/draft-leach-uuids-guids-01.txt
|
||||
*/
|
||||
class Uuid
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* The raw time type
|
||||
*/
|
||||
typedef uint64_t UuidTime;
|
||||
|
||||
/**
|
||||
* Structure holding a 6-byte system node id.
|
||||
*/
|
||||
typedef struct {
|
||||
char nodeId[6];
|
||||
} UuidNode;
|
||||
|
||||
/**
|
||||
* The value of the id.
|
||||
*/
|
||||
long long int id;
|
||||
|
||||
/**
|
||||
* A string representation of the id, in hexadecimal notation.
|
||||
*/
|
||||
std::string idAsString;
|
||||
|
||||
/**
|
||||
* The low part of time.
|
||||
*/
|
||||
uint32_t timeLow;
|
||||
|
||||
/**
|
||||
* The middle part of time.
|
||||
*/
|
||||
uint16_t timeMid;
|
||||
|
||||
/**
|
||||
* The high part of time, with version included.
|
||||
*/
|
||||
uint16_t timeHiAndVersion;
|
||||
|
||||
/**
|
||||
* Clock sequence number high and reserved parts.
|
||||
*/
|
||||
uint8_t clockSeqHiAndReserved;
|
||||
|
||||
/**
|
||||
* Clock sequenc number, low part.
|
||||
*/
|
||||
uint8_t clockSeqLow;
|
||||
|
||||
/**
|
||||
* The 6 byte system node id.
|
||||
*/
|
||||
uint8_t node[6];
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
Uuid(void) throw ()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two UUIDs
|
||||
*
|
||||
* @param id1 one id to compare
|
||||
* @param id2 the other id to compare
|
||||
* @return true if they are equal, false otherwise
|
||||
*/
|
||||
static bool
|
||||
compare(const Uuid & id1,
|
||||
const Uuid & id2) throw ();
|
||||
|
||||
/**
|
||||
* Return the current time, but always return a different value,
|
||||
* even if the system clock granularity is not fine enough.
|
||||
*
|
||||
* @param timestamp an out parameter for the current time.
|
||||
*/
|
||||
static void
|
||||
getCurrentTime(UuidTime * timestamp) throw ();
|
||||
|
||||
/**
|
||||
* Try to generate a truely random number (which is not possible,
|
||||
* of course).
|
||||
*
|
||||
* @return a random number
|
||||
*/
|
||||
static uint16_t
|
||||
trueRandom(void) throw ();
|
||||
|
||||
/**
|
||||
* Read the last saved state of the UUID generator from
|
||||
* non-volatile storage.
|
||||
*
|
||||
* @param clockSeq out parameter for the clock sequence
|
||||
* @param timestamp out parameter for the timestamp
|
||||
* @param node out parameter for the system node id
|
||||
*/
|
||||
int
|
||||
readState(uint16_t * clockSeq,
|
||||
UuidTime * timestamp,
|
||||
UuidNode * node) throw ();
|
||||
|
||||
/**
|
||||
* Write the last saved state of the UUID generator to
|
||||
* non-volatile storage.
|
||||
*
|
||||
* @param clockSeq the clock sequence
|
||||
* @param timestamp the timestamp
|
||||
* @param node the system node id
|
||||
*/
|
||||
void
|
||||
writeState(uint16_t clockSeq,
|
||||
UuidTime timestamp,
|
||||
UuidNode node) throw ();
|
||||
|
||||
/**
|
||||
* Generate a type 1 UUID.
|
||||
* This function will fill out the object's internal attributes
|
||||
* for it to become a proper UUID.
|
||||
*
|
||||
* For UUID types, see
|
||||
* http://www.webdav.org/specs/draft-leach-uuids-guids-01.txt
|
||||
*
|
||||
* @param clockSeq the clock sequence
|
||||
* @param timestamp the time stamp
|
||||
* @param node the system node id
|
||||
*/
|
||||
void
|
||||
format(uint16_t clockSeq,
|
||||
UuidTime timestamp,
|
||||
UuidNode node) throw ();
|
||||
|
||||
/**
|
||||
* Get the system time in the UUID UTC base time,
|
||||
* which is October 15, 1582.
|
||||
*
|
||||
* @param uuidTime an out parameter for the time.
|
||||
*/
|
||||
static void
|
||||
getSystemTime(UuidTime * uuidTime) throw ();
|
||||
|
||||
/**
|
||||
* Return the IEEE node id.
|
||||
*
|
||||
* @param node an out parameter for the node id.
|
||||
*/
|
||||
static void
|
||||
getIeeeNodeIdentifier(UuidNode * node) throw ();
|
||||
|
||||
/**
|
||||
* Create a string representation of the UUID.
|
||||
* Store the result in the idAsString attribute.
|
||||
*/
|
||||
void
|
||||
representAsString(void) throw ();
|
||||
|
||||
|
||||
public:
|
||||
/**
|
||||
* Generate a globally unique id. This is used for testing.
|
||||
* In real life, unique IDs are generated by the storage server.
|
||||
*/
|
||||
static Ptr<Uuid>::Ref
|
||||
generateId(void) throw ();
|
||||
|
||||
/**
|
||||
* Compare this is with an other one.
|
||||
*
|
||||
* @param otherId the other unqiue id to compare to.
|
||||
* @return true if this an otherId have the same ID value,
|
||||
* false otherwise.
|
||||
*/
|
||||
bool
|
||||
operator==(const Uuid & otherId) const throw ()
|
||||
{
|
||||
return compare(*this, otherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare this is with an other one.
|
||||
*
|
||||
* @param otherId the other unqiue id to compare to.
|
||||
* @return true if this an otherId do not have the same ID value,
|
||||
* false otherwise.
|
||||
*/
|
||||
bool
|
||||
operator!=(const Uuid & otherId) const throw ()
|
||||
{
|
||||
return !compare(*this, otherId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the string value of this globally unique id.
|
||||
*
|
||||
* @return the string value of this id.
|
||||
*/
|
||||
operator std::string() const throw ()
|
||||
{
|
||||
return idAsString;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* ================================================= external data structures */
|
||||
|
||||
|
||||
/* ====================================================== function prototypes */
|
||||
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os,
|
||||
const Uuid & id)
|
||||
{
|
||||
os << (std::string) id;
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
} // namespace Core
|
||||
} // namespace LiveSupport
|
||||
|
||||
#endif // LiveSupport_Core_Uuid_h
|
||||
|
|
@ -72,8 +72,8 @@ documentation and/or software.
|
|||
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
Author : $Author: fgerlits $
|
||||
Version : $Revision: 1.2 $
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.3 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/Md5.cxx,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
|
@ -108,6 +108,7 @@ Md5::Md5(const std::string &s) throw(std::invalid_argument)
|
|||
init();
|
||||
update (s);
|
||||
finalize();
|
||||
calcNumericRepresentation();
|
||||
}
|
||||
|
||||
|
||||
|
@ -119,6 +120,7 @@ Md5::Md5(std::istream& stream) throw(std::invalid_argument)
|
|||
init();
|
||||
update (stream);
|
||||
finalize();
|
||||
calcNumericRepresentation();
|
||||
}
|
||||
|
||||
|
||||
|
@ -130,6 +132,7 @@ Md5::Md5(FILE *file) throw(std::invalid_argument)
|
|||
init();
|
||||
update(file);
|
||||
finalize ();
|
||||
calcNumericRepresentation();
|
||||
}
|
||||
|
||||
|
||||
|
@ -149,6 +152,32 @@ std::string Md5::hexDigest() throw()
|
|||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Calculate the numeric representation of the checksum
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
Md5 :: calcNumericRepresentation(void) throw ()
|
||||
{
|
||||
low64 = (uint64_t) digest[15];
|
||||
low64 |= ((uint64_t) digest[14]) << 8;
|
||||
low64 |= ((uint64_t) digest[13]) << 16;
|
||||
low64 |= ((uint64_t) digest[12]) << 24;
|
||||
low64 |= ((uint64_t) digest[11]) << 32;
|
||||
low64 |= ((uint64_t) digest[10]) << 40;
|
||||
low64 |= ((uint64_t) digest[9]) << 48;
|
||||
low64 |= ((uint64_t) digest[8]) << 56;
|
||||
|
||||
high64 = (uint64_t) digest[7];
|
||||
high64 |= ((uint64_t) digest[6]) << 8;
|
||||
high64 |= ((uint64_t) digest[5]) << 16;
|
||||
high64 |= ((uint64_t) digest[4]) << 24;
|
||||
high64 |= ((uint64_t) digest[3]) << 32;
|
||||
high64 |= ((uint64_t) digest[2]) << 40;
|
||||
high64 |= ((uint64_t) digest[1]) << 48;
|
||||
high64 |= ((uint64_t) digest[0]) << 56;
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Convert the md5 sum to a hexadecimal string.
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
@ -559,3 +588,6 @@ inline void Md5::II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
|
|||
a = rotate_left (a, s) +b;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Author : $Author: fgerlits $
|
||||
Version : $Revision: 1.1 $
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.2 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/Md5Test.cxx,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
|
@ -99,6 +99,8 @@ Md5Test :: firstTest(void)
|
|||
|
||||
std::string s = someString;
|
||||
CPPUNIT_ASSERT(s == "9007a3599f5d3ae2ac11a29308f964eb");
|
||||
CPPUNIT_ASSERT(someString.low64bits() == 0xac11a29308f964ebLL);
|
||||
CPPUNIT_ASSERT(someString.high64bits() == 0x9007a3599f5d3ae2LL);
|
||||
|
||||
// test the construction from a FILE*
|
||||
FILE *f = fopen("src/Md5Test.h", "r");
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Author : $Author: fgerlits $
|
||||
Version : $Revision: 1.2 $
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.3 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/UniqueId.cxx,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
|
@ -35,6 +35,8 @@
|
|||
|
||||
#include <cstdlib>
|
||||
|
||||
#include "LiveSupport/Core/Uuid.h"
|
||||
#include "LiveSupport/Core/Md5.h"
|
||||
#include "LiveSupport/Core/UniqueId.h"
|
||||
|
||||
|
||||
|
@ -57,10 +59,12 @@ using namespace LiveSupport::Core;
|
|||
Ptr<UniqueId>::Ref
|
||||
UniqueId :: generateId(void) throw ()
|
||||
{
|
||||
// TODO: implement this properly, e.g. use a GUID pattern
|
||||
// one example is the algorithm found in xdoclet for GUID
|
||||
|
||||
Ptr<UniqueId>::Ref id(new UniqueId(rand()));
|
||||
Ptr<Uuid>::Ref uuid = Uuid::generateId();
|
||||
// as uuid is 128 bits, but we have only 64 bits, create an md5 hash
|
||||
// (which is still 128 bits), and use its values to create a 64 value
|
||||
// hopefully this is unique enough
|
||||
Md5 md5((std::string)*uuid);
|
||||
Ptr<UniqueId>::Ref id(new UniqueId(md5.high64bits() + md5.low64bits()));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
|
318
livesupport/modules/core/src/Uuid.cxx
Normal file
318
livesupport/modules/core/src/Uuid.cxx
Normal file
|
@ -0,0 +1,318 @@
|
|||
/*------------------------------------------------------------------------------
|
||||
|
||||
Copyright (c) 2004 Media Development Loan Fund
|
||||
|
||||
This file is part of the LiveSupport project.
|
||||
http://livesupport.campware.org/
|
||||
To report bugs, send an e-mail to bugs@campware.org
|
||||
|
||||
LiveSupport 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 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
LiveSupport 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 LiveSupport; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.1 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/Uuid.cxx,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
|
||||
/* ============================================================ include files */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "configure.h"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#error need sys/time.h
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#error need unistd.h
|
||||
#endif
|
||||
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include "LiveSupport/Core/Uuid.h"
|
||||
|
||||
|
||||
using namespace LiveSupport::Core;
|
||||
|
||||
/* =================================================== local data structures */
|
||||
|
||||
|
||||
/* ================================================ local constants & macros */
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Set the following to the number of 100ns ticks of the actual
|
||||
* resolution of your system's clock
|
||||
*----------------------------------------------------------------------------*/
|
||||
#define UUIDS_PER_TICK 1024
|
||||
|
||||
|
||||
/* =============================================== local function prototypes */
|
||||
|
||||
|
||||
/* ============================================================= module code */
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Generate a globally unique id.
|
||||
*----------------------------------------------------------------------------*/
|
||||
Ptr<Uuid>::Ref
|
||||
Uuid :: generateId(void) throw ()
|
||||
{
|
||||
Ptr<Uuid>::Ref id(new Uuid());
|
||||
|
||||
UuidTime timestamp;
|
||||
UuidTime lastTime;
|
||||
uint16_t clockseq;
|
||||
UuidNode node;
|
||||
UuidNode lastNode;
|
||||
int f;
|
||||
|
||||
/* acquire system wide lock so we're alone */
|
||||
//LOCK;
|
||||
|
||||
/* get current time */
|
||||
getCurrentTime(×tamp);
|
||||
|
||||
/* get node ID */
|
||||
getIeeeNodeIdentifier(&node);
|
||||
|
||||
/* get saved state from NV storage */
|
||||
f = id->readState(&clockseq, &lastTime, &lastNode);
|
||||
|
||||
/* if no NV state, or if clock went backwards, or node ID changed
|
||||
(e.g., net card swap) change clockseq */
|
||||
if (!f || memcmp(&node, &lastNode, sizeof(UuidNode))) {
|
||||
clockseq = trueRandom();
|
||||
} else if (timestamp < lastTime) {
|
||||
clockseq++;
|
||||
}
|
||||
|
||||
/* stuff fields into the UUID */
|
||||
id->format(clockseq, timestamp, node);
|
||||
id->representAsString();
|
||||
|
||||
/* save the state for next time */
|
||||
id->writeState(clockseq, timestamp, node);
|
||||
|
||||
//UNLOCK;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Format the UUID
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
Uuid :: format(uint16_t clockSeq,
|
||||
UuidTime timestamp,
|
||||
UuidNode node) throw ()
|
||||
{
|
||||
/* Construct a version 1 uuid with the information we've gathered
|
||||
* plus a few constants. */
|
||||
timeLow = (unsigned long)(timestamp & 0xFFFFFFFF);
|
||||
timeMid = (unsigned short)((timestamp >> 32) & 0xFFFF);
|
||||
timeHiAndVersion = (unsigned short)((timestamp >> 48) & 0x0FFF);
|
||||
timeHiAndVersion |= (1 << 12);
|
||||
clockSeqLow = clockSeq & 0xFF;
|
||||
clockSeqHiAndReserved = (clockSeq & 0x3F00) >> 8;
|
||||
clockSeqHiAndReserved |= 0x80;
|
||||
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
this->node[i] = node.nodeId[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Create a string representation of the UUID
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
Uuid :: representAsString(void) throw ()
|
||||
{
|
||||
std::stringstream sstr;
|
||||
|
||||
sstr << std::hex << std::setw(8) << std::setfill('0') << timeLow << '-'
|
||||
<< std::hex << std::setw(4) << std::setfill('0') << timeMid << '-'
|
||||
<< std::hex << std::setw(4) << std::setfill('0')
|
||||
<< timeHiAndVersion << '-'
|
||||
<< std::hex << std::setw(2) << std::setfill('0')
|
||||
<< (unsigned short) clockSeqHiAndReserved << '-'
|
||||
<< std::hex << std::setw(2) << std::setfill('0')
|
||||
<< (unsigned short) clockSeqLow << '-';
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
sstr << std::hex << std::setw(2) << std::setfill('0')
|
||||
<< (unsigned short) this->node[i];
|
||||
}
|
||||
|
||||
idAsString = sstr.str();
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Read the current state from non-volatile storage
|
||||
*----------------------------------------------------------------------------*/
|
||||
int
|
||||
Uuid :: readState(uint16_t * clockSeq,
|
||||
UuidTime * timestamp,
|
||||
UuidNode * node) throw ()
|
||||
{
|
||||
// TODO: read the state from non-volatile storage
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Write the current state to non-volatile storage
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
Uuid :: writeState(uint16_t clockSeq,
|
||||
UuidTime timestamp,
|
||||
UuidNode node) throw ()
|
||||
{
|
||||
// TODO: write the current state to non-volatile storage
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Get the current time into a timestamp
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
Uuid :: getCurrentTime(UuidTime *timestamp) throw ()
|
||||
{
|
||||
UuidTime timeNow;
|
||||
static UuidTime timeLast;
|
||||
static uint16_t uuidsThisTick;
|
||||
static bool inited = false;
|
||||
|
||||
if (!inited) {
|
||||
getSystemTime(&timeNow);
|
||||
uuidsThisTick = UUIDS_PER_TICK;
|
||||
inited = true;
|
||||
};
|
||||
|
||||
while (true) {
|
||||
getSystemTime(&timeNow);
|
||||
|
||||
/* if clock reading changed since last UUID generated... */
|
||||
if (timeLast != timeNow) {
|
||||
/* reset count of uuids gen'd with this clock reading */
|
||||
uuidsThisTick = 0;
|
||||
break;
|
||||
};
|
||||
if (uuidsThisTick < UUIDS_PER_TICK) {
|
||||
uuidsThisTick++;
|
||||
break;
|
||||
};
|
||||
/* going too fast for our clock; spin */
|
||||
};
|
||||
|
||||
/* add the count of uuids to low order bits of the clock reading */
|
||||
*timestamp = timeNow + uuidsThisTick;
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Get the system time in the UUID UTC base time, which is October 15, 1582
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
Uuid :: getSystemTime(UuidTime * uuidTime) throw ()
|
||||
{
|
||||
struct timeval tp;
|
||||
|
||||
gettimeofday(&tp, (struct timezone *)0);
|
||||
|
||||
/* Offset between UUID formatted times and Unix formatted times.
|
||||
UUID UTC base time is October 15, 1582.
|
||||
Unix base time is January 1, 1970.
|
||||
*/
|
||||
*uuidTime = (tp.tv_sec * 10000000)
|
||||
+ (tp.tv_usec * 10)
|
||||
+ 0x01B21DD213814000LL;
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Get the IEEE node identifier
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
Uuid :: getIeeeNodeIdentifier(UuidNode * node) throw ()
|
||||
{
|
||||
long hostId = gethostid();
|
||||
|
||||
node->nodeId[5] = (char) (hostId & 0x0000000000ffL);
|
||||
node->nodeId[4] = (char) ((hostId & 0x00000000ff00L) >> 8);
|
||||
node->nodeId[3] = (char) ((hostId & 0x000000ff0000L) >> 16);
|
||||
node->nodeId[2] = (char) ((hostId & 0x0000ff000000L) >> 24);
|
||||
// these will be 0, as the returned node is only 32 bits
|
||||
node->nodeId[1] = 0;
|
||||
node->nodeId[0] = 0;
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Generate a random number
|
||||
*----------------------------------------------------------------------------*/
|
||||
uint16_t
|
||||
Uuid :: trueRandom(void) throw ()
|
||||
{
|
||||
static bool inited = false;
|
||||
UuidTime timeNow;
|
||||
|
||||
if (!inited) {
|
||||
getSystemTime(&timeNow);
|
||||
timeNow = timeNow/UUIDS_PER_TICK;
|
||||
srand((unsigned int)(((timeNow >> 32) ^ timeNow)&0xffffffff));
|
||||
inited = true;
|
||||
};
|
||||
|
||||
return rand();
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Compare two ids.
|
||||
*----------------------------------------------------------------------------*/
|
||||
bool
|
||||
Uuid :: compare(const Uuid & id1,
|
||||
const Uuid & id2) throw ()
|
||||
{
|
||||
if (!(id1.timeLow == id2.timeLow
|
||||
&& id1.timeMid == id2.timeMid
|
||||
&& id1.timeHiAndVersion == id2.timeHiAndVersion
|
||||
&& id1.clockSeqHiAndReserved == id2.clockSeqHiAndReserved
|
||||
&& id1.clockSeqLow == id2.clockSeqLow)) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
if (id1.node[i] != id2.node[i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
99
livesupport/modules/core/src/UuidTest.cxx
Normal file
99
livesupport/modules/core/src/UuidTest.cxx
Normal file
|
@ -0,0 +1,99 @@
|
|||
/*------------------------------------------------------------------------------
|
||||
|
||||
Copyright (c) 2004 Media Development Loan Fund
|
||||
|
||||
This file is part of the LiveSupport project.
|
||||
http://livesupport.campware.org/
|
||||
To report bugs, send an e-mail to bugs@campware.org
|
||||
|
||||
LiveSupport 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 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
LiveSupport 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 LiveSupport; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.1 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/UuidTest.cxx,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
|
||||
/* ============================================================ include files */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "configure.h"
|
||||
#endif
|
||||
|
||||
#if HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#else
|
||||
#error "Need unistd.h"
|
||||
#endif
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "LiveSupport/Core/Uuid.h"
|
||||
#include "UuidTest.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace LiveSupport::Core;
|
||||
|
||||
/* =================================================== local data structures */
|
||||
|
||||
|
||||
/* ================================================ local constants & macros */
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(UuidTest);
|
||||
|
||||
|
||||
/* =============================================== local function prototypes */
|
||||
|
||||
|
||||
/* ============================================================= module code */
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Set up the test environment
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
UuidTest :: setUp(void) throw ()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Clean up the test environment
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
UuidTest :: tearDown(void) throw ()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Test to see if the singleton Hello object is accessible
|
||||
*----------------------------------------------------------------------------*/
|
||||
void
|
||||
UuidTest :: firstTest(void)
|
||||
throw (CPPUNIT_NS::Exception)
|
||||
{
|
||||
Ptr<Uuid>::Ref id1;
|
||||
Ptr<Uuid>::Ref id2;
|
||||
|
||||
id1 = Uuid::generateId();
|
||||
id2 = Uuid::generateId();
|
||||
|
||||
CPPUNIT_ASSERT(*id1 != *id2);
|
||||
}
|
||||
|
107
livesupport/modules/core/src/UuidTest.h
Normal file
107
livesupport/modules/core/src/UuidTest.h
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*------------------------------------------------------------------------------
|
||||
|
||||
Copyright (c) 2004 Media Development Loan Fund
|
||||
|
||||
This file is part of the LiveSupport project.
|
||||
http://livesupport.campware.org/
|
||||
To report bugs, send an e-mail to bugs@campware.org
|
||||
|
||||
LiveSupport 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 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
LiveSupport 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 LiveSupport; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
|
||||
Author : $Author: maroy $
|
||||
Version : $Revision: 1.1 $
|
||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/UuidTest.h,v $
|
||||
|
||||
------------------------------------------------------------------------------*/
|
||||
#ifndef UuidTest_h
|
||||
#define UuidTest_h
|
||||
|
||||
#ifndef __cplusplus
|
||||
#error This is a C++ include file
|
||||
#endif
|
||||
|
||||
|
||||
/* ============================================================ include files */
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "configure.h"
|
||||
#endif
|
||||
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
|
||||
namespace LiveSupport {
|
||||
namespace Core {
|
||||
|
||||
/* ================================================================ constants */
|
||||
|
||||
|
||||
/* =================================================================== macros */
|
||||
|
||||
|
||||
/* =============================================================== data types */
|
||||
|
||||
/**
|
||||
* Unit test for the Uuid class.
|
||||
*
|
||||
* @author $Author: maroy $
|
||||
* @version $Revision: 1.1 $
|
||||
* @see Uuid
|
||||
*/
|
||||
class UuidTest : public CPPUNIT_NS::TestFixture
|
||||
{
|
||||
CPPUNIT_TEST_SUITE(UuidTest);
|
||||
CPPUNIT_TEST(firstTest);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
protected:
|
||||
|
||||
/**
|
||||
* A simple test.
|
||||
*
|
||||
* @exception CPPUNIT_NS::Exception on test failures.
|
||||
*/
|
||||
void
|
||||
firstTest(void) throw (CPPUNIT_NS::Exception);
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Set up the environment for the test case.
|
||||
*/
|
||||
void
|
||||
setUp(void) throw ();
|
||||
|
||||
/**
|
||||
* Clean up the environment after the test case.
|
||||
*/
|
||||
void
|
||||
tearDown(void) throw ();
|
||||
};
|
||||
|
||||
|
||||
/* ================================================= external data structures */
|
||||
|
||||
|
||||
/* ====================================================== function prototypes */
|
||||
|
||||
|
||||
} // namespace Core
|
||||
} // namespace LiveSupport
|
||||
|
||||
#endif // UuidTest_h
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue