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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37  *
38  ********************************************************************/
39 
40 /*
41  $Id: playerclient.h 8463 2009-12-16 00:33:35Z gbiggs $
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 #else
75  // we have to define this so we don't have to
76  // comment out all the instances of scoped_lock
77  // in all the proxies
78  namespace boost
79  {
80  class PLAYERCC_EXPORT thread
81  {
82  public:
83  thread() {};
84  };
85 
86  class PLAYERCC_EXPORT mutex
87  {
88  public:
89  mutex() {};
91  {
92  public: scoped_lock(mutex /*m*/) {};
93  };
94  };
95  }
96 
97 #endif
98 
99 namespace PlayerCc
100 {
101 
102 class ClientProxy;
103 
115 class PLAYERCC_EXPORT PlayerClient
116 {
117  friend class ClientProxy;
118 
119  // our thread type
120  typedef boost::thread thread_t;
121 
122  // our mutex type
123  typedef boost::mutex mutex_t;
124 
125  private:
126  // list of proxies associated with us
127  std::list<PlayerCc::ClientProxy*> mProxyList;
128 
129  std::list<playerc_device_info_t> mDeviceList;
130 
131  // Connect to the indicated host and port.
132  // @exception throws PlayerError if unsuccessfull
133  void Connect(const std::string aHostname, uint32_t aPort);
134 
135  // Disconnect from server.
136  void Disconnect();
137 
138  // our c-client from playerc
139  playerc_client_t* mClient;
140 
141  // The hostname of the server, stored for convenience
142  std::string mHostname;
143 
144  // The port number of the server, stored for convenience
145  uint32_t mPort;
146 
147  // Which transport (TCP or UDP) we're using
148  unsigned int mTransport;
149 
150  // Is the thread currently stopped or stopping?
151  bool mIsStop;
152 
153  // This is the thread where we run @ref Run()
154  thread_t* mThread;
155 
156  // A helper function for starting the thread
157  void RunThread();
158 
159  public:
160 
162  PlayerClient(const std::string aHostname=PLAYER_HOSTNAME,
163  uint32_t aPort=PLAYER_PORTNUM,
164  int transport=PLAYERC_TRANSPORT_TCP);
165 
167  ~PlayerClient();
168 
170  bool Connected() { return (NULL!=mClient && mClient->connected == 1) ? true : false; }
171 
174 
175  // ideally, we'd use the read_write mutex, but I was having some problems
176  // (with their code) because it's under development
177  //boost::read_write_mutex mMutex;
178 
180  void StartThread();
181 
183  void StopThread();
184 
186  void Run(uint32_t aTimeout=10); // aTimeout in ms
187 
189  void Stop();
190 
197  bool Peek(uint32_t timeout=0);
198  //bool Peek2(uint32_t timeout=0);
199 
201  void SetRequestTimeout(uint32_t seconds) { playerc_client_set_request_timeout(this->mClient,seconds); }
202 
203 
207  void SetRetryLimit(int limit) { playerc_client_set_retry_limit(this->mClient,limit); }
208 
211  int GetRetryLimit() { return(this->mClient->retry_limit); }
212 
215  void SetRetryTime(double time) { playerc_client_set_retry_time(this->mClient,time); }
216 
219  double GetRetryTime() { return(this->mClient->retry_time); }
220 
228  void Read();
229 
234  void ReadIfWaiting();
235 
236 // /// @brief You can change the rate at which your client receives data from the
237 // /// server with this method. The value of @p freq is interpreted as Hz;
238 // /// this will be the new rate at which your client receives data (when in
239 // /// continuous mode).
240 // ///
241 // /// @exception throws PlayerError if unsuccessfull
242 // void SetFrequency(uint32_t aFreq);
243 
256  void SetDataMode(uint32_t aMode);
257 
274  void SetReplaceRule(bool aReplace,
275  int aType = -1,
276  int aSubtype = -1,
277  int aInterf = -1);
278 
281  void RequestDeviceList();
282 
283  std::list<playerc_device_info_t> GetDeviceList();
284 
286  std::string GetHostname() const { return(mHostname); };
287 
289  uint32_t GetPort() const { return(mPort); };
290 
292  int LookupCode(std::string aName) const;
293 
295  std::string LookupName(int aCode) const;
296 
298  uint32_t GetOverflowCount();
299 };
300 
301 
302 
303 }
304 
305 namespace std
306 {
307  PLAYERCC_EXPORT std::ostream& operator << (std::ostream& os, const PlayerCc::PlayerClient& c);
308 }
309 
310 #endif
311 
const std::string PLAYER_HOSTNAME
The default hostname for PlayerClient.
Definition: utility.h:63
void SetRequestTimeout(uint32_t seconds)
Set the timeout for client requests.
Definition: playerclient.h:201
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:173
uint32_t GetPort() const
Returns the port.
Definition: playerclient.h:289
The PlayerClient is used for communicating with the player server.
Definition: playerclient.h:115
double GetRetryTime()
Get connection retry time, which is number of seconds to wait between reconnection attempts...
Definition: playerclient.h:219
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:80
PLAYERC_EXPORT void playerc_client_set_request_timeout(playerc_client_t *client, uint32_t seconds)
Set the timeout for client requests.
Definition: playerclient.h:90
void SetRetryLimit(int limit)
Set connection retry limit, which is the number of times that we'll try to reconnect to the server af...
Definition: playerclient.h:207
#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:215
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:460
int GetRetryLimit()
Get connection retry limit, which is the number of times that we'll try to reconnect to the server af...
Definition: playerclient.h:211
Definition: playerclient.h:86
PLAYERC_EXPORT void playerc_client_set_retry_time(playerc_client_t *client, double time)
Set the connection retry sleep time.
std::string GetHostname() const
Returns the hostname.
Definition: playerclient.h:286
bool Connected()
Are we currently connected?
Definition: playerclient.h:170

Last updated 12 September 2005 21:38:45