playerclient.h
00001 /*
00002  *  Player - One Hell of a Robot Server
00003  *  Copyright (C) 2000-2003
00004  *     Brian Gerkey, Kasper Stoy, Richard Vaughan, & Andrew Howard
00005  *
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  */
00022 /********************************************************************
00023  *
00024  *  This library is free software; you can redistribute it and/or
00025  *  modify it under the terms of the GNU Lesser General Public
00026  *  License as published by the Free Software Foundation; either
00027  *  version 2.1 of the License, or (at your option) any later version.
00028  *
00029  *  This library is distributed in the hope that it will be useful,
00030  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00031  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00032  *  Lesser General Public License for more details.
00033  *
00034  *  You should have received a copy of the GNU Lesser General Public
00035  *  License along with this library; if not, write to the Free Software
00036  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00037  *
00038  ********************************************************************/
00039 
00040 /*
00041   $Id: playerclient.h 8463 2009-12-16 00:33:35Z gbiggs $
00042 */
00043 
00044 #ifndef PLAYERCLIENT_H
00045 #define PLAYERCLIENT_H
00046 
00047 #include "libplayerc++/utility.h"
00048 #include "libplayerc++/playerc++config.h"
00049 
00050 #include <string>
00051 #include <list>
00052 
00053 #if defined (WIN32)
00054   #if defined (PLAYER_STATIC)
00055     #define PLAYERCC_EXPORT
00056   #elif defined (playerc___EXPORTS)
00057     #define PLAYERCC_EXPORT    __declspec (dllexport)
00058   #else
00059     #define PLAYERCC_EXPORT    __declspec (dllimport)
00060   #endif
00061 #else
00062   #define PLAYERCC_EXPORT
00063 #endif
00064 
00065 #ifdef HAVE_BOOST_SIGNALS
00066   #include <boost/signal.hpp>
00067 #endif
00068 
00069 #ifdef HAVE_BOOST_THREAD
00070   #include <boost/thread/mutex.hpp>
00071   #include <boost/thread/thread.hpp>
00072   #include <boost/thread/xtime.hpp>
00073   #include <boost/bind.hpp>
00074 #else
00075   // we have to define this so we don't have to
00076   // comment out all the instances of scoped_lock
00077   // in all the proxies
00078   namespace boost
00079   {
00080     class PLAYERCC_EXPORT thread
00081     {
00082       public:
00083         thread() {};
00084     };
00085 
00086     class PLAYERCC_EXPORT mutex
00087     {
00088       public:
00089         mutex() {};
00090         class scoped_lock
00091         {
00092           public: scoped_lock(mutex /*m*/) {};
00093         };
00094     };
00095   }
00096 
00097 #endif
00098 
00099 namespace PlayerCc
00100 {
00101 
00102 class ClientProxy;
00103 
00115 class PLAYERCC_EXPORT PlayerClient
00116 {
00117   friend class ClientProxy;
00118 
00119   // our thread type
00120   typedef boost::thread thread_t;
00121 
00122   // our mutex type
00123   typedef boost::mutex mutex_t;
00124 
00125   private:
00126     // list of proxies associated with us
00127     std::list<PlayerCc::ClientProxy*> mProxyList;
00128 
00129     std::list<playerc_device_info_t> mDeviceList;
00130 
00131     // Connect to the indicated host and port.
00132     // @exception throws PlayerError if unsuccessfull
00133     void Connect(const std::string aHostname, uint32_t aPort);
00134 
00135     // Disconnect from server.
00136     void Disconnect();
00137 
00138     //  our c-client from playerc
00139     playerc_client_t* mClient;
00140 
00141     // The hostname of the server, stored for convenience
00142     std::string mHostname;
00143 
00144     // The port number of the server, stored for convenience
00145     uint32_t mPort;
00146 
00147     // Which transport (TCP or UDP) we're using
00148     unsigned int mTransport;
00149 
00150     // Is the thread currently stopped or stopping?
00151     bool mIsStop;
00152 
00153     // This is the thread where we run @ref Run()
00154     thread_t* mThread;
00155 
00156     // A helper function for starting the thread
00157     void RunThread();
00158 
00159   public:
00160 
00162     PlayerClient(const std::string aHostname=PLAYER_HOSTNAME,
00163                  uint32_t aPort=PLAYER_PORTNUM,
00164                  int transport=PLAYERC_TRANSPORT_TCP);
00165 
00167     ~PlayerClient();
00168 
00170     bool Connected() { return (NULL!=mClient && mClient->connected == 1) ? true : false; } 
00171 
00173     mutex_t mMutex;
00174 
00175     // ideally, we'd use the read_write mutex, but I was having some problems
00176     // (with their code) because it's under development
00177     //boost::read_write_mutex mMutex;
00178 
00180     void StartThread();
00181 
00183     void StopThread();
00184 
00186     void Run(uint32_t aTimeout=10); // aTimeout in ms
00187 
00189     void Stop();
00190 
00197     bool Peek(uint32_t timeout=0);
00198     //bool Peek2(uint32_t timeout=0);
00199 
00201     void SetRequestTimeout(uint32_t seconds) { playerc_client_set_request_timeout(this->mClient,seconds); }
00202 
00203 
00207     void SetRetryLimit(int limit) { playerc_client_set_retry_limit(this->mClient,limit); }
00208 
00211     int GetRetryLimit() { return(this->mClient->retry_limit); }
00212 
00215     void SetRetryTime(double time) { playerc_client_set_retry_time(this->mClient,time); }
00216 
00219     double GetRetryTime() { return(this->mClient->retry_time); }
00220 
00228     void Read();
00229 
00234     void ReadIfWaiting();
00235 
00236 //    /// @brief You can change the rate at which your client receives data from the
00237 //    /// server with this method.  The value of @p freq is interpreted as Hz;
00238 //    /// this will be the new rate at which your client receives data (when in
00239 //    /// continuous mode).
00240 //    ///
00241 //    /// @exception throws PlayerError if unsuccessfull
00242 //     void SetFrequency(uint32_t aFreq);
00243 
00256     void SetDataMode(uint32_t aMode);
00257 
00274     void SetReplaceRule(bool aReplace,
00275                         int aType = -1,
00276                         int aSubtype = -1,
00277                         int aInterf = -1);
00278 
00281     void RequestDeviceList();
00282 
00283     std::list<playerc_device_info_t> GetDeviceList();
00284 
00286     std::string GetHostname() const { return(mHostname); };
00287 
00289     uint32_t GetPort() const { return(mPort); };
00290 
00292     int LookupCode(std::string aName) const;
00293 
00295     std::string LookupName(int aCode) const;
00296 
00298     uint32_t GetOverflowCount();
00299 };
00300 
00301 
00302 
00303 }
00304 
00305 namespace std
00306 {
00307   PLAYERCC_EXPORT std::ostream& operator << (std::ostream& os, const PlayerCc::PlayerClient& c);
00308 }
00309 
00310 #endif
00311 

Last updated 25 May 2011 21:17:00