PublisherNew.py

説明を見る。
00001 #!/usr/bin/env python
00002 # -*- coding: euc-jp -*-
00003 
00004 ##
00005 # @file  PublisherNew.py
00006 # @brief PublisherNew class
00007 # @date  $Date: 2007/09/27 $
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 import threading
00019 
00020 import OpenRTM
00021 
00022 
00023 ##
00024 # @if jp
00025 # @class PublisherNew
00026 # @brief PublisherNew クラス
00027 #
00028 # バッファ内に新規データが格納されたタイミングで、その新規データを送信する。
00029 # データ送出タイミングを待つコンシューマを、送出する側とは異なるスレッドで
00030 # 動作させる場合に使用。
00031 # Publisherの駆動は、データ送出のタイミングになるまでブロックされ、
00032 # 送出タイミングの通知を受けると、即座にコンシューマの送出処理を呼び出す。
00033 #
00034 # @else
00035 # @class PublisherNew
00036 # @brief PublisherNew class
00037 # @endif
00038 class PublisherNew(OpenRTM.PublisherBase):
00039   """
00040   """
00041 
00042 
00043 
00044   ##
00045   # @if jp
00046   # @brief コンストラクタ
00047   #
00048   # コンストラクタ
00049   # 本 Publisher 用新規スレッドを生成する。
00050   #
00051   # @param self
00052   # @param consumer データ送出を待つコンシューマ
00053   # @param property 本Publisherの駆動制御情報を設定したPropertyオブジェクト
00054   #                 (本Publisherでは未使用)
00055   # @else
00056   # @brief Constructor
00057   # @endif
00058   def __init__(self, consumer, property):
00059     self._data = self.NewData()
00060     self._consumer = consumer
00061     self._running = True
00062     self._thread = threading.Thread(target=self.run)
00063     self._thread.start()
00064 
00065 
00066   ##
00067   # @if jp
00068   # @brief デストラクタ
00069   #
00070   # デストラクタ
00071   #
00072   # @param self
00073   #
00074   # @else
00075   # @brief Destructor
00076   #
00077   # @endif
00078   def __del__(self):
00079     del self._consumer
00080 
00081 
00082   ##
00083   # @if jp
00084   # @brief Observer関数
00085   #
00086   # 送出タイミング時に呼び出す。
00087   # ブロックしている当該Publisherの駆動が開始され、コンシューマへの送出処理が
00088   # 行われる。
00089   #
00090   # @param self
00091   #
00092   # @else
00093   # @brief Observer function
00094   # @endif
00095   def update(self):
00096     if not self._data._cond.acquire(0):
00097       return
00098 
00099     self._data._updated = True
00100     self._data._cond.notify()
00101     self._data._cond.release()
00102     return
00103 
00104 
00105   ##
00106   # @if jp
00107   # @brief タスク開始関数
00108   #
00109   # 本Publisher駆動制御用スレッドの実行を開始する。
00110   #
00111   # @param self
00112   #
00113   # @else
00114   # @brief Thread execution function
00115   # @endif
00116   def run(self):
00117     while self._running:
00118       self._data._cond.acquire()
00119       # Waiting for new data updated
00120       while not self._data._updated and self._running:
00121         self._data._cond.wait()
00122 
00123       if self._data._updated:
00124         self._consumer.push()
00125         self._data._updated = False
00126 
00127       self._data._cond.release()
00128 
00129 
00130   ##
00131   # @if jp
00132   # @brief タスク終了関数
00133   #
00134   # ACE_Task::release() のオーバーライド
00135   # 駆動フラグをfalseに設定し、本 Publisher の駆動を停止する。
00136   # ただし、駆動スレッドがブロックされている場合には、
00137   # 最大1回コンシューマの送出処理が呼び出される場合がある。
00138   #
00139   # @param self
00140   #
00141   # @else
00142   # @brief Task terminate function
00143   #
00144   # ACE_Task::release() override function
00145   #
00146   # @endif
00147   def release(self):
00148     if not self._data._cond.acquire(0):
00149       return
00150 
00151     self._running = False
00152     self._data._cond.notify()
00153     self._data._cond.release()
00154     #self.wait()
00155 
00156 
00157   # NewData condition struct
00158   ##
00159   # @if jp
00160   # @class NewData
00161   # @brief データ状態管理用内部クラス
00162   # @else
00163   # @endif
00164   class NewData:
00165     def __init__(self):
00166       self._mutex = threading.RLock()
00167       self._cond = threading.Condition(self._mutex)
00168       self._updated = False

OpenRTMに対してMon Mar 17 15:11:06 2008に生成されました。  doxygen 1.5.4