You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/08/05 23:05:30 UTC

svn commit: r562959 - in /activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util: UUID.cpp UUID.h

Author: tabish
Date: Sun Aug  5 14:05:27 2007
New Revision: 562959

URL: http://svn.apache.org/viewvc?view=rev&rev=562959
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103

Working on the UUID impl.

Modified:
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.h

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.cpp?view=diff&rev=562959&r1=562958&r2=562959
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.cpp Sun Aug  5 14:05:27 2007
@@ -28,6 +28,12 @@
 
     memcpy( &apr_uuid.data[0], &leastSigBits, sizeof( long long ) );
     memcpy( &apr_uuid.data[sizeof(long long)], &mostSigBits, sizeof(long long ) );
+
+    this->mostSigBits = mostSigBits;
+    this->leastSigBits = leastSigBits;
+
+    // Version indicator, set when a UUID is generated
+    this->uuidVersion = (int)( mostSigBits & 0x000000000000F000LL ) >> 12;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -79,37 +85,95 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 long long UUID::node() throw ( lang::exceptions::UnsupportedOperationException ) {
-    return 0; //TODO
+
+    if( this->version() != 1 ) {
+        throw exceptions::UnsupportedOperationException(
+            __FILE__, __LINE__,
+            "UUID::node - Only a Version 1 UUID supports this operation." );
+    }
+
+    return ( this->leastSigBits & 0x0000FFFFFFFFFFFFULL );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 long long UUID::timestamp() throw ( lang::exceptions::UnsupportedOperationException ) {
-    return 0; //TODO
+
+    if( this->version() != 1 ) {
+        throw exceptions::UnsupportedOperationException(
+            __FILE__, __LINE__,
+            "UUID::node - Only a Version 1 UUID supports this operation." );
+    }
+
+    // Mask out the version and shift values around to make time.
+    long long timeLow  = ( mostSigBits & 0xFFFFFFFF00000000ULL) >> 32;
+    long long timeMid  = ( mostSigBits & 0x00000000FFFF0000ULL) << 16;
+    long long timeHigh = ( mostSigBits & 0x0000000000000FFFULL) << 48;
+
+    return timeLow | timeMid | timeHigh;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-long long UUID::clockSequence() throw ( lang::exceptions::UnsupportedOperationException ) {
-    return 0; //TODO
+int UUID::clockSequence() throw ( lang::exceptions::UnsupportedOperationException ) {
+
+    if( this->version() != 1 ) {
+        throw exceptions::UnsupportedOperationException(
+            __FILE__, __LINE__,
+            "UUID::node - Only a Version 1 UUID supports this operation." );
+    }
+
+    return (int)( ( this->leastSigBits & 0x3FFF000000000000ULL ) >> 48 );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-long long UUID::variant() throw ( lang::exceptions::UnsupportedOperationException ) {
-    return 0; //TODO
+int UUID::variant() throw ( lang::exceptions::UnsupportedOperationException ) {
+
+    // determine variant field
+    if( ( this->leastSigBits & 0x8000000000000000ULL ) == 0 ) {
+        // MSB0 not set, NCS backwards compatibility variant
+        return 0;
+    } else if( ( this->leastSigBits & 0x4000000000000000ULL ) != 0 ) {
+        // MSB1 set, either MS reserved or future reserved
+        return (int)( ( this->leastSigBits & 0xE000000000000000ULL ) >> 61 );
+    }
+
+    // MSB1 not set, RFC 4122 variant
+    return 2;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-long long UUID::version() throw ( lang::exceptions::UnsupportedOperationException ) {
-    return 0; //TODO
+int UUID::version() throw ( lang::exceptions::UnsupportedOperationException ) {
+    return this->uuidVersion;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 UUID UUID::randomUUID() {
-    return UUID( 0, 0); //TODO
+
+    apr_uuid_t temp;
+    // Generate some random bytes.
+    apr_generate_random_bytes( temp.data, 16 );
+
+    long long mostSigBits = 0;
+    long long leastSigBits = 0;
+
+    // Extract to data from the uuid data
+    memcpy( &leastSigBits, &temp.data[0], sizeof(long long) );
+    memcpy( &mostSigBits, &temp.data[sizeof(long long)], sizeof(long long) );
+
+    // Set the variant and version fields, could compact but want to be clear
+    // on what is being set.
+    mostSigBits &= ( 0xFFFFFFFFFFFF0FFFULL | ( 0x4ULL << 12 ) );
+    leastSigBits &= ( 0x3FFFFFFFFFFFFFFFULL | ( 0x2ULL << 62 ) );
+
+    return UUID( mostSigBits, leastSigBits );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-UUID UUID::nameUUIDFromBytes( const char* name DECAF_UNUSED ) {
-    return UUID( 0, 0); //TODO
+UUID UUID::nameUUIDFromBytes( const std::string& name DECAF_UNUSED ) {
+
+    long long mostSigBits = 0;
+    long long leastSigBits = 0;
+
+    return UUID( mostSigBits, leastSigBits ); //TODO
 }
 
 ////////////////////////////////////////////////////////////////////////////////

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.h?view=diff&rev=562959&r1=562958&r2=562959
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/UUID.h Sun Aug  5 14:05:27 2007
@@ -69,6 +69,15 @@
         // APR Uuid Type
         apr_uuid_t apr_uuid;
 
+        // Copy of the High part of the data
+        unsigned long long mostSigBits;
+
+        // Copy of the Low part of the data
+        unsigned long long leastSigBits;
+
+        // Version indicator, set when a UUID is generated
+        int uuidVersion;
+
     public:
 
         /**
@@ -85,7 +94,7 @@
          * @param name - a byte array to be used to construct a UUID.
          * @return type 3 UUID
          */
-        static UUID nameUUIDFromBytes( const char* name );
+        static UUID nameUUIDFromBytes( const std::string& name );
 
         /**
          * Creates a UUID from the string standard representation as described
@@ -200,7 +209,7 @@
          * @returns the clockSequeunce associated with a V1 UUID
          * @throws UnsupportedOperationException
          */
-        virtual long long clockSequence()
+        virtual int clockSequence()
             throw ( lang::exceptions::UnsupportedOperationException );
 
         /**
@@ -212,10 +221,10 @@
          *     * 6 Reserved, Microsoft Corporation backward compatibility
          *     * 7 Reserved for future definition
          *
-         * @returns the clockSequeunce associated with a V1 UUID
+         * @returns the variant associated with a V1 UUID
          * @throws UnsupportedOperationException
          */
-        virtual long long variant()
+        virtual int variant()
             throw ( lang::exceptions::UnsupportedOperationException );
 
         /**
@@ -227,10 +236,10 @@
          *     * 3 Name-based UUID
          *     * 4 Randomly generated UUID
          *
-         * @returns the clockSequeunce associated with a V1 UUID
+         * @returns the version associated with a V1 UUID
          * @throws UnsupportedOperationException
          */
-        virtual long long version()
+        virtual int version()
             throw ( lang::exceptions::UnsupportedOperationException );
 
     };