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 6558 2008-06-13 23:37:38Z thjc $
00042 */
00043 
00044 #ifndef PLAYERCLIENT_H
00045 #define PLAYERCLIENT_H
00046 
00047 #include "libplayerc++/playerc++config.h"
00048 #include "libplayerc++/utility.h"
00049 
00050 #include <string>
00051 #include <list>
00052 
00053 #ifdef HAVE_BOOST_SIGNALS
00054   #include <boost/signal.hpp>
00055 #endif
00056 
00057 #ifdef HAVE_BOOST_THREAD
00058   #include <boost/thread/mutex.hpp>
00059   #include <boost/thread/thread.hpp>
00060   #include <boost/thread/xtime.hpp>
00061   #include <boost/bind.hpp>
00062 #else
00063   // we have to define this so we don't have to
00064   // comment out all the instances of scoped_lock
00065   // in all the proxies
00066   namespace boost
00067   {
00068     class thread
00069     {
00070       public:
00071         thread() {};
00072     };
00073 
00074     class mutex
00075     {
00076       public:
00077         mutex() {};
00078         class scoped_lock
00079         {
00080           public: scoped_lock(mutex /*m*/) {};
00081         };
00082     };
00083   }
00084 
00085 #endif
00086 
00087 namespace PlayerCc
00088 {
00089 
00090 class ClientProxy;
00091 
00103 class PlayerClient
00104 {
00105   friend class ClientProxy;
00106 
00107   // our thread type
00108   typedef boost::thread thread_t;
00109 
00110   // our mutex type
00111   typedef boost::mutex mutex_t;
00112 
00113   private:
00114     // list of proxies associated with us
00115     std::list<PlayerCc::ClientProxy*> mProxyList;
00116 
00117     std::list<playerc_device_info_t> mDeviceList;
00118 
00119     // Connect to the indicated host and port.
00120     // @exception throws PlayerError if unsuccessfull
00121     void Connect(const std::string aHostname, uint32_t aPort);
00122 
00123     // Disconnect from server.
00124     void Disconnect();
00125 
00126     //  our c-client from playerc
00127     playerc_client_t* mClient;
00128 
00129     // The hostname of the server, stored for convenience
00130     std::string mHostname;
00131 
00132     // The port number of the server, stored for convenience
00133     uint32_t mPort;
00134 
00135     // Which transport (TCP or UDP) we're using
00136     unsigned int mTransport;
00137 
00138     // Is the thread currently stopped or stopping?
00139     bool mIsStop;
00140 
00141     // This is the thread where we run @ref Run()
00142     thread_t* mThread;
00143 
00144     // A helper function for starting the thread
00145     void RunThread();
00146 
00147   public:
00148 
00150     PlayerClient(const std::string aHostname=PLAYER_HOSTNAME,
00151                  uint32_t aPort=PLAYER_PORTNUM,
00152                  int transport=PLAYERC_TRANSPORT_TCP);
00153 
00155     ~PlayerClient();
00156 
00158     mutex_t mMutex;
00159 
00160     // ideally, we'd use the read_write mutex, but I was having some problems
00161     // (with their code) because it's under development
00162     //boost::read_write_mutex mMutex;
00163 
00165     void StartThread();
00166 
00168     void StopThread();
00169 
00171     void Run(uint32_t aTimeout=10); // aTimeout in ms
00172 
00174     void Stop();
00175 
00182     bool Peek(uint32_t timeout=0);
00183     //bool Peek2(uint32_t timeout=0);
00184 
00186     void SetRequestTimeout(uint32_t seconds) { playerc_client_set_request_timeout(this->mClient,seconds); }
00187 
00188     
00192     void SetRetryLimit(int limit) { playerc_client_set_retry_limit(this->mClient,limit); }
00193 
00196     int GetRetryLimit() { return(this->mClient->retry_limit); }
00197 
00200     void SetRetryTime(double time) { playerc_client_set_retry_time(this->mClient,time); }
00201 
00204     double GetRetryTime() { return(this->mClient->retry_time); }
00205 
00213     void Read();
00214 
00219     void ReadIfWaiting();
00220 
00221 //    /// @brief You can change the rate at which your client receives data from the
00222 //    /// server with this method.  The value of @p freq is interpreted as Hz;
00223 //    /// this will be the new rate at which your client receives data (when in
00224 //    /// continuous mode).
00225 //    ///
00226 //    /// @exception throws PlayerError if unsuccessfull
00227 //     void SetFrequency(uint32_t aFreq);
00228 
00241     void SetDataMode(uint32_t aMode);
00242 
00259     void SetReplaceRule(bool aReplace,
00260                         int aType = -1,
00261                         int aSubtype = -1,
00262                         int aInterf = -1);
00263 
00266     void RequestDeviceList();
00267 
00268     std::list<playerc_device_info_t> GetDeviceList();
00269 
00271     std::string GetHostname() const { return(mHostname); };
00272 
00274     uint32_t GetPort() const { return(mPort); };
00275 
00277     int LookupCode(std::string aName) const;
00278 
00280     std::string LookupName(int aCode) const;
00281     
00283     uint32_t GetOverflowCount();
00284 };
00285 
00286 
00287 
00288 }
00289 
00290 namespace std
00291 {
00292   std::ostream& operator << (std::ostream& os, const PlayerCc::PlayerClient& c);
00293 }
00294 
00295 #endif
00296 

Last updated 12 September 2005 21:38:45