00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import copy
00019 import sys
00020 import threading
00021 import time
00022 from omniORB import CORBA
00023 import OpenRTM_aist
00024 import RTC,RTM,RTM__POA
00025 import SDOPackage
00026
00027
00028 class ManagerServant(RTM__POA.Manager):
00029 """
00030 """
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 def __init__(self):
00046 self._mgr = OpenRTM_aist.Manager.instance()
00047 self._owner = None
00048 self._rtcout = self._mgr.getLogbuf("ManagerServant")
00049 self._isMaster = False
00050 self._masters = []
00051 self._slaves = []
00052 self._masterMutex = threading.RLock()
00053 self._slaveMutex = threading.RLock()
00054 self._objref = None
00055
00056 config = copy.deepcopy(self._mgr.getConfig())
00057
00058 if OpenRTM_aist.toBool(config.getProperty("manager.is_master"), "YES", "NO", True):
00059
00060 self._rtcout.RTC_TRACE("This manager is master.")
00061
00062 if (not self.createINSManager()):
00063 self._rtcout.RTC_WARN("Manager CORBA servant creation failed.")
00064 return
00065
00066 self._isMaster = True
00067 self._rtcout.RTC_TRACE("Manager CORBA servant was successfully created.")
00068 return
00069 else:
00070
00071 self._rtcout.RTC_TRACE("This manager is slave.")
00072 try:
00073 owner = self.findManager(config.getProperty("corba.master_manager"))
00074 if not owner:
00075 self._rtcout.RTC_INFO("Master manager not found")
00076 return
00077
00078 if not self.createINSManager():
00079 self._rtcout.RTC_WARN("Manager CORBA servant creation failed.")
00080 return
00081
00082 self.add_master_manager(owner)
00083 owner.add_slave_manager(self._objref)
00084 return
00085 except:
00086 self._rtcout.RTC_ERROR("Unknown exception cought.")
00087 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00088
00089
00090 return
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 def __del__(self):
00104 guard_master = OpenRTM_aist.ScopedLock(self._masterMutex)
00105 for i in range(len(self._masters)):
00106 try:
00107 if CORBA.is_nil(self._masters[i]):
00108 continue
00109 self._masters[i].remove_slave_manager(self._objref)
00110 except:
00111 self._masters[i] = RTM.Manager._nil
00112 self._masters = []
00113
00114 guard_slave = OpenRTM_aist.ScopedLock(self._slaveMutex)
00115 for i in range(len(self._slaves)):
00116 try:
00117 if CORBA.is_nil(self._slaves[i]):
00118 continue
00119 self._slaves[i].remove_master_manager(self._objref)
00120 except:
00121 self._slaves[i] = RTM.Manager._nil
00122 self._slaves = []
00123
00124 del guard_slave
00125 del guard_master
00126 return
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 def load_module(self, pathname, initfunc):
00154 self._rtcout.RTC_TRACE("ManagerServant::load_module(%s, %s)", (pathname, initfunc))
00155 self._mgr.load(pathname, initfunc)
00156 return RTC.RTC_OK
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 def unload_module(self, pathname):
00180 self._rtcout.RTC_TRACE("ManagerServant::unload_module(%s)", pathname)
00181 self._mgr.unload(pathname)
00182 return RTC.RTC_OK
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 def get_loadable_modules(self):
00204 self._rtcout.RTC_TRACE("get_loadable_modules()")
00205
00206
00207 prof = self._mgr.getLoadableModules()
00208 cprof = [ RTM.ModuleProfile([]) for i in prof ]
00209
00210 for i in range(len(prof)):
00211 OpenRTM_aist.NVUtil.copyFromProperties(cprof[i].properties, prof[i])
00212
00213 return cprof
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 def get_loaded_modules(self):
00235 self._rtcout.RTC_TRACE("get_loaded_modules()")
00236 prof = self._mgr.getLoadedModules()
00237 cprof = [RTM.ModuleProfile([]) for i in prof]
00238
00239 for i in range(len(prof)):
00240 OpenRTM_aist.NVUtil.copyFromProperties(cprof[i].properties, prof[i])
00241
00242 return cprof
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 def get_factory_profiles(self):
00266 self._rtcout.RTC_TRACE("get_factory_profiles()")
00267 prof = self._mgr.getFactoryProfiles()
00268 cprof = [RTM.ModuleProfile([]) for i in prof]
00269
00270 for i in range(len(prof)):
00271 OpenRTM_aist.NVUtil.copyFromProperties(cprof[i].properties, prof[i])
00272
00273 return cprof
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 def create_component(self, module_name):
00296 self._rtcout.RTC_TRACE("create_component(%s)", module_name)
00297
00298 arg = module_name
00299 pos0 = arg.find("&manager=")
00300 pos1 = arg.find("?manager=")
00301
00302 if pos0 == -1 and pos1 == -1:
00303
00304 rtc = self._mgr.createComponent(module_name)
00305 if not rtc:
00306 return RTC.RTObject._nil
00307 return rtc.getObjRef()
00308
00309
00310
00311
00312
00313
00314 if pos0 == -1:
00315 pos = pos1
00316 else:
00317 pos = pos0
00318
00319 endpos = arg.find('&', pos + 1)
00320 if endpos == -1:
00321 mgrstr = arg[(pos + 1):]
00322 else:
00323 mgrstr = arg[(pos + 1): endpos]
00324 self._rtcout.RTC_VERBOSE("Manager arg: %s", mgrstr)
00325 mgrvstr = mgrstr.split(":")
00326 if len(mgrvstr) != 2:
00327 self._rtcout.RTC_WARN("Invalid manager name: %s", mgrstr)
00328 return RTC.RTObject._nil
00329
00330 eqpos = mgrstr.find("=")
00331 if eqpos == -1:
00332 self._rtcout.RTC_WARN("Invalid argument: %s", module_name)
00333 return RTC.RTObject._nil
00334
00335 mgrstr = mgrstr[eqpos + 1:]
00336 self._rtcout.RTC_DEBUG("Manager is %s", mgrstr)
00337
00338
00339 mgrobj = self.findManager(mgrstr)
00340 if CORBA.is_nil(mgrobj):
00341 cmd = "rtcd_python -p "
00342 cmd += mgrvstr[1]
00343
00344 self._rtcout.RTC_DEBUG("Invoking command: %s.", cmd)
00345 ret = OpenRTM_aist.launch_shell(cmd)
00346 if ret == -1:
00347 self._rtcout.RTC_DEBUG("%s: failed", cmd)
00348 return RTC.RTObject._nil
00349
00350
00351 time.sleep(0.01)
00352 count = 0
00353 while CORBA.is_nil(mgrobj):
00354 mgrobj = self.findManager(mgrstr)
00355 count += 1
00356 if count > 1000:
00357 break
00358 time.sleep(0.01)
00359
00360 if CORBA.is_nil(mgrobj):
00361 self._rtcout.RTC_WARN("Manager cannot be found.")
00362 return RTC.RTObject._nil
00363
00364
00365 if endpos == -1:
00366 arg = arg[:pos]
00367 else:
00368 arg = arg[:pos] + arg[endpos:]
00369 self._rtcout.RTC_DEBUG("Creating component on %s", mgrstr)
00370 self._rtcout.RTC_DEBUG("arg: %s", arg)
00371 try:
00372 rtobj = mgrobj.create_component(arg)
00373 self._rtcout.RTC_DEBUG("Component created %s", arg)
00374 return rtobj
00375 except CORBA.SystemException:
00376 self._rtcout.RTC_DEBUG("Exception was caught while creating component.")
00377 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00378 return RTC.RTObject._nil
00379 except:
00380 self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
00381
00382 return RTC.RTObject._nil
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 def delete_component(self, instance_name):
00405 self._rtcout.RTC_TRACE("delete_component(%s)", instance_name)
00406 self._mgr.deleteComponent(instance_name)
00407 return RTC.RTC_OK
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 def get_components(self):
00429 self._rtcout.RTC_TRACE("get_components()")
00430
00431
00432 rtcs = self._mgr.getComponents()
00433 crtcs = []
00434
00435 for rtc in rtcs:
00436 crtcs.append(rtc.getObjRef())
00437
00438
00439 self._rtcout.RTC_DEBUG("%d slave managers exists.", len(self._slaves))
00440 for i in range(len(self._slaves)):
00441 try:
00442 if not CORBA.is_nil(self._slaves[i]):
00443 srtcs = self._slaves[i].get_components()
00444 OpenRTM_aist.CORBA_SeqUtil.push_back_list(crtcs, srtcs)
00445 continue
00446 except:
00447 self._RTC_INFO("slave (%d) has disappeared.", i)
00448 self._slaves[i] = RTM.Manager._nil
00449
00450 OpenRTM_aist.CORBA_SeqUtil.erase(self._slaves, i)
00451 i -= 1
00452
00453 return crtcs
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 def get_component_profiles(self):
00477 rtcs = self._mgr.getComponents()
00478 cprofs = [rtc.get_component_profile() for rtc in rtcs]
00479
00480
00481 guard = OpenRTM_aist.ScopedLock(self._slaveMutex)
00482 self._rtcout.RTC_DEBUG("%d slave managers exists.", len(self._slaves))
00483
00484 for i in range(len(self._slaves)):
00485 try:
00486 if not CORBA.is_nil(self._slaves[i]):
00487 sprofs = self._slaves[i].get_component_profiles()
00488 OpenRTM_aist.CORBA_SeqUtil.push_back_list(cprofs, sprofs)
00489 continue
00490 except:
00491 self._rtcout.RTC_INFO("slave (%d) has disappeared.", i)
00492 self._slaves[i] = RTM.Manager._nil
00493
00494 OpenRTM_aist.CORBA_SeqUtil.erase(self._slaves, i)
00495 i -= 1
00496
00497 del guard
00498 return cprofs
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519 def get_profile(self):
00520 self._rtcout.RTC_TRACE("get_profile()")
00521 prof = RTM.ModuleProfile([])
00522 OpenRTM_aist.NVUtil.copyFromProperties(prof.properties, self._mgr.getConfig().getNode("manager"))
00523
00524 return prof
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545 def get_configuration(self):
00546 self._rtcout.RTC_TRACE("get_configuration()")
00547 nvlist = []
00548 OpenRTM_aist.NVUtil.copyFromProperties(nvlist, self._mgr.getConfig())
00549 return nvlist
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574 def set_configuration(self, name, value):
00575 self._rtcout.RTC_TRACE("set_configuration(name = %s, value = %s)", (name, value))
00576 self._mgr.getConfig().setProperty(name, value)
00577 return RTC.RTC_OK
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601 def is_master(self):
00602
00603
00604 ret = ""
00605 if self._isMaster:
00606 ret = "YES"
00607 else:
00608 ret = "NO"
00609 self._rtcout.RTC_TRACE("is_master(): %s", ret)
00610 return self._isMaster
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635 def get_master_managers(self):
00636 self._rtcout.RTC_TRACE("get_master_managers()")
00637 guard = OpenRTM_aist.ScopedLock(self._masterMutex)
00638
00639 return self._masters
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666 def add_master_manager(self, mgr):
00667 guard = OpenRTM_aist.ScopedLock(self._masterMutex)
00668 self._rtcout.RTC_TRACE("add_master_manager(), %d masters", len(self._masters))
00669 index = OpenRTM_aist.CORBA_SeqUtil.find(self._masters, self.is_equiv(mgr))
00670
00671 if not (index < 0):
00672 self._rtcout.RTC_ERROR("Already exists.")
00673 return RTC.BAD_PARAMETER
00674
00675 OpenRTM_aist.CORBA_SeqUtil.push_back(self._masters, mgr)
00676 self._rtcout.RTC_TRACE("add_master_manager() done, %d masters", len(self._masters))
00677 del guard
00678 return RTC.RTC_OK
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701 def remove_master_manager(self, mgr):
00702 guard = OpenRTM_aist.ScopedLock(self._masterMutex)
00703 self._rtcout.RTC_TRACE("remove_master_manager(), %d masters", len(self._masters))
00704
00705 index = OpenRTM_aist.CORBA_SeqUtil.find(self._masters, self.is_equiv(mgr))
00706
00707 if index < 0:
00708 self._rtcout.RTC_ERROR("Not found.")
00709 return RTC.BAD_PARAMETER
00710
00711 OpenRTM_aist.CORBA_SeqUtil.erase(self._masters, index)
00712 self._rtcout.RTC_TRACE("remove_master_manager() done, %d masters", len(self._masters))
00713 del guard
00714 return RTC.RTC_OK
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739 def get_slave_managers(self):
00740 guard = OpenRTM_aist.ScopedLock(self._slaveMutex)
00741 self._rtcout.RTC_TRACE("get_slave_managers(), %d slaves", len(self._slaves))
00742 return self._slaves
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765 def add_slave_manager(self, mgr):
00766 guard = OpenRTM_aist.ScopedLock(self._slaveMutex)
00767 self._rtcout.RTC_TRACE("add_slave_manager(), %d slaves", len(self._slaves))
00768
00769 index = OpenRTM_aist.CORBA_SeqUtil.find(self._slaves, self.is_equiv(mgr))
00770
00771 if not (index < 0):
00772 self._rtcout.RTC_ERROR("Already exists.")
00773 return RTC.BAD_PARAMETER
00774
00775 OpenRTM_aist.CORBA_SeqUtil.push_back(self._slaves, mgr)
00776 self._rtcout.RTC_TRACE("add_slave_manager() done, %d slaves", len(self._slaves))
00777 del guard
00778 return RTC.RTC_OK
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801 def remove_slave_manager(self, mgr):
00802 guard = OpenRTM_aist.ScopedLock(self._slaveMutex)
00803 self._rtcout.RTC_TRACE("remove_slave_manager(), %d slaves", len(self._slaves))
00804 index = OpenRTM_aist.CORBA_SeqUtil.find(self._slaves, self.is_equiv(mgr))
00805
00806 if index < 0:
00807 self._rtcout.RTC_ERROR("Not found.")
00808 return RTC.BAD_PARAMETER
00809
00810 OpenRTM_aist.CORBA_SeqUtil.erase(self._slaves, index)
00811 self._rtcout.RTC_TRACE("remove_slave_manager() done, %d slaves", len(self._slaves))
00812 del guard
00813 return RTC.RTC_OK
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826 def fork(self):
00827
00828 return RTC.RTC_OK
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841 def shutdown(self):
00842 self._mgr.terminate()
00843 return RTC.RTC_OK
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856 def restart(self):
00857
00858 return RTC.RTC_OK
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871 def get_service(self, name):
00872 return CORBA.Object._nil
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885 def getObjRef(self):
00886 return self._objref
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899 def createINSManager(self):
00900 try:
00901 poa = self._mgr.getORB().resolve_initial_references("omniINSPOA")
00902 poa._get_the_POAManager().activate()
00903 id = self._mgr.getConfig().getProperty("manager.name")
00904 poa.activate_object_with_id(id, self)
00905 mgrobj = poa.id_to_reference(id)
00906 self._objref = mgrobj._narrow(RTM.Manager)
00907 except:
00908 self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
00909 return False
00910
00911 return True
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924 def findManager(self, host_port):
00925 self._rtcout.RTC_TRACE("findManager(host_port = %s)", host_port)
00926 try:
00927 config = copy.deepcopy(self._mgr.getConfig())
00928 mgrloc = "corbaloc:iiop:"
00929 mgrloc += host_port
00930 mgrloc += "/" + config.getProperty("manager.name")
00931 self._rtcout.RTC_DEBUG("corbaloc: %s", mgrloc)
00932
00933 mobj = self._mgr.getORB().string_to_object(mgrloc)
00934 mgr = mobj._narrow(RTM.Manager)
00935 return mgr
00936
00937 except CORBA.SystemException:
00938 self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
00939
00940 except:
00941 self._rtcout.RTC_ERROR("Unknown exception cought.")
00942 self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
00943
00944 return RTM.Manager._nil
00945
00946
00947 class is_equiv:
00948 def __init__(self, mgr):
00949 self._mgr = mgr
00950
00951 def __call__(self, mgr):
00952 if not self._mgr or not mgr:
00953 return self._mgr == mgr
00954
00955 return self._mgr._is_equivalent(mgr)
00956