Skip to content

Commit

Permalink
Enroll ntsa::Endpoint, ntsa::IpEndpoint, and ntsa::LocalName into the…
Browse files Browse the repository at this point in the history
… bdlat introspection framework
  • Loading branch information
mattrm456 authored Sep 24, 2024
1 parent df8b580 commit 4a9c402
Show file tree
Hide file tree
Showing 9 changed files with 1,044 additions and 0 deletions.
44 changes: 44 additions & 0 deletions groups/nts/ntsa/ntsa_endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,49 @@ bsl::ostream& Endpoint::print(bsl::ostream& stream,
return stream;
}

const bdlat_SelectionInfo* Endpoint::lookupSelectionInfo(int id)
{
const int numSelections =
sizeof(SELECTION_INFO_ARRAY) / sizeof(SELECTION_INFO_ARRAY[0]);

if (id < 0 || id >= numSelections) {
return 0;
}

return &SELECTION_INFO_ARRAY[id];
}

const bdlat_SelectionInfo* Endpoint::lookupSelectionInfo(
const char* name,
int nameLength)
{
const bsl::size_t numSelections =
sizeof(SELECTION_INFO_ARRAY) / sizeof(SELECTION_INFO_ARRAY[0]);

for (bsl::size_t i = 0; i < numSelections; ++i) {
const bdlat_SelectionInfo& selectionInfo = SELECTION_INFO_ARRAY[i];
if (selectionInfo.d_nameLength == nameLength) {
const int compare =
bsl::memcmp(selectionInfo.d_name_p, name, nameLength);
if (compare == 0) {
return &selectionInfo;
}
}
}

return 0;
}

const char Endpoint::CLASS_NAME[15] = "ntsa::Endpoint";

// clang-format off
const bdlat_SelectionInfo Endpoint::SELECTION_INFO_ARRAY[3] =
{
{ ntsa::EndpointType::e_UNDEFINED, "undefined", 9, "", 0 },
{ ntsa::EndpointType::e_IP, "ip", 2, "", 0 },
{ ntsa::EndpointType::e_LOCAL, "local", 5, "", 0 }
};
// clang-format on

} // close package namespace
} // close enterprise namespace
153 changes: 153 additions & 0 deletions groups/nts/ntsa/ntsa_endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ BSLS_IDENT("$Id: $")
#include <ntsa_transport.h>
#include <ntscfg_platform.h>
#include <ntsscm_version.h>
#include <bdlat_typetraits.h>
#include <bdlat_selectioninfo.h>
#include <bdlat_choicefunctions.h>
#include <bslh_hash.h>
#include <bsls_assert.h>
#include <bsls_objectbuffer.h>
Expand Down Expand Up @@ -93,6 +96,10 @@ class Endpoint
ntsa::EndpointType::Value d_type;

public:
/// Defines a type alias for the base serialization type customized by this
/// type.
typedef bslstl::StringRef BaseType;

/// Create a new endpoint having an undefined type.
Endpoint();

Expand Down Expand Up @@ -254,6 +261,51 @@ class Endpoint
int level = 0,
int spacesPerLevel = 4) const;

/// Set the value of this object to be the default for the selection
/// indicated by the specified 'id'. Return 0 on success, and non-zero
/// value otherwise (i.e., the selection is not found).
int makeSelection(int id);

/// Set the value of this object to be the default for the selection
/// indicated by the specified 'name' of the specified 'nameLength'.
/// Return 0 on success, and non-zero value otherwise (i.e., the selection
/// is not found).
int makeSelection(const char* name, int nameLength);

/// Return the selection ID of the current selection in the choice.
int selectionId() const;

/// Invoke the specified 'manipulator' on the address of the modifiable
/// selection, supplying 'manipulator' with the corresponding selection
/// information structure. Return the value returned from the invocation
/// of 'manipulator' if this object has a defined selection, and -1
/// otherwise.
template <typename MANIPULATOR>
int manipulateSelection(MANIPULATOR& manipulator);

/// Invoke the specified 'accessor' on the non-modifiable selection,
/// supplying 'accessor' with the corresponding selection information
/// structure. Return the value returned from the invocation of 'accessor'
/// if this object has a defined selection, and -1 otherwise.
template <typename ACCESSOR>
int accessSelection(ACCESSOR& accessor) const;

/// Return the compiler-independant name for this class.
static const char CLASS_NAME[15];

/// The selection info array, indexed by selection index.
static const bdlat_SelectionInfo SELECTION_INFO_ARRAY[3];

/// Return selection information for the selection indicated by the
/// specified 'id' if the selection exists, and 0 otherwise.
static const bdlat_SelectionInfo* lookupSelectionInfo(int id);

/// Return selection information for the selection indicated by the
/// specified 'name' of the specified 'nameLength' if the selection
/// exists, and 0 otherwise.
static const bdlat_SelectionInfo* lookupSelectionInfo(
const char* name, int nameLength);

/// Defines the traits of this type. These traits can be used to select,
/// at compile-time, the most efficient algorithm to manipulate objects
/// of this type.
Expand Down Expand Up @@ -552,6 +604,107 @@ void hashAppend(HASH_ALGORITHM& algorithm, const Endpoint& value)
}
}

NTSCFG_INLINE
int Endpoint::makeSelection(int id)
{
switch (id) {
case ntsa::EndpointType::e_UNDEFINED:
this->reset();
break;
case ntsa::EndpointType::e_IP:
this->makeIp();
break;
case ntsa::EndpointType::e_LOCAL:
this->makeLocal();
break;
default:
return -1;
}

return 0;
}

NTSCFG_INLINE
int Endpoint::makeSelection(const char* name, int nameLength)
{
const bdlat_SelectionInfo *selectionInfo =
ntsa::Endpoint::lookupSelectionInfo(name, nameLength);
if (selectionInfo == 0) {
return -1;
}

return this->makeSelection(selectionInfo->d_id);
}

NTSCFG_INLINE
int Endpoint::selectionId() const
{
return static_cast<int>(d_type);
}

template <typename MANIPULATOR>
int Endpoint::manipulateSelection(MANIPULATOR& manipulator)
{
int rc;

if (d_type == ntsa::EndpointType::e_UNDEFINED) {
;
}
else if (d_type == ntsa::EndpointType::e_IP) {
rc = manipulator(&d_ip.object(), SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else if (d_type == ntsa::EndpointType::e_LOCAL) {
rc = manipulator(&d_local.object(), SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else {
return -1;
}

return 0;
}

template <typename ACCESSOR>
int Endpoint::accessSelection(ACCESSOR& accessor) const
{
int rc;

const bdlat_SelectionInfo *selectionInfo =
ntsa::Endpoint::lookupSelectionInfo(d_type);
if (selectionInfo == 0) {
return -1;
}

if (d_type == ntsa::EndpointType::e_UNDEFINED) {
;
}
else if (d_type == ntsa::EndpointType::e_IP) {
rc = accessor(d_ip.object(), SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else if (d_type == ntsa::EndpointType::e_LOCAL) {
rc = accessor(d_local.object(), SELECTION_INFO_ARRAY[d_type]);
if (rc != 0) {
return rc;
}
}
else {
return -1;
}

return 0;
}

} // close package namespace

BDLAT_DECL_CHOICE_TRAITS(ntsa::Endpoint)

} // close enterprise namespace
#endif
110 changes: 110 additions & 0 deletions groups/nts/ntsa/ntsa_endpoint.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

#include <ntsa_endpoint.h>
#include <ntscfg_test.h>
#include <balber_berdecoder.h>
#include <balber_berencoder.h>
#include <baljsn_decoder.h>
#include <baljsn_encoder.h>
#include <bdlsb_memoutstreambuf.h>
#include <bdlsb_fixedmeminstreambuf.h>
#include <bslma_testallocator.h>
#include <bsl_sstream.h>

Expand Down Expand Up @@ -200,10 +206,114 @@ NTSCFG_TEST_CASE(3)
}
}

NTSCFG_TEST_CASE(4)
{
int rc;

ntscfg::TestAllocator ta;
{
ntsa::Endpoint e1("10.26.55.100:12345");
ntsa::Endpoint e2;

bdlsb::MemOutStreamBuf osb(&ta);

balber::BerEncoder encoder(0, &ta);
rc = encoder.encode(&osb, e1);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

rc = osb.pubsync();
NTSCFG_TEST_EQ(rc, 0);

NTSCFG_TEST_GT(osb.length(), 0);
NTSCFG_TEST_NE(osb.data(), 0);

NTSCFG_TEST_LOG_DEBUG << "Encoded:\n"
<< bdlb::PrintStringHexDumper(
osb.data(),
static_cast<bsl::size_t>(osb.length()))
<< NTSCFG_TEST_LOG_END;

bdlsb::FixedMemInStreamBuf isb(osb.data(), osb.length());

balber::BerDecoder decoder(0, &ta);
rc = decoder.decode(&isb, &e2);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

NTSCFG_TEST_EQ(e2, e1);
}
NTSCFG_TEST_EQ(ta.numBlocksInUse(), 0);
}

NTSCFG_TEST_CASE(5)
{
int rc;

ntscfg::TestAllocator ta;
{
bsl::vector<ntsa::Endpoint> e1(&ta);
bsl::vector<ntsa::Endpoint> e2(&ta);

e1.push_back(ntsa::Endpoint("10.26.55.100:12345"));

bdlsb::MemOutStreamBuf osb;

baljsn::Encoder encoder(&ta);
rc = encoder.encode(&osb, e1);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

rc = osb.pubsync();
NTSCFG_TEST_EQ(rc, 0);

NTSCFG_TEST_GT(osb.length(), 0);
NTSCFG_TEST_NE(osb.data(), 0);

NTSCFG_TEST_LOG_DEBUG << "Encoded: "
<< bsl::string_view(
osb.data(),
static_cast<bsl::size_t>(osb.length()))
<< NTSCFG_TEST_LOG_END;

bdlsb::FixedMemInStreamBuf isb(osb.data(), osb.length());

baljsn::Decoder decoder(&ta);
rc = decoder.decode(&isb, &e2);
if (rc != 0) {
NTSCFG_TEST_LOG_DEBUG << encoder.loggedMessages()
<< NTSCFG_TEST_LOG_END;

NTSCFG_TEST_EQ(rc, 0);
}

NTSCFG_TEST_EQ(e2.size(), e1.size());

for (bsl::size_t i = 0; i < e1.size(); ++i) {
NTSCFG_TEST_EQ(e2[i], e1[i]);
}
}
NTSCFG_TEST_EQ(ta.numBlocksInUse(), 0);
}

NTSCFG_TEST_DRIVER
{
NTSCFG_TEST_REGISTER(1);
NTSCFG_TEST_REGISTER(2);
NTSCFG_TEST_REGISTER(3);
NTSCFG_TEST_REGISTER(4);
NTSCFG_TEST_REGISTER(5);
}
NTSCFG_TEST_DRIVER_END;
Loading

0 comments on commit 4a9c402

Please sign in to comment.