LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions | Private Attributes
LogCabin::RPC::Address Class Reference

This class resolves user-friendly addresses for services into socket-level addresses. More...

#include <Address.h>

List of all members.

Public Types

typedef Core::Time::SteadyClock Clock
 Clock used for timeouts.
typedef Clock::time_point TimePoint
 Type for absolute time values used for timeouts.

Public Member Functions

 Address (const std::string &str, uint16_t defaultPort)
 Constructor.
 Address ()
 Default constructor.
 Address (const Address &other)
 Copy constructor.
Addressoperator= (const Address &other)
 Assignment.
bool isValid () const
 Return true if the sockaddr returned by getSockAddr() is valid.
const sockaddr * getSockAddr () const
 Return a sockaddr that may be used to connect a socket to this Address.
socklen_t getSockAddrLen () const
 Return the length in bytes of the sockaddr in getSockAddr().
std::string getResolvedString () const
 Return a string describing the sockaddr within this Address.
std::string toString () const
 Return a string describing this Address.
void refresh (TimePoint timeout)
 Convert (a random one of) the host(s) and port(s) to a sockaddr.

Private Attributes

std::string originalString
 The host name(s) or numeric address(es) as passed into the constructor.
std::vector< std::pair
< std::string, std::string > > 
hosts
 A list of (host, port) pairs as parsed from originalString.
sockaddr_storage storage
 Storage for the sockaddr returned by getSockAddr.
socklen_t len
 The length in bytes of storage that are in use.

Detailed Description

This class resolves user-friendly addresses for services into socket-level addresses.

It supports DNS lookups for addressing hosts by name, and it supports multiple (alternative) addresses.

Definition at line 34 of file Address.h.


Member Typedef Documentation

Clock used for timeouts.

Definition at line 37 of file Address.h.

Type for absolute time values used for timeouts.

Definition at line 39 of file Address.h.


Constructor & Destructor Documentation

LogCabin::RPC::Address::Address ( const std::string &  str,
uint16_t  defaultPort 
)

Constructor.

You will usually need to call refresh() before using this class.

Parameters:
strA string representation of the host and, optionally, a port number.
  • hostname:port
  • hostname
  • IPv4Address:port
  • IPv4Address
  • [IPv6Address]:port
  • [IPv6Address] Or a comma-delimited list of these to represent multiple hosts.
defaultPortThe port number to use if none is specified in str.

Definition at line 35 of file Address.cc.

Default constructor.

Definition at line 71 of file Address.cc.

Copy constructor.

Definition at line 79 of file Address.cc.


Member Function Documentation

Address & LogCabin::RPC::Address::operator= ( const Address other)

Assignment.

Definition at line 89 of file Address.cc.

Return true if the sockaddr returned by getSockAddr() is valid.

Returns:
True if refresh() has ever succeeded for this host and port. False otherwise.

Definition at line 99 of file Address.cc.

const sockaddr * LogCabin::RPC::Address::getSockAddr ( ) const

Return a sockaddr that may be used to connect a socket to this Address.

Returns:
The returned value will never be NULL and it is always safe to read the protocol field from it, even if getSockAddrLen() returns 0.

Definition at line 105 of file Address.cc.

Return the length in bytes of the sockaddr in getSockAddr().

This is the value you'll want to pass in to connect() or bind().

Definition at line 111 of file Address.cc.

Return a string describing the sockaddr within this Address.

This string will reflect the numeric address produced by the latest successful call to refresh(), or "Unspecified".

Definition at line 117 of file Address.cc.

std::string LogCabin::RPC::Address::toString ( ) const

Return a string describing this Address.

This will contain both the user-provided string passed into the constructor and the numeric address produced by the latest successful call to refresh(). It's the best representation to use in error messages for the user.

Definition at line 151 of file Address.cc.

Convert (a random one of) the host(s) and port(s) to a sockaddr.

If the host is a name instead of numeric, this will run a DNS query and select a random result. If this query fails, any previous sockaddr will be left intact.

Parameters:
timeoutNot yet implemented.
Warning:
Timeouts have not been implemented. See https://github.com/logcabin/logcabin/issues/75

Definition at line 164 of file Address.cc.


Member Data Documentation

The host name(s) or numeric address(es) as passed into the constructor.

Definition at line 123 of file Address.h.

std::vector<std::pair<std::string, std::string> > LogCabin::RPC::Address::hosts [private]

A list of (host, port) pairs as parsed from originalString.

  • First component: the host name or numeric address as parsed from the string passed into the constructor. This has brackets stripped out of IPv6 addresses and is in the form needed by getaddrinfo().
  • Second component: an ASCII representation of the port number to use. It is stored in string form because that's sometimes how it comes into the constructor and always what refresh() needs to call getaddrinfo().

Definition at line 134 of file Address.h.

sockaddr_storage LogCabin::RPC::Address::storage [private]

Storage for the sockaddr returned by getSockAddr.

This is always zeroed out from len to the end.

Definition at line 140 of file Address.h.

socklen_t LogCabin::RPC::Address::len [private]

The length in bytes of storage that are in use.

The remaining bytes of storage are always zeroed out.

Definition at line 146 of file Address.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines