GuidGenerator.cpp

Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Distributed under the OpenDDS License.
00005  * See: http://www.opendds.org/license.html
00006  */
00007 
00008 #include "dds/DCPS/RTPS/GuidGenerator.h"
00009 
00010 #include "dds/DCPS/GuidUtils.h"
00011 
00012 #include "dds/DdsDcpsGuidTypeSupportImpl.h"
00013 
00014 #include "ace/OS_NS_unistd.h"
00015 #include "ace/OS_NS_stdlib.h"
00016 #include "ace/OS_NS_netdb.h"
00017 #include "ace/OS_NS_sys_time.h"
00018 
00019 namespace OpenDDS {
00020   namespace RTPS {
00021 
00022 GuidGenerator::GuidGenerator()
00023   : pid_(ACE_OS::getpid()),
00024     counter_(0)
00025 {
00026 
00027   if (pid_ == -1) {
00028     unsigned seed = static_cast<unsigned>(ACE_OS::gettimeofday().usec());
00029     pid_ = static_cast<pid_t>(ACE_OS::rand_r(&seed));
00030   }
00031 
00032   ACE_OS::macaddr_node_t macaddress;
00033   const int result = ACE_OS::getmacaddress(&macaddress);
00034 
00035   if (-1 != result) {
00036     ACE_OS::memcpy(node_id_, macaddress.node, NODE_ID_SIZE);
00037   } else {
00038     node_id_[0] = static_cast<unsigned char>(ACE_OS::rand());
00039     node_id_[1] = static_cast<unsigned char>(ACE_OS::rand());
00040     node_id_[2] = static_cast<unsigned char>(ACE_OS::rand());
00041     node_id_[3] = static_cast<unsigned char>(ACE_OS::rand());
00042     node_id_[4] = static_cast<unsigned char>(ACE_OS::rand());
00043     node_id_[5] = static_cast<unsigned char>(ACE_OS::rand());
00044   }
00045 }
00046 
00047 ACE_UINT16
00048 GuidGenerator::getCount()
00049 {
00050   ACE_Guard<ACE_SYNCH_MUTEX> guard(this->counter_lock_);
00051   return counter_++;
00052 }
00053 
00054 void
00055 GuidGenerator::populate(DCPS::GUID_t &container)
00056 {
00057   container.guidPrefix[0] = DCPS::VENDORID_OCI[0];
00058   container.guidPrefix[1] = DCPS::VENDORID_OCI[1];
00059 
00060   ACE_UINT16 count = this->getCount();
00061   ACE_OS::memcpy(&container.guidPrefix[2], node_id_, NODE_ID_SIZE);
00062   container.guidPrefix[8] = static_cast<CORBA::Octet>(this->pid_ >> 8);
00063   container.guidPrefix[9] = static_cast<CORBA::Octet>(this->pid_ & 0xFF);
00064   container.guidPrefix[10] = static_cast<CORBA::Octet>(count >> 8);
00065   container.guidPrefix[11] = static_cast<CORBA::Octet>(count & 0xFF);
00066 }
00067 
00068 } // namespace RTPS
00069 } // namespace OpenDDS

Generated on Fri Feb 12 20:05:23 2016 for OpenDDS by  doxygen 1.4.7