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