1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import java.io.IOException;
20import java.io.FileDescriptor;
21
22/**
23 * Non-standard class for creating an inbound UNIX-domain socket
24 * in the Linux abstract namespace.
25 */
26public class LocalServerSocket {
27    private final LocalSocketImpl impl;
28    private final LocalSocketAddress localAddress;
29
30    /** 50 seems a bit much, but it's what was here */
31    private static final int LISTEN_BACKLOG = 50;
32
33    /**
34     * Creates a new server socket listening at specified name.
35     * On the Android platform, the name is created in the Linux
36     * abstract namespace (instead of on the filesystem).
37     *
38     * @param name address for socket
39     * @throws IOException
40     */
41    public LocalServerSocket(String name) throws IOException
42    {
43        impl = new LocalSocketImpl();
44
45        impl.create(LocalSocket.SOCKET_STREAM);
46
47        localAddress = new LocalSocketAddress(name);
48        impl.bind(localAddress);
49
50        impl.listen(LISTEN_BACKLOG);
51    }
52
53    /**
54     * Create a LocalServerSocket from a file descriptor that's already
55     * been created and bound. listen() will be called immediately on it.
56     * Used for cases where file descriptors are passed in via environment
57     * variables
58     *
59     * @param fd bound file descriptor
60     * @throws IOException
61     */
62    public LocalServerSocket(FileDescriptor fd) throws IOException
63    {
64        impl = new LocalSocketImpl(fd);
65        impl.listen(LISTEN_BACKLOG);
66        localAddress = impl.getSockAddress();
67    }
68
69    /**
70     * Obtains the socket's local address
71     *
72     * @return local address
73     */
74    public LocalSocketAddress getLocalSocketAddress()
75    {
76        return localAddress;
77    }
78
79    /**
80     * Accepts a new connection to the socket. Blocks until a new
81     * connection arrives.
82     *
83     * @return a socket representing the new connection.
84     * @throws IOException
85     */
86    public LocalSocket accept() throws IOException
87    {
88        LocalSocketImpl acceptedImpl = new LocalSocketImpl();
89
90        impl.accept (acceptedImpl);
91
92        return new LocalSocket(acceptedImpl, LocalSocket.SOCKET_UNKNOWN);
93    }
94
95    /**
96     * Returns file descriptor or null if not yet open/already closed
97     *
98     * @return fd or null
99     */
100    public FileDescriptor getFileDescriptor() {
101        return impl.getFileDescriptor();
102    }
103
104    /**
105     * Closes server socket.
106     *
107     * @throws IOException
108     */
109    public void close() throws IOException
110    {
111        impl.close();
112    }
113}
114