clientproxy.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 * Desc: Player C++ client
42 * Authors: Brad Kratochvil, Toby Collett
43 *
44 * Date: 23 Sep 2005
45 # CVS: $Id$
46 **************************************************************************/
47
48
49#ifndef PLAYERCC_CLIENTPROXY_H
50#define PLAYERCC_CLIENTPROXY_H
51
52#if defined (WIN32)
53 #if defined (PLAYER_STATIC)
54 #define PLAYERCC_EXPORT
55 #elif defined (playerc___EXPORTS)
56 #define PLAYERCC_EXPORT __declspec (dllexport)
57 #else
58 #define PLAYERCC_EXPORT __declspec (dllimport)
59 #endif
60#else
61 #define PLAYERCC_EXPORT
62#endif
63
64namespace PlayerCc
65{
66
79class PLAYERCC_EXPORT ClientProxy
80{
81 friend class PlayerClient;
82
83 public:
84
85#ifdef HAVE_BOOST_SIGNALS
88 typedef boost::signals2::connection connection_t;
89
92
94 typedef boost::signals2::signal<void (void)> read_signal_t;
95#else
96 // if we're not using boost, just define them.
97 typedef int connection_t;
98 // we redefine boost::mustex::scoped_lock in playerclient.h
100 // if we're not using boost, just define them.
101 typedef int read_signal_t;
102#endif
103
104 protected:
105
106 // The ClientProxy constructor
107 // @attention Potected, so it can only be instantiated by other clients
108 //
109 // @throw PlayerError Throws a PlayerError if unable to connect to the client
110 ClientProxy(PlayerClient* aPc, uint32_t aIndex);
111
112 // destructor will try to close access to the device
113 virtual ~ClientProxy();
114
115 // Subscribe to the proxy
116 // This needs to be defined for every proxy.
117 // @arg aIndex the index of the devce we want to connect to
118
119 // I wish these could be pure virtual,
120 // but they're used in the constructor/destructor
121 virtual void Subscribe(uint32_t /*aIndex*/) {};
122
123 // Unsubscribe from the proxy
124 // This needs to be defined for every proxy.
125 virtual void Unsubscribe() {};
126
127 // The controlling client object.
128 PlayerClient* mPc;
129
130 // A reference to the C client
131 playerc_client_t* mClient;
132
133 // contains convenience information about the device
134 playerc_device_t *mInfo;
135
136 // if set to true, the current data is "fresh"
137 bool mFresh;
138
139 // @brief Get a variable from the client
140 // All Get functions need to use this when accessing data from the
141 // c library to make sure the data access is thread safe.
142 template<typename T>
143 T GetVar(const T &aV) const
144 { // these have to be defined here since they're templates
145 scoped_lock_t lock(mPc->mMutex);
146 T v = aV;
147 return v;
148 }
149
150 // @brief Get a variable from the client by reference
151 // All Get functions need to use this when accessing data from the
152 // c library to make sure the data access is thread safe. In this
153 // case, a begin, end, and destination pointer must be given (similar
154 // to C++ copy). It is up to the user to ensure there is memory
155 // allocated at aDest.
156 template<typename T>
157 void GetVarByRef(const T aBegin, const T aEnd, T aDest) const
158 { // these have to be defined here since they're templates
159 scoped_lock_t lock(mPc->mMutex);
160 std::copy(aBegin, aEnd, aDest);
161 }
162
163 private:
164
165 // The last time that data was read by this client in [s].
166 double mLastTime;
167
168 // A boost::signal which is used for our callbacks.
169 // The signal will normally be of a type such as:
170 // - boost::signal<void ()>
171 // - boost::signal<void (T)>
172 // where T can be any type.
173 //
174 // @attention we currently only use signals that return void because we
175 // don't have checks to make sure a signal is registered. If an empty
176 // signal is called:
177 //
178 // @attention "Calling the function call operator may invoke undefined
179 // behavior if no slots are connected to the signal, depending on the
180 // combiner used. The default combiner is well-defined for zero slots when
181 // the return type is void but is undefined when the return type is any
182 // other type (because there is no way to synthesize a return value)."
183 //
184 read_signal_t mReadSignal;
185
186 // Outputs the signal if there is new data
187 void ReadSignal();
188
189 public:
190
197 bool IsValid() const { return 0!=GetVar(mInfo->datatime); };
198
206 bool IsFresh() const { return GetVar(mFresh); };
207
213 void NotFresh();
214
221 std::string GetDriverName() const { return mInfo->drivername; };
222
224 double GetDataTime() const { return GetVar(mInfo->datatime); };
225
228 double GetElapsedTime() const
229 { return GetVar(mInfo->datatime) - GetVar(mInfo->lasttime); };
230
235 PlayerClient * GetPlayerClient() const { return mPc;}
236
243 uint32_t GetIndex() const { return GetVar(mInfo->addr.index); };
244
251 uint32_t GetInterface() const { return GetVar(mInfo->addr.interf); };
252
258 std::string GetInterfaceStr() const
259 { return interf_to_str(GetVar(mInfo->addr.interf)); };
260
275 void SetReplaceRule(bool aReplace,
276 int aType = -1,
277 int aSubtype = -1);
278
287 int HasCapability(uint32_t aType, uint32_t aSubtype);
288
297 int GetBoolProp(char *aProperty, bool *aValue);
298
307 int SetBoolProp(char *aProperty, bool aValue);
308
317 int GetIntProp(char *aProperty, int32_t *aValue);
318
327 int SetIntProp(char *aProperty, int32_t aValue);
328
337 int GetDblProp(char *aProperty, double *aValue);
338
347 int SetDblProp(char *aProperty, double aValue);
348
357 int GetStrProp(char *aProperty, char **aValue);
358
367 int SetStrProp(char *aProperty, char *aValue);
368
375 template<typename T>
376 connection_t ConnectReadSignal(T aSubscriber)
377 {
378#ifdef HAVE_BOOST_SIGNALS
379 scoped_lock_t lock(mPc->mMutex);
380 return mReadSignal.connect(aSubscriber);
381#else
382 return -1;
383#endif
384 }
385
390 void DisconnectReadSignal(connection_t aSubscriber)
391 {
392#ifdef HAVE_BOOST_SIGNALS
393 scoped_lock_t lock(mPc->mMutex);
394 aSubscriber.disconnect();
395#else
396 // This line is here to prevent compiler warnings of "unused varaibles"
397 aSubscriber = aSubscriber;
398#endif
399 }
400
401};
402
403}// namespace
404
405#endif
The client proxy base class.
Definition clientproxy.h:80
connection_t ConnectReadSignal(T aSubscriber)
Connect a read signal to this proxy.
Definition clientproxy.h:376
bool IsValid() const
Proxy has any information.
Definition clientproxy.h:197
uint32_t GetIndex() const
Get device index.
Definition clientproxy.h:243
int HasCapability(uint32_t aType, uint32_t aSubtype)
Request capabilities of device.
std::string GetInterfaceStr() const
Get Interface Name.
Definition clientproxy.h:258
bool IsFresh() const
Check for fresh data.
Definition clientproxy.h:206
void SetReplaceRule(bool aReplace, int aType=-1, int aSubtype=-1)
Set a replace rule for this proxy on the server.
uint32_t GetInterface() const
Get Interface Code.
Definition clientproxy.h:251
void NotFresh()
Reset Fresh flag.
int SetDblProp(char *aProperty, double aValue)
Set a double property.
double GetElapsedTime() const
Returns the time between the current data time and the time of the last data sample [s].
Definition clientproxy.h:228
int GetStrProp(char *aProperty, char **aValue)
Request a string property.
double GetDataTime() const
Returns the received timestamp of the last data sample [s].
Definition clientproxy.h:224
void DisconnectReadSignal(connection_t aSubscriber)
Disconnect a signal from this proxy.
Definition clientproxy.h:390
int GetDblProp(char *aProperty, double *aValue)
Request a double property.
int GetBoolProp(char *aProperty, bool *aValue)
Request a boolean property.
std::string GetDriverName() const
Get the underlying driver's name.
Definition clientproxy.h:221
int SetBoolProp(char *aProperty, bool aValue)
Set a boolean property.
int GetIntProp(char *aProperty, int32_t *aValue)
Request an integer property.
int SetStrProp(char *aProperty, char *aValue)
Set a string property.
int SetIntProp(char *aProperty, int32_t aValue)
Set an integer property.
PlayerClient * GetPlayerClient() const
Get a pointer to the Player Client.
Definition clientproxy.h:235
The PlayerClient is used for communicating with the player server.
Definition playerclient.h:121
mutex_t mMutex
A mutex for handling synchronization.
Definition playerclient.h:181
Definition playerclient.h:96
Client object data.
Definition playerc.h:508
Common device info.
Definition playerc.h:865
double lasttime
Data timestamp from the previous data.
Definition playerc.h:888
char drivername[PLAYER_MAX_DRIVER_STRING_LEN]
The driver name.
Definition playerc.h:878
double datatime
Data timestamp, i.e., the time at which the data was generated (s).
Definition playerc.h:885
player_devaddr_t addr
Device address.
Definition playerc.h:875
uint16_t index
Which device of that interface.
Definition player.h:155
uint16_t interf
The interface provided by the device; must be one of PLAYER_*_CODE.
Definition player.h:153