REC RPC library
rec_rpc_ClientInfo.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011, REC Robotics Equipment Corporation GmbH, Planegg, Germany
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 - Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 - Neither the name of the REC Robotics Equipment Corporation GmbH nor the names of
14  its contributors may be used to endorse or promote products derived from this software
15  without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
18 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 #ifndef _REC_RPC_CLIENTINFO_H_
28 #define _REC_RPC_CLIENTINFO_H_
29 
30 #include <QObject>
31 #include <QHostAddress>
32 #include <QString>
33 #include <QDataStream>
34 #include <QMetaType>
35 #include <QDebug>
36 
37 namespace rec
38 {
39  namespace rpc
40  {
46  class ClientInfo : public QObject
47  {
48  public:
51  : port( -1 )
52  {
53  }
54 
62  ClientInfo( const QHostAddress& address_, const int port_, const QString& name_ = QString::null )
63  : address( address_ )
64  , port( port_)
65  , name( name_ )
66  {
67  }
68 
74  ClientInfo( const ClientInfo& other )
75  : address( other.address )
76  , port( other.port )
77  , name( other.name )
78  {
79  }
80 
86  ClientInfo& operator=( const ClientInfo& other )
87  {
88  address = other.address;
89  port = other.port;
90  name = other.name;
91  return *this;
92  }
93 
99  bool isNull() const
100  {
101  return -1 == port;
102  }
103 
109  bool isLocal() const
110  {
111  return ( -1 != port ) && address.isNull();
112  }
113 
115  void clear()
116  {
117  address = QHostAddress::Null;
118  port = -1;
119  name.clear();
120  }
121 
128  bool operator==( const ClientInfo& other ) const
129  {
130  if( other.address == address
131  && other.port == port )
132  {
133  return true;
134  }
135  return false;
136  }
137 
144  bool operator!=( const ClientInfo& other ) const
145  {
146  if( other.address != address
147  || other.port != port )
148  {
149  return true;
150  }
151  return false;
152  }
153 
154  QString toString() const
155  {
156  QString str = name;
157  str += " " + address.toString() + ":" + QString::number( port );
158  return str;
159  }
160 
162  QHostAddress address;
164  int port;
166  QString name;
167  };
168 
170  typedef QSet< ClientInfo > ClientInfoSet;
172  typedef QMap< QString, ClientInfo > ClientInfoMap;
173  }
174 }
175 
176 /* \cond */
177 QT_BEGIN_NAMESPACE
178 static QDataStream& operator<<(QDataStream& s, const rec::rpc::ClientInfo& info )
179 {
180  //s << info.address << info.port << info.name;
181  s << info.address << info.port;
182  return s;
183 }
184 
185 static QDataStream& operator>>(QDataStream& s, rec::rpc::ClientInfo& info )
186 {
187  if ( ( s >> info.address ).status() != QDataStream::Ok )
188  return s;
189  if ( ( s >> info.port ).status() != QDataStream::Ok )
190  return s;
191  //if ( ( s >> info.name ).status() != QDataStream::Ok )
192  // return s;
193  return s;
194 }
195 
196 static QDebug operator<<( QDebug dbg, const rec::rpc::ClientInfo& p )
197 {
198  dbg.nospace() << "(" << p.address.toString() << ", " << p.port << ", " << p.name << ", " << ( p.isLocal() ? "local" : "remote" ) << ")";
199 
200  return dbg.space();
201 }
202 
203 static bool operator<(const rec::rpc::ClientInfo &i1, const rec::rpc::ClientInfo &i2)
204 {
205  if( i1.address != i2.address )
206  {
207  if ( i1.address.protocol() != i2.address.protocol() )
208  return ( i1.address.protocol() < i2.address.protocol() );
209  if ( i1.address.protocol() == QAbstractSocket::IPv4Protocol )
210  {
211  quint32 a1 = i1.address.toIPv4Address();
212  quint32 a2 = i2.address.toIPv4Address();
213  if ( a1 != a2 )
214  return a1 < a2;
215  }
216  else if ( i1.address.protocol() == QAbstractSocket::IPv6Protocol )
217  {
218  QIPv6Address a1 = i1.address.toIPv6Address();
219  QIPv6Address a2 = i2.address.toIPv6Address();
220  for( unsigned int i = 0; i < 16; ++i )
221  {
222  if ( a1[i] != a2[i] )
223  return a1[i] < a2[i];
224  }
225  }
226  }
227 
228  if( i1.port != i2.port )
229  {
230  return i1.port < i2.port;
231  }
232  //else
233  //{
234  // return i1.name < i2.name;
235  //}
236 }
237 
238 inline uint qHash( const rec::rpc::ClientInfo& i )
239 {
240  return qHash( qMakePair( i.address, i.port ) );
241 }
242 
243 QT_END_NAMESPACE
244 
245 Q_DECLARE_METATYPE( rec::rpc::ClientInfo )
246 Q_DECLARE_METATYPE( rec::rpc::ClientInfoMap )
247 /* \endcond */
248 
249 #endif //_REC_RPC_CLIENTINFO_H_
QString name
Name (can be empty)
ClientInfo & operator=(const ClientInfo &other)
Assignment operator.
bool isNull() const
Null check.
ClientInfo(const QHostAddress &address_, const int port_, const QString &name_=QString::null)
Constructor with initializations.
void clear()
Clear all data.
RPC client info.
int port
Client&#39;s TCP port.
bool operator==(const ClientInfo &other) const
Equality test.
QSet< ClientInfo > ClientInfoSet
Set of ClientInfos typedef.
QHostAddress address
Client&#39;s network address.
ClientInfo()
Default constructor.
QMap< QString, ClientInfo > ClientInfoMap
Map of ClientInfos typedef.
QDataStream & operator<<(QDataStream &out, const Serializable &data)
Serialize the data and write it into a data stream.
QString toString() const
QDataStream & operator>>(QDataStream &in, Serializable &data)
Read data from a data stream and deserialize it.
bool isLocal() const
Check if client it connected via local socket.
ClientInfo(const ClientInfo &other)
Copy constructor.
bool operator!=(const ClientInfo &other) const
Unequality test.