1616 */
1717#include " client_connection_map.h"
1818
19+ #include < nlohmann/json.hpp>
20+ #include < utilities/json_utilities.h>
21+
1922// ///////////////////////////////////////////////////////////////////////
2023// Implementation of the ConnectionMap class
2124// ///////////////////////////////////////////////////////////////////////
@@ -41,63 +44,38 @@ bool ConnectionMap::contains(bool ssl, sa_family_t family) {
4144 }
4245}
4346
44- void ConnectionMap::initialize (cJSON* ports) {
47+ void ConnectionMap::initialize (const nlohmann::json& ports) {
4548 invalidate ();
46- cJSON* array = cJSON_GetObjectItem (ports, " ports" );
47- if (array == nullptr ) {
48- std::string msg (" ports not found in portnumber file: " );
49- msg.append (to_string (ports, false ));
50- throw std::runtime_error (msg);
49+ auto array = ports.find (" ports" );
50+ if (array == ports.end ()) {
51+ throw std::runtime_error (" ports not found in portnumber file: " +
52+ ports.dump ());
5153 }
5254
53- auto numEntries = cJSON_GetArraySize (array);
5455 sa_family_t family;
55- for (int ii = 0 ; ii < numEntries; ++ii ) {
56- auto obj = cJSON_GetArrayItem (array, ii );
57- auto fam = cJSON_GetObjectItem (obj, " family" );
58- if (strcmp ( fam-> valuestring , " AF_INET " ) == 0 ) {
56+ for (const auto obj : *array ) {
57+ // auto fam = port.find("family" );
58+ auto fam = cb::jsonGet<std::string> (obj, " family" );
59+ if (fam == " AF_INET " ) {
5960 family = AF_INET;
60- } else if (strcmp (fam->valuestring , " AF_INET6" ) == 0 ) {
61- family = AF_INET6;
6261 } else {
63- std::string msg (" Unsupported network family: " );
64- msg.append (to_string (obj, false ));
65- throw std::runtime_error (msg);
66- }
67-
68- auto ssl = cJSON_GetObjectItem (obj, " ssl" );
69- if (ssl == nullptr ) {
70- std::string msg (" ssl missing for entry: " );
71- msg.append (to_string (obj, false ));
72- throw std::runtime_error (msg);
73- }
74-
75- auto port = cJSON_GetObjectItem (obj, " port" );
76- if (port == nullptr ) {
77- std::string msg (" port missing for entry: " );
78- msg.append (to_string (obj, false ));
79- throw std::runtime_error (msg);
80- }
81-
82- auto protocol = cJSON_GetObjectItem (obj, " protocol" );
83- if (protocol == nullptr ) {
84- std::string msg (" protocol missing for entry: " );
85- msg.append (to_string (obj, false ));
86- throw std::runtime_error (msg);
62+ family = AF_INET6;
8763 }
8864
89- auto portval = static_cast <in_port_t >(port->valueint );
90- bool useSsl = ssl->type == cJSON_True ? true : false ;
65+ auto ssl = cb::jsonGet<bool >(obj, " ssl" );
66+ auto port = cb::jsonGet<size_t >(obj, " port" );
67+ auto protocol = cb::jsonGet<std::string>(obj, " protocol" );
9168
9269 MemcachedConnection* connection;
93- if (strcmp ( protocol-> valuestring , " memcached " ) != 0 ) {
70+ if (protocol != " memcached " ) {
9471 throw std::logic_error (
9572 " ConnectionMap::initialize: Invalid value passed for "
9673 " protocol: " +
97- std::string (protocol-> valuestring ));
74+ std::string (protocol));
9875 }
9976
100- connection = new MemcachedConnection (" " , portval, family, useSsl);
77+ auto portVal = static_cast <in_port_t >(port);
78+ connection = new MemcachedConnection (" " , portVal, family, ssl);
10179 connections.push_back (std::unique_ptr<MemcachedConnection>{connection});
10280 }
10381}
0 commit comments