ManagerServant.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
17 
18 import copy
19 import sys
20 import threading
21 import time
22 from omniORB import CORBA
23 import OpenRTM_aist
24 import RTC,RTM,RTM__POA
25 import SDOPackage
26 
27 
28 class ManagerServant(RTM__POA.Manager):
29  """
30  """
31 
32 
45  def __init__(self):
46  self._mgr = OpenRTM_aist.Manager.instance()
47  self._owner = None
48  self._rtcout = self._mgr.getLogbuf("ManagerServant")
49  self._isMaster = False
50  self._masters = []
51  self._slaves = []
52  self._masterMutex = threading.RLock()
53  self._slaveMutex = threading.RLock()
54  self._objref = None
55 
56  config = copy.deepcopy(self._mgr.getConfig())
57 
58  if OpenRTM_aist.toBool(config.getProperty("manager.is_master"), "YES", "NO", True):
59  # this is master manager
60  self._rtcout.RTC_TRACE("This manager is master.")
61 
62  if (not self.createINSManager()):
63  self._rtcout.RTC_WARN("Manager CORBA servant creation failed.")
64  return
65 
66  self._isMaster = True
67  self._rtcout.RTC_TRACE("Manager CORBA servant was successfully created.")
68  return
69  else:
70  # this is slave manager
71  self._rtcout.RTC_TRACE("This manager is slave.")
72  try:
73  owner = self.findManager(config.getProperty("corba.master_manager"))
74  if not owner:
75  self._rtcout.RTC_INFO("Master manager not found")
76  return
77 
78  if not self.createINSManager():
79  self._rtcout.RTC_WARN("Manager CORBA servant creation failed.")
80  return
81 
82  self.add_master_manager(owner)
83  owner.add_slave_manager(self._objref)
84  return
85  except:
86  self._rtcout.RTC_ERROR("Unknown exception cought.")
87  self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
88 
89 
90  return
91 
92 
93 
103  def __del__(self):
104  guard_master = OpenRTM_aist.ScopedLock(self._masterMutex)
105  for i in range(len(self._masters)):
106  try:
107  if CORBA.is_nil(self._masters[i]):
108  continue
110  except:
111  self._masters[i] = RTM.Manager._nil
112  self._masters = []
113 
114  guard_slave = OpenRTM_aist.ScopedLock(self._slaveMutex)
115  for i in range(len(self._slaves)):
116  try:
117  if CORBA.is_nil(self._slaves[i]):
118  continue
120  except:
121  self._slaves[i] = RTM.Manager._nil
122  self._slaves = []
123 
124  del guard_slave
125  del guard_master
126  return
127 
128 
129 
153  def load_module(self, pathname, initfunc):
154  self._rtcout.RTC_TRACE("ManagerServant::load_module(%s, %s)", (pathname, initfunc))
155  self._mgr.load(pathname, initfunc)
156  return RTC.RTC_OK
157 
158 
159 
179  def unload_module(self, pathname):
180  self._rtcout.RTC_TRACE("ManagerServant::unload_module(%s)", pathname)
181  self._mgr.unload(pathname)
182  return RTC.RTC_OK
183 
184 
185 
204  self._rtcout.RTC_TRACE("get_loadable_modules()")
205 
206  # copy local module profiles
207  prof = self._mgr.getLoadableModules()
208  cprof = [ RTM.ModuleProfile([]) for i in prof ]
209 
210  for i in range(len(prof)):
211  OpenRTM_aist.NVUtil.copyFromProperties(cprof[i].properties, prof[i])
212 
213  return cprof
214 
215 
216 
235  self._rtcout.RTC_TRACE("get_loaded_modules()")
236  prof = self._mgr.getLoadedModules()
237  cprof = [RTM.ModuleProfile([]) for i in prof]
238 
239  for i in range(len(prof)):
240  OpenRTM_aist.NVUtil.copyFromProperties(cprof[i].properties, prof[i])
241 
242  return cprof
243 
244 
245 
266  self._rtcout.RTC_TRACE("get_factory_profiles()")
267  prof = self._mgr.getFactoryProfiles()
268  cprof = [RTM.ModuleProfile([]) for i in prof]
269 
270  for i in range(len(prof)):
271  OpenRTM_aist.NVUtil.copyFromProperties(cprof[i].properties, prof[i])
272 
273  return cprof
274 
275 
276 
295  def create_component(self, module_name):
296  self._rtcout.RTC_TRACE("create_component(%s)", module_name)
297 
298  arg = module_name
299  pos0 = arg.find("&manager=")
300  pos1 = arg.find("?manager=")
301 
302  if pos0 == -1 and pos1 == -1:
303  # create on this manager
304  rtc = self._mgr.createComponent(module_name)
305  if not rtc:
306  return RTC.RTObject._nil
307  return rtc.getObjRef()
308 
309  # create other manager
310 
311  # extract manager's location
312  # since Python2.5
313  # pos = (lambda x: pos0 if x == -1 else pos1)(pos0)
314  if pos0 == -1:
315  pos = pos1
316  else:
317  pos = pos0
318 
319  endpos = arg.find('&', pos + 1)
320  if endpos == -1:
321  mgrstr = arg[(pos + 1):]
322  else:
323  mgrstr = arg[(pos + 1): endpos]
324  self._rtcout.RTC_VERBOSE("Manager arg: %s", mgrstr)
325  mgrvstr = mgrstr.split(":")
326  if len(mgrvstr) != 2:
327  self._rtcout.RTC_WARN("Invalid manager name: %s", mgrstr)
328  return RTC.RTObject._nil
329 
330  eqpos = mgrstr.find("=")
331  if eqpos == -1:
332  self._rtcout.RTC_WARN("Invalid argument: %s", module_name)
333  return RTC.RTObject._nil
334 
335  mgrstr = mgrstr[eqpos + 1:]
336  self._rtcout.RTC_DEBUG("Manager is %s", mgrstr)
337 
338  # find manager
339  mgrobj = self.findManager(mgrstr)
340  if CORBA.is_nil(mgrobj):
341  cmd = "rtcd_python -p "
342  cmd += mgrvstr[1] # port number
343 
344  self._rtcout.RTC_DEBUG("Invoking command: %s.", cmd)
345  ret = OpenRTM_aist.launch_shell(cmd)
346  if ret == -1:
347  self._rtcout.RTC_DEBUG("%s: failed", cmd)
348  return RTC.RTObject._nil
349 
350  # find manager
351  time.sleep(0.01)
352  count = 0
353  while CORBA.is_nil(mgrobj):
354  mgrobj = self.findManager(mgrstr)
355  count += 1
356  if count > 1000:
357  break
358  time.sleep(0.01)
359 
360  if CORBA.is_nil(mgrobj):
361  self._rtcout.RTC_WARN("Manager cannot be found.")
362  return RTC.RTObject._nil
363 
364  # create component on the manager
365  if endpos == -1:
366  arg = arg[:pos]
367  else:
368  arg = arg[:pos] + arg[endpos:]
369  self._rtcout.RTC_DEBUG("Creating component on %s", mgrstr)
370  self._rtcout.RTC_DEBUG("arg: %s", arg)
371  try:
372  rtobj = mgrobj.create_component(arg)
373  self._rtcout.RTC_DEBUG("Component created %s", arg)
374  return rtobj
375  except CORBA.SystemException:
376  self._rtcout.RTC_DEBUG("Exception was caught while creating component.")
377  self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
378  return RTC.RTObject._nil
379  except:
380  self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
381 
382  return RTC.RTObject._nil
383 
384 
385 
404  def delete_component(self, instance_name):
405  self._rtcout.RTC_TRACE("delete_component(%s)", instance_name)
406  self._mgr.deleteComponent(instance_name)
407  return RTC.RTC_OK
408 
409 
410 
428  def get_components(self):
429  self._rtcout.RTC_TRACE("get_components()")
430 
431  # get local component references
432  rtcs = self._mgr.getComponents()
433  crtcs = []
434 
435  for rtc in rtcs:
436  crtcs.append(rtc.getObjRef())
437 
438  # get slaves' component references
439  self._rtcout.RTC_DEBUG("%d slave managers exists.", len(self._slaves))
440  for i in range(len(self._slaves)):
441  try:
442  if not CORBA.is_nil(self._slaves[i]):
443  srtcs = self._slaves[i].get_components()
445  continue
446  except:
447  self._RTC_INFO("slave (%d) has disappeared.", i)
448  self._slaves[i] = RTM.Manager._nil
449 
451  i -= 1
452 
453  return crtcs
454 
455 
456 
477  rtcs = self._mgr.getComponents()
478  cprofs = [rtc.get_component_profile() for rtc in rtcs]
479 
480  # copy slaves' component profiles
482  self._rtcout.RTC_DEBUG("%d slave managers exists.", len(self._slaves))
483 
484  for i in range(len(self._slaves)):
485  try:
486  if not CORBA.is_nil(self._slaves[i]):
487  sprofs = self._slaves[i].get_component_profiles()
489  continue
490  except:
491  self._rtcout.RTC_INFO("slave (%d) has disappeared.", i)
492  self._slaves[i] = RTM.Manager._nil
493 
495  i -= 1
496 
497  del guard
498  return cprofs
499 
500 
501 
519  def get_profile(self):
520  self._rtcout.RTC_TRACE("get_profile()")
521  prof = RTM.ModuleProfile([])
522  OpenRTM_aist.NVUtil.copyFromProperties(prof.properties, self._mgr.getConfig().getNode("manager"))
523 
524  return prof
525 
526 
527 
545  def get_configuration(self):
546  self._rtcout.RTC_TRACE("get_configuration()")
547  nvlist = []
548  OpenRTM_aist.NVUtil.copyFromProperties(nvlist, self._mgr.getConfig())
549  return nvlist
550 
551 
552 
574  def set_configuration(self, name, value):
575  self._rtcout.RTC_TRACE("set_configuration(name = %s, value = %s)", (name, value))
576  self._mgr.getConfig().setProperty(name, value)
577  return RTC.RTC_OK
578 
579 
580 
581 
601  def is_master(self):
602  # since Python2.5
603  # self._rtcout.RTC_TRACE("is_master(): %s", (lambda x: "YES" if x else "NO")(self._isMaster))
604  ret = ""
605  if self._isMaster:
606  ret = "YES"
607  else:
608  ret = "NO"
609  self._rtcout.RTC_TRACE("is_master(): %s", ret)
610  return self._isMaster
611 
612 
613 
636  self._rtcout.RTC_TRACE("get_master_managers()")
638 
639  return self._masters
640 
641 
642 
666  def add_master_manager(self, mgr):
668  self._rtcout.RTC_TRACE("add_master_manager(), %d masters", len(self._masters))
669  index = OpenRTM_aist.CORBA_SeqUtil.find(self._masters, self.is_equiv(mgr))
670 
671  if not (index < 0): # found in my list
672  self._rtcout.RTC_ERROR("Already exists.")
673  return RTC.BAD_PARAMETER
674 
676  self._rtcout.RTC_TRACE("add_master_manager() done, %d masters", len(self._masters))
677  del guard
678  return RTC.RTC_OK
679 
680 
681 
701  def remove_master_manager(self, mgr):
703  self._rtcout.RTC_TRACE("remove_master_manager(), %d masters", len(self._masters))
704 
705  index = OpenRTM_aist.CORBA_SeqUtil.find(self._masters, self.is_equiv(mgr))
706 
707  if index < 0: # not found in my list
708  self._rtcout.RTC_ERROR("Not found.")
709  return RTC.BAD_PARAMETER
710 
712  self._rtcout.RTC_TRACE("remove_master_manager() done, %d masters", len(self._masters))
713  del guard
714  return RTC.RTC_OK
715 
716 
717 
741  self._rtcout.RTC_TRACE("get_slave_managers(), %d slaves", len(self._slaves))
742  return self._slaves
743 
744 
745 
765  def add_slave_manager(self, mgr):
767  self._rtcout.RTC_TRACE("add_slave_manager(), %d slaves", len(self._slaves))
768 
769  index = OpenRTM_aist.CORBA_SeqUtil.find(self._slaves, self.is_equiv(mgr))
770 
771  if not (index < 0): # found in my list
772  self._rtcout.RTC_ERROR("Already exists.")
773  return RTC.BAD_PARAMETER
774 
776  self._rtcout.RTC_TRACE("add_slave_manager() done, %d slaves", len(self._slaves))
777  del guard
778  return RTC.RTC_OK
779 
780 
781 
801  def remove_slave_manager(self, mgr):
803  self._rtcout.RTC_TRACE("remove_slave_manager(), %d slaves", len(self._slaves))
804  index = OpenRTM_aist.CORBA_SeqUtil.find(self._slaves, self.is_equiv(mgr))
805 
806  if index < 0: # not found in my list
807  self._rtcout.RTC_ERROR("Not found.")
808  return RTC.BAD_PARAMETER
809 
811  self._rtcout.RTC_TRACE("remove_slave_manager() done, %d slaves", len(self._slaves))
812  del guard
813  return RTC.RTC_OK
814 
815 
816 
826  def fork(self):
827  # self._mgr.fork()
828  return RTC.RTC_OK
829 
830 
831 
841  def shutdown(self):
842  self._mgr.terminate()
843  return RTC.RTC_OK
844 
845 
846 
856  def restart(self):
857  # self._mgr.restart()
858  return RTC.RTC_OK
859 
860 
861 
871  def get_service(self, name):
872  return CORBA.Object._nil
873 
874 
875 
885  def getObjRef(self):
886  return self._objref
887 
888 
889 
899  def createINSManager(self):
900  try:
901  poa = self._mgr.getORB().resolve_initial_references("omniINSPOA")
902  poa._get_the_POAManager().activate()
903  id = self._mgr.getConfig().getProperty("manager.name")
904  poa.activate_object_with_id(id, self)
905  mgrobj = poa.id_to_reference(id)
906  self._objref = mgrobj._narrow(RTM.Manager)
907  except:
908  self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
909  return False
910 
911  return True
912 
913 
914 
924  def findManager(self, host_port):
925  self._rtcout.RTC_TRACE("findManager(host_port = %s)", host_port)
926  try:
927  config = copy.deepcopy(self._mgr.getConfig())
928  mgrloc = "corbaloc:iiop:"
929  mgrloc += host_port
930  mgrloc += "/" + config.getProperty("manager.name")
931  self._rtcout.RTC_DEBUG("corbaloc: %s", mgrloc)
932 
933  mobj = self._mgr.getORB().string_to_object(mgrloc)
934  mgr = mobj._narrow(RTM.Manager)
935  return mgr
936 
937  except CORBA.SystemException:
938  self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
939 
940  except:
941  self._rtcout.RTC_ERROR("Unknown exception cought.")
942  self._rtcout.RTC_DEBUG(OpenRTM_aist.Logger.print_exception())
943 
944  return RTM.Manager._nil
945 
946 
947  class is_equiv:
948  def __init__(self, mgr):
949  self._mgr = mgr
950 
951  def __call__(self, mgr):
952  if not self._mgr or not mgr:
953  return self._mgr == mgr
954 
955  return self._mgr._is_equivalent(mgr)
956 
def get_loaded_modules(self)
Getting loaded module profiles.
def get_service(self, name)
Get the reference of RTC.
def erase(seq, index)
Erase the element of the specified index.
def createINSManager(self)
À®¸ù:true, ¼ºÇÔ:false.
def load_module(self, pathname, initfunc)
Loading a module.
def get_components(self)
Getting RT-Component list running on this manager.
def get_loadable_modules(self)
Getting loadable module profiles.
def push_back_list(seq1, seq2)
def set_configuration(self, name, value)
Setting manager&#39;s configuration.
def get_component_profiles(self)
Getting RT-Component&#39;s profile list running on this manager.
def push_back(seq, elem)
Push the new element back to the CORBA sequence.
def fork(self)
The copy of the process is generated.
def create_component(self, module_name)
Creating an RT-Component.
def remove_slave_manager(self, mgr)
Removing a slave manager.
def add_master_manager(self, mgr)
Getting a master manager.
def __del__(self)
Virtual destructor.
def shutdown(self)
This method shutdowns RTC.
def delete_component(self, instance_name)
Deleting an RT-Component.
def get_factory_profiles(self)
Getting component factory profiles.
def get_master_managers(self)
Getting master managers.
def get_configuration(self)
Getting this manager&#39;s configuration.
def get_profile(self)
Getting this manager&#39;s profile.
def get_slave_managers(self)
Getting slave managers.
def remove_master_manager(self, mgr)
Removing a master manager.
def findManager(self, host_port)
Find the reference of Manager.
def getObjRef(self)
Get the reference of Manager.
def unload_module(self, pathname)
Unloading a module.
def is_master(self)
Whether this manager is master or not.
def add_slave_manager(self, mgr)
Getting a slave manager.
def find(seq, f)
Return the index of CORBA sequence element that functor matches.
def copyFromProperties(nv, prop)
Copy to NVList from Proeprties.
Definition: NVUtil.py:85
def restart(self)
This method restarts RTC.


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:06