ia64/xen-unstable

changeset 19361:24af58657d8e

xend: Implement DGRAM (connectionless) type socket listeners

Introduce SocketDgramListener and UnixDgramListener classes.
We already have STREAM (connection) type socket listener classes in
the source tree, but we need DGRAM (connectionless) type listeners to
receive udev events.

Signed-off-by: Yosuke Iwamatsu <y-iwamatsu@ab.jp.nec.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 17 10:36:20 2009 +0000 (2009-03-17)
parents 0e1449d6f231
children c05fa254405d
files tools/python/xen/web/connection.py tools/python/xen/web/unix.py
line diff
     1.1 --- a/tools/python/xen/web/connection.py	Fri Mar 13 10:09:25 2009 +0000
     1.2 +++ b/tools/python/xen/web/connection.py	Tue Mar 17 10:36:20 2009 +0000
     1.3 @@ -292,3 +292,40 @@ def hostAllowed(addrport, hosts_allowed)
     1.4                  return True
     1.5          log.warn("Rejected connection from %s (%s).", addrport[0], fqdn)
     1.6          return False
     1.7 +
     1.8 +
     1.9 +class SocketDgramListener:
    1.10 +    """A connectionless server socket, running listen in a thread.
    1.11 +    """
    1.12 +
    1.13 +    def __init__(self, protocol_class):
    1.14 +        self.protocol = protocol_class()
    1.15 +        self.sock = self.createSocket()
    1.16 +        threading.Thread(target=self.main).start()
    1.17 +
    1.18 +
    1.19 +    def close(self):
    1.20 +        try:
    1.21 +            self.sock.close()
    1.22 +        except:
    1.23 +            pass
    1.24 +
    1.25 +
    1.26 +    def createSocket(self):
    1.27 +        raise NotImplementedError()
    1.28 +
    1.29 +
    1.30 +    def main(self):
    1.31 +        try:
    1.32 +            while True:
    1.33 +                try:
    1.34 +                    data = self.sock.recv(BUFFER_SIZE)
    1.35 +                    self.protocol.dataReceived(data)
    1.36 +                except socket.error, ex:
    1.37 +                    if ex.args[0] not in (EWOULDBLOCK, EAGAIN, EINTR):
    1.38 +                        break
    1.39 +        finally:
    1.40 +            try:
    1.41 +                self.close()
    1.42 +            except:
    1.43 +                pass
     2.1 --- a/tools/python/xen/web/unix.py	Fri Mar 13 10:09:25 2009 +0000
     2.2 +++ b/tools/python/xen/web/unix.py	Tue Mar 17 10:36:20 2009 +0000
     2.3 @@ -27,16 +27,19 @@ from xen.util import mkdir
     2.4  import connection
     2.5  
     2.6  
     2.7 -def bind(path):
     2.8 -    """Create a Unix socket, and bind it to the given path.  The socket is
     2.9 -created such that only the current user may access it."""
    2.10 +def bind(path, type = socket.SOCK_STREAM):
    2.11 +    """Create a Unix socket, and bind it to the given path.
    2.12 +    The socket is created such that only the current user may access it."""
    2.13  
    2.14 -    parent = os.path.dirname(path)
    2.15 -    mkdir.parents(parent, stat.S_IRWXU, True)
    2.16 -    if os.path.exists(path):
    2.17 -        os.unlink(path)
    2.18 +    if path[0] == '\0': # Abstract namespace is used for the path
    2.19 +        pass
    2.20 +    else:
    2.21 +        parent = os.path.dirname(path)
    2.22 +        mkdir.parents(parent, stat.S_IRWXU, True)
    2.23 +        if os.path.exists(path):
    2.24 +            os.unlink(path)
    2.25  
    2.26 -    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    2.27 +    sock = socket.socket(socket.AF_UNIX, type)
    2.28      sock.bind(path)
    2.29      return sock
    2.30  
    2.31 @@ -48,8 +51,19 @@ class UnixListener(connection.SocketList
    2.32  
    2.33  
    2.34      def createSocket(self):
    2.35 -        return bind(self.path)
    2.36 +        return bind(self.path, socket.SOCK_STREAM)
    2.37  
    2.38  
    2.39      def acceptConnection(self, sock, _):
    2.40          connection.SocketServerConnection(sock, self.protocol_class)
    2.41 +
    2.42 +
    2.43 +class UnixDgramListener(connection.SocketDgramListener):
    2.44 +    def __init__(self, path, protocol_class):
    2.45 +        self.path = path
    2.46 +        connection.SocketDgramListener.__init__(self, protocol_class)
    2.47 +
    2.48 +
    2.49 +    def createSocket(self):
    2.50 +        return bind(self.path, socket.SOCK_DGRAM)
    2.51 +