playerclient.h

00001 /*
00002   $Id: playerclient.h,v 1.20.2.2 2006/09/25 17:00:16 gerkey Exp $
00003 */
00004 
00005 #ifndef PLAYERCLIENT_H
00006 #define PLAYERCLIENT_H
00007 
00008 #include "libplayerc++/playerc++config.h"
00009 #include "libplayerc++/utility.h"
00010 
00011 #include <string>
00012 #include <list>
00013 
00014 #ifdef HAVE_BOOST_SIGNALS
00015   #include <boost/signal.hpp>
00016   #include <boost/bind.hpp>
00017 #endif
00018 
00019 #ifdef HAVE_BOOST_THREAD
00020   #include <boost/thread/mutex.hpp>
00021   #include <boost/thread/thread.hpp>
00022   #include <boost/thread/xtime.hpp>
00023 #else
00024   // we have to define this so we don't have to
00025   // comment out all the instances of scoped_lock
00026   // in all the proxies
00027   namespace boost
00028   {
00029     class thread
00030     {
00031       public:
00032         thread() {};
00033     };
00034 
00035     class mutex
00036     {
00037       public:
00038         mutex() {};
00039         class scoped_lock
00040         {
00041           public: scoped_lock(mutex m) {};
00042         };
00043     };
00044   }
00045 
00046 #endif
00047 
00048 namespace PlayerCc
00049 {
00050 
00051 class ClientProxy;
00052 
00064 class PlayerClient
00065 {
00066   friend class ClientProxy;
00067 
00068   // our thread type
00069   typedef boost::thread thread_t;
00070 
00071   // our mutex type
00072   typedef boost::mutex mutex_t;
00073 
00074   private:
00075     // list of proxies associated with us
00076     std::list<PlayerCc::ClientProxy*> mProxyList;
00077 
00078     std::list<playerc_device_info_t> mDeviceList;
00079 
00080     // Connect to the indicated host and port.
00081     // @exception throws PlayerError if unsuccessfull
00082     void Connect(const std::string aHostname, uint aPort);
00083 
00084     // Disconnect from server.
00085     void Disconnect();
00086 
00087     //  our c-client from playerc
00088     playerc_client_t* mClient;
00089 
00090     // The hostname of the server, stored for convenience
00091     std::string mHostname;
00092 
00093     // The port number of the server, stored for convenience
00094     uint mPort;
00095 
00096     // Which transport (TCP or UDP) we're using
00097     unsigned int mTransport;
00098 
00099     // Is the thread currently stopped or stopping?
00100     bool mIsStop;
00101 
00102     // This is the thread where we run @ref Run()
00103     thread_t* mThread;
00104 
00105     // A helper function for starting the thread
00106     void RunThread();
00107 
00108   public:
00109 
00111     PlayerClient(const std::string aHostname=PLAYER_HOSTNAME,
00112                  uint aPort=PLAYER_PORTNUM,
00113                  int transport=PLAYERC_TRANSPORT_TCP);
00114 
00116     ~PlayerClient();
00117 
00119     mutex_t mMutex;
00120 
00121     // ideally, we'd use the read_write mutex, but I was having some problems
00122     // (with their code) because it's under development
00123     //boost::read_write_mutex mMutex;
00124 
00126     void StartThread();
00127 
00129     void StopThread();
00130 
00132     void Run(uint aTimeout=10); // aTimeout in ms
00133 
00135     void Stop();
00136 
00143     bool Peek(uint timeout=0);
00144 
00148     void SetRetryLimit(int limit) { playerc_client_set_retry_limit(this->mClient,limit); }
00149 
00152     int GetRetryLimit() { return(this->mClient->retry_limit); }
00153 
00156     void SetRetryTime(double time) { playerc_client_set_retry_time(this->mClient,time); }
00157 
00160     double GetRetryTime() { return(this->mClient->retry_time); }
00161 
00169     void Read();
00170 
00175     void ReadIfWaiting();
00176 
00177 //    /// @brief You can change the rate at which your client receives data from the
00178 //    /// server with this method.  The value of @p freq is interpreted as Hz;
00179 //    /// this will be the new rate at which your client receives data (when in
00180 //    /// continuous mode).
00181 //    ///
00182 //    /// @exception throws PlayerError if unsuccessfull
00183 //     void SetFrequency(uint aFreq);
00184 
00197     void SetDataMode(uint aMode);
00198 
00215     void SetReplaceRule(bool aReplace,
00216                         int aType = -1,
00217                         int aSubtype = -1,
00218                         int aInterf = -1);
00219 
00222     void RequestDeviceList();
00223 
00224     std::list<playerc_device_info_t> GetDeviceList();
00225 
00227     std::string GetHostname() const { return(mHostname); };
00228 
00230     uint GetPort() const { return(mPort); };
00231 
00233     int LookupCode(std::string aName) const;
00234 
00236     std::string LookupName(int aCode) const;
00237 };
00238 
00239 
00240 
00241 }
00242 
00243 namespace std
00244 {
00245   std::ostream& operator << (std::ostream& os, const PlayerCc::PlayerClient& c);
00246 }
00247 
00248 #endif
00249 

Last updated 12 September 2005 21:38:45