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$
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/signals2.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
104namespace PlayerCc
105{
106
107class ClientProxy;
108
120class 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
173
178 bool Connected() { return (NULL!=mClient && mClient->connected == 1) ? true : false; }
179
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
189
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
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
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
307};
308
309
310
311}
312
313namespace std
314{
315 PLAYERCC_EXPORT std::ostream& operator << (std::ostream& os, const PlayerCc::PlayerClient& c);
316}
317
318#endif
319
The client proxy base class.
Definition clientproxy.h:80
The PlayerClient is used for communicating with the player server.
Definition playerclient.h:121
uint32_t GetOverflowCount()
Get count of the number of discarded messages on the server since the last call to this method.
std::string LookupName(int aCode) const
Get the name for a given interface code.
mutex_t mMutex
A mutex for handling synchronization.
Definition playerclient.h:181
double GetRetryTime()
Get connection retry time, which is number of seconds to wait between reconnection attempts.
Definition playerclient.h:227
std::string GetHostname() const
Returns the hostname.
Definition playerclient.h:294
bool Peek(uint32_t timeout=0)
Check whether there is data waiting on the connection, blocking for up to timeout milliseconds (set t...
void SetReplaceRule(bool aReplace, int aType=-1, int aSubtype=-1, int aInterf=-1)
Set a replace rule for the clients queue on the server.
void SetDataMode(uint32_t aMode)
Set whether the client operates in Push/Pull modes.
void ReadIfWaiting()
A nonblocking Read.
void StopThread()
Stop the run thread.
PlayerClient(const std::string aHostname=PLAYER_HOSTNAME, uint32_t aPort=PLAYER_PORTNUM, int transport=PLAYERC_TRANSPORT_TCP)
Make a client and connect it as indicated.
void StartThread()
Start the run thread.
void Run(uint32_t aTimeout=10)
This starts a blocking loop on Read()
~PlayerClient()
destructor
int LookupCode(std::string aName) const
Get the interface code for a given name.
void Stop()
Stops the Run() loop.
void SetRequestTimeout(uint32_t seconds)
Set the timeout for client requests.
Definition playerclient.h:209
uint32_t GetPort() const
Returns the port.
Definition playerclient.h:297
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:219
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:215
void RequestDeviceList()
Get the list of available device ids.
bool Connected()
Query connection to Player server.
Definition playerclient.h:178
void Read()
A blocking Read.
void SetRetryTime(double time)
Set connection retry time, which is number of seconds to wait between reconnection attempts.
Definition playerclient.h:223
Definition playerclient.h:96
Definition playerclient.h:92
Definition playerclient.h:86
#define PLAYERC_TRANSPORT_TCP
The valid transports.
Definition playerc.h:114
PLAYERC_EXPORT void playerc_client_set_retry_limit(playerc_client_t *client, int limit)
Set the connection retry limit.
PLAYERC_EXPORT void playerc_client_set_request_timeout(playerc_client_t *client, uint32_t seconds)
Set the timeout for client requests.
PLAYERC_EXPORT void playerc_client_set_retry_time(playerc_client_t *client, double time)
Set the connection retry sleep time.
Client object data.
Definition playerc.h:508
int connected
Whether or not we're currently connected.
Definition playerc.h:521
int retry_limit
How many times we'll try to reconnect after a socket error.
Definition playerc.h:526
double retry_time
How long to sleep, in seconds, to sleep between reconnect attempts.
Definition playerc.h:530