blob: 2b93fc2235fc16dc10848a83a2c036a34f7fa7cf [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 inbound UNIX-domain socket
24 * on the Android platform, this is created in the Linux non-filesystem
25 * namespace.
26 *
27 * On simulator platforms, this may be created in a temporary directory on
28 * the filesystem
29 */
30public class LocalServerSocket {
31 private final LocalSocketImpl impl;
32 private final LocalSocketAddress localAddress;
33
34 /** 50 seems a bit much, but it's what was here */
35 private static final int LISTEN_BACKLOG = 50;
36
37 /**
38 * Crewates a new server socket listening at specified name.
39 * On the Android platform, the name is created in the Linux
40 * abstract namespace (instead of on the filesystem).
41 *
42 * @param name address for socket
43 * @throws IOException
44 */
45 public LocalServerSocket(String name) throws IOException
46 {
47 impl = new LocalSocketImpl();
48
49 impl.create(true);
50
51 localAddress = new LocalSocketAddress(name);
52 impl.bind(localAddress);
53
54 impl.listen(LISTEN_BACKLOG);
55 }
56
57 /**
58 * Create a LocalServerSocket from a file descriptor that's already
59 * been created and bound. listen() will be called immediately on it.
60 * Used for cases where file descriptors are passed in via environment
61 * variables
62 *
63 * @param fd bound file descriptor
64 * @throws IOException
65 */
66 public LocalServerSocket(FileDescriptor fd) throws IOException
67 {
68 impl = new LocalSocketImpl(fd);
69 impl.listen(LISTEN_BACKLOG);
70 localAddress = impl.getSockAddress();
71 }
72
73 /**
74 * Obtains the socket's local address
75 *
76 * @return local address
77 */
78 public LocalSocketAddress getLocalSocketAddress()
79 {
80 return localAddress;
81 }
82
83 /**
84 * Accepts a new connection to the socket. Blocks until a new
85 * connection arrives.
86 *
87 * @return a socket representing the new connection.
88 * @throws IOException
89 */
90 public LocalSocket accept() throws IOException
91 {
92 LocalSocketImpl acceptedImpl = new LocalSocketImpl();
93
94 impl.accept (acceptedImpl);
95
96 return new LocalSocket(acceptedImpl);
97 }
98
99 /**
100 * Returns file descriptor or null if not yet open/already closed
101 *
102 * @return fd or null
103 */
104 public FileDescriptor getFileDescriptor() {
105 return impl.getFileDescriptor();
106 }
107
108 /**
109 * Closes server socket.
110 *
111 * @throws IOException
112 */
113 public void close() throws IOException
114 {
115 impl.close();
116 }
117}