InPortCorbaConsumer.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #ifndef InPortCorbaConsumer_h
00021 #define InPortCorbaConsumer_h
00022 
00023 #include <rtm/idl/DataPortSkel.h>
00024 #include <rtm/BufferBase.h>
00025 #include <rtm/CorbaConsumer.h>
00026 #include <rtm/InPortConsumer.h>
00027 #include <rtm/Manager.h>
00028 #include <iostream>
00029 
00030 namespace RTC
00031 {
00059   template <class DataType>
00060   class InPortCorbaConsumer
00061     : public InPortConsumer,
00062       public CorbaConsumer<RTC::InPortAny>
00063   {
00064   public:
00082     InPortCorbaConsumer(BufferBase<DataType>& buffer)
00083       : rtclog("InPortCorbaConsumer"), m_buffer(buffer)
00084     {
00085       rtclog.setLevel("PARANOID");
00086     }
00087     
00105     InPortCorbaConsumer(const InPortCorbaConsumer<DataType>& consumer)
00106       : CorbaConsumer<RTC::InPortAny>(consumer),
00107         rtclog("InPortCorbaConsumer"), m_buffer(consumer.m_buffer)
00108     {
00109       rtclog.setLevel("PARANOID");
00110     }
00111     
00133     InPortCorbaConsumer&
00134     operator=(const InPortCorbaConsumer<DataType>& consumer)
00135     {
00136       if (this == &consumer) return *this;
00137       m_buffer = consumer.m_buffer;
00138       m_var = consumer.m_var;  
00139     }
00140     
00154     virtual ~InPortCorbaConsumer(void)
00155     {
00156     }
00157     
00175     void put(DataType& data)
00176     {
00177       RTC_PARANOID(("put()"));
00178       CORBA::Any tmp;
00179       tmp <<= data;
00180       _ptr()->put(tmp);
00181     }
00182     
00196     void push()
00197     {
00198       RTC_PARANOID(("push()"));
00199       DataType data;
00200       CORBA::Any tmp;
00201       m_buffer.read(data);
00202       tmp <<= data;
00203       
00204       // 本当はエラー処理をすべき
00205       if (CORBA::is_nil(_ptr())) return;
00206       try
00207         {
00208           _ptr()->put(tmp);
00209         }
00210       catch(...)
00211         {
00212           RTC_INFO(("exception while invoking _ptr()->put()"));
00213           //hoge オブジェクトが無効になったらdisconnectすべき
00214           return;
00215         }
00216     }
00217     
00235     virtual InPortCorbaConsumer* clone() const
00236     {
00237       RTC_TRACE(("clone()"));
00238       return new InPortCorbaConsumer<DataType>(*this);
00239     }
00240     
00263     virtual bool subscribeInterface(const SDOPackage::NVList& properties)
00264     {
00265       RTC_TRACE(("subscribeInterface()"));
00266       RTC_DEBUG_STR((NVUtil::toString(properties)));
00267 
00268 //      if (!NVUtil::isStringValue(properties,
00269 //                               "dataport.dataflow_type",
00270 //                               "Push"))
00271 //      {
00272 //        return false;
00273 //      }
00274 
00275        // getting InPort's ref from IOR string
00276       if (subscribeFromIor(properties)) { return true; }
00277 
00278       // getting InPort's ref from Object reference
00279       if (subscribeFromRef(properties)) { return true; }
00280 
00281       return false;;
00282     }
00283     
00301     virtual void unsubscribeInterface(const SDOPackage::NVList& properties)
00302     {
00303       RTC_TRACE(("unsubscribeInterface()"));
00304       RTC_DEBUG_STR((NVUtil::toString(properties)));
00305 
00306       if (unsubscribeFromIor(properties)) { return; }
00307       unsubscribeFromRef(properties);
00308     }
00309 
00310   private:
00324     bool subscribeFromIor(const SDOPackage::NVList& properties)
00325     {
00326       RTC_TRACE(("subscribeFromIor()"));
00327 
00328       CORBA::Long index;
00329       index = NVUtil::find_index(properties,
00330                                  "dataport.corba_any.inport_ior");
00331       if (index < 0)
00332         {
00333           RTC_ERROR(("inport_ior not found"));
00334           return false;
00335         }
00336 
00337       const char* ior(0);
00338       if (!(properties[index].value >>= ior))
00339         {
00340           RTC_ERROR(("inport_ior has no string"));
00341           return false;
00342         }
00343 
00344       CORBA::ORB_ptr orb = RTC::Manager::instance().getORB();
00345       CORBA::Object_var obj = orb->string_to_object(ior);
00346       
00347       if (CORBA::is_nil(obj))
00348         {
00349           RTC_ERROR(("invalid IOR string has been passed"));
00350           return false;
00351         }
00352 
00353       if (!setObject(obj.in()))
00354         {
00355           RTC_WARN(("Setting object to consumer failed."));
00356           return false;
00357         }
00358       return true;
00359     }
00360 
00374     bool subscribeFromRef(const SDOPackage::NVList& properties)
00375     {
00376       RTC_TRACE(("subscribeFromRef()"));
00377       CORBA::Long index;
00378       index = NVUtil::find_index(properties,
00379                                  "dataport.corba_any.inport_ref");
00380       if (index < 0)
00381         {
00382           RTC_ERROR(("inport_ref not found"));
00383           return false;
00384         }
00385 
00386       CORBA::Object_var obj;
00387       if (!(properties[index].value >>= CORBA::Any::to_object(obj.out())))
00388         {
00389           RTC_ERROR(("prop[inport_ref] is not objref"));
00390           return true;
00391         }
00392 
00393       if (CORBA::is_nil(obj))
00394         {
00395           RTC_ERROR(("prop[inport_ref] is not objref"));
00396           return false;
00397         }
00398       
00399       if (!setObject(obj.in()))
00400         {
00401           RTC_ERROR(("Setting object to consumer failed."));
00402           return false;
00403         }
00404       return true;
00405     }
00406 
00420     bool unsubscribeFromIor(const SDOPackage::NVList& properties)
00421     {
00422       RTC_TRACE(("unsubscribeFromIor()"));
00423       CORBA::Long index;
00424       index = NVUtil::find_index(properties,
00425                                  "dataport.corba_any.inport_ior");
00426       if (index < 0)
00427         {
00428           RTC_ERROR(("inport_ior not found"));
00429           return false;
00430         }
00431 
00432       const char* ior;
00433       if (!(properties[index].value >>= ior))
00434         {
00435           RTC_ERROR(("prop[inport_ior] is not string"));
00436           return false;
00437         }
00438 
00439       CORBA::ORB_ptr orb = RTC::Manager::instance().getORB();
00440       CORBA::Object_var var = orb->string_to_object(ior);
00441       if (!(_ptr()->_is_equivalent(var)))
00442         {
00443           RTC_ERROR(("connector property inconsistency"));
00444           return false;
00445         }
00446 
00447       releaseObject();
00448       return true;
00449     }
00450 
00464     bool unsubscribeFromRef(const SDOPackage::NVList& properties)
00465     {
00466       RTC_TRACE(("unsubscribeFromRef()"));
00467       CORBA::Long index;
00468       index = NVUtil::find_index(properties,
00469                                  "dataport.corba_any.inport_ref");
00470       if (index < 0) { return false; }
00471 
00472       CORBA::Object_var obj;
00473       if (!(properties[index].value >>= CORBA::Any::to_object(obj.out()))) 
00474         {
00475           return false;
00476         }
00477 
00478       if (!(_ptr()->_is_equivalent(obj.in()))) { return false; }
00479 
00480       releaseObject();
00481       return true;
00482     }
00483 
00484 
00485 
00486     
00487   private:
00488     mutable Logger rtclog;
00489     BufferBase<DataType>& m_buffer;
00490   };
00491 };     // namespace RTC
00492 #endif // InPortCorbaConsumer_h

Generated on Sun May 24 14:08:45 2009 for OpenRTM by  doxygen 1.5.3