util/Zeroconf.py
changeset 1736 7e61baa047f0
parent 1730 64d8f52bc8c8
child 1737 a39c2918c015
equal deleted inserted replaced
1735:c02818d7e29f 1736:7e61baa047f0
   181            _TYPE_SRV : "srv",
   181            _TYPE_SRV : "srv",
   182            _TYPE_ANY : "any" }
   182            _TYPE_ANY : "any" }
   183 
   183 
   184 # utility functions
   184 # utility functions
   185 
   185 
       
   186 
   186 def currentTimeMillis():
   187 def currentTimeMillis():
   187     """Current system time in milliseconds"""
   188     """Current system time in milliseconds"""
   188     return time.time() * 1000
   189     return time.time() * 1000
   189 
   190 
   190 # Exceptions
   191 # Exceptions
   191 
   192 
       
   193 
   192 class NonLocalNameException(Exception):
   194 class NonLocalNameException(Exception):
   193     pass
   195     pass
   194 
   196 
       
   197 
   195 class NonUniqueNameException(Exception):
   198 class NonUniqueNameException(Exception):
   196     pass
   199     pass
   197 
   200 
       
   201 
   198 class NamePartTooLongException(Exception):
   202 class NamePartTooLongException(Exception):
   199     pass
   203     pass
   200 
   204 
       
   205 
   201 class AbstractMethodException(Exception):
   206 class AbstractMethodException(Exception):
   202     pass
   207     pass
   203 
   208 
       
   209 
   204 class BadTypeInNameException(Exception):
   210 class BadTypeInNameException(Exception):
   205     pass
   211     pass
   206 
   212 
   207 # implementation classes
   213 # implementation classes
       
   214 
   208 
   215 
   209 class DNSEntry(object):
   216 class DNSEntry(object):
   210     """A DNS entry"""
   217     """A DNS entry"""
   211 
   218 
   212     def __init__(self, name, type, clazz):
   219     def __init__(self, name, type, clazz):
   252             result += ",%s]" % (other)
   259             result += ",%s]" % (other)
   253         else:
   260         else:
   254             result += "]"
   261             result += "]"
   255         return result
   262         return result
   256 
   263 
       
   264 
   257 class DNSQuestion(DNSEntry):
   265 class DNSQuestion(DNSEntry):
   258     """A DNS question entry"""
   266     """A DNS question entry"""
   259 
   267 
   260     def __init__(self, name, type, clazz):
   268     def __init__(self, name, type, clazz):
   261         if not name.endswith(".local."):
   269         if not name.endswith(".local."):
   330     def toString(self, other):
   338     def toString(self, other):
   331         """String representation with addtional information"""
   339         """String representation with addtional information"""
   332         arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other)
   340         arg = "%s/%s,%s" % (self.ttl, self.getRemainingTTL(currentTimeMillis()), other)
   333         return DNSEntry.toString(self, "record", arg)
   341         return DNSEntry.toString(self, "record", arg)
   334 
   342 
       
   343 
   335 class DNSAddress(DNSRecord):
   344 class DNSAddress(DNSRecord):
   336     """A DNS address record"""
   345     """A DNS address record"""
   337 
   346 
   338     def __init__(self, name, type, clazz, ttl, address):
   347     def __init__(self, name, type, clazz, ttl, address):
   339         DNSRecord.__init__(self, name, type, clazz, ttl)
   348         DNSRecord.__init__(self, name, type, clazz, ttl)
   354         try:
   363         try:
   355             return socket.inet_ntoa(self.address)
   364             return socket.inet_ntoa(self.address)
   356         except:
   365         except:
   357             return self.address
   366             return self.address
   358 
   367 
       
   368 
   359 class DNSHinfo(DNSRecord):
   369 class DNSHinfo(DNSRecord):
   360     """A DNS host information record"""
   370     """A DNS host information record"""
   361 
   371 
   362     def __init__(self, name, type, clazz, ttl, cpu, os):
   372     def __init__(self, name, type, clazz, ttl, cpu, os):
   363         DNSRecord.__init__(self, name, type, clazz, ttl)
   373         DNSRecord.__init__(self, name, type, clazz, ttl)
   377 
   387 
   378     def __repr__(self):
   388     def __repr__(self):
   379         """String representation"""
   389         """String representation"""
   380         return self.cpu + " " + self.os
   390         return self.cpu + " " + self.os
   381 
   391 
       
   392 
   382 class DNSPointer(DNSRecord):
   393 class DNSPointer(DNSRecord):
   383     """A DNS pointer record"""
   394     """A DNS pointer record"""
   384 
   395 
   385     def __init__(self, name, type, clazz, ttl, alias):
   396     def __init__(self, name, type, clazz, ttl, alias):
   386         DNSRecord.__init__(self, name, type, clazz, ttl)
   397         DNSRecord.__init__(self, name, type, clazz, ttl)
   397         return 0
   408         return 0
   398 
   409 
   399     def __repr__(self):
   410     def __repr__(self):
   400         """String representation"""
   411         """String representation"""
   401         return self.toString(self.alias)
   412         return self.toString(self.alias)
       
   413 
   402 
   414 
   403 class DNSText(DNSRecord):
   415 class DNSText(DNSRecord):
   404     """A DNS text record"""
   416     """A DNS text record"""
   405 
   417 
   406     def __init__(self, name, type, clazz, ttl, text):
   418     def __init__(self, name, type, clazz, ttl, text):
   422         if len(self.text) > 10:
   434         if len(self.text) > 10:
   423             return self.toString(self.text[:7] + "...")
   435             return self.toString(self.text[:7] + "...")
   424         else:
   436         else:
   425             return self.toString(self.text)
   437             return self.toString(self.text)
   426 
   438 
       
   439 
   427 class DNSService(DNSRecord):
   440 class DNSService(DNSRecord):
   428     """A DNS service record"""
   441     """A DNS service record"""
   429 
   442 
   430     def __init__(self, name, type, clazz, ttl, priority, weight, port, server):
   443     def __init__(self, name, type, clazz, ttl, priority, weight, port, server):
   431         DNSRecord.__init__(self, name, type, clazz, ttl)
   444         DNSRecord.__init__(self, name, type, clazz, ttl)
   448         return 0
   461         return 0
   449 
   462 
   450     def __repr__(self):
   463     def __repr__(self):
   451         """String representation"""
   464         """String representation"""
   452         return self.toString("%s:%s" % (self.server, self.port))
   465         return self.toString("%s:%s" % (self.server, self.port))
       
   466 
   453 
   467 
   454 class DNSIncoming(object):
   468 class DNSIncoming(object):
   455     """Object representation of an incoming DNS packet"""
   469     """Object representation of an incoming DNS packet"""
   456 
   470 
   457     def __init__(self, data):
   471     def __init__(self, data):
   887 
   901 
   888     def notify(self):
   902     def notify(self):
   889         self.condition.acquire()
   903         self.condition.acquire()
   890         self.condition.notify()
   904         self.condition.notify()
   891         self.condition.release()
   905         self.condition.release()
       
   906 
   892 
   907 
   893 class Listener(object):
   908 class Listener(object):
   894     """A Listener is used by this module to listen on the multicast
   909     """A Listener is used by this module to listen on the multicast
   895     group to which DNS messages are sent, allowing the implementation
   910     group to which DNS messages are sent, allowing the implementation
   896     to cache information as it arrives.
   911     to cache information as it arrives.