playerclient.h
1 /*
2  * Player - One Hell of a Robot Server
3  * Copyright (C) 2000-2003
4  * Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22 /********************************************************************
23  *
24  * This library is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2.1 of the License, or (at your option) any later version.
28  *
29  * This library is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with this library; if not, write to the Free Software
36  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37  *
38  ********************************************************************/
39 
40 /*
41  $Id: playerclient.h 9120 2013-01-07 00:18:52Z jpgr87 $
42 */
43 
44 #ifndef PLAYERCLIENT_H
45 #define PLAYERCLIENT_H
46 
47 #include "libplayerc++/utility.h"
48 #include "libplayerc++/playerc++config.h"
49 
50 #include <string>
51 #include <list>
52 
53 #if defined (WIN32)
54  #if defined (PLAYER_STATIC)
55  #define PLAYERCC_EXPORT
56  #elif defined (playerc___EXPORTS)
57  #define PLAYERCC_EXPORT __declspec (dllexport)
58  #else
59  #define PLAYERCC_EXPORT __declspec (dllimport)
60  #endif
61 #else
62  #define PLAYERCC_EXPORT
63 #endif
64 
65 #ifdef HAVE_BOOST_SIGNALS
66  #include <boost/signal.hpp>
67 #endif
68 
69 #ifdef HAVE_BOOST_THREAD
70  #include <boost/thread/mutex.hpp>
71  #include <boost/thread/thread.hpp>
72  #include <boost/thread/xtime.hpp>
73  #include <boost/bind.hpp>
74  #include <boost/version.hpp>
75  #if BOOST_VERSION < 105000
76  #define TIME_UTC_ TIME_UTC
77  #endif
78 
79 #else
80  // we have to define this so we don't have to
81  // comment out all the instances of scoped_lock
82  // in all the proxies
83  namespace boost
84  {
85  class PLAYERCC_EXPORT thread
86  {
87  public:
88  thread() {};
89  };
90 
91  class PLAYERCC_EXPORT mutex
92  {
93  public:
94  mutex() {};
96  {
97  public: scoped_lock(mutex /*m*/) {};
98  };
99  };
100  }
101 
102 #endif
103 
104 namespace PlayerCc
105 {
106 
107 class ClientProxy;
108 
120 class PLAYERCC_EXPORT PlayerClient
121 {
122  friend class ClientProxy;
123 
124  // our thread type
125  typedef boost::thread thread_t;
126 
127  // our mutex type
128  typedef boost::mutex mutex_t;
129 
130  private:
131  // list of proxies associated with us
132  std::list<PlayerCc::ClientProxy*> mProxyList;
133 
134  std::list<playerc_device_info_t> mDeviceList;
135 
136  // Connect to the indicated host and port.
137  // @exception throws PlayerError if unsuccessfull
138  void Connect(const std::string aHostname, uint32_t aPort);
139 
140  // Disconnect from server.
141  void Disconnect();
142 
143  // our c-client from playerc
144  playerc_client_t* mClient;
145 
146  // The hostname of the server, stored for convenience
147  std::string mHostname;
148 
149  // The port number of the server, stored for convenience
150  uint32_t mPort;
151 
152  // Which transport (TCP or UDP) we're using
153  unsigned int mTransport;
154 
155  // Is the thread currently stopped or stopping?
156  bool mIsStop;
157 
158  // This is the thread where we run @ref Run()
159  thread_t* mThread;
160 
161  // A helper function for starting the thread
162  void RunThread();
163 
164  public:
165 
167  PlayerClient(const std::string aHostname=PLAYER_HOSTNAME,
168  uint32_t aPort=PLAYER_PORTNUM,
169  int transport=PLAYERC_TRANSPORT_TCP);
170 
172  ~PlayerClient();
173 
178  bool Connected() { return (NULL!=mClient && mClient->connected == 1) ? true : false; }
179 
181  mutex_t mMutex;
182 
183  // ideally, we'd use the read_write mutex, but I was having some problems
184  // (with their code) because it's under development
185  //boost::read_write_mutex mMutex;
186 
188  void StartThread();
189 
191  void StopThread();
192 
194  void Run(uint32_t aTimeout=10); // aTimeout in ms
195 
197  void Stop();
198 
205  bool Peek(uint32_t timeout=0);
206  //bool Peek2(uint32_t timeout=0);
207 
209  void SetRequestTimeout(uint32_t seconds) { playerc_client_set_request_timeout(this->mClient,seconds); }
210 
211 
215  void SetRetryLimit(int limit) { playerc_client_set_retry_limit(this->mClient,limit); }
216 
219  int GetRetryLimit() { return(this->mClient->retry_limit); }
220 
223  void SetRetryTime(double time) { playerc_client_set_retry_time(this->mClient,time); }
224 
227  double GetRetryTime() { return(this->mClient->retry_time); }
228 
236  void Read();
237 
242  void ReadIfWaiting();
243 
244 // /// @brief You can change the rate at which your client receives data from the
245 // /// server with this method. The value of @p freq is interpreted as Hz;
246 // /// this will be the new rate at which your client receives data (when in
247 // /// continuous mode).
248 // ///
249 // /// @exception throws PlayerError if unsuccessfull
250 // void SetFrequency(uint32_t aFreq);
251 
264  void SetDataMode(uint32_t aMode);
265 
282  void SetReplaceRule(bool aReplace,
283  int aType = -1,
284  int aSubtype = -1,
285  int aInterf = -1);
286 
289  void RequestDeviceList();
290 
291  std::list<playerc_device_info_t> GetDeviceList();
292 
294  std::string GetHostname() const { return(mHostname); };
295 
297  uint32_t GetPort() const { return(mPort); };
298 
300  int LookupCode(std::string aName) const;
301 
303  std::string LookupName(int aCode) const;
304 
306  uint32_t GetOverflowCount();
307 };
308 
309 
310 
311 }
312 
313 namespace std
314 {
315  PLAYERCC_EXPORT std::ostream& operator << (std::ostream& os, const PlayerCc::PlayerClient& c);
316 }
317 
318 #endif
319 
const std::string PLAYER_HOSTNAME
The default hostname for PlayerClient.
Definition: utility.h:63
Definition: playerclient.h:83
int retry_limit
How many times we&#39;ll try to reconnect after a socket error.
Definition: playerc.h:525
int connected
Whether or not we&#39;re currently connected.
Definition: playerc.h:520
void SetRequestTimeout(uint32_t seconds)
Set the timeout for client requests.
Definition: playerclient.h:209
Definition: playerclient.h:313
T limit(T a, T min, T max)
Limit a value to the range of min, max.
Definition: utility.h:114
mutex_t mMutex
A mutex for handling synchronization.
Definition: playerclient.h:181
The PlayerClient is used for communicating with the player server.
Definition: playerclient.h:120
double GetRetryTime()
Get connection retry time, which is number of seconds to wait between reconnection attempts...
Definition: playerclient.h:227
The client proxy base class.
Definition: clientproxy.h:79
const int PLAYER_PORTNUM
The default port number for PlayerClient.
Definition: utility.h:61
Definition: playerclient.h:85
PLAYERC_EXPORT void playerc_client_set_request_timeout(playerc_client_t *client, uint32_t seconds)
Set the timeout for client requests.
Definition: playerclient.h:95
void SetRetryLimit(int limit)
Set connection retry limit, which is the number of times that we&#39;ll try to reconnect to the server af...
Definition: playerclient.h:215
#define PLAYERC_TRANSPORT_TCP
The valid transports.
Definition: playerc.h:114
void SetRetryTime(double time)
Set connection retry time, which is number of seconds to wait between reconnection attempts...
Definition: playerclient.h:223
std::string GetHostname() const
Returns the hostname.
Definition: playerclient.h:294
PLAYERC_EXPORT void playerc_client_set_retry_limit(playerc_client_t *client, int limit)
Set the connection retry limit.
Client object data.
Definition: playerc.h:506
double retry_time
How long to sleep, in seconds, to sleep between reconnect attempts.
Definition: playerc.h:529
int GetRetryLimit()
Get connection retry limit, which is the number of times that we&#39;ll try to reconnect to the server af...
Definition: playerclient.h:219
uint32_t GetPort() const
Returns the port.
Definition: playerclient.h:297
Definition: playerclient.h:91
Definition: playerclient.h:104
PLAYERC_EXPORT void playerc_client_set_retry_time(playerc_client_t *client, double time)
Set the connection retry sleep time.
bool Connected()
Query connection to Player server.
Definition: playerclient.h:178