transport.endpoint stuff
authorTero Marttila <terom@fixme.fi>
Sun, 23 Aug 2009 22:31:43 +0300
changeset 32 2168a9ffaef7
parent 31 82121ae07c03
child 33 c71de00715d6
transport.endpoint stuff
qmsk/net/transport/__init__.py
qmsk/net/transport/endpoint.py
--- a/qmsk/net/transport/__init__.py	Sun Aug 23 22:31:20 2009 +0300
+++ b/qmsk/net/transport/__init__.py	Sun Aug 23 22:31:43 2009 +0300
@@ -29,9 +29,10 @@
 
         Endpoint        - some transport-level endpoint (local/remote address/socket)
 
-            Client          - connection-oriented endpoint that can establish a Transport to some remote Service
 
-            Service         - listening endpoint that recieves connections from remote Clients and represents those as Transports
+        Client          - connection-oriented endpoint that can establish a Transport to some remote Service
+
+        Service         - listening endpoint that recieves connections from remote Clients and represents those as Transports
 
 
     Next, there are some mid-level helpers, mostly for dealing with 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qmsk/net/transport/endpoint.py	Sun Aug 23 22:31:43 2009 +0300
@@ -0,0 +1,101 @@
+
+from qmsk.net.socket import address, constants, af_unix
+
+class AddrInfo (address.addrinfo) :
+    """
+        Represents a sockaddr along with related address-family etc. information
+    """
+
+class Endpoint (object) :
+    """
+        Abstract network address interface, 
+    """
+
+    def resolve (self, family, socktype, protocol = 0, passive = True) :
+        """
+            Translate this Endpoint into a sequence of AddrInfo objects for the given family/socktype.
+        """
+        
+        raise NotImplemented()
+
+
+class InetAddr (Endpoint, address.endpoint) :
+    """
+        An internet address, either IPv4 or IPv6.
+
+           hostname     - [str] literal address or DNS hostname
+           service      - [str] port number or service name
+    """
+    
+    def resolve (self, family, socktype, protocol = 0, passive = True) :
+        """
+            Resolve using getaddrinfo
+        """
+
+        flags = 0
+
+        if passive :
+            flags |= socket.AI_PASSIVE
+
+        return self.getaddrinfo(0, family, socktype, protocol, flags)
+
+class UnixAddr (Endpoint) :
+    """
+        A local AF_UNIX address, as a path.
+    """
+
+    def __init__ (self, path) :
+        self.path = path
+        self.addr = af_unix.sockaddr_un(path)
+
+    def resolve (self, family, socktype, protocol = 0, passive = True) :
+        if family != constants.AF_UNIX :
+            raise ValueError("Address family mismatch: %s" % (family, ))
+
+        if not socktype :
+            raise ValueError("Unknown socktype: %s" % (socktype, ))
+
+        return [AddrInfo(0, constants.AF_UNIX, socktype, protocol, self.addr, self.path)]
+
+class SockAddr (Endpoint) :
+    """
+        A specific address, suitable both as an endpoint.
+
+            addr        - low-level sockaddr object
+            family      - AF_*
+            socktype    - SOCK_*
+    """
+
+    def __init__ (self, addr, family = constants.AF_UNSPEC, socktype = 0) :
+        if not family :
+            family = addr.family
+
+        self.addr = addr
+        self.family = family
+        self.socktype = socktype
+
+    def resolve (self, family, socktype, protocol = 0, passive = True) :
+        """
+            Returns a single AddrInfo object representing this address 
+        """
+    
+        if family and family != self.family :
+            raise ValueError("Address family mismatch: %s should be %s" % (family, self.family))
+        
+        elif not family :
+            family = self.family
+
+
+        if socktype and self.socktype and socktype != self.socktype :
+            raise ValueError("Socket type mismatch: %s should be %s" % (socktype, self.socktype))
+
+        if not socktype :
+            if self.socktype :
+                socktype = self.socktype
+
+            else :
+                raise ValueError("Socket type unknown")
+
+
+        return [AddrInfo(0, family, socktype, protocol, self.addr, None)]
+