OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
xmpp_server_address_parser.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 #include <algorithm>
8 #include <sstream>
9 #include <boost/algorithm/string.hpp>
10 
11 XmppServerAddressParser::XmppServerAddressParser(uint16_t default_port, uint32_t max_servers_count) :
12  default_port(default_port),
13  max_servers_count(max_servers_count) {
14 
15 }
16 
17 void XmppServerAddressParser::ParseAddress(const std::string &address,
18  std::string *out_ip, uint16_t *out_port) const {
19  std::vector<std::string> parts;
20  boost::split(parts, address, boost::is_any_of(":"));
21 
22  *out_ip = parts[0];
23  *out_port = default_port;
24 
25  if (parts.size() >= 2) {
26  uint16_t port;
27  std::istringstream converter(parts[1]);
28  if (converter >> port) {
29  *out_port = port;
30  }
31  }
32 }
33 
34 void XmppServerAddressParser::ParseAddresses(const std::vector<std::string> &addresses,
35  std::string out_ips[], uint16_t out_ports[]) const {
36  const uint32_t count = std::min(static_cast<uint32_t>(addresses.size()), max_servers_count);
37  for (uint32_t i = 0; i < count; ++i) {
38  ParseAddress(addresses[i], &out_ips[i], &out_ports[i]);
39  }
40 }
XmppServerAddressParser(uint16_t default_port, uint32_t max_servers_count)
void ParseAddresses(const std::vector< std::string > &addresses, std::string out_ips[], uint16_t out_ports[]) const
void ParseAddress(const std::string &address, std::string *out_ip, uint16_t *out_port) const