qmsk/net/transport/endpoint.py
changeset 37 14db3fe42b6c
parent 35 50dc1517f797
child 38 f0fc793a3754
equal deleted inserted replaced
36:4d5c02fe9c27 37:14db3fe42b6c
     9 class Endpoint (object) :
     9 class Endpoint (object) :
    10     """
    10     """
    11         Abstract network address interface, 
    11         Abstract network address interface, 
    12     """
    12     """
    13 
    13 
    14     def resolve (self, family, socktype, protocol = 0, passive = True) :
    14     def resolve (self, socktype, protocol = 0, passive = True) :
    15         """
    15         """
    16             Translate this Endpoint into a sequence of AddrInfo objects for the given family/socktype.
    16             Translate this Endpoint into a sequence of AddrInfo objects for the given socktype.
    17         """
    17         """
    18         
    18         
    19         raise NotImplemented()
    19         raise NotImplemented()
    20 
    20 
    21 
    21 
    22 class InetAddr (Endpoint, address.endpoint) :
    22 class InetAddr (Endpoint) :
    23     """
    23     """
    24         An internet address, either IPv4 or IPv6.
    24         An internet address, either IPv4 or IPv6.
    25 
    25 
    26            hostname     - [str] literal address or DNS hostname
    26            hostname     - [str] literal address or DNS hostname
    27            service      - [str] port number or service name
    27            service      - [str] port number or service name
       
    28            family       - AF_* associated with this address
    28     """
    29     """
       
    30 
       
    31     def __init__ (self, hostname=None, service=None, family=constants.AF_UNSPEC) :
       
    32         """
       
    33             Initialize with given parameters, but doesn't perform any lookups yet.
       
    34         """
       
    35 
       
    36         self.endpoint = address.endpoint(hostname, service)
       
    37         self.family = family
    29     
    38     
    30     def resolve (self, family, socktype, protocol = 0, passive = True) :
    39     @property
       
    40     def hostname (self) :
       
    41         return self.endpoint.hostname
       
    42 
       
    43     @property
       
    44     def service (self) :
       
    45         return self.endpoint.service
       
    46 
       
    47     def resolve (self, socktype, protocol = 0, passive = True) :
    31         """
    48         """
    32             Resolve using getaddrinfo
    49             Resolve using getaddrinfo
    33         """
    50         """
    34 
    51 
    35         flags = 0
    52         flags = 0
    36 
    53 
    37         if passive :
    54         if passive :
    38             flags |= constants.AI_PASSIVE
    55             flags |= constants.AI_PASSIVE
    39 
    56 
    40         return self.getaddrinfo(family, socktype, protocol, flags)
    57         return self.endpoint.getaddrinfo(self.family, socktype, protocol, flags)
    41 
    58 
    42 class UnixAddr (Endpoint) :
    59 class UnixAddr (Endpoint) :
    43     """
    60     """
    44         A local AF_UNIX address, as a path.
    61         A local AF_UNIX address, as a path.
    45     """
    62     """
    46 
    63 
    47     def __init__ (self, path) :
    64     def __init__ (self, path) :
    48         self.path = path
    65         self.path = path
    49         self.addr = af_unix.sockaddr_un(path)
    66         self.addr = af_unix.sockaddr_un(path)
    50 
    67 
    51     def resolve (self, family, socktype, protocol = 0, passive = True) :
    68     def resolve (self, socktype, protocol = 0, passive = True) :
    52         if family != constants.AF_UNIX :
       
    53             raise ValueError("Address family mismatch: %s" % (family, ))
       
    54 
       
    55         if not socktype :
    69         if not socktype :
    56             raise ValueError("Unknown socktype: %s" % (socktype, ))
    70             raise ValueError("Unknown socktype: %s" % (socktype, ))
    57 
    71 
    58         return [AddrInfo(0, constants.AF_UNIX, socktype, protocol, self.addr, self.path)]
    72         return [AddrInfo(0, constants.AF_UNIX, socktype, protocol, self.addr, self.path)]
    59 
    73 
    72 
    86 
    73         self.addr = addr
    87         self.addr = addr
    74         self.family = family
    88         self.family = family
    75         self.socktype = socktype
    89         self.socktype = socktype
    76 
    90 
    77     def resolve (self, family, socktype, protocol = 0, passive = True) :
    91     def resolve (self, socktype, protocol = 0, passive = True) :
    78         """
    92         """
    79             Returns a single AddrInfo object representing this address 
    93             Returns a single AddrInfo object representing this address 
    80         """
    94         """
    81     
    95     
    82         if family and family != self.family :
       
    83             raise ValueError("Address family mismatch: %s should be %s" % (family, self.family))
       
    84         
       
    85         elif not family :
       
    86             family = self.family
       
    87 
       
    88 
       
    89         if socktype and self.socktype and socktype != self.socktype :
    96         if socktype and self.socktype and socktype != self.socktype :
    90             raise ValueError("Socket type mismatch: %s should be %s" % (socktype, self.socktype))
    97             raise ValueError("Socket type mismatch: %s should be %s" % (socktype, self.socktype))
    91 
    98 
    92         if not socktype :
    99         if not socktype :
    93             if self.socktype :
   100             if self.socktype :
    95 
   102 
    96             else :
   103             else :
    97                 raise ValueError("Socket type unknown")
   104                 raise ValueError("Socket type unknown")
    98 
   105 
    99 
   106 
   100         return [AddrInfo(0, family, socktype, protocol, self.addr, None)]
   107         return [AddrInfo(0, self.family, socktype, protocol, self.addr, None)]
   101 
   108