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 344 : XmppServerAddressParser::XmppServerAddressParser(uint16_t default_port, uint32_t max_servers_count) : 13 344 : default_port(default_port), 14 344 : max_servers_count(max_servers_count) { 15 : 16 344 : } 17 : 18 340 : void XmppServerAddressParser::ParseAddress(const std::string &address, 19 : std::string *out_ip, uint16_t *out_port) const { 20 340 : std::vector<std::string> parts; 21 340 : boost::split(parts, address, boost::is_any_of(":")); 22 : 23 340 : *out_ip = parts[0]; 24 340 : *out_port = default_port; 25 : 26 340 : if (parts.size() >= 2) { 27 : uint16_t port; 28 338 : std::istringstream converter(parts[1]); 29 338 : if (converter >> port) { 30 334 : *out_port = port; 31 : } 32 338 : } 33 340 : } 34 : 35 340 : void XmppServerAddressParser::ParseAddresses(const std::vector<std::string> &addresses, 36 : std::string out_ips[], uint16_t out_ports[]) const { 37 340 : const uint32_t count = std::min(static_cast<uint32_t>(addresses.size()), max_servers_count); 38 676 : for (uint32_t i = 0; i < count; ++i) { 39 336 : ParseAddress(addresses[i], &out_ips[i], &out_ports[i]); 40 : } 41 340 : }