Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JHostname.hh
Go to the documentation of this file.
1#ifndef __JNET__JHOSTNAME__
2#define __JNET__JHOSTNAME__
3
4#include <string>
5#include <istream>
6#include <ostream>
7#include <sstream>
8#include <ctype.h>
9
10#include "JLang/JEquals.hh"
11
12/**
13 * \author mdejong
14 */
15
16namespace JNET {}
17namespace JPP { using namespace JNET; }
18
19namespace JNET {
20
21 using JLANG::JEquals;
22
23
24 /**
25 * Default ControlHost port.
26 */
27 static const int DISPATCH_PORT = 5553;
28
29
30 /**
31 * Auxiliary data structure for hostname and port number.
32 */
33 struct JHostname :
34 JEquals<JHostname>
35 {
36 /**
37 * Separation character between hostname and port number.
38 */
39 static const char SEPARATOR = ':';
40
41
42 /**
43 * Default constructor.
44 */
46 hostname(""),
47 port(-1)
48 {}
49
50
51 /**
52 * Constructor.
53 *
54 * The argument correponds to the hostname and an optional port number of the server.
55 * The syntax is <tt>hostname[:port]</tt>.
56 * The default port number is DISPATCH_PORT.
57 * If the complete buffer corresponds to an integer value, it is interpreted as the port number.
58 *
59 * \param buffer host name and optional port number
60 */
61 JHostname(const std::string& buffer)
62 {
63 set(buffer);
64 }
65
66
67 /**
68 * Constructor.
69 *
70 * \param hostname hostname
71 * \param port port number
72 */
73 JHostname(const std::string& hostname,
74 const int port)
75 {
76 this->hostname = hostname;
77 this->port = port;
78 }
79
80
81 /**
82 * Equal method.
83 *
84 * \param hostname host name and port
85 * \result true if this host name and port is equal to given host name and port; else false
86 */
87 inline bool equals(const JHostname& hostname) const
88 {
89 return (this->hostname == hostname.hostname &&
90 this->port == hostname.port);
91 }
92
93
94 /**
95 * Set hostname and port number.
96 *
97 * The argument correponds to the hostname and an optional port number of the server.\n
98 * The syntax is <tt>hostname[:port]</tt>.
99 * The default port number is DISPATCH_PORT.\n
100 * If the complete buffer corresponds to an integer value, it is interpreted as the port number.
101 *
102 * \param buffer host name and optional port number
103 */
104 void set(const std::string& buffer)
105 {
106 using namespace std;
107
108 const string::size_type pos = buffer.find(SEPARATOR);
109
110 if (pos != string::npos) {
111
112 this->hostname = buffer.substr(0, pos);
113
114 istringstream(buffer.substr(pos + 1)) >> this->port;
115
116 } else {
117
118 bool is_number = true;
119
120 for (string::const_iterator i = buffer.begin(); is_number && i != buffer.end(); ++i) {
121 is_number &= isdigit(*i);
122 }
123
124 if (is_number) {
125
126 this->hostname = "";
127
128 istringstream(buffer) >> this->port;
129
130 } else {
131
132 this->hostname = buffer;
133 this->port = DISPATCH_PORT;
134 }
135 }
136 }
137
138
139 /**
140 * Read hostname from input stream.
141 *
142 * \param in input stream
143 * \param object hostname
144 * \return input stream
145 */
146 friend inline std::istream& operator>>(std::istream& in, JHostname& object)
147 {
148 std::string buffer;
149
150 if (in >> buffer) {
151 object.set(buffer);
152 }
153
154 return in;
155 }
156
157
158 /**
159 * Write hostname to output stream.
160 *
161 * \param out output stream
162 * \param object hostname
163 * \return output stream
164 */
165 friend inline std::ostream& operator<<(std::ostream& out, const JHostname& object)
166 {
167 return out << object.hostname << SEPARATOR << object.port;
168 }
169
170
171 std::string hostname;
172 int port;
173 };
174}
175
176#endif
static const int DISPATCH_PORT
Default ControlHost port.
Definition JHostname.hh:27
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84
Auxiliary data structure for hostname and port number.
Definition JHostname.hh:35
JHostname()
Default constructor.
Definition JHostname.hh:45
static const char SEPARATOR
Separation character between hostname and port number.
Definition JHostname.hh:39
friend std::ostream & operator<<(std::ostream &out, const JHostname &object)
Write hostname to output stream.
Definition JHostname.hh:165
JHostname(const std::string &hostname, const int port)
Constructor.
Definition JHostname.hh:73
void set(const std::string &buffer)
Set hostname and port number.
Definition JHostname.hh:104
JHostname(const std::string &buffer)
Constructor.
Definition JHostname.hh:61
friend std::istream & operator>>(std::istream &in, JHostname &object)
Read hostname from input stream.
Definition JHostname.hh:146
bool equals(const JHostname &hostname) const
Equal method.
Definition JHostname.hh:87
std::string hostname
Definition JHostname.hh:171