00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 import string
00022 import sys
00023 import time
00024
00025 from omniORB import CORBA
00026 import OpenRTM
00027 import RTC
00028 import OpenRTM_aist
00029
00030
00031 periodicecsharedcomposite_spec = ["implementation_id", "PeriodicECSharedComposite",
00032 "type_name", "PeriodicECSharedComposite",
00033 "description", "PeriodicECSharedComposite",
00034 "version", "1.0",
00035 "vendor", "jp.go.aist",
00036 "category", "composite.PeriodicECShared",
00037 "activity_type", "DataFlowComponent",
00038 "max_instance", "0",
00039 "language", "Python",
00040 "lang_type", "script",
00041 "exported_ports", "",
00042 "conf.default.members", "",
00043 "conf.default.exported_ports", "",
00044 ""]
00045
00046
00047 def stringToStrVec(v, _is):
00048 str = [_is]
00049 OpenRTM_aist.eraseBlank(str)
00050 v[0] = str[0].split(",")
00051 return True
00052
00053
00054 class setCallback(OpenRTM_aist.ConfigurationSetListener):
00055 def __init__(self, org):
00056 self._org = org
00057 pass
00058
00059 def __call__(self, config_set):
00060 self._org.updateDelegatedPorts()
00061
00062
00063
00064 class addCallback(OpenRTM_aist.ConfigurationSetListener):
00065 def __init__(self, org):
00066 self._org = org
00067 pass
00068
00069 def __call__(self, config_set):
00070 self._org.updateDelegatedPorts()
00071 return
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 class PeriodicECOrganization(OpenRTM_aist.Organization_impl):
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 def __init__(self, rtobj):
00109 OpenRTM_aist.Organization_impl.__init__(self,rtobj.getObjRef())
00110 self._rtobj = rtobj
00111 self._ec = None
00112 self._rtcMembers = []
00113 self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("rtobject.PeriodicECOrganization")
00114 self._expPorts = []
00115 return
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 def add_members(self, sdo_list):
00142 self._rtcout.RTC_DEBUG("add_members()")
00143 self.updateExportedPortsList()
00144 for sdo in sdo_list:
00145 dfc = [None]
00146 if not self.sdoToDFC(sdo, dfc):
00147 continue
00148 member = self.Member(dfc[0])
00149 self.stopOwnedEC(member)
00150 self.addOrganizationToTarget(member)
00151 self.addParticipantToEC(member)
00152 self.addPort(member, self._expPorts)
00153 self._rtcMembers.append(member)
00154
00155 result = OpenRTM_aist.Organization_impl.add_members(self,sdo_list)
00156
00157 return result
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 def set_members(self, sdo_list):
00185 self._rtcout.RTC_DEBUG("set_members()")
00186
00187 self.removeAllMembers()
00188 self.updateExportedPortsList()
00189
00190 for sdo in sdo_list:
00191 dfc = [None]
00192 if not self.sdoToDFC(sdo, dfc):
00193 continue
00194
00195 member = self.Member(dfc[0])
00196 self.stopOwnedEC(member)
00197 self.addOrganizationToTarget(member)
00198 self.addParticipantToEC(member)
00199 self.addPort(member, self._expPorts)
00200 self._rtcMembers.append(member)
00201
00202 result = OpenRTM_aist.Organization_impl.set_members(self, sdo_list)
00203
00204 return result
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 def remove_member(self, id):
00230 self._rtcout.RTC_DEBUG("remove_member(id = %s)", id)
00231 rm_rtc = []
00232 for member in self._rtcMembers:
00233 if str(id) != str(member._profile.instance_name):
00234 continue
00235 self.removePort(member, self._expPorts)
00236 self._rtobj.getProperties().setProperty("conf.default.exported_ports", OpenRTM_aist.flatten(self._expPorts))
00237 self.removeParticipantFromEC(member)
00238 self.removeOrganizationFromTarget(member)
00239 self.startOwnedEC(member)
00240 rm_rtc.append(member)
00241
00242 for m in rm_rtc:
00243 self._rtcMembers.remove(m)
00244
00245 result = OpenRTM_aist.Organization_impl.remove_member(self, id)
00246 return result
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 def removeAllMembers(self):
00257 self._rtcout.RTC_DEBUG("removeAllMembers()")
00258 self.updateExportedPortsList()
00259 for member in self._rtcMembers:
00260 self.removePort(member, self._expPorts)
00261 self.removeParticipantFromEC(member)
00262 self.removeOrganizationFromTarget(member)
00263 self.startOwnedEC(member)
00264 OpenRTM_aist.Organization_impl.remove_member(self, member._profile.instance_name)
00265
00266 self._rtcMembers = []
00267 self._expPorts = []
00268 return
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 def sdoToDFC(self, sdo, dfc):
00280 if CORBA.is_nil(sdo):
00281 return False
00282
00283 dfc[0] = sdo._narrow(OpenRTM.DataFlowComponent)
00284 if CORBA.is_nil(dfc[0]):
00285 return False
00286
00287 return True
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 def stopOwnedEC(self, member):
00299 ecs = member._eclist
00300 for ec in ecs:
00301 ret = ec.stop()
00302
00303 return
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 def startOwnedEC(self, member):
00314 ecs = member._eclist
00315 for ec in ecs:
00316 ret = ec.start()
00317
00318 return
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 def addOrganizationToTarget(self, member):
00330 conf = member._config
00331 if CORBA.is_nil(conf):
00332 return
00333
00334 conf.add_organization(self._objref)
00335 return
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 def removeOrganizationFromTarget(self, member):
00347
00348 if CORBA.is_nil(member._config):
00349 return
00350
00351
00352 ret = member._config.remove_organization(self._pId)
00353 return
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 def addParticipantToEC(self, member):
00365 if CORBA.is_nil(self._ec) or self._ec is None:
00366 ecs = self._rtobj.get_owned_contexts()
00367 if len(ecs) > 0:
00368 self._ec = ecs[0]
00369 else:
00370 return
00371
00372 ret = self._ec.add_component(member._rtobj)
00373
00374 orglist = member._rtobj.get_organizations()
00375 for org in orglist:
00376 sdos = org.get_members()
00377 for sdo in sdos:
00378 dfc = [None]
00379 if not self.sdoToDFC(sdo, dfc):
00380 continue
00381 self._ec.add_component(dfc[0])
00382 return
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393 def removeParticipantFromEC(self, member):
00394 if CORBA.is_nil(self._ec) or self._ec is None:
00395 ecs = self._rtobj.get_owned_contexts()
00396 if len(ecs) > 0:
00397 self._ec = ecs[0]
00398 else:
00399 self._rtcout.RTC_FATAL("no owned EC")
00400 return
00401 self._ec.remove_component(member._rtobj)
00402
00403 orglist = member._rtobj.get_organizations()
00404
00405 for org in orglist:
00406 sdos = org.get_members()
00407 for sdo in sdos:
00408 dfc = [None]
00409 if not self.sdoToDFC(sdo, dfc):
00410 continue
00411 self._ec.remove_component(dfc[0])
00412 return
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 def addPort(self, member, portlist):
00445 self._rtcout.RTC_TRACE("addPort(%s)", OpenRTM_aist.flatten(portlist))
00446 if len(portlist) == 0:
00447 return
00448
00449 comp_name = member._profile.instance_name
00450 plist = member._profile.port_profiles
00451
00452
00453 for prof in plist:
00454
00455 port_name = prof.name
00456
00457 self._rtcout.RTC_DEBUG("port_name: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
00458 if not port_name in portlist:
00459 self._rtcout.RTC_DEBUG("Not found: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
00460 continue
00461
00462 self._rtcout.RTC_DEBUG("Found: %s is in %s", (port_name,OpenRTM_aist.flatten(portlist)))
00463 self._rtobj.addPort(prof.port_ref)
00464 self._rtcout.RTC_DEBUG("Port %s was delegated.", port_name)
00465
00466 return
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 def removePort(self, member, portlist):
00478 self._rtcout.RTC_DEBUG("removePort()")
00479 if len(portlist) == 0:
00480 return
00481
00482 comp_name = member._profile.instance_name
00483 plist = member._profile.port_profiles
00484
00485
00486 for prof in plist:
00487
00488 port_name = prof.name
00489
00490 self._rtcout.RTC_DEBUG("port_name: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
00491 if not port_name in portlist:
00492 self._rtcout.RTC_DEBUG("Not found: %s is in %s?", (port_name,OpenRTM_aist.flatten(portlist)))
00493 continue
00494
00495 self._rtcout.RTC_DEBUG("Found: %s is in %s", (port_name,OpenRTM_aist.flatten(portlist)))
00496 self._rtobj.removePort(prof.port_ref)
00497 portlist.remove(port_name)
00498 self._rtcout.RTC_DEBUG("Port %s was deleted.", port_name)
00499
00500 return
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510 def updateExportedPortsList(self):
00511 plist = self._rtobj.getProperties().getProperty("conf.default.exported_ports")
00512 if plist:
00513 p = [plist]
00514 OpenRTM_aist.eraseBlank(p)
00515 self._expPorts = p[0].split(",")
00516
00517 return
00518
00519
00520
00521
00522
00523
00524
00525
00526 def updateDelegatedPorts(self):
00527 oldPorts = self._expPorts
00528 ports = self._rtobj.getProperties().getProperty("conf.default.exported_ports")
00529 newPorts = ports.split(",")
00530
00531
00532 removedPorts = list(set(oldPorts).difference(set(newPorts)))
00533 createdPorts = list(set(newPorts).difference(set(oldPorts)))
00534
00535 self._rtcout.RTC_VERBOSE("old ports: %s", OpenRTM_aist.flatten(oldPorts))
00536 self._rtcout.RTC_VERBOSE("new ports: %s", OpenRTM_aist.flatten(newPorts))
00537 self._rtcout.RTC_VERBOSE("remove ports: %s", OpenRTM_aist.flatten(removedPorts))
00538 self._rtcout.RTC_VERBOSE("add ports: %s", OpenRTM_aist.flatten(createdPorts))
00539
00540 for member in self._rtcMembers:
00541 self.removePort(member, removedPorts)
00542 self.addPort(member, createdPorts)
00543
00544 self._expPorts = newPorts
00545 return
00546
00547
00548 class Member:
00549 def __init__(self, rtobj):
00550 self._rtobj = rtobj
00551 self._profile = rtobj.get_component_profile()
00552 self._eclist = rtobj.get_owned_contexts()
00553 self._config = rtobj.get_configuration()
00554 return
00555
00556 def __call__(self, x):
00557 tmp = x
00558 tmp.swap(self)
00559 return self
00560
00561
00562 def swap(self, x):
00563 rtobj = x._rtobj
00564 profile = x._profile
00565 eclist = x._eclist
00566 config = x._config
00567
00568 x._rtobj = self._rtobj
00569 x._profile = self._profile
00570 x._eclist = self._eclist
00571 x._config = self._config
00572
00573 self._rtobj = rtobj
00574 self._profile = profile
00575 self._eclist = eclist
00576 self._config = config
00577 return
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617 class PeriodicECSharedComposite(OpenRTM_aist.RTObject_impl):
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637 def __init__(self, manager):
00638 OpenRTM_aist.RTObject_impl.__init__(self,manager)
00639 self._ref = self._this()
00640 self._objref = self._ref
00641 self._org = OpenRTM_aist.PeriodicECOrganization(self)
00642 OpenRTM_aist.CORBA_SeqUtil.push_back(self._sdoOwnedOrganizations,
00643 self._org.getObjRef())
00644
00645 self._members = [[]]
00646 self.bindParameter("members", self._members, "", stringToStrVec)
00647 self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf("rtobject.periodic_ec_shared")
00648 self._configsets.addConfigurationSetListener(\
00649 OpenRTM_aist.ConfigurationSetListenerType.ON_SET_CONFIG_SET,
00650 setCallback(self._org))
00651
00652 self._configsets.addConfigurationSetListener(\
00653 OpenRTM_aist.ConfigurationSetListenerType.ON_ADD_CONFIG_SET,
00654 addCallback(self._org))
00655
00656 return
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 def __del__(self):
00673 self._rtcout.RTC_TRACE("destructor of PeriodicECSharedComposite")
00674 pass
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692 def onInitialize(self):
00693 self._rtcout.RTC_TRACE("onInitialize()")
00694
00695 active_set = self._properties.getProperty("configuration.active_config",
00696 "default")
00697 if self._configsets.haveConfig(active_set):
00698 self._configsets.update(active_set)
00699 else:
00700 self._configsets.update("default")
00701
00702 mgr = OpenRTM_aist.Manager.instance()
00703 sdos = []
00704 for member in self._members[0]:
00705 if member == "":
00706 continue
00707
00708 rtc = mgr.getComponent(member)
00709
00710 if rtc is None:
00711 print "no RTC found: ", member
00712 continue
00713
00714 sdo = rtc.getObjRef()
00715 if CORBA.is_nil(sdo):
00716 continue
00717
00718 OpenRTM_aist.CORBA_SeqUtil.push_back(sdos, sdo)
00719
00720 try:
00721 self._org.set_members(sdos)
00722 except:
00723 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00724
00725 return RTC.RTC_OK
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759 def onActivated(self, exec_handle):
00760 self._rtcout.RTC_TRACE("onActivated(%d)", exec_handle)
00761 ecs = self.get_owned_contexts()
00762 sdos = self._org.get_members()
00763
00764 for sdo in sdos:
00765 rtc = sdo._narrow(RTC.RTObject)
00766 ecs[0].activate_component(rtc)
00767
00768 len_ = len(self._members[0])
00769
00770
00771
00772 if len_ > 1:
00773 str_ = "s were"
00774 else:
00775 str_ = "was"
00776
00777 self._rtcout.RTC_DEBUG("%d member RTC%s activated.", (len_, str_))
00778
00779 return RTC.RTC_OK
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813 def onDeactivated(self, exec_handle):
00814 self._rtcout.RTC_TRACE("onDeactivated(%d)", exec_handle)
00815 ecs = self.get_owned_contexts()
00816 sdos = self._org.get_members()
00817
00818 for sdo in sdos:
00819 rtc = sdo._narrow(RTC.RTObject)
00820 ecs[0].deactivate_component(rtc)
00821
00822 return RTC.RTC_OK
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855 def onReset(self, exec_handle):
00856 self._rtcout.RTC_TRACE("onReset(%d)", exec_handle)
00857 ecs = self.get_owned_contexts()
00858 sdos = self._org.get_members()
00859
00860 for sdo in sdos:
00861 rtc = sdo._narrow(RTC.RTObject)
00862 ecs[0].reset_component(rtc)
00863
00864 return RTC.RTC_OK
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891 def onFinalize(self):
00892 self._rtcout.RTC_TRACE("onFinalize()")
00893 self._org.removeAllMembers()
00894 self._rtcout.RTC_PARANOID("onFinalize() done")
00895 return RTC.RTC_OK
00896
00897
00898
00899 def PeriodicECSharedCompositeInit(manager):
00900 profile = OpenRTM_aist.Properties(defaults_str=periodicecsharedcomposite_spec)
00901 manager.registerFactory(profile,
00902 OpenRTM_aist.PeriodicECSharedComposite,
00903 OpenRTM_aist.Delete)
00904