00001 #!/usr/bin/env python 00002 # -*- coding: euc-jp -*- 00003 00004 ## 00005 # @file DataOutPort.py 00006 # @brief Base class of OutPort 00007 # @date $Date: 2007/09/20 $ 00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara 00009 # 00010 # Copyright (C) 2006-2008 00011 # Noriaki Ando 00012 # Task-intelligence Research Group, 00013 # Intelligent Systems Research Institute, 00014 # National Institute of 00015 # Advanced Industrial Science and Technology (AIST), Japan 00016 # All rights reserved. 00017 00018 00019 import OpenRTM 00020 import RTC, RTC__POA 00021 00022 00023 ## 00024 # @if jp 00025 # @class DataOutPort 00026 # @brief Outort 用 Port 00027 # 00028 # データ出力ポートの実装クラス。 00029 # 00030 # @since 0.4.0 00031 # 00032 # @else 00033 # @class DataOutPort 00034 # @brief OutPort abstruct class 00035 # @endif 00036 class DataOutPort(OpenRTM.PortBase): 00037 """ 00038 """ 00039 00040 00041 00042 ## 00043 # @if jp 00044 # @brief コンストラクタ 00045 # 00046 # コンストラクタ 00047 # 00048 # @param self 00049 # @param name ポート名称 00050 # @param outport 当該データ出力ポートに関連付けるOutPortオブジェクト 00051 # 00052 # @else 00053 # @brief Constructor 00054 # @endif 00055 def __init__(self, name, outport): 00056 OpenRTM.PortBase.__init__(self, name) 00057 self._outport = outport 00058 # PortProfile::properties を設定 00059 self.addProperty("port.port_type", "DataOutPort") 00060 self._providers = [] 00061 self._providers.append(OpenRTM.OutPortCorbaProvider(outport)) 00062 self._providers[-1].publishInterfaceProfile(self._profile.properties) 00063 self._consumers = [] 00064 self._consumers.append(OpenRTM.InPortCorbaConsumer(outport)) 00065 self._pf = OpenRTM.PublisherFactory() 00066 00067 00068 ## 00069 # @if jp 00070 # 00071 # @brief Interface 情報を公開する 00072 # 00073 # このオペレーションは、notify_connect() 処理シーケンスの始めにコール 00074 # される関数である。 00075 # notify_connect() では、 00076 # 00077 # - publishInterfaces() 00078 # - connectNext() 00079 # - subscribeInterfaces() 00080 # - updateConnectorProfile() 00081 # 00082 # の順に protected 関数がコールされ接続処理が行われる。 00083 # <br> 00084 # このオペレーションは、新規の connector_id に対しては接続の生成、 00085 # 既存の connector_id に対しては更新が適切に行われる必要がある。 00086 # 00087 # @param self 00088 # @param connector_profile 接続に関するプロファイル情報 00089 # 00090 # @return ReturnCode_t 型のリターンコード 00091 # 00092 # @else 00093 # 00094 # @brief Publish interface information 00095 # 00096 # This operation is pure virutal method that would be called at the 00097 # beginning of the notify_connect() process sequence. 00098 # In the notify_connect(), the following methods would be called in order. 00099 # 00100 # - publishInterfaces() 00101 # - connectNext() 00102 # - subscribeInterfaces() 00103 # - updateConnectorProfile() 00104 # 00105 # This operation should create the new connection for the new 00106 # connector_id, and should update the connection for the existing 00107 # connection_id. 00108 # 00109 # @param connector_profile The connection profile information 00110 # 00111 # @return The return code of ReturnCode_t type. 00112 # 00113 # @endif 00114 def publishInterfaces(self, connector_profile): 00115 for provider in self._providers: 00116 provider.publishInterface(connector_profile.properties) 00117 return RTC.RTC_OK 00118 00119 00120 ## 00121 # @if jp 00122 # 00123 # @brief Interface に接続する 00124 # 00125 # このオペレーションは、notify_connect() 処理シーケンスの中間にコール 00126 # される関数である。 00127 # notify_connect() では、 00128 # 00129 # - publishInterfaces() 00130 # - connectNext() 00131 # - subscribeInterfaces() 00132 # - updateConnectorProfile() 00133 # 00134 # の順に protected 関数がコールされ接続処理が行われる。 00135 # 00136 # @param self 00137 # @param connector_profile 接続に関するプロファイル情報 00138 # 00139 # @return ReturnCode_t 型のリターンコード 00140 # 00141 # @else 00142 # 00143 # @brief Publish interface information 00144 # 00145 # This operation is pure virutal method that would be called at the 00146 # mid-flow of the notify_connect() process sequence. 00147 # In the notify_connect(), the following methods would be called in order. 00148 # 00149 # - publishInterfaces() 00150 # - connectNext() 00151 # - subscribeInterfaces() 00152 # - updateConnectorProfile() 00153 # 00154 # @param connector_profile The connection profile information 00155 # 00156 # @return The return code of ReturnCode_t type. 00157 # 00158 # @endif 00159 def subscribeInterfaces(self, connector_profile): 00160 subscribe = self.subscribe(prof=connector_profile) 00161 for consumer in self._consumers: 00162 subscribe(consumer) 00163 00164 if not subscribe._consumer: 00165 return RTC.RTC_OK 00166 00167 00168 # Publisherを生成 00169 prop = OpenRTM.NVUtil.toProperties(connector_profile.properties) 00170 publisher = self._pf.create(subscribe._consumer.clone(), prop) 00171 00172 # PublisherをOutPortにアタッチ 00173 self._outport.attach(connector_profile.connector_id, publisher) 00174 00175 return RTC.RTC_OK 00176 00177 00178 ## 00179 # @if jp 00180 # 00181 # @brief Interface の接続を解除する 00182 # 00183 # このオペレーションは、notify_disconnect() 処理シーケンスの終わりにコール 00184 # される関数である。 00185 # notify_disconnect() では、 00186 # - disconnectNext() 00187 # - unsubscribeInterfaces() 00188 # - eraseConnectorProfile() 00189 # の順に protected 関数がコールされ接続解除処理が行われる。 00190 # 00191 # @param self 00192 # @param connector_profile 接続に関するプロファイル情報 00193 # 00194 # @else 00195 # 00196 # @brief Disconnect interface connection 00197 # 00198 # This operation is pure virutal method that would be called at the 00199 # end of the notify_disconnect() process sequence. 00200 # In the notify_disconnect(), the following methods would be called. 00201 # - disconnectNext() 00202 # - unsubscribeInterfaces() 00203 # - eraseConnectorProfile() 00204 # 00205 # @param connector_profile The connection profile information 00206 # 00207 # @endif 00208 def unsubscribeInterfaces(self, connector_profile): 00209 publisher = self._outport.detach(connector_profile.connector_id) 00210 self._pf.destroy(publisher) 00211 return 00212 00213 00214 00215 ## 00216 # @if jp 00217 # @brief Interface接続用Functor 00218 # 00219 # Interface接続処理を実行するためのFunctor。 00220 # @else 00221 # 00222 # @endif 00223 class subscribe: 00224 00225 00226 00227 def __init__(self, prof=None, subs=None): 00228 """ 00229 \brief functor 00230 \param prof(RTC.ConnectorProfile) 00231 \param subs(subscribe) 00232 """ 00233 if prof and not subs: 00234 self._prof = prof 00235 self._consumer = None 00236 elif not prof and subs: 00237 self._prof = subs._prof 00238 self._consumer = subs._consumer 00239 else: 00240 print "DataOutPort.subscribe: Invalid parameter." 00241 00242 00243 def __call__(self, cons): 00244 """ 00245 \brief operator()の実装 00246 \param cons(OpenRTM.InPortConsumer) 00247 """ 00248 if cons.subscribeInterface(self._prof.properties): 00249 self._consumer = cons