ia64/xen-unstable

view tools/python/xen/web/unix.py @ 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 36fe7ca48e54
children
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2005 Mike Wray <mike.wray@hp.com>
16 # Copyright (C) 2005-2006 XenSource Ltd.
17 #============================================================================
20 import os
21 import os.path
22 import socket
23 import stat
25 from xen.util import mkdir
27 import connection
30 def bind(path, type = socket.SOCK_STREAM):
31 """Create a Unix socket, and bind it to the given path.
32 The socket is created such that only the current user may access it."""
34 if path[0] == '\0': # Abstract namespace is used for the path
35 pass
36 else:
37 parent = os.path.dirname(path)
38 mkdir.parents(parent, stat.S_IRWXU, True)
39 if os.path.exists(path):
40 os.unlink(path)
42 sock = socket.socket(socket.AF_UNIX, type)
43 sock.bind(path)
44 return sock
47 class UnixListener(connection.SocketListener):
48 def __init__(self, path, protocol_class):
49 self.path = path
50 connection.SocketListener.__init__(self, protocol_class)
53 def createSocket(self):
54 return bind(self.path, socket.SOCK_STREAM)
57 def acceptConnection(self, sock, _):
58 connection.SocketServerConnection(sock, self.protocol_class)
61 class UnixDgramListener(connection.SocketDgramListener):
62 def __init__(self, path, protocol_class):
63 self.path = path
64 connection.SocketDgramListener.__init__(self, protocol_class)
67 def createSocket(self):
68 return bind(self.path, socket.SOCK_DGRAM)