Line data Source code
1 : /* 2 : * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "xmpp_server_address_parser.h" 7 : 8 : #include <algorithm> 9 : #include <sstream> 10 : #include <boost/algorithm/string.hpp> 11 : 12 346 : XmppServerAddressParser::XmppServerAddressParser(uint16_t default_port, uint32_t max_servers_count) : 13 346 : default_port(default_port), 14 346 : max_servers_count(max_servers_count) { 15 : 16 346 : } 17 : 18 342 : void XmppServerAddressParser::ParseAddress(const std::string &address, 19 : std::string *out_ip, uint16_t *out_port) const { 20 342 : std::vector<std::string> parts; 21 342 : boost::split(parts, address, boost::is_any_of(":")); 22 : 23 342 : *out_ip = parts[0]; 24 342 : *out_port = default_port; 25 : 26 342 : if (parts.size() >= 2) { 27 : uint16_t port; 28 340 : std::istringstream converter(parts[1]); 29 340 : if (converter >> port) { 30 336 : *out_port = port; 31 : } 32 340 : } 33 342 : } 34 : 35 342 : void XmppServerAddressParser::ParseAddresses(const std::vector<std::string> &addresses, 36 : std::string out_ips[], uint16_t out_ports[]) const { 37 342 : const uint32_t count = std::min(static_cast<uint32_t>(addresses.size()), max_servers_count); 38 680 : for (uint32_t i = 0; i < count; ++i) { 39 338 : ParseAddress(addresses[i], &out_ips[i], &out_ports[i]); 40 : } 41 342 : }