[openrtm-commit:01696] r641 - trunk/OpenRTM-aist-Python/OpenRTM_aist
openrtm @ openrtm.org
openrtm @ openrtm.org
2016年 2月 1日 (月) 14:24:43 JST
Author: miyamoto
Date: 2016-02-01 14:24:43 +0900 (Mon, 01 Feb 2016)
New Revision: 641
Added:
trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMConsumer.py
trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMProvider.py
trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMConsumer.py
trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMProvider.py
Modified:
trunk/OpenRTM-aist-Python/OpenRTM_aist/FactoryInit.py
trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py
Log:
[incompat,new_func,new_file,->RELENG_1_2] communication method using shared memory has been implemente. refs #3410
Modified: trunk/OpenRTM-aist-Python/OpenRTM_aist/FactoryInit.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/FactoryInit.py 2016-02-01 05:18:31 UTC (rev 640)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/FactoryInit.py 2016-02-01 05:24:43 UTC (rev 641)
@@ -38,5 +38,8 @@
OpenRTM_aist.InPortDirectConsumerInit()
OpenRTM_aist.OutPortDirectProviderInit()
OpenRTM_aist.OutPortDirectConsumerInit()
-
+ OpenRTM_aist.InPortSHMProviderInit()
+ OpenRTM_aist.InPortSHMConsumerInit()
+ OpenRTM_aist.OutPortSHMProviderInit()
+ OpenRTM_aist.OutPortSHMConsumerInit()
Added: trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMConsumer.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMConsumer.py (rev 0)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMConsumer.py 2016-02-01 05:24:43 UTC (rev 641)
@@ -0,0 +1,177 @@
+#!/usr/bin/env python
+# -*- coding: euc-jp -*-
+
+##
+# @file InPortSHMConsumer.py
+# @brief InPortSHMConsumer class
+# @date $Date: 2016-01-12 $
+# @author Nobuhiko Miyamoto
+#
+
+
+import sys
+from omniORB import any
+from omniORB import CORBA
+import OpenRTM_aist
+import OpenRTM
+import mmap, os
+from omniORB import cdrMarshal
+import CORBA
+
+##
+# @if jp
+#
+# @class InPortSHMConsumer
+#
+# @brief InPortSHMConsumer クラス
+#
+# 通信手段に 共有メモリ を利用した入力ポートコンシューマの実装クラス。
+#
+#
+# @else
+# @class InPortCorbaCdrConsumer
+#
+# @brief InPortCorbaCdrConsumer class
+#
+#
+#
+# @endif
+#
+class InPortSHMConsumer(OpenRTM_aist.InPortCorbaCdrConsumer):
+ """
+ """
+
+ ##
+ # @if jp
+ # @brief コンストラクタ
+ #
+ # コンストラクタ
+ #
+ # @param self
+ #
+ # @else
+ # @brief Constructor
+ #
+ # Constructor
+ #
+ # @param self
+ #
+ # @endif
+ #
+ def __init__(self):
+ OpenRTM_aist.InPortCorbaCdrConsumer.__init__(self)
+ self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("InPortSHMConsumer")
+ self._properties = None
+ self._shmem = None
+ self.shm_address = ""
+ return
+
+ ##
+ # @if jp
+ # @brief デストラクタ
+ #
+ # デストラクタ
+ #
+ # @param self
+ # @param CorbaConsumer
+ #
+ # @else
+ # @brief Destructor
+ #
+ # Destructor
+ #
+ # @param self
+ # @param CorbaConsumer
+ #
+ # @endif
+ #
+ def __del__(self, CorbaConsumer=OpenRTM_aist.CorbaConsumer):
+ self._rtcout.RTC_PARANOID("~InPortSHMConsumer()")
+ CorbaConsumer.__del__(self)
+ if self._shmem:
+ self._shmem.close()
+ return
+
+ ##
+ # @if jp
+ # @brief 設定初期化
+ #
+ # InPortConsumerの各種設定を行う
+ # プロバイダでコネクタプロファイルに共有メモリの空間名を保存するため、init関数で共有メモリの初期化を行う
+ #
+ # @param self
+ # @param prop コネクタプロパティ
+ #
+ # @else
+ # @brief Initializing configuration
+ #
+ #
+ # @endif
+ #
+ # virtual void init(coil::Properties& prop);
+ def init(self, prop):
+
+ self._rtcout.RTC_TRACE("init()")
+ self._properties = prop
+
+
+ self.shm_address = prop.getProperty("shared_memory.address")
+ if self.shm_address:
+ if self._shmem is None:
+ self._shmem = mmap.mmap(0, 256, self.shm_address, mmap.ACCESS_WRITE)
+ return
+
+ ##
+ # @if jp
+ # @brief 接続先へのデータ送信
+ #
+ # 接続先のポートへデータを送信する
+ #
+ #
+ # CORBAでデータサイズだけ送信して、データは共有メモリに書き込む
+ #
+ # @param self
+ # @param data 送信するデータ
+ # @return リターンコード
+ #
+ # @else
+ # @brief Send data to the destination port
+ #
+ # @param self
+ # @param data
+ # @return
+ #
+ # @endif
+ #
+ # virtual ReturnCode put(const cdrMemoryStream& data);
+ def put(self, data):
+ self._rtcout.RTC_PARANOID("put()")
+
+ try:
+ ref_ = self.getObject()
+ if ref_:
+ inportcdr = ref_._narrow(OpenRTM.InPortCdr)
+ #print dir(ref_)
+ if self._shmem is not None:
+ self._shmem.seek(os.SEEK_SET)
+
+ self._shmem.write(data)
+ data_size = len(data)
+ mar_data_size = cdrMarshal(CORBA.TC_ushort, data_size)
+
+ return self.convertReturnCode(inportcdr.put(mar_data_size))
+ return self.CONNECTION_LOST
+ except:
+ self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
+ return self.CONNECTION_LOST
+
+
+
+ return self.UNKNOWN_ERROR
+
+
+def InPortSHMConsumerInit():
+ factory = OpenRTM_aist.InPortConsumerFactory.instance()
+ factory.addFactory("shared_memory",
+ OpenRTM_aist.InPortSHMConsumer,
+ OpenRTM_aist.Delete)
Added: trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMProvider.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMProvider.py (rev 0)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/InPortSHMProvider.py 2016-02-01 05:24:43 UTC (rev 641)
@@ -0,0 +1,177 @@
+#!/usr/bin/env python
+# -*- coding: euc-jp -*-
+
+##
+# @file InPortSHMProvider.py
+# @brief InPortSHMProvider class
+# @date $Date: 2016/01/08 $
+# @author Nobuhiko Miyamoto
+
+
+import sys
+from omniORB import *
+from omniORB import any
+
+import OpenRTM_aist
+import OpenRTM__POA,OpenRTM
+import mmap
+from omniORB import cdrUnmarshal
+import CORBA
+
+##
+# @if jp
+# @class InPortSHMProvider
+# @brief InPortSHMProvider クラス
+#
+# 通信手段に 共有メモリ を利用した入力ポートプロバイダーの実装クラス。
+#
+#
+# @else
+# @class InPortCorbaCdrProvider
+# @brief InPortCorbaCdrProvider class
+#
+#
+#
+# @endif
+#
+class InPortSHMProvider(OpenRTM_aist.InPortCorbaCdrProvider):
+
+ """
+ """
+
+ ##
+ # @if jp
+ # @brief コンストラクタ
+ #
+ # コンストラクタ
+ # Interface Typeにはshared_memoryを指定する
+ # 共有メモリの空間名はUUIDで作成し、コネクタプロファイルのdataport.shared_memory.addressに保存する
+ #
+ # self
+ #
+ # @else
+ # @brief Constructor
+ #
+ # Constructor
+ #
+ # self
+ # @endif
+ #
+ def __init__(self):
+ OpenRTM_aist.InPortCorbaCdrProvider.__init__(self)
+
+ # PortProfile setting
+ self.setInterfaceType("shared_memory")
+
+
+
+ self._buffer = None
+
+ self._profile = None
+ self._listeners = None
+ self._shmem = None
+
+ self.shm_address = str(OpenRTM_aist.uuid1())
+ self._properties.append(OpenRTM_aist.NVUtil.newNV("dataport.shared_memory.address",self.shm_address))
+
+
+ return
+
+ ##
+ # @if jp
+ # @brief デストラクタ
+ #
+ # デストラクタ
+ #
+ # @param self
+ #
+ # @else
+ # @brief Destructor
+ #
+ # Destructor
+ #
+ # @param self
+ #
+ # @endif
+ #
+ def __del__(self):
+ oid = OpenRTM_aist.Manager.instance().getPOA.servant_to_id(self)
+ OpenRTM_aist.Manager.instance().getPOA.deactivate_object(oid)
+
+ return
+
+
+ # void init(coil::Properties& prop)
+ def init(self, prop):
+
+ #print prop
+ pass
+
+
+
+ ##
+ # @if jp
+ # @brief バッファにデータを書き込む
+ #
+ # CORBAでデータサイズだけ受信して、共有メモリからデータを取り出しバッファに書き込む
+ #
+ # @param data 書込対象データ
+ #
+ # @else
+ # @brief
+ #
+ #
+ #
+ # @param data
+ #
+ # @endif
+ #
+ # ::OpenRTM::PortStatus put(const ::OpenRTM::CdrData& data)
+ # throw (CORBA::SystemException);
+ def put(self, data):
+ #print self._connector.profile().properties
+ #self._connector.profile().properties.setProperty("dataport.dataflow_type","push")
+ try:
+ self._rtcout.RTC_PARANOID("InPortCorbaCdrProvider.put()")
+
+ if not self._buffer:
+ self.onReceiverError(data)
+ return OpenRTM.PORT_ERROR
+
+ self._rtcout.RTC_PARANOID("received data size: %d", len(data))
+
+ self.onReceived(data)
+
+ if not self._connector:
+ return OpenRTM.PORT_ERROR
+
+ data_size = cdrUnmarshal(CORBA.TC_ushort, data)
+
+ #if self._shmem is None:
+ self._shmem = mmap.mmap(0, data_size, self.shm_address, mmap.ACCESS_READ)
+ #shm_data = cdrUnmarshal(any.to_any(self._connector._dataType).typecode(), self._shmem.read(16),self._connector._endian)
+ shm_data = self._shmem.read(data_size)
+ #print dir(self._connector._provider._this())
+ #print self._connector._provider._this()
+ ret = self._connector.write(shm_data)
+
+
+ return self.convertReturn(ret, shm_data)
+
+ except:
+ self._rtcout.RTC_TRACE(OpenRTM_aist.Logger.print_exception())
+ return OpenRTM.UNKNOWN_ERROR
+ return OpenRTM.UNKNOWN_ERROR
+
+
+
+
+ return self.convertReturn(ret, data)
+
+
+
+def InPortSHMProviderInit():
+ factory = OpenRTM_aist.InPortProviderFactory.instance()
+ factory.addFactory("shared_memory",
+ OpenRTM_aist.InPortSHMProvider,
+ OpenRTM_aist.Delete)
Added: trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMConsumer.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMConsumer.py (rev 0)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMConsumer.py 2016-02-01 05:24:43 UTC (rev 641)
@@ -0,0 +1,183 @@
+#!/usr/bin/env python
+# -*- coding: euc-jp -*-
+
+
+##
+# @file OutPortSHMProvider.py
+# @brief OutPortSHMProvider class
+# @date $Date: 2016-01-12 $
+# @author Nobuhiko Miyamoto
+#
+#
+
+import sys
+from omniORB import any
+import OpenRTM_aist
+import OpenRTM
+
+import mmap
+from omniORB import cdrUnmarshal
+import CORBA
+
+##
+# @if jp
+# @class OutPortSHMConsumer
+#
+# @brief OutPortSHMConsumer クラス
+#
+# 通信手段に 共有メモリ を利用した出力ポートプロバイダーの実装クラス。
+#
+#
+# @else
+# @class OutPortSHMConsumer
+#
+# @brief OutPortSHMConsumer class
+#
+#
+# @endif
+#
+class OutPortSHMConsumer(OpenRTM_aist.OutPortCorbaCdrConsumer):
+ """
+ """
+
+ ##
+ # @if jp
+ # @brief コンストラクタ
+ #
+ # コンストラクタ
+ #
+ # @else
+ # @brief Constructor
+ #
+ # Constructor
+ #
+ # @endif
+ #
+ def __init__(self):
+ OpenRTM_aist.OutPortCorbaCdrConsumer.__init__(self)
+ self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("OutPortSHMConsumer")
+
+ self._shmem = None
+ return
+
+ ##
+ # @if jp
+ # @brief デストラクタ
+ #
+ # デストラクタ
+ #
+ # @else
+ # @brief Destructor
+ #
+ # Destructor
+ #
+ # @endif
+ #
+ def __del__(self, CorbaConsumer=OpenRTM_aist.CorbaConsumer):
+ self._rtcout.RTC_PARANOID("~OutPortSHMConsumer()")
+ CorbaConsumer.__del__(self)
+
+ pass
+
+
+ ##
+ # @if jp
+ # @brief 設定初期化
+ #
+ # OutPortConsumerの各種設定を行う
+ #
+ # @param self
+ # @param prop コネクタプロパティ
+ #
+ # @else
+ # @brief Initializing configuration
+ #
+ #
+ # @endif
+ #
+ # virtual void init(coil::Properties& prop);
+ def init(self, prop):
+ self._rtcout.RTC_TRACE("init()")
+
+ self._properties = prop
+ self.shm_address = prop.getProperty("shared_memory.address")
+
+ return
+
+
+
+
+
+ ##
+ # @if jp
+ # @brief データを読み出す
+ #
+ # 設定されたデータを読み出す。
+ #
+ # CORBAでデータサイズだけ送受信して、データは共有メモリから読み込む
+ #
+ # @param data 読み出したデータを受け取るオブジェクト
+ #
+ # @return リターンコード
+ #
+ # @else
+ # @brief Read data
+ #
+ # Read set data
+ #
+ # @param data Object to receive the read data
+ #
+ # @return Return Code
+ #
+ # @endif
+ #
+ # virtual ReturnCode get(cdrMemoryStream& data);
+ def get(self, data):
+ self._rtcout.RTC_PARANOID("get()")
+
+ try:
+ outportcdr = self.getObject()._narrow(OpenRTM.OutPortCdr)
+ ret,cdr_data = outportcdr.get()
+
+ if ret == OpenRTM.PORT_OK:
+ self._rtcout.RTC_DEBUG("get() successful")
+
+ data_size = cdrUnmarshal(CORBA.TC_ushort, cdr_data)
+ self._shmem = mmap.mmap(0, data_size, self.shm_address, mmap.ACCESS_READ)
+ shm_data = self._shmem.read(data_size)
+
+
+ data[0] = shm_data
+ self.onReceived(data[0])
+ self.onBufferWrite(data[0])
+
+ if self._buffer.full():
+ self._rtcout.RTC_INFO("InPort buffer is full.")
+ self.onBufferFull(data[0])
+ self.onReceiverFull(data[0])
+
+ self._buffer.put(data[0])
+ self._buffer.advanceWptr()
+ self._buffer.advanceRptr()
+
+ return self.PORT_OK
+ return self.convertReturn(ret,data[0])
+
+ except:
+ self._rtcout.RTC_WARN("Exception caught from OutPort.get().")
+ self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
+ return self.CONNECTION_LOST
+
+ self._rtcout.RTC_ERROR("get(): Never comes here.")
+ return self.UNKNOWN_ERROR
+
+
+
+
+
+def OutPortSHMConsumerInit():
+ factory = OpenRTM_aist.OutPortConsumerFactory.instance()
+ factory.addFactory("shared_memory",
+ OpenRTM_aist.OutPortSHMConsumer,
+ OpenRTM_aist.Delete)
+ return
Added: trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMProvider.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMProvider.py (rev 0)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/OutPortSHMProvider.py 2016-02-01 05:24:43 UTC (rev 641)
@@ -0,0 +1,154 @@
+#!/usr/bin/env python
+# -*- coding: euc-jp -*-
+
+##
+# @file OutPortSHMProvider.py
+# @brief OutPortSHMProvider class
+# @date $Date: 2016-01-12 $
+# @author Nobuhiko Miyamoto
+#
+#
+
+import sys
+from omniORB import *
+from omniORB import any
+
+import OpenRTM_aist
+import OpenRTM__POA,OpenRTM
+import mmap, os
+from omniORB import cdrMarshal
+import CORBA
+
+##
+# @if jp
+# @class OutPortSHMProvider
+# @brief OutPortSHMProvider クラス
+#
+# OutPortProvider
+#
+# 通信手段に 共有メモリ を利用した出力ポートプロバイダの実装クラス。
+#
+#
+# @else
+# @class OutPortSHMProvider
+# @brief OutPortSHMProvider class
+#
+#
+#
+# @endif
+#
+class OutPortSHMProvider(OpenRTM_aist.OutPortCorbaCdrProvider):
+ ##
+ # @if jp
+ # @brief コンストラクタ
+ # 共有メモリの空間名はUUIDで作成し、コネクタプロファイルのdataport.shared_memory.addressに保存する
+ #
+ # コンストラクタ
+ #
+ # @else
+ # @brief Constructor
+ #
+ # Constructor
+ #
+ # @endif
+ #
+ def __init__(self):
+ OpenRTM_aist.OutPortCorbaCdrProvider.__init__(self)
+ self.setInterfaceType("shared_memory")
+
+ self.shm_address = str(OpenRTM_aist.uuid1())
+
+ self._properties.append(OpenRTM_aist.NVUtil.newNV("dataport.shared_memory.address",self.shm_address))
+
+ self._shmem = mmap.mmap(0, 256, self.shm_address, mmap.ACCESS_WRITE)
+
+
+
+ return
+
+
+ ##
+ # @if jp
+ # @brief デストラクタ
+ #
+ # デストラクタ
+ #
+ # @else
+ # @brief Destructor
+ #
+ # Destructor
+ #
+ # @endif
+ #
+ def __del__(self):
+ oid = self._default_POA().servant_to_id(self)
+ self._default_POA().deactivate_object(oid)
+
+ self._shmem.close()
+ return
+
+
+
+ # virtual void init(coil::Properties& prop);
+ def init(self, prop):
+ pass
+
+
+
+ ##
+ # @if jp
+ # @brief バッファからデータを取得する
+ #
+ # @return (リターンコード、取得データ)
+ #
+ # @else
+ # @brief Get data from the buffer
+ #
+ #
+ # @return
+ #
+ # @endif
+ #
+ # virtual ::OpenRTM::PortStatus get(::OpenRTM::CdrData_out data);
+ def get(self):
+ self._rtcout.RTC_PARANOID("OutPortSHMProvider.get()")
+ if not self._buffer:
+ self.onSenderError()
+ return (OpenRTM.UNKNOWN_ERROR, None)
+
+ try:
+ if self._buffer.empty():
+ self._rtcout.RTC_ERROR("buffer is empty.")
+ return (OpenRTM.BUFFER_EMPTY, None)
+
+ cdr = [None]
+ ret = self._buffer.read(cdr)
+
+ if ret == OpenRTM_aist.BufferStatus.BUFFER_OK:
+ if not cdr:
+ self._rtcout.RTC_ERROR("buffer is empty.")
+ return (OpenRTM.BUFFER_EMPTY, None)
+
+ except:
+ self._rtcout.RTC_TRACE(OpenRTM_aist.Logger.print_exception())
+ return (OpenRTM.UNKNOWN_ERROR, None)
+
+ self._shmem.seek(os.SEEK_SET)
+ self._shmem.write(cdr[0])
+
+
+ data_size = len(cdr[0])
+ mar_data_size = cdrMarshal(CORBA.TC_ushort, data_size)
+
+
+
+ return self.convertReturn(ret, mar_data_size)
+
+
+
+
+def OutPortSHMProviderInit():
+ factory = OpenRTM_aist.OutPortProviderFactory.instance()
+ factory.addFactory("shared_memory",
+ OpenRTM_aist.OutPortSHMProvider,
+ OpenRTM_aist.Delete)
Modified: trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py
===================================================================
--- trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py 2016-02-01 05:18:31 UTC (rev 640)
+++ trunk/OpenRTM-aist-Python/OpenRTM_aist/__init__.py 2016-02-01 05:24:43 UTC (rev 641)
@@ -104,4 +104,8 @@
from InPortDirectProvider import *
from OutPortDirectConsumer import *
from OutPortDirectProvider import *
+from InPortSHMConsumer import *
+from InPortSHMProvider import *
+from OutPortSHMConsumer import *
+from OutPortSHMProvider import *
from CORBA_RTCUtil import *
More information about the openrtm-commit
mailing list