#ifndef __JNET__JHOSTNAME__ #define __JNET__JHOSTNAME__ #include #include #include #include #include #include "JLang/JEquals.hh" /** * \author mdejong */ namespace JNET {} namespace JPP { using namespace JNET; } namespace JNET { using JLANG::JEquals; /** * Default ControlHost port. */ static const int DISPATCH_PORT = 5553; /** * Auxiliary data structure for hostname and port number. */ struct JHostname : JEquals { /** * Separation character between hostname and port number. */ static const char SEPARATOR = ':'; /** * Default constructor. */ JHostname() : hostname(""), port(-1) {} /** * Constructor. * * The argument correponds to the hostname and an optional port number of the server. * The syntax is hostname[:port]. * The default port number is DISPATCH_PORT. * If the complete buffer corresponds to an integer value, it is interpreted as the port number. * * \param buffer host name and optional port number */ JHostname(const std::string& buffer) { set(buffer); } /** * Constructor. * * \param hostname hostname * \param port port number */ JHostname(const std::string& hostname, const int port) { this->hostname = hostname; this->port = port; } /** * Equal method. * * \param hostname host name and port * \result true if this host name and port is equal to given host name and port; else false */ inline bool equals(const JHostname& hostname) const { return (this->hostname == hostname.hostname && this->port == hostname.port); } /** * Set hostname and port number. * * The argument correponds to the hostname and an optional port number of the server.\n * The syntax is hostname[:port]. * The default port number is DISPATCH_PORT.\n * If the complete buffer corresponds to an integer value, it is interpreted as the port number. * * \param buffer host name and optional port number */ void set(const std::string& buffer) { using namespace std; const string::size_type pos = buffer.find(SEPARATOR); if (pos != string::npos) { this->hostname = buffer.substr(0, pos); istringstream(buffer.substr(pos + 1)) >> this->port; } else { bool is_number = true; for (string::const_iterator i = buffer.begin(); is_number && i != buffer.end(); ++i) { is_number &= isdigit(*i); } if (is_number) { this->hostname = ""; istringstream(buffer) >> this->port; } else { this->hostname = buffer; this->port = DISPATCH_PORT; } } } /** * Read hostname from input stream. * * \param in input stream * \param object hostname * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JHostname& object) { std::string buffer; if (in >> buffer) { object.set(buffer); } return in; } /** * Write hostname to output stream. * * \param out output stream * \param object hostname * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JHostname& object) { return out << object.hostname << SEPARATOR << object.port; } std::string hostname; int port; }; } #endif