playerclient.h

00001 /*
00002   $Id: playerclient.h,v 1.20 2006/02/28 05:15:41 gerkey Exp $
00003 */
00004 
00005 #ifndef PLAYERCLIENT_H
00006 #define PLAYERCLIENT_H
00007 
00008 #include "libplayerc++/playerc++config.h"
00009 
00010 #include <string>
00011 #include <list>
00012 
00013 #ifdef HAVE_BOOST_SIGNALS
00014   #include <boost/signal.hpp>
00015   #include <boost/bind.hpp>
00016 #endif
00017 
00018 #ifdef HAVE_BOOST_THREAD
00019   #include <boost/thread/mutex.hpp>
00020   #include <boost/thread/thread.hpp>
00021   #include <boost/thread/xtime.hpp>
00022 #else
00023   // we have to define this so we don't have to
00024   // comment out all the instances of scoped_lock
00025   // in all the proxies
00026   namespace boost
00027   {
00028     class thread
00029     {
00030       public:
00031         thread() {};
00032     };
00033 
00034     class mutex
00035     {
00036       public:
00037         mutex() {};
00038         class scoped_lock
00039         {
00040           public: scoped_lock(mutex m) {};
00041         };
00042     };
00043   }
00044 
00045 #endif
00046 
00047 namespace PlayerCc
00048 {
00062 const int PLAYER_PORTNUM(6665);
00064 const std::string PLAYER_HOSTNAME("localhost");
00065 
00066 class ClientProxy;
00067 
00079 class PlayerClient
00080 {
00081   friend class ClientProxy;
00082 
00083   // our thread type
00084   typedef boost::thread thread_t;
00085 
00086   // our mutex type
00087   typedef boost::mutex mutex_t;
00088 
00089   private:
00090     // list of proxies associated with us
00091     std::list<PlayerCc::ClientProxy*> mProxyList;
00092 
00093     std::list<playerc_device_info_t> mDeviceList;
00094 
00095     // Connect to the indicated host and port.
00096     // @exception throws PlayerError if unsuccessfull
00097     void Connect(const std::string aHostname, uint aPort);
00098 
00099     // Disconnect from server.
00100     void Disconnect();
00101 
00102     //  our c-client from playerc
00103     playerc_client_t* mClient;
00104 
00105     // The hostname of the server, stored for convenience
00106     std::string mHostname;
00107 
00108     // The port number of the server, stored for convenience
00109     uint mPort;
00110 
00111     // Is the thread currently stopped or stopping?
00112     bool mIsStop;
00113 
00114     // This is the thread where we run @ref Run()
00115     thread_t* mThread;
00116 
00117     // A helper function for starting the thread
00118     void RunThread();
00119 
00120   public:
00121 
00123     PlayerClient(const std::string aHostname=PLAYER_HOSTNAME,
00124                  uint aPort=PLAYER_PORTNUM);
00125 
00127     ~PlayerClient();
00128 
00130     mutex_t mMutex;
00131 
00132     // ideally, we'd use the read_write mutex, but I was having some problems
00133     // (with their code) because it's under development
00134     //boost::read_write_mutex mMutex;
00135 
00137     void StartThread();
00138 
00140     void StopThread();
00141 
00143     void Run(uint aTimeout=10); // aTimeout in ms
00144 
00146     void Stop();
00147 
00154     bool Peek(uint timeout=0);
00155 
00163     void Read();
00164 
00169     void ReadIfWaiting();
00170 
00177 //     void SetFrequency(uint aFreq);
00178 
00187     void SetDataMode(uint aMode);
00188 
00207     void SetReplaceRule(int aInterf, int aIndex, int aType, int aSubtype, int aReplace);
00208 
00211     void RequestDeviceList();
00212 
00213     std::list<playerc_device_info_t> GetDeviceList();
00214 
00216     std::string GetHostname() const { return(mHostname); };
00217 
00219     uint GetPort() const { return(mPort); };
00220 
00222     int LookupCode(std::string aName) const;
00223 
00225     std::string LookupName(int aCode) const;
00226 };
00227 
00231 }
00232 
00233 namespace std
00234 {
00235   std::ostream& operator << (std::ostream& os, const PlayerCc::PlayerClient& c);
00236 }
00237 
00238 #endif
00239 

Last updated 12 September 2005 21:38:45