OutPortCorbaCdrConsumer.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: euc-jp -*-
00003 
00004 
00005 ##
00006 # @file  OutPortCorbaCdrConsumer.py
00007 # @brief OutPortCorbaCdrConsumer class
00008 # @date  $Date: 2008-01-13 10:28:27 $
00009 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara
00010 #
00011 # Copyright (C) 2009
00012 #     Noriaki Ando
00013 #     Task-intelligence Research Group,
00014 #     Intelligent Systems Research Institute,
00015 #     National Institute of
00016 #         Advanced Industrial Science and Technology (AIST), Japan
00017 #     All rights reserved.
00018 #
00019 
00020 import sys
00021 from omniORB import any
00022 import OpenRTM_aist
00023 import OpenRTM
00024 
00025 ##
00026 # @if jp
00027 # @class OutPortCorbaCdrConsumer
00028 #
00029 # @brief OutPortCorbaCdrConsumer クラス
00030 #
00031 # 通信手段に CORBA を利用した出力ポートコンシューマの実装クラス。
00032 #
00033 # @param DataType 本ポートにて扱うデータ型
00034 #
00035 # @since 1.0.0
00036 #
00037 # @else
00038 # @class OutPortCorbaCdrConsumer
00039 #
00040 # @brief OutPortCorbaCdrConsumer class
00041 #
00042 # This is an implementation class of the output Consumer 
00043 # that uses CORBA for means of communication.
00044 #
00045 # @param DataType Data type for this port
00046 #
00047 # @since 1.0.0
00048 #
00049 # @endif
00050 #
00051 class OutPortCorbaCdrConsumer(OpenRTM_aist.OutPortConsumer,OpenRTM_aist.CorbaConsumer):
00052   """
00053   """
00054 
00055   ##
00056   # @if jp
00057   # @brief コンストラクタ
00058   #
00059   # コンストラクタ
00060   #
00061   # @param buffer 本ポートに割り当てるバッファ
00062   #
00063   # @else
00064   # @brief Constructor
00065   #
00066   # Constructor
00067   #
00068   # @param buffer Buffer that is attached to this port
00069   #
00070   # @endif
00071   #
00072   def __init__(self):
00073     OpenRTM_aist.CorbaConsumer.__init__(self)
00074     self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("OutPortCorbaCdrConsumer")
00075     self._buffer = None
00076     self._profile = None
00077     self._listeners = None
00078     return
00079 
00080   ##
00081   # @if jp
00082   # @brief デストラクタ
00083   #
00084   # デストラクタ
00085   #
00086   # @else
00087   # @brief Destructor
00088   #
00089   # Destructor
00090   #
00091   # @endif
00092   #
00093   def __del__(self, CorbaConsumer=OpenRTM_aist.CorbaConsumer):
00094     self._rtcout.RTC_PARANOID("~OutPortCorbaCdrConsumer()")
00095     CorbaConsumer.__del__(self)
00096     pass
00097 
00098 
00099   ##
00100   # @if jp
00101   # @brief 設定初期化
00102   #
00103   # OutPortConsumerの各種設定を行う。実装クラスでは、与えられた
00104   # Propertiesから必要な情報を取得して各種設定を行う。この init() 関
00105   # 数は、OutPortProvider生成直後および、接続時にそれぞれ呼ばれる可
00106   # 能性がある。したがって、この関数は複数回呼ばれることを想定して記
00107   # 述されるべきである。
00108   # 
00109   # @param prop 設定情報
00110   #
00111   # @else
00112   #
00113   # @brief Initializing configuration
00114   #
00115   # This operation would be called to configure in initialization.
00116   # In the concrete class, configuration should be performed
00117   # getting appropriate information from the given Properties data.
00118   # This function might be called right after instantiation and
00119   # connection sequence respectivly.  Therefore, this function
00120   # should be implemented assuming multiple call.
00121   #
00122   # @param prop Configuration information
00123   #
00124   # @endif
00125   #
00126   # virtual void init(coil::Properties& prop);
00127   def init(self, prop):
00128     self._rtcout.RTC_TRACE("init()")
00129     return
00130 
00131 
00132   ##
00133   # @if jp
00134   # @brief バッファをセットする
00135   #
00136   # OutPortConsumerがデータを取り出すバッファをセットする。
00137   # すでにセットされたバッファがある場合、以前のバッファへの
00138   # ポインタに対して上書きされる。
00139   # OutPortProviderはバッファの所有権を仮定していないので、
00140   # バッファの削除はユーザの責任で行わなければならない。
00141   #
00142   # @param buffer OutPortProviderがデータを取り出すバッファへのポインタ
00143   #
00144   # @else
00145   # @brief Setting outside buffer's pointer
00146   #
00147   # A pointer to a buffer from which OutPortProvider retrieve data.
00148   # If already buffer is set, previous buffer's pointer will be
00149   # overwritten by the given pointer to a buffer.  Since
00150   # OutPortProvider does not assume ownership of the buffer
00151   # pointer, destructor of the buffer should be done by user.
00152   # 
00153   # @param buffer A pointer to a data buffer to be used by OutPortProvider
00154   #
00155   # @endif
00156   #
00157   # virtual void setBuffer(CdrBufferBase* buffer);
00158   def setBuffer(self, buffer):
00159     self._rtcout.RTC_TRACE("setBuffer()")
00160     self._buffer = buffer
00161     return
00162 
00163 
00164   # void OutPortCorbaCdrConsumer::setListener(ConnectorInfo& info,
00165   #                                           ConnectorListeners* listeners)
00166   def setListener(self, info, listeners):
00167     self._rtcout.RTC_TRACE("setListener()")
00168     self._listeners = listeners
00169     self._profile = info
00170     return
00171 
00172 
00173   ##
00174   # @if jp
00175   # @brief データを読み出す
00176   #
00177   # 設定されたデータを読み出す。
00178   #
00179   # @param data 読み出したデータを受け取るオブジェクト
00180   #
00181   # @return データ読み出し処理結果(読み出し成功:true、読み出し失敗:false)
00182   #
00183   # @else
00184   # @brief Read data
00185   #
00186   # Read set data
00187   #
00188   # @param data Object to receive the read data
00189   #
00190   # @return Read result (Successful:true, Failed:false)
00191   #
00192   # @endif
00193   #
00194   # virtual ReturnCode get(cdrMemoryStream& data);
00195   def get(self, data):
00196     self._rtcout.RTC_PARANOID("get()")
00197 
00198     try:
00199       outportcdr = self.getObject()._narrow(OpenRTM.OutPortCdr)
00200       ret,cdr_data = outportcdr.get()
00201       
00202       if ret == OpenRTM.PORT_OK:
00203         self._rtcout.RTC_DEBUG("get() successful")
00204         data[0] = cdr_data
00205         self.onReceived(data[0])
00206         self.onBufferWrite(data[0])
00207         
00208         if self._buffer.full():
00209           self._rtcout.RTC_INFO("InPort buffer is full.")
00210           self.onBufferFull(data[0])
00211           self.onReceiverFull(data[0])
00212           
00213         self._buffer.put(data[0])
00214         self._buffer.advanceWptr()
00215         self._buffer.advanceRptr()
00216 
00217         return self.PORT_OK
00218       return self.convertReturn(ret,data[0])
00219 
00220     except:
00221       self._rtcout.RTC_WARN("Exception caught from OutPort.get().")
00222       self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00223       return self.CONNECTION_LOST
00224 
00225     self._rtcout.RTC_ERROR("get(): Never comes here.")
00226     return self.UNKNOWN_ERROR
00227 
00228 
00229   ##
00230   # @if jp
00231   # @brief データ受信通知への登録
00232   #
00233   # 指定されたプロパティに基づいて、データ受信通知の受け取りに登録する。
00234   #
00235   # @param properties 登録情報
00236   #
00237   # @return 登録処理結果(登録成功:true、登録失敗:false)
00238   #
00239   # @else
00240   # @brief Subscribe the data receive notification
00241   #
00242   # Subscribe the data receive notification based on specified property
00243   # information
00244   #
00245   # @param properties Subscription information
00246   #
00247   # @return Subscription result (Successful:true, Failed:false)
00248   #
00249   # @endif
00250   #
00251   # virtual bool subscribeInterface(const SDOPackage::NVList& properties);
00252   def subscribeInterface(self, properties):
00253     self._rtcout.RTC_TRACE("subscribeInterface()")
00254     index = OpenRTM_aist.NVUtil.find_index(properties,"dataport.corba_cdr.outport_ior")
00255         
00256     if index < 0:
00257       self._rtcout.RTC_DEBUG("dataport.corba_cdr.outport_ior not found.")
00258       return False
00259 
00260     if OpenRTM_aist.NVUtil.isString(properties,"dataport.corba_cdr.outport_ior"):
00261       self._rtcout.RTC_DEBUG("dataport.corba_cdr.outport_ior found.")
00262       ior = ""
00263       try:
00264         ior = any.from_any(properties[index].value, keep_structs=True)
00265       except:
00266         self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00267             
00268       orb = OpenRTM_aist.Manager.instance().getORB()
00269       obj = orb.string_to_object(ior)
00270       ret = self.setObject(obj)
00271       if ret:
00272         self._rtcout.RTC_DEBUG("CorbaConsumer was set successfully.")
00273       else:
00274         self._rtcout.RTC_ERROR("Invalid object reference.")
00275         
00276       return ret
00277 
00278     return False
00279 
00280 
00281   ##
00282   # @if jp
00283   # @brief データ受信通知からの登録解除
00284   #
00285   # データ受信通知の受け取りから登録を解除する。
00286   #
00287   # @param properties 登録解除情報
00288   #
00289   # @else
00290   # @brief Unsubscribe the data receive notification
00291   #
00292   # Unsubscribe the data receive notification.
00293   #
00294   # @param properties Unsubscription information
00295   #
00296   # @endif
00297   #
00298   # virtual void unsubscribeInterface(const SDOPackage::NVList& properties);
00299   def unsubscribeInterface(self, properties):
00300     self._rtcout.RTC_TRACE("unsubscribeInterface()")
00301     index = OpenRTM_aist.NVUtil.find_index(properties,
00302                                            "dataport.corba_cdr.outport_ref")
00303     if index < 0:
00304       self._rtcout.RTC_DEBUG("dataport.corba_cdr.outport_ior not found.")
00305       return
00306     
00307     ior = ""
00308     try:
00309       ior = any.from_any(properties[index].value, keep_structs=True)
00310                 
00311       if ior:
00312         self._rtcout.RTC_DEBUG("dataport.corba_cdr.outport_ior found.")
00313         orb = OpenRTM_aist.Manager.instance().getORB()
00314         obj = orb.string_to_object(ior)
00315         if self._ptr()._is_equivalent(obj):
00316           self.releaseObject()
00317           self._rtcout.RTC_DEBUG("CorbaConsumer's reference was released.")
00318           return
00319 
00320         self._rtcout.RTC_ERROR("hmm. Inconsistent object reference.")
00321 
00322     except:
00323       self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00324 
00325     return
00326 
00327 
00328   ##
00329   # @if jp
00330   # @brief リターンコード変換 (DataPortStatus -> BufferStatus)
00331   # @else
00332   # @brief Return codes conversion
00333   # @endif
00334   #
00335   # ReturnCode convertReturn(::OpenRTM::PortStatus status,
00336   #                          const cdrMemoryStream& data)
00337   def convertReturn(self, status, data):
00338     if status == OpenRTM.PORT_OK:
00339       # never comes here
00340       return self.PORT_OK
00341 
00342     elif status == OpenRTM.PORT_ERROR:
00343       self.onSenderError()
00344       return self.PORT_ERROR
00345 
00346     elif status == OpenRTM.BUFFER_FULL:
00347       # never comes here
00348       return self.BUFFER_FULL
00349 
00350     elif status == OpenRTM.BUFFER_EMPTY:
00351       self.onSenderEmpty()
00352       return self.BUFFER_EMPTY
00353 
00354     elif status == OpenRTM.BUFFER_TIMEOUT:
00355       self.onSenderTimeout()
00356       return self.BUFFER_TIMEOUT
00357 
00358     elif status == OpenRTM.UNKNOWN_ERROR:
00359       self.onSenderError()
00360       return self.UNKNOWN_ERROR
00361 
00362     else:
00363       self.onSenderError()
00364       return self.UNKNOWN_ERROR
00365 
00366     self.onSenderError()
00367     return self.UNKNOWN_ERROR
00368 
00369     
00370   ##
00371   # @brief Connector data listener functions
00372   #
00373   # inline void onBufferWrite(const cdrMemoryStream& data)
00374   def onBufferWrite(self, data):
00375     if self._listeners is not None and self._profile is not None:
00376       self._listeners.connectorData_[OpenRTM_aist.ConnectorDataListenerType.ON_BUFFER_WRITE].notify(self._profile, data)
00377 
00378     return
00379 
00380 
00381   # inline void onBufferFull(const cdrMemoryStream& data)
00382   def onBufferFull(self, data):
00383     if self._listeners is not None and self._profile is not None:
00384       self._listeners.connectorData_[OpenRTM_aist.ConnectorDataListenerType.ON_BUFFER_FULL].notify(self._profile, data)
00385 
00386     return
00387 
00388 
00389   # inline void onReceived(const cdrMemoryStream& data)
00390   def onReceived(self, data):
00391     if self._listeners is not None and self._profile is not None:
00392       self._listeners.connectorData_[OpenRTM_aist.ConnectorDataListenerType.ON_RECEIVED].notify(self._profile, data)
00393 
00394     return
00395 
00396 
00397   # inline void onReceiverFull(const cdrMemoryStream& data)
00398   def onReceiverFull(self, data):
00399     if self._listeners is not None and self._profile is not None:
00400       self._listeners.connectorData_[OpenRTM_aist.ConnectorDataListenerType.ON_RECEIVER_FULL].notify(self._profile, data)
00401 
00402     return
00403 
00404 
00405   ##
00406   # @brief Connector listener functions
00407   #
00408   # inline void onSenderEmpty()
00409   def onSenderEmpty(self):
00410     if self._listeners is not None and self._profile is not None:
00411       self._listeners.connector_[OpenRTM_aist.ConnectorListenerType.ON_SENDER_EMPTY].notify(self._profile)
00412 
00413     return
00414 
00415 
00416   # inline void onSenderTimeout()
00417   def onSenderTimeout(self):
00418     if self._listeners is not None and self._profile is not None:
00419       self._listeners.connector_[OpenRTM_aist.ConnectorListenerType.ON_SENDER_TIMEOUT].notify(self._profile)
00420 
00421     return
00422 
00423       
00424   # inline void onSenderError()
00425   def onSenderError(self):
00426     if self._listeners is not None and self._profile is not None:
00427       self._listeners.connector_[OpenRTM_aist.ConnectorListenerType.ON_SENDER_ERROR].notify(self._profile)
00428 
00429     return
00430 
00431 
00432 def OutPortCorbaCdrConsumerInit():
00433   factory = OpenRTM_aist.OutPortConsumerFactory.instance()
00434   factory.addFactory("corba_cdr",
00435                      OpenRTM_aist.OutPortCorbaCdrConsumer,
00436                      OpenRTM_aist.Delete)
00437   return


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Aug 27 2015 14:17:28