Urbi SDK Remote for C++  2.7.5
remote-ucontext-impl.hh
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009-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 
00011 #ifndef LIBUOBJECT_REMOTE_UCONTEXT_IMPL_HH
00012 # define LIBUOBJECT_REMOTE_UCONTEXT_IMPL_HH
00013 
00014 # include <libport/package-info.hh>
00015 
00016 # include <serialize/binary-o-serializer.hh>
00017 # include <urbi/uobject.hh>
00018 # include <urbi/usyncclient.hh>
00019 
00020 namespace urbi
00021 {
00022   namespace impl
00023   {
00024     class URBI_SDK_API RemoteUContextImpl
00025       : public impl::UContextImpl
00026     {
00027     public:
00029       RemoteUContextImpl(USyncClient* client);
00030       virtual ~RemoteUContextImpl();
00031       virtual void newUObjectClass(baseURBIStarter* s);
00032       virtual void newUObjectHubClass(baseURBIStarterHub* s);
00033       virtual void uobject_unarmorAndSend(const char* str);
00034       virtual void send(const char* str);
00035       virtual void send(const void* buf, size_t size);
00036       void send(const std::string& s)
00037       {
00038         send(s.c_str(), s.size());
00039       }
00040       virtual void call(const std::string& object,
00041                         const std::string& method,
00042                         UAutoValue v1 = UAutoValue(),
00043                         UAutoValue v2 = UAutoValue(),
00044                         UAutoValue v3 = UAutoValue(),
00045                         UAutoValue v4 = UAutoValue(),
00046                         UAutoValue v5 = UAutoValue(),
00047                         UAutoValue v6 = UAutoValue());
00048       virtual void declare_event(const UEvent* owner);
00049       virtual void emit(const std::string& object,
00050                         UAutoValue& v1,
00051                         UAutoValue& v2,
00052                         UAutoValue& v3,
00053                         UAutoValue& v4,
00054                         UAutoValue& v5,
00055                         UAutoValue& v6,
00056                         UAutoValue& v7
00057                         );
00058       virtual UObjectMode getRunningMode() const;
00059       virtual std::pair<int, int> kernelVersion() const;
00060       virtual void yield() const;
00061       virtual void yield_until(libport::utime_t deadline) const;
00062       virtual void yield_until_things_changed() const;
00063       virtual void side_effect_free_set(bool s);
00064       virtual bool side_effect_free_get() const;
00065       virtual UVarImpl* getVarImpl();
00066       virtual UObjectImpl* getObjectImpl();
00067       virtual UGenericCallbackImpl* getGenericCallbackImpl();
00068       virtual TimerHandle setTimer(UTimerCallback* cb);
00069       virtual void registerHub(UObjectHub*);
00070       virtual void removeHub(UObjectHub*) ;
00071       virtual void setHubUpdate(UObjectHub*, ufloat);
00072       virtual void instanciated(UObject* obj);
00073       virtual void lock();
00074       virtual void unlock();
00075       virtual boost::asio::io_service& getIoService();
00076 
00077     public:
00079       UCallbackAction dispatcher(const UMessage& msg);
00080       USyncClient* getClient();
00087       void makeRTPLink(const std::string& key);
00088 
00090       void assignMessage(const std::string& name, const UValue& v, time_t ts,
00091                          bool bypass=false, UValue* target=0,
00092                          time_t* tsTarget = 0);
00093 
00099       void evalFunctionMessage(const std::string& name,
00100                                const std::string& var,
00101                                UList& args);
00102 
00106       void setRTPMessage(const std::string& varname,
00107                          int state);
00108 
00110       void setSerializationMode(bool);
00111 
00113       UMessage* syncGet(const std::string& exp, libport::utime_t timeout=0);
00114 
00118       void markDataSent();
00119       USyncClient* backend_;
00120 
00124       std::string hookPointName();
00126       bool closed_;
00127 #define TABLE(Type, Name)                       \
00128     private:                                    \
00129       Type Name ## _;                           \
00130     public:                                     \
00131       Type& Name()                              \
00132       {                                         \
00133         return Name ## _;                       \
00134       }
00135       TABLE(UTable, accessmap);
00136       TABLE(UTable, eventmap);
00137       TABLE(UTable, eventendmap);
00138       TABLE(UTable, functionmap);
00139       TABLE(UTable, monitormap);
00140       TABLE(UVarTable, varmap);
00141       TABLE(UTimerTable, timermap);
00142 #undef TABLE
00143       libport::Lockable tableLock;
00144       UValue localCall(const std::string& object,
00145                 const std::string& method,
00146                 UAutoValue v1 = UAutoValue(),
00147                 UAutoValue v2 = UAutoValue(),
00148                 UAutoValue v3 = UAutoValue(),
00149                 UAutoValue v4 = UAutoValue(),
00150                 UAutoValue v5 = UAutoValue(),
00151                 UAutoValue v6 = UAutoValue(),
00152                 UAutoValue v7 = UAutoValue(),
00153                 UAutoValue v8 = UAutoValue());
00154       UTable& tableByName(const std::string& n);
00155       UCallbackAction clientError(const UMessage&);
00156       UCallbackAction onRTPListenMessage(const UMessage&);
00157       // Create it on demand.
00158       UObject* getDummyUObject();
00159       void onTimer(UTimerCallback* cb);
00160       UObject* dummyUObject;
00161       typedef boost::unordered_map<std::string,
00162         std::pair<libport::AsyncCallHandler, UTimerCallback*> > TimerMap;
00163       TimerMap timerMap;
00164       libport::Lockable mapLock;
00165       // Pool of RTP connections (key= UVar name)
00166       typedef boost::unordered_map<std::string, UObject*> RTPLinks;
00167       RTPLinks rtpLinks;
00168       // Use RTP connections in this context if available
00169       bool enableRTP;
00170       unsigned int dispatchDepth;
00171       // Stream to use for urbiscript output
00172       LockableOstream* outputStream;
00173       // True when something was sent. Reset by dispatcher().
00174       bool dataSent;
00175       // Send serialized binary messages if set.
00176       bool serializationMode;
00177       libport::serialize::BinaryOSerializer* oarchive;
00178       libport::PackageInfo::Version version;
00179       // Name of the hook-point UObject for UVars.
00180       std::string hookPointName_;
00181       // Shared RTP link cached instance.
00182       UObject* sharedRTP_;
00183       #define URBI_REMOTE_RTP_INIT_CHANNEL "__remote_rtp_init"
00184     };
00185 
00186     class URBI_SDK_API RemoteUObjectImpl: public UObjectImpl
00187     {
00188     public:
00189       virtual ~RemoteUObjectImpl();
00190       virtual void initialize(UObject* owner);
00191       virtual void clean();
00192       virtual void setUpdate(ufloat period);
00193       virtual bool removeTimer(TimerHandle h);
00194       void onUpdate();
00195 
00196     private:
00197       UObject* owner_;
00198       ufloat period;
00199       libport::AsyncCallHandler updateHandler;
00200     };
00201     class RemoteUGenericCallbackImpl;
00202     class URBI_SDK_API RemoteUVarImpl: public UVarImpl
00203     {
00204     public:
00205       virtual void initialize(UVar* owner);
00206       virtual void clean();
00207       virtual void setOwned();
00208       virtual void sync();
00209       virtual void request();
00210       virtual void keepSynchronized();
00211       virtual void set(const UValue& v);
00212       virtual const UValue& get() const;
00213       virtual ufloat& in();
00214       virtual ufloat& out();
00215       virtual UDataType type() const;
00216       virtual UValue getProp(UProperty prop);
00217       virtual void setProp(UProperty prop, const UValue& v);
00218       virtual bool setBypass(bool enable);
00219       virtual time_t timestamp() const;
00220       virtual void unnotify();
00221       virtual void useRTP(bool enable);
00222       virtual void setInputPort(bool enable);
00223     private:
00224       // transmit the value to the remote kernel
00225       void transmit(const UValue& v, libport::utime_t timestamp);
00226       // Transmit in serialized mode.
00227       void transmitSerialized(const UValue& v, libport::utime_t time);
00228       USyncClient* client_;
00229       // Last value, shared among all UVarImpls with the same name.
00230       UValue* value_;
00231       UVar* owner_;
00232       time_t* timestamp_; // shared among all UVarImpls with same name.
00233       friend class RemoteUGenericCallbackImpl;
00234       friend class RemoteUContextImpl;
00235       std::vector<RemoteUGenericCallbackImpl*> callbacks_;
00236       bool bypass_;
00237     };
00238 
00239     class URBI_SDK_API RemoteUGenericCallbackImpl: public UGenericCallbackImpl
00240     {
00241     public:
00242       virtual void initialize(UGenericCallback* owner, bool owned);
00243       virtual void initialize(UGenericCallback* owner);
00244       virtual void registerCallback();
00245       virtual void clear();
00246 
00247     private:
00248       UGenericCallback* owner_;
00249       friend class RemoteUVarImpl;
00250     };
00251   }
00252 }
00253 #endif // !LIBUOBJECT_REMOTE_UCONTEXT_IMPL_HH