00001 #!/usr/bin/env python 00002 # -*- coding: euc-jp -*- 00003 00004 ## 00005 # @file PortAdmin.py 00006 # @brief RTC's Port administration class 00007 # @date $Date: 2007/09/03 $ 00008 # @author Noriaki Ando <n-ando@aist.go.jp> and Shinji Kurihara 00009 # 00010 # Copyright (C) 2006-2008 00011 # Task-intelligence Research Group, 00012 # Intelligent Systems Research Institute, 00013 # National Institute of 00014 # Advanced Industrial Science and Technology (AIST), Japan 00015 # All rights reserved. 00016 00017 import traceback 00018 import sys 00019 00020 import RTC, RTC__POA 00021 import OpenRTM 00022 00023 00024 00025 ## 00026 # @if jp 00027 # @class PortAdmin 00028 # @brief PortAdmin クラス 00029 # 00030 # 各種 Port の管理を行うクラス。 00031 # Port の登録/登録解除など各種管理操作を実行するとともに、登録されている 00032 # Port の管理を行うクラス。 00033 # 00034 # @since 0.4.0 00035 # 00036 # @else 00037 # @class PortAdmin 00038 # @brief PortAdmin class 00039 # @endif 00040 class PortAdmin: 00041 """ 00042 """ 00043 00044 00045 00046 ## 00047 # @if jp 00048 # @class comp_op 00049 # @brief Port 管理用内部クラス 00050 # @else 00051 # 00052 # @endif 00053 class comp_op: 00054 def __init__(self, name=None, factory=None): 00055 if name: 00056 self._name = name 00057 if factory: 00058 self._name = factory.getProfile().name 00059 00060 def __call__(self, obj): 00061 name_ = obj.getProfile().name 00062 return self._name == name_ 00063 00064 00065 ## 00066 # @if jp 00067 # @class find_port_name 00068 # @brief Port 検索用ファンクタ 00069 # @else 00070 # @endif 00071 class find_port_name: 00072 def __init__(self, name): 00073 self._name = name 00074 00075 def __call__(self, p): 00076 prof = p.get_port_profile() 00077 name_ = prof.name 00078 return self._name == name_ 00079 00080 00081 ## 00082 # @if jp 00083 # @brief コンストラクタ 00084 # 00085 # コンストラクタ 00086 # 00087 # @param self 00088 # @param orb ORB 00089 # @param poa POA 00090 # 00091 # @else 00092 # @brief Constructor 00093 # @endif 00094 def __init__(self, orb, poa): 00095 # ORB オブジェクト 00096 self._orb = orb 00097 00098 # POA オブジェクト 00099 self._poa = poa 00100 00101 # Portのオブジェクトリファレンスのリスト. PortList 00102 self._portRefs = [] 00103 00104 # サーバントを直接格納するオブジェクトマネージャ 00105 self._portServants = OpenRTM.ObjectManager(self.comp_op) 00106 00107 00108 ## 00109 # @if jp 00110 # 00111 # @brief Port リストの取得 00112 # 00113 # registerPort() により登録された Port の リストを取得する。 00114 # 00115 # @param self 00116 # 00117 # @return Port リスト 00118 # 00119 # @else 00120 # 00121 # @brief Get PortList 00122 # 00123 # This operation returns the pointer to the PortList of Ports regsitered 00124 # by registerPort(). 00125 # 00126 # @return PortList+ The pointer points PortList 00127 # 00128 # @endif 00129 def getPortList(self): 00130 return self._portRefs 00131 00132 00133 ## 00134 # @if jp 00135 # 00136 # @brief Port のオブジェクト参照の取得 00137 # 00138 # port_name で指定した Port のオブジェクト参照を返す。 00139 # port_name で指定する Port はあらかじめ registerPort() で登録されてい 00140 # なければならない。 00141 # 00142 # @param self 00143 # @param port_name 参照を返すPortの名前 00144 # 00145 # @return Port_ptr Portのオブジェクト参照 00146 # 00147 # @else 00148 # 00149 # @brief Get PortList 00150 # 00151 # This operation returns the pointer to the PortList of Ports regsitered 00152 # by registerPort(). 00153 # 00154 # @param port_name The name of Port to be returned the reference. 00155 # 00156 # @return Port_ptr Port's object reference. 00157 # 00158 # @endif 00159 def getPortRef(self, port_name): 00160 index = OpenRTM.CORBA_SeqUtil.find(self._portRefs, self.find_port_name(port_name)) 00161 if index >= 0: 00162 return self._portRefs[index] 00163 return None 00164 00165 00166 ## 00167 # @if jp 00168 # 00169 # @brief Port のサーバントのポインタの取得 00170 # 00171 # port_name で指定した Port のサーバントのポインタを返す。 00172 # port_name で指定する Port はあらかじめ registerPort() で登録されてい 00173 # なければならない。 00174 # 00175 # @param self 00176 # @param port_name 参照を返すPortの名前 00177 # 00178 # @return PortBase* Portサーバント基底クラスのポインタ 00179 # 00180 # @else 00181 # 00182 # @brief Getpointer to the Port's servant 00183 # 00184 # This operation returns the pointer to the PortBase servant regsitered 00185 # by registerPort(). 00186 # 00187 # @param port_name The name of Port to be returned the servant pointer. 00188 # 00189 # @return PortBase* Port's servant's pointer. 00190 # 00191 # @endif 00192 def getPort(self, port_name): 00193 return self._portServants.find(port_name) 00194 00195 00196 ## 00197 # @if jp 00198 # 00199 # @brief Port を登録する 00200 # 00201 # 引数 port で指定された Port のサーバントを登録する。 00202 # 登録された Port のサーバントはコンストラクタで与えられたPOA 上で 00203 # activate され、そのオブジェクト参照はPortのProfileにセットされる。 00204 # 00205 # @param self 00206 # @param port Port サーバント 00207 # 00208 # @else 00209 # 00210 # @brief Regsiter Port 00211 # 00212 # This operation registers the Port's servant given by argument. 00213 # The given Port's servant will be activated on the POA that is given 00214 # to the constructor, and the created object reference is set 00215 # to the Port's profile. 00216 # 00217 # @param port The Port's servant. 00218 # 00219 # @endif 00220 def registerPort(self, port): 00221 self._portRefs.append(port.getPortRef()) 00222 self._portServants.registerObject(port) 00223 00224 00225 ## 00226 # @if jp 00227 # 00228 # @brief Port の登録を解除する 00229 # 00230 # 引数 port で指定された Port の登録を解除する。 00231 # 削除時に Port は deactivate され、PortのProfileのリファレンスには、 00232 # nil値が代入される。 00233 # 00234 # @param self 00235 # @param port Port サーバント 00236 # 00237 # @else 00238 # 00239 # @brief Delete the Port's registration 00240 # 00241 # This operation unregisters the Port's registration. 00242 # When the Port is unregistered, Port is deactivated, and the object 00243 # reference in the Port's profile is set to nil. 00244 # 00245 # @param port The Port's servant. 00246 # 00247 # @endif 00248 def deletePort(self, port): 00249 try: 00250 port.disconnect_all() 00251 00252 tmp = port.getProfile().name 00253 OpenRTM.CORBA_SeqUtil.erase_if(self._portRefs, self.find_port_name(tmp)) 00254 00255 self._poa.deactivate_object(self._poa.servant_to_id(port)) 00256 port.setPortRef(RTC.Port._nil) 00257 00258 self._portServants.unregisterObject(tmp) 00259 except: 00260 traceback.print_exception(*sys.exc_info()) 00261 00262 00263 ## 00264 # @if jp 00265 # 00266 # @brief 名称指定によりPort の登録を解除する 00267 # 00268 # 引数で指定された名前を持つ Port の登録を削除する。 00269 # 削除時に Port は deactivate され、PortのProfileのリファレンスには、 00270 # nil値が代入される。 00271 # 00272 # @param self 00273 # @param port_name Port の名前 00274 # 00275 # @else 00276 # 00277 # @brief Delete the Port' registration 00278 # 00279 # This operation delete the Port's registration specified by port_ name. 00280 # When the Port is unregistered, Port is deactivated, and the object 00281 # reference in the Port's profile is set to nil. 00282 # 00283 # @param port_name The Port's name. 00284 # 00285 # @endif 00286 def deletePortByName(self, port_name): 00287 if not port_name: 00288 return 00289 00290 p = self._portServants.find(port_name) 00291 self.deletePort(p) 00292 00293 00294 ## 00295 # @if jp 00296 # 00297 # @brief 全ての Port をdeactivateし登録を削除する 00298 # 00299 # 登録されている全てのPortに対して、サーバントのdeactivateを行い、 00300 # 登録リストから削除する。 00301 # 00302 # @param self 00303 # 00304 # @else 00305 # 00306 # @brief Unregister the Port 00307 # 00308 # This operation deactivates the all Port and deletes the all Port's 00309 # registrations from the list. 00310 # 00311 # @endif 00312 def finalizePorts(self): 00313 ports = [] 00314 ports = self._portServants.getObjects() 00315 len_ = len(ports) 00316 for i in range(len_): 00317 idx = (len_ - 1) - i 00318 self.deletePort(ports[idx]) 00319 00320 00321