00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import threading
00019 import traceback
00020 import sys
00021
00022 import OpenRTM_aist
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 class NamingBase:
00042 """
00043 """
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 def bindObject(self, name, rtobj):
00061 pass
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 def unbindObject(self, name):
00079 pass
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 def isAlive(self):
00098 pass
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 class NamingOnCorba(NamingBase):
00118 """
00119 """
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 def __init__(self, orb, names):
00136 self._rtcout = OpenRTM_aist.Manager.instance().getLogbuf('manager.namingoncorba')
00137 self._cosnaming = OpenRTM_aist.CorbaNaming(orb,names)
00138 self._endpoint = ""
00139 self._replaceEndpoint = False
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 def bindObject(self, name, rtobj):
00157 self._rtcout.RTC_TRACE("bindObject(name = %s, rtobj or mgr)", name)
00158 try:
00159 self._cosnaming.rebindByString(name, rtobj.getObjRef(), True)
00160 except:
00161 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00162
00163 return
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 def unbindObject(self, name):
00180 self._rtcout.RTC_TRACE("unbindObject(name = %s)", name)
00181 try:
00182 self._cosnaming.unbind(name)
00183 except:
00184 self._rtcout.RTC_ERROR(OpenRTM_aist.Logger.print_exception())
00185
00186 return
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 def isAlive(self):
00206 self._rtcout.RTC_TRACE("isAlive()")
00207 return self._cosnaming.isAlive()
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 class NamingManager:
00227 """
00228 """
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 def __init__(self, manager):
00246 self._manager = manager
00247 self._rtcout = manager.getLogbuf('manager.namingmanager')
00248
00249
00250 self._names = []
00251 self._namesMutex = threading.RLock()
00252 self._compNames = []
00253 self._mgrNames = []
00254 self._compNamesMutex = threading.RLock()
00255 self._mgrNamesMutex = threading.RLock()
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 def registerNameServer(self, method, name_server):
00274 self._rtcout.RTC_TRACE("NamingManager::registerNameServer(%s, %s)",
00275 (method, name_server))
00276 name = self.createNamingObj(method, name_server)
00277 self._names.append(self.Names(method, name_server, name))
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 def bindObject(self, name, rtobj):
00295 self._rtcout.RTC_TRACE("NamingManager::bindObject(%s)", name)
00296 guard = OpenRTM_aist.ScopedLock(self._namesMutex)
00297 for i in range(len(self._names)):
00298 if self._names[i].ns:
00299 try:
00300 self._names[i].ns.bindObject(name, rtobj)
00301 except:
00302 del self._names[i].ns
00303 self._names[i].ns = 0
00304
00305 self.registerCompName(name, rtobj)
00306
00307
00308 def bindManagerObject(self, name, mgr):
00309 self._rtcout.RTC_TRACE("NamingManager::bindManagerObject(%s)", name)
00310 guard = OpenRTM_aist.ScopedLock(self._namesMutex)
00311 for i in range(len(self._names)):
00312 if self._names[i].ns:
00313 try:
00314 self._names[i].ns.bindObject(name, mgr)
00315 except:
00316 del self._names[i].ns
00317 self._names[i].ns = 0
00318
00319 self.registerMgrName(name, mgr)
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 def update(self):
00336 self._rtcout.RTC_TRACE("NamingManager::update()")
00337 guard = OpenRTM_aist.ScopedLock(self._namesMutex)
00338 rebind = OpenRTM_aist.toBool(self._manager.getConfig().getProperty("naming.update.rebind"),
00339 "YES","NO",False)
00340 for i in range(len(self._names)):
00341 if self._names[i].ns is None:
00342 self._rtcout.RTC_DEBUG("Retrying connection to %s/%s",
00343 (self._names[i].method,
00344 self._names[i].nsname))
00345 self.retryConnection(self._names[i])
00346
00347 else:
00348 try:
00349 if rebind:
00350 self.bindCompsTo(self._names[i].ns)
00351 if not self._names[i].ns.isAlive():
00352 self._rtcout.RTC_INFO("Name server: %s (%s) disappeared.",
00353 (self._names[i].nsname,
00354 self._names[i].method))
00355 del self._names[i].ns
00356 self._names[i].ns = None
00357 except:
00358 self._rtcout.RTC_INFO("Name server: %s (%s) disappeared.",
00359 (self._names[i].nsname,
00360 self._names[i].method))
00361 del self._names[i].ns
00362 self._names[i].ns = None
00363
00364
00365 return
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 def unbindObject(self, name):
00382 self._rtcout.RTC_TRACE("NamingManager::unbindObject(%s)", name)
00383 guard = OpenRTM_aist.ScopedLock(self._namesMutex)
00384 for i in range(len(self._names)):
00385 if self._names[i].ns:
00386 self._names[i].ns.unbindObject(name)
00387 self.unregisterCompName(name)
00388 self.unregisterMgrName(name)
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 def unbindAll(self):
00404 self._rtcout.RTC_TRACE("NamingManager::unbindAll(): %d names.", len(self._compNames))
00405
00406 guard = OpenRTM_aist.ScopedLock(self._compNamesMutex)
00407 len_ = len(self._compNames)
00408 for i in range(len_):
00409 idx = (len_ - 1) - i
00410 self.unbindObject(self._compNames[idx].name)
00411
00412 guard = OpenRTM_aist.ScopedLock(self._mgrNamesMutex)
00413 len_ = len(self._mgrNames)
00414 for i in range(len_):
00415 idx = (len_ - 1) - i
00416 self.unbindObject(self._mgrNames[idx].name)
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433 def getObjects(self):
00434 comps = []
00435 guard = OpenRTM_aist.ScopedLock(self._compNamesMutex)
00436 for i in range(len(self._compNames)):
00437 comps.append(self._compNames[i].rtobj)
00438 return comps
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 def createNamingObj(self, method, name_server):
00458 self._rtcout.RTC_TRACE("createNamingObj(method = %s, nameserver = %s)",
00459 (method, name_server))
00460 mth = method
00461 if mth == "corba":
00462 try:
00463 name = OpenRTM_aist.NamingOnCorba(self._manager.getORB(),name_server)
00464 if name is None:
00465 return None
00466 self._rtcout.RTC_INFO("NameServer connection succeeded: %s/%s",
00467 (method, name_server))
00468 return name
00469 except:
00470 self._rtcout.RTC_INFO("NameServer connection failed: %s/%s",
00471 (method, name_server))
00472 return None
00473
00474 return None
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 def bindCompsTo(self, ns):
00491 for i in range(len(self._compNames)):
00492 ns.bindObject(self._compNames[i].name, self._compNames[i].rtobj)
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 def registerCompName(self, name, rtobj):
00510 for i in range(len(self._compNames)):
00511 if self._compNames[i].name == name:
00512 self._compNames[i].rtobj = rtobj
00513 return
00514
00515 self._compNames.append(self.Comps(name, rtobj))
00516 return
00517
00518
00519 def registerMgrName(self, name, mgr):
00520 for i in range(len(self._mgrNames)):
00521 if self._mgrNames[i].name == name:
00522 self._mgrNames[i].mgr = mgr
00523 return
00524
00525 self._mgrNames.append(self.Mgr(name, mgr))
00526 return
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 def unregisterCompName(self, name):
00543 len_ = len(self._compNames)
00544 for i in range(len_):
00545 idx = (len_-1) - i
00546 if self._compNames[idx].name == name:
00547 del self._compNames[idx]
00548 return
00549 return
00550
00551
00552 def unregisterMgrName(self, name):
00553 len_ = len(self._mgrNames)
00554 for i in range(len_):
00555 idx = (len_ -1) - i
00556 if self._mgrNames[idx].name == name:
00557 del self._mgrNames[idx]
00558 return
00559 return
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582 def retryConnection(self, ns):
00583
00584 nsobj = 0
00585 try:
00586 nsobj = self.createNamingObj(ns.method, ns.nsname)
00587 if nsobj != 0:
00588 self._rtcout.RTC_INFO("Connected to a name server: %s/%s",
00589 (ns.method, ns.nsname))
00590 ns.ns = nsobj
00591 self.bindCompsTo(nsobj)
00592 return
00593 else:
00594 self._rtcout.RTC_DEBUG("Name service: %s/%s still not available.",
00595 (ns.method, ns.nsname))
00596
00597 except:
00598 self._rtcout.RTC_DEBUG("Name server: %s/%s disappeared again.",
00599 (ns.method, ns.nsname))
00600 if nsobj != 0:
00601 del ns.ns
00602 ns.ns = 0
00603
00604 return
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615 class Names:
00616 def __init__(self, meth, name, naming):
00617 self.method = meth
00618 self.nsname = name
00619 self.ns = naming
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630 class Comps:
00631 def __init__(self, n, obj):
00632 self.name = n
00633 self.rtobj = obj
00634
00635
00636 class Mgr:
00637 def __init__(self, n, obj):
00638 self.name = n
00639 self.mgr = obj