Urbi SDK Remote for C++  2.7.5
usyncclient.hh
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2006-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00013 
00014 
00015 #ifndef URBI_USYNCCLIENT_HH
00016 # define URBI_USYNCCLIENT_HH
00017 
00018 # include <libport/finally.hh>
00019 # include <libport/fwd.hh>
00020 # include <libport/lockable.hh>
00021 # include <libport/semaphore.hh>
00022 # include <libport/utime.hh>
00023 # include <libport/pthread.h>
00024 
00025 # include <urbi/uclient.hh>
00026 
00027 namespace urbi
00028 {
00030   enum UTransmitFormat
00031   {
00033     URBI_TRANSMIT_JPEG,
00035     URBI_TRANSMIT_YCbCr
00036   };
00037 
00040 
00054   class URBI_SDK_API USyncClient: public UClient
00055   {
00056   public:
00057     typedef boost::function1<void, USyncClient*> connect_callback_type;
00058     typedef UClient super_type;
00059     typedef super_type::error_type error_type;
00060 
00066     struct URBI_SDK_API options
00067       : public super_type::options
00068     {
00069       options();
00073       UCLIENT_OPTION(bool, startCallbackThread);
00076       UCLIENT_OPTION(connect_callback_type, connectCallback);
00077     };
00078 
00087     USyncClient(const std::string& host,
00088                 unsigned port = URBI_PORT,
00089                 size_t buflen = URBI_BUFLEN,
00090                 const USyncClient::options& opt = options());
00091 
00092     ~USyncClient();
00093 
00095     struct send_options
00096     {
00097       send_options();
00098 
00099       send_options& timeout(libport::utime_t);
00100       send_options& tag(const char*, const char* = 0);
00101 
00103       libport::utime_t timeout_;
00104       const char* mtag_;
00105       const char* mmod_;
00106       static const send_options default_options;
00107     };
00108 
00109   protected:
00110     virtual error_type onClose ();
00111 
00112   protected:
00125     UMessage*
00126     syncGet_(const char* expression, va_list& arg,
00127              const send_options& options = send_options::default_options);
00128 
00129   public:
00132     ATTRIBUTE_PRINTF(2, 3)
00133     UMessage* syncGet(const char* expression, ...);
00134 
00137     UMessage* syncGet(const std::string& exp);
00138 
00140     ATTRIBUTE_PRINTF(3, 4)
00141     UMessage* syncGet(libport::utime_t useconds,
00142                       const char* expression, ...);
00145     ATTRIBUTE_PRINTF(2, 5)
00146     UMessage* syncGetTag(const char* expression,
00147                          const char* mtag, const char* mmod, ...);
00149     ATTRIBUTE_PRINTF(3, 6)
00150     UMessage* syncGetTag(libport::utime_t useconds,
00151                          const char* expression,
00152                          const char* mtag, const char* mmod, ...);
00153 
00155     int syncSend(const void* buffer, size_t length);
00156 
00159     int syncGetImage(const char* cameraDevice, void* buffer,
00160                      size_t& buffersize,
00161                      int format, int transmitFormat,
00162                      size_t& width, size_t& height,
00163                      libport::utime_t useconds = 0);
00164 
00167     int syncGetValue(const char* valName, UValue& val,
00168                      libport::utime_t useconds = 0);
00169     int syncGetValue(const char* tag, const char* valName, UValue& val,
00170                      libport::utime_t useconds = 0);
00171 
00174     int syncGetDevice(const char* device, ufloat &val,
00175                       libport::utime_t useconds = 0);
00176 
00180     int syncGetResult(const char* command, ufloat &val,
00181                       libport::utime_t useconds = 0);
00182 
00186     int syncGetNormalizedDevice(const char* device, ufloat &val,
00187                                 libport::utime_t useconds = 0);
00188 
00191     int syncGetDevice(const char* device, const char* field, ufloat &val,
00192                       libport::utime_t useconds = 0);
00193 
00195     int syncGetSound(const char* device, int duration, USound &sound,
00196                      libport::utime_t useconds = 0);
00197 
00200     UMessage* waitForTag(const std::string& tag, libport::utime_t useconds = 0);
00201 
00203     void lockQueue();
00204 
00206     virtual void notifyCallbacks(const UMessage &msg);
00207 
00217     bool processEvents(libport::utime_t timeout = -1);
00218 
00224     void stopCallbackThread();
00225     void callbackThread();
00226 
00233     void setSynchronous(bool enable);
00239     using super_type::waitForKernelVersion;
00240     void waitForKernelVersion(bool hasProcessingThread);
00241 
00242     void setDefaultOptions(const USyncClient::send_options& opt);
00243     const USyncClient::send_options&
00244       getOptions(const USyncClient::send_options& opt =
00245                  USyncClient::send_options::default_options) const;
00246 
00253     static boost::shared_ptr<libport::Finally>
00254     listen(const std::string& host, const std::string& port,
00255            boost::system::error_code& erc,
00256            connect_callback_type connectCallback,
00257            size_t buflen = URBI_BUFLEN,
00258            bool startCallbackThread = true);
00259 
00260     virtual void onConnect();
00261 
00263     bool isCallbackThread() const;
00264   protected:
00265     int joinCallbackThread_();
00266 
00267     static libport::Socket* onAccept(connect_callback_type l, size_t buflen,
00268                                      bool startThread);
00269     // Incremented at each queue push, decremented on pop.
00270     libport::Semaphore sem_;
00271     // Semaphore to delay execution of callback thread until ctor finishes.
00272     libport::Semaphore callbackSem_;
00273 
00274     // The list of incoming messages waiting to be processed.
00275     std::list<UMessage*> queue;
00276     libport::Lockable queueLock_;
00277 
00281     UMessage* message_;
00282     libport::Semaphore syncLock_;
00283     std::string syncTag;
00284 
00285     send_options default_options_;
00286 
00287     bool stopCallbackThread_;
00288     pthread_t cbThread;
00289     // Used to block until the callback thread is realy stopped.
00290     libport::Semaphore stopCallbackSem_;
00291     connect_callback_type connectCallback_;
00292     // True if waitForTag is waiting from the polling thread.
00293     bool waitingFromPollThread_;
00294     // If set, bypass callback thread and send messages synchronously.
00295     bool synchronous_;
00296   };
00297 
00298 } // namespace urbi
00299 
00300 #endif // ! URBI_USYNCCLIENT_HH