GuidConverter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include "DCPS/DdsDcps_pch.h"
00009
00010 #include "GuidConverter.h"
00011 #include "dds/DdsDcpsGuidTypeSupportImpl.h"
00012
00013 #include "ace/ACE.h"
00014 #include "ace/OS_NS_stdio.h"
00015
00016 OPENDDS_BEGIN_VERSIONED_NAMESPACE_DECL
00017
00018 namespace OpenDDS {
00019 namespace DCPS {
00020
00021 GuidConverter::GuidConverter(const GUID_t& guid)
00022 : guid_(guid)
00023 {}
00024
00025 GuidConverter::~GuidConverter()
00026 {}
00027
00028 long
00029 GuidConverter::checksum() const
00030 {
00031 return ACE::crc32(reinterpret_cast<const void*>(&guid_), sizeof(guid_));
00032 }
00033
00034 long
00035 GuidConverter::vendorId() const
00036 {
00037 return guid_.guidPrefix[0] << 8
00038 | guid_.guidPrefix[1];
00039 }
00040
00041 long
00042 GuidConverter::entityId() const
00043 {
00044 return entityKey() << 8
00045 | guid_.entityId.entityKind;
00046 }
00047
00048 long
00049 GuidConverter::entityKey() const
00050 {
00051 return guid_.entityId.entityKey[0] << 16
00052 | guid_.entityId.entityKey[1] << 8
00053 | guid_.entityId.entityKey[2];
00054 }
00055
00056 EntityKind
00057 GuidConverter::entityKind() const
00058 {
00059 switch (guid_.entityId.entityKind) {
00060 case ENTITYKIND_OPENDDS_TOPIC:
00061 return KIND_TOPIC;
00062
00063 case ENTITYKIND_BUILTIN_TOPIC:
00064 return KIND_BUILTIN_TOPIC;
00065
00066 case ENTITYKIND_USER_READER_NO_KEY:
00067 case ENTITYKIND_USER_READER_WITH_KEY:
00068 return KIND_READER;
00069
00070 case ENTITYKIND_USER_WRITER_NO_KEY:
00071 case ENTITYKIND_USER_WRITER_WITH_KEY:
00072 case ENTITYKIND_OPENDDS_NIL_WRITER:
00073 return KIND_WRITER;
00074
00075 case ENTITYKIND_BUILTIN_READER_NO_KEY:
00076 case ENTITYKIND_BUILTIN_READER_WITH_KEY:
00077 return KIND_BUILTIN_READER;
00078
00079 case ENTITYKIND_BUILTIN_WRITER_NO_KEY:
00080 case ENTITYKIND_BUILTIN_WRITER_WITH_KEY:
00081 return KIND_BUILTIN_WRITER;
00082
00083 case ENTITYKIND_BUILTIN_PARTICIPANT:
00084 return KIND_PARTICIPANT;
00085
00086 case ENTITYKIND_OPENDDS_PUBLISHER:
00087 return KIND_PUBLISHER;
00088
00089 case ENTITYKIND_OPENDDS_SUBSCRIBER:
00090 return KIND_SUBSCRIBER;
00091
00092 case ENTITYKIND_OPENDDS_USER:
00093 return KIND_USER;
00094
00095 case ENTITYKIND_USER_UNKNOWN:
00096 case ENTITYKIND_BUILTIN_UNKNOWN:
00097 default:
00098 return KIND_UNKNOWN;
00099 }
00100 }
00101
00102 bool GuidConverter::isBuiltinDomainEntity() const
00103 {
00104 switch (guid_.entityId.entityKind) {
00105
00106 case ENTITYKIND_BUILTIN_READER_NO_KEY:
00107 case ENTITYKIND_BUILTIN_READER_WITH_KEY:
00108 case ENTITYKIND_BUILTIN_WRITER_NO_KEY:
00109 case ENTITYKIND_BUILTIN_WRITER_WITH_KEY:
00110 case ENTITYKIND_BUILTIN_TOPIC:
00111 return true;
00112
00113 default:
00114 return false;
00115 }
00116 }
00117
00118 bool GuidConverter::isUserDomainEntity() const
00119 {
00120 switch (guid_.entityId.entityKind) {
00121
00122 case ENTITYKIND_USER_READER_NO_KEY:
00123 case ENTITYKIND_USER_READER_WITH_KEY:
00124 case ENTITYKIND_USER_WRITER_NO_KEY:
00125 case ENTITYKIND_USER_WRITER_WITH_KEY:
00126 case ENTITYKIND_OPENDDS_TOPIC:
00127 return true;
00128
00129 default:
00130 return false;
00131 }
00132 }
00133
00134 bool GuidConverter::isWriter() const
00135 {
00136 EntityKind kind = entityKind();
00137 return kind == KIND_WRITER || kind == KIND_BUILTIN_WRITER;
00138 }
00139
00140 bool GuidConverter::isReader() const
00141 {
00142 EntityKind kind = entityKind();
00143 return kind == KIND_READER || kind == KIND_BUILTIN_READER;
00144 }
00145
00146 bool GuidConverter::isTopic() const
00147 {
00148 EntityKind kind = entityKind();
00149 return kind == KIND_TOPIC || kind == KIND_BUILTIN_TOPIC;
00150 }
00151
00152 GuidConverter::operator OPENDDS_STRING() const
00153 {
00154 OPENDDS_STRING ret(to_string(guid_));
00155 ret += "(";
00156 ret += to_dds_string((unsigned long) checksum(), true);
00157 ret += ")";
00158 return ret;
00159 }
00160
00161 #ifdef DDS_HAS_WCHAR
00162 GuidConverter::operator std::wstring() const
00163 {
00164 std::wostringstream os;
00165 const OPENDDS_STRING guid_str(to_string(guid_));
00166 os << guid_str.c_str() << "(" << std::hex << checksum() << ")";
00167 return os.str();
00168 }
00169 #endif
00170
00171 #ifndef OPENDDS_SAFETY_PROFILE
00172
00173 std::ostream&
00174 operator<<(std::ostream& os, const GuidConverter& rhs)
00175 {
00176 return os << OPENDDS_STRING(rhs);
00177 }
00178
00179 #ifdef DDS_HAS_WCHAR
00180 std::wostream&
00181 operator<<(std::wostream& os, const GuidConverter& rhs)
00182 {
00183 return os << std::wstring(rhs);
00184 }
00185 #endif //DDS_HAS_WCHAR
00186 #endif //OPENDDS_SAFETY_PROFILE
00187
00188 OPENDDS_STRING
00189 GuidConverter::uniqueId() const
00190 {
00191 char id[64];
00192 ACE_OS::snprintf(id, sizeof id,
00193 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
00194 guid_.guidPrefix[ 0],
00195 guid_.guidPrefix[ 1],
00196 guid_.guidPrefix[ 2],
00197 guid_.guidPrefix[ 3],
00198 guid_.guidPrefix[ 4],
00199 guid_.guidPrefix[ 5],
00200 guid_.guidPrefix[ 6],
00201 guid_.guidPrefix[ 7],
00202 guid_.guidPrefix[ 8],
00203 guid_.guidPrefix[ 9],
00204 guid_.guidPrefix[10],
00205 guid_.guidPrefix[11]);
00206 return id;
00207 }
00208
00209 }
00210 }
00211
00212 OPENDDS_END_VERSIONED_NAMESPACE_DECL