Package node_manager_fkie :: Module name_resolution
[frames] | no frames]

Source Code for Module node_manager_fkie.name_resolution

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  from threading import RLock 
34 35 -class MasterEntry(object):
36
37 - def __init__(self, masteruri=None, mastername=None, address=None, hostname=None):
38 self.masteruri = masteruri 39 self._masternames = [mastername] if mastername else [] 40 self._hostnames = [hostname] if hostname else [] 41 self._addresses = [address] if address else []
42
43 - def __repr__(self):
44 return ''.join([str(self.masteruri), ':\n', 45 ' masternames: ', str(self._masternames), '\n', 46 ' hostnames: ', str(self._hostnames), '\n', 47 ' addresses: ', str(self._addresses), '\n'])
48
49 - def hasMastername(self, mastername):
50 if mastername in self._masternames: 51 return True 52 return False
53
54 - def hasHostname(self, hostname):
55 if hostname in self._hostnames: 56 return True 57 return False
58
59 - def hasAddress(self, address):
60 if address in self._addresses: 61 return True 62 return False
63
64 - def addMastername(self, mastername):
65 if mastername and not self.hasMastername(mastername): 66 self._masternames.append(mastername)
67
68 - def addHostname(self, hostname):
69 if hostname and not self.hasHostname(hostname): 70 self._hostnames.append(hostname)
71
72 - def addAddress(self, address):
73 if address and not self.hasAddress(address): 74 self._addresses.append(address)
75
76 - def getMastername(self):
77 if len(self._masternames) > 0: 78 return self._masternames[0] 79 return None
80
81 - def getMasternames(self):
82 return list(self._masternames)
83
84 - def getAddress(self):
85 if len(self._addresses) > 0: 86 return self._addresses[0] 87 return None
88
89 - def getHostname(self):
90 if len(self._hostnames) > 0: 91 return self._hostnames[0] 92 return None
93
94 - def removeExtMastername(self, mastername):
95 if len(self._masternames) > 1: 96 try: 97 self._masternames.remove(mastername) 98 except: 99 pass
100
101 - def removeExtHostname(self, hostname):
102 if len(self._hostnames) > 1: 103 try: 104 self._hostnames.remove(hostname) 105 except: 106 pass
107
108 - def removeExtAddress(self, address):
109 if len(self._addresses) > 1: 110 try: 111 self._addresses.remove(address) 112 except: 113 pass
114
115 116 -class NameResolution(object):
117 ''' 118 This class stores the association between master URI, master name and 119 host name or IP. Both the setter and the getter methods are thread safe. 120 ''' 121
122 - def __init__(self):
123 self.mutex = RLock() 124 self._masters = [] #sets with masters 125 self._hosts = [] #sets with hosts 126 self._address = [] # avoid the mixing of ip and name as address
127
128 - def removeMasterEntry(self, masteruri):
129 with self.mutex: 130 for m in self._masters: 131 if masteruri and m.masteruri == masteruri: 132 self._masters.remove(m) 133 return
134
135 - def removeInfo(self, mastername, hostname):
136 with self.mutex: 137 for m in self._masters: 138 if m.hasMastername(mastername) and (m.hasHostname(hostname) or m.hasAddress(hostname)): 139 m.removeExtMastername(mastername) 140 m.removeExtHostname(hostname) 141 m.removeExtAddress(hostname) 142 return
143 144 # def remove(self, name=None, masteruri=None, host=None): 145 # ''' 146 # Remove an association. If one of the given parameter found in the association, 147 # the association will be removed from the name resolution list. 148 # @param name: the name of the ROS master. 149 # @type name: C{str} (Default: C{None}) 150 # @param masteruri: the URI of the ROS master. 151 # @type masteruri: C{str} (Default: C{None}) 152 # @param host: the host name or IP 153 # @type host: C{str} (Default: C{None}) 154 # ''' 155 # try: 156 # self.mutex.acquire() 157 # for s in list(self._masters): 158 # if (name, 'name') in s or (masteruri, 'uri') in s or (host, 'host') in s: 159 # try: 160 # s.remove((name, 'name')) 161 # except: 162 # pass 163 # try: 164 # s.remove((masteruri, 'uri')) 165 # except: 166 # pass 167 # try: 168 # s.remove((host, 'host')) 169 # self._address.remove(host) 170 # except: 171 # pass 172 # if len(s) < 2: 173 # self._masters.remove(s) 174 # finally: 175 # self.mutex.release() 176
177 - def addMasterEntry(self, masteruri, mastername, address, hostname=None):
178 with self.mutex: 179 mastername = self._validateMastername(mastername, masteruri) 180 for m in self._masters: 181 if m.masteruri and m.masteruri == masteruri: 182 m.addMastername(mastername) 183 m.addHostname(hostname) 184 m.addAddress(address) 185 return 186 elif m.masteruri is None and m.hasMastername(mastername): 187 m.masteruri = masteruri 188 m.addMastername(mastername) 189 m.addHostname(hostname) 190 m.addAddress(address) 191 return 192 self._masters.append(MasterEntry(masteruri, mastername, address, hostname))
193
194 - def addInfo(self, mastername, address, hostname=None):
195 with self.mutex: 196 # mastername = self._validateMastername(mastername) 197 for m in self._masters: 198 if m.hasMastername(mastername): 199 m.addMastername(mastername) 200 m.addHostname(hostname) 201 m.addAddress(address) 202 return 203 elif mastername is None: 204 if m.hasHostname(hostname) or m.hasAddress(address): 205 m.addHostname(hostname) 206 m.addAddress(address) 207 if not mastername is None: 208 self._masters.append(MasterEntry(None, mastername, address, hostname))
209
210 - def _validateMastername(self, mastername, masteruri):
211 ''' 212 Not thead safe 213 ''' 214 # with self.mutex: 215 mm = self.masteruri(mastername) 216 if mm and mm != masteruri: 217 print "EROROR name", mastername, "bereits fuer", mm, "vergeben!" 218 nr = 2 219 while mm and mm != masteruri: 220 mm = self.masteruri('_'.join([mastername, str(nr)])) 221 nr = nr + 1 222 return '_'.join([mastername, str(nr)]) 223 return mastername
224
225 - def hasMaster(self, masteruri):
226 with self.mutex: 227 for m in self._masters: 228 if m.masteruri == masteruri: 229 return True 230 return False
231
232 - def mastername(self, masteruri, address=None):
233 with self.mutex: 234 for m in self._masters: 235 if m.masteruri == masteruri: 236 if not address is None: 237 if m.hasAddress(address): 238 return m.getMastername() 239 else: 240 return m.getMastername() 241 return None
242
243 - def masternames(self, masteruri):
244 with self.mutex: 245 for m in self._masters: 246 if m.masteruri == masteruri: 247 return m.getMasternames() 248 return list()
249
250 - def masternameByAddress(self, address):
251 with self.mutex: 252 for m in self._masters: 253 if m.hasAddress(address): 254 return m.getMastername() 255 return None
256
257 - def masteruri(self, mastername):
258 with self.mutex: 259 for m in self._masters: 260 if m.hasMastername(mastername): 261 return m.masteruri 262 return None
263
264 - def masterurisByHost(self, hostname):
265 with self.mutex: 266 result = [] 267 for m in self._masters: 268 if m.hasHostname(hostname) and m.masteruri and not m.masteruri in result: 269 result.append(m.masteruri) 270 return result
271
272 - def address(self, masteruri):
273 with self.mutex: 274 for m in self._masters: 275 if m.masteruri == masteruri or m.hasHostname(masteruri) or m.hasMastername(masteruri): 276 return m.getAddress() 277 return None
278
279 - def hostname(self, address):
280 with self.mutex: 281 for m in self._masters: 282 if m.hasAddress(address) or m.hasMastername(address): 283 result = m.getHostname() 284 return result if result else address 285 return address
286 287 @classmethod
288 - def getHostname(cls, url):
289 ''' 290 Returns the host name used in a url 291 292 @return: host or None if url is invalid 293 @rtype: C{str} 294 ''' 295 if url is None: 296 return None 297 from urlparse import urlparse 298 o = urlparse(url) 299 return o.hostname
300