NamingManager.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: euc-jp -*-
3 
4 
16 
17 
18 import threading
19 import traceback
20 import sys
21 
22 import OpenRTM_aist
23 
24 
25 
41 class NamingBase:
42  """
43  """
44 
45 
60  def bindObject(self, name, rtobj):
61  pass
62 
63 
64 
78  def unbindObject(self, name):
79  pass
80 
81 
97  def isAlive(self):
98  pass
99 
100 
101 
118  """
119  """
120 
121 
135  def __init__(self, orb, names):
136  self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf('manager.namingoncorba')
138  self._endpoint = ""
139  self._replaceEndpoint = False
140 
141 
156  def bindObject(self, name, rtobj):
157  self._rtcout.RTC_TRACE("bindObject(name = %s, rtobj or mgr)", name)
158  try:
159  self._cosnaming.rebindByString(name, rtobj.getObjRef(), True)
160  except:
161  self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
162 
163  return
164 
165 
166 
179  def unbindObject(self, name):
180  self._rtcout.RTC_TRACE("unbindObject(name = %s)", name)
181  try:
182  self._cosnaming.unbind(name)
183  except:
184  self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
185 
186  return
187 
188 
189 
205  def isAlive(self):
206  self._rtcout.RTC_TRACE("isAlive()")
207  return self._cosnaming.isAlive()
208 
209 
210 
227  """
228  """
229 
230 
231 
232 
245  def __init__(self, manager):
246  self._manager = manager
247  self._rtcout = manager.getLogbuf('manager.namingmanager')
248  #self._rtcout.setLogLevel(manager.getConfig().getProperty("logger.log_level"))
249  #self._rtcout.setLogLock(OpenRTM_aist.toBool(manager.getConfig().getProperty("logger.stream_lock"), "enable", "disable", False))
250  self._names = []
251  self._namesMutex = threading.RLock()
252  self._compNames = []
253  self._mgrNames = []
254  self._compNamesMutex = threading.RLock()
255  self._mgrNamesMutex = threading.RLock()
256 
257 
258 
273  def registerNameServer(self, method, name_server):
274  self._rtcout.RTC_TRACE("NamingManager::registerNameServer(%s, %s)",
275  (method, name_server))
276  name = self.createNamingObj(method, name_server)
277  self._names.append(self.Names(method, name_server, name))
278 
279 
280 
294  def bindObject(self, name, rtobj):
295  self._rtcout.RTC_TRACE("NamingManager::bindObject(%s)", name)
297  for i in range(len(self._names)):
298  if self._names[i].ns:
299  try:
300  self._names[i].ns.bindObject(name, rtobj)
301  except:
302  del self._names[i].ns
303  self._names[i].ns = 0
304 
305  self.registerCompName(name, rtobj)
306 
307 
308  def bindManagerObject(self, name, mgr):
309  self._rtcout.RTC_TRACE("NamingManager::bindManagerObject(%s)", name)
311  for i in range(len(self._names)):
312  if self._names[i].ns:
313  try:
314  self._names[i].ns.bindObject(name, mgr)
315  except:
316  del self._names[i].ns
317  self._names[i].ns = 0
318 
319  self.registerMgrName(name, mgr)
320 
321 
322 
335  def update(self):
336  self._rtcout.RTC_TRACE("NamingManager::update()")
338  rebind = OpenRTM_aist.toBool(self._manager.getConfig().getProperty("naming.update.rebind"),
339  "YES","NO",False)
340  for i in range(len(self._names)):
341  if self._names[i].ns is None:
342  self._rtcout.RTC_DEBUG("Retrying connection to %s/%s",
343  (self._names[i].method,
344  self._names[i].nsname))
345  self.retryConnection(self._names[i])
346 
347  else:
348  try:
349  if rebind:
350  self.bindCompsTo(self._names[i].ns)
351  if not self._names[i].ns.isAlive():
352  self._rtcout.RTC_INFO("Name server: %s (%s) disappeared.",
353  (self._names[i].nsname,
354  self._names[i].method))
355  del self._names[i].ns
356  self._names[i].ns = None
357  except:
358  self._rtcout.RTC_INFO("Name server: %s (%s) disappeared.",
359  (self._names[i].nsname,
360  self._names[i].method))
361  del self._names[i].ns
362  self._names[i].ns = None
363 
364 
365  return
366 
367 
368 
381  def unbindObject(self, name):
382  self._rtcout.RTC_TRACE("NamingManager::unbindObject(%s)", name)
384  for i in range(len(self._names)):
385  if self._names[i].ns:
386  self._names[i].ns.unbindObject(name)
387  self.unregisterCompName(name)
388  self.unregisterMgrName(name)
389 
390 
391 
403  def unbindAll(self):
404  self._rtcout.RTC_TRACE("NamingManager::unbindAll(): %d names.", len(self._compNames))
405 
407  len_ = len(self._compNames)
408  for i in range(len_):
409  idx = (len_ - 1) - i
410  self.unbindObject(self._compNames[idx].name)
411 
413  len_ = len(self._mgrNames)
414  for i in range(len_):
415  idx = (len_ - 1) - i
416  self.unbindObject(self._mgrNames[idx].name)
417 
418 
419 
433  def getObjects(self):
434  comps = []
436  for i in range(len(self._compNames)):
437  comps.append(self._compNames[i].rtobj)
438  return comps
439 
440 
441 
457  def createNamingObj(self, method, name_server):
458  self._rtcout.RTC_TRACE("createNamingObj(method = %s, nameserver = %s)",
459  (method, name_server))
460  mth = method
461  if mth == "corba":
462  try:
463  name = OpenRTM_aist.NamingOnCorba(self._manager.getORB(),name_server)
464  if name is None:
465  return None
466  self._rtcout.RTC_INFO("NameServer connection succeeded: %s/%s",
467  (method, name_server))
468  return name
469  except:
470  self._rtcout.RTC_INFO("NameServer connection failed: %s/%s",
471  (method, name_server))
472  return None
473 
474  return None
475 
476 
477 
490  def bindCompsTo(self, ns):
491  for i in range(len(self._compNames)):
492  ns.bindObject(self._compNames[i].name, self._compNames[i].rtobj)
493 
494 
495 
509  def registerCompName(self, name, rtobj):
510  for i in range(len(self._compNames)):
511  if self._compNames[i].name == name:
512  self._compNames[i].rtobj = rtobj
513  return
514 
515  self._compNames.append(self.Comps(name, rtobj))
516  return
517 
518 
519  def registerMgrName(self, name, mgr):
520  for i in range(len(self._mgrNames)):
521  if self._mgrNames[i].name == name:
522  self._mgrNames[i].mgr = mgr
523  return
524 
525  self._mgrNames.append(self.Mgr(name, mgr))
526  return
527 
528 
529 
542  def unregisterCompName(self, name):
543  len_ = len(self._compNames)
544  for i in range(len_):
545  idx = (len_-1) - i
546  if self._compNames[idx].name == name:
547  del self._compNames[idx]
548  return
549  return
550 
551 
552  def unregisterMgrName(self, name):
553  len_ = len(self._mgrNames)
554  for i in range(len_):
555  idx = (len_ -1) - i
556  if self._mgrNames[idx].name == name:
557  del self._mgrNames[idx]
558  return
559  return
560 
561 
562 
582  def retryConnection(self, ns):
583  # recreate NamingObj
584  nsobj = 0
585  try:
586  nsobj = self.createNamingObj(ns.method, ns.nsname)
587  if nsobj != 0: # if succeed
588  self._rtcout.RTC_INFO("Connected to a name server: %s/%s",
589  (ns.method, ns.nsname))
590  ns.ns = nsobj
591  self.bindCompsTo(nsobj) # rebind all comps to new NS
592  return
593  else:
594  self._rtcout.RTC_DEBUG("Name service: %s/%s still not available.",
595  (ns.method, ns.nsname))
596 
597  except:
598  self._rtcout.RTC_DEBUG("Name server: %s/%s disappeared again.",
599  (ns.method, ns.nsname))
600  if nsobj != 0:
601  del ns.ns
602  ns.ns = 0
603 
604  return
605 
606 
607  # Name Servers' method/name and object
608 
615  class Names:
616  def __init__(self, meth, name, naming):
617  self.method = meth
618  self.nsname = name
619  self.ns = naming
620 
621 
622  # Components' name and object
623 
630  class Comps:
631  def __init__(self, n, obj):
632  self.name = n
633  self.rtobj = obj
634 
635 
636  class Mgr:
637  def __init__(self, n, obj):
638  self.name = n
639  self.mgr = obj
def __init__(self, meth, name, naming)
def createNamingObj(self, method, name_server)
CORBA Naming Service helper class.
Definition: CorbaNaming.py:61
def bindObject(self, name, rtobj)
def isAlive(self)
Check if the name service is alive.
def retryConnection(self, ns)
Rebind the component to NameServer.
def isAlive(self)
Check if the name service is alive.
def registerNameServer(self, method, name_server)


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:34