コンシューマRTCの場合
コンシューマRTC の場合の設定・修正箇所もほぼ同じです。
RTC Builder
MyServiceProvider 用のRTC.xmlをコピーして開き、
基本タブのモジュール名を MyServiceConsumer に、サービスポートタブの myservice0 インターフェースの方向を Required に変更します。
MyServiceConsumer.py の修正
import 節
CorbaConsumer を使うために、import 節に一行追加します。
# Import RTM module import jp.go.aist.rtm.RTC OpenRTM_aist = jp.go.aist.rtm.RTC OpenRTM_aist.__dict__['Properties'] = jp.go.aist.rtm.RTC.util.Properties OpenRTM_aist.__dict__['CorbaPort'] = jp.go.aist.rtm.RTC.port.CorbaPort OpenRTM_aist.__dict__['CorbaConsumer'] = jp.go.aist.rtm.RTC.port.CorbaConsumer # この行を追加
SimpleService.MyService もインポートします。
# Import Service stub modules # <rtc-template block="consumer_import"> from SimpleService import MyService # この行を追加
MyServiceConsumer クラス
- __init__
これにより、MyServiceConsumer.__init__ でこのように書けます。
self._myservice = OpenRTM_aist.CorbaConsumer(MyService)
- onExecute
onExecute の実装例です。
def onExecute(self, ec_id): print "onExecute" print "\n" print "Command list: " print " echo [msg] : echo message." print " set_value [value]: set value." print " get_value : get current value." print " get_echo_history : get input messsage history." print " get_value_history: get input value history." print "> ", args = str(sys.stdin.readline()) argv = string.split(args) argv[-1] = argv[-1].rstrip("\n") if argv[0] == "echo" and len(argv) > 1: retmsg = self._myservice._ptr().echo(argv[1]) print "echo() return: ", retmsg return self.super__onExecute(ec_id) if argv[0] == "set_value" and len(argv) > 1: val = float(argv[1]) self._myservice._ptr().set_value(val) print "Set remote value: ", val return self.super__onExecute(ec_id) if argv[0] == "get_value": retval = self._myservice._ptr().get_value() print "Current remote value: ", retval return self.super__onExecute(ec_id) if argv[0] == "get_echo_history": echo_history = self._myservice._ptr().get_echo_history() for i in range(len(echo_history)): print repr(i) + ": " + echo_history[i] return self.super__onExecute(ec_id) if argv[0] == "get_value_history": value_history = self._myservice._ptr().get_value_history() print value_history, len(value_history) for i in range(len(value_history)): print repr(i) + ": " + repr(value_history[i]) return self.super__onExecute(ec_id) print "Invalid command or argument(s)." return self.super__onExecute(ec_id)
プロバイダRTC の echo 命令を呼ぶには、self._myservice._ptr().echo(...) とします。_ptr() を忘れがちなので注意してください。
あとの変更点は MyServiceProvider と同じです。
Jython でこれができるということは、他にも応用が効きます。例えばJRuby! 近いうちにJRubyでのやり方も紹介します。