Urbi SDK Remote for C++  2.7.5
ucallbacks.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007-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 
00012 
00013 #include <iostream>
00014 #include <sstream>
00015 #include <list>
00016 
00017 #include <libport/containers.hh>
00018 #include <libport/debug.hh>
00019 #include <libport/lexical-cast.hh>
00020 
00021 #include <urbi/ucallbacks.hh>
00022 #include <urbi/usyncclient.hh>
00023 #include <urbi/uexternal.hh>
00024 #include <urbi/uobject.hh>
00025 
00026 #include <libuobject/remote-ucontext-impl.hh>
00027 
00028 GD_CATEGORY(Urbi.LibUObject);
00029 
00030 namespace urbi
00031 {
00032 
00033   namespace
00034   {
00035     static
00036     std::string
00037     callback_name(const std::string& name, const std::string& type,
00038                   int size)
00039     {
00040       std::string res = name;
00041       if (type == "function" || type == "event" || type == "eventend")
00042         res += "__" + string_cast(size);
00043       return res;
00044     }
00045   }
00046 
00047   namespace impl
00048   {
00049     void
00050     RemoteUGenericCallbackImpl::initialize(UGenericCallback* owner, bool)
00051     {
00052       owner_ = owner;
00053       std::string type = owner_->type;
00054       //owner_->name =
00055       // callback_name(owner_->name, owner_->type, owner_->nbparam);
00056       GD_FINFO("Registering %s %s %s into %s from %s",
00057                type,
00058                owner_->name,
00059                owner_->nbparam,
00060                owner_->name,
00061                owner_->objname);
00062       LockableOstream& cl
00063         = *dynamic_cast<RemoteUContextImpl*>(owner_->ctx_)->outputStream;
00064       if (type == "var")
00065         URBI_SEND_PIPED_COMMAND_C
00066           (cl,
00067            libport::format("external %s %s from %s",
00068                            type, owner_->name, owner_->objname));
00069       else if (type == "event" || type == "function")
00070         URBI_SEND_PIPED_COMMAND_C
00071           (cl,
00072            libport::format("external %s (%s) %s from %s",
00073                            type, owner_->nbparam, owner_->name,
00074                            owner_->objname));
00075       else if (type == "varaccess")
00076         GD_WARN("NotifyAccess facility is not available for modules in "
00077                 "remote mode.");
00078     }
00079 
00081     void
00082     RemoteUGenericCallbackImpl::initialize(UGenericCallback* owner)
00083     {
00084       owner_ = owner;
00085       LockableOstream& cl
00086         = *dynamic_cast<RemoteUContextImpl*>(owner_->ctx_)->outputStream;
00087       URBI_SEND_PIPED_COMMAND_C
00088         (cl,
00089            libport::format("external %s %s",
00090                            owner->type, owner_->name));
00091     }
00092 
00093     void
00094     RemoteUGenericCallbackImpl::clear()
00095     {
00096     }
00097 
00098     void
00099     RemoteUGenericCallbackImpl::registerCallback()
00100     {
00101       RemoteUContextImpl* ctx = static_cast<RemoteUContextImpl*>(owner_->ctx_);
00102       GD_FINFO("Pushing %s in %s", owner_->name, owner_->type);
00103       UTable& t =
00104         dynamic_cast<RemoteUContextImpl*>(owner_->ctx_)
00105         ->tableByName(owner_->type);
00106       t[callback_name(owner_->name, owner_->type, owner_->nbparam)]
00107         .push_back(owner_);
00108       if (owner_->target)
00109       {
00110         static_cast<RemoteUVarImpl*>(owner_->target->impl_)
00111           ->callbacks_.push_back(this);
00112         std::string targetname = owner_->target->get_name();
00113         // Register the UVar to be updated if it's not there.
00114         std::list<UVar*> &us = ctx->varmap()[targetname];
00115         if (!libport::has(us, owner_->target))
00116           us.push_back(owner_->target);
00117       }
00118     }
00119 
00120   }
00121 
00122 } // namespace urbi