Jpp  15.0.1-rc.1-highQE
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JNET {}
16 namespace JPP { using namespace JNET; }
17 
18 namespace JNET {
19 
20 
21  /**
22  * Default ControlHost port.
23  */
24  static const int DISPATCH_PORT = 5553;
25 
26 
27  /**
28  * Auxiliary data structure for hostname and port number.
29  */
30  struct JHostname {
31 
32  /**
33  * Separation character between hostname and port number.
34  */
35  static const char SEPARATOR = ':';
36 
37 
38  /**
39  * Default constructor.
40  */
42  hostname(""),
43  port(-1)
44  {}
45 
46 
47  /**
48  * Constructor.
49  *
50  * The argument correponds to the hostname and an optional port number of the server.
51  * The syntax is <tt>hostname[:port]</tt>.
52  * The default port number is DISPATCH_PORT.
53  * If the complete buffer corresponds to an integer value, it is interpreted as the port number.
54  *
55  * \param buffer host name and optional port number
56  */
57  JHostname(const std::string& buffer)
58  {
59  set(buffer);
60  }
61 
62 
63  /**
64  * Constructor.
65  *
66  * \param hostname hostname
67  * \param port port number
68  */
69  JHostname(const std::string& hostname,
70  const int port)
71  {
72  this->hostname = hostname;
73  this->port = port;
74  }
75 
76 
77  /**
78  * Set hostname and port number.
79  *
80  * The argument correponds to the hostname and an optional port number of the server.
81  * The syntax is <tt>hostname[:port]</tt>.
82  * The default port number is DISPATCH_PORT.
83  * If the complete buffer corresponds to an integer value, it is interpreted as the port number.
84  *
85  * \param buffer host name and optional port number
86  */
87  void set(const std::string& buffer)
88  {
89  using namespace std;
90 
91  const string::size_type pos = buffer.find(SEPARATOR);
92 
93  if (pos != string::npos) {
94 
95  this->hostname = buffer.substr(0, pos);
96 
97  istringstream(buffer.substr(pos + 1)) >> this->port;
98 
99  } else {
100 
101  bool is_number = true;
102 
103  for (string::const_iterator i = buffer.begin(); is_number && i != buffer.end(); ++i) {
104  is_number &= isdigit(*i);
105  }
106 
107  if (is_number) {
108 
109  this->hostname = "";
110 
111  istringstream(buffer) >> this->port;
112 
113  } else {
114 
115  this->hostname = buffer;
116  this->port = DISPATCH_PORT;
117  }
118  }
119  }
120 
121 
122  /**
123  * Read hostname from input stream.
124  *
125  * \param in input stream
126  * \param object hostname
127  * \return input stream
128  */
129  friend inline std::istream& operator>>(std::istream& in, JHostname& object)
130  {
131  std::string buffer;
132 
133  if (in >> buffer) {
134  object.set(buffer);
135  }
136 
137  return in;
138  }
139 
140 
141  /**
142  * Write hostname to output stream.
143  *
144  * \param out output stream
145  * \param object hostname
146  * \return output stream
147  */
148  friend inline std::ostream& operator<<(std::ostream& out, const JHostname& object)
149  {
150  return out << object.hostname << SEPARATOR << object.port;
151  }
152 
153 
154  std::string hostname;
155  int port;
156  };
157 }
158 
159 #endif
JHostname(const std::string &buffer)
Constructor.
Definition: JHostname.hh:57
static const int DISPATCH_PORT
Default ControlHost port.
Definition: JHostname.hh:24
void set(const std::string &buffer)
Set hostname and port number.
Definition: JHostname.hh:87
Auxiliary data structure for hostname and port number.
Definition: JHostname.hh:30
std::string hostname
Definition: JHostname.hh:154
JHostname(const std::string &hostname, const int port)
Constructor.
Definition: JHostname.hh:69
static const char SEPARATOR
Separation character between hostname and port number.
Definition: JHostname.hh:35
friend std::ostream & operator<<(std::ostream &out, const JHostname &object)
Write hostname to output stream.
Definition: JHostname.hh:148
JHostname()
Default constructor.
Definition: JHostname.hh:41
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41
friend std::istream & operator>>(std::istream &in, JHostname &object)
Read hostname from input stream.
Definition: JHostname.hh:129