summaryrefslogtreecommitdiffstats
path: root/core/java/android/net/LinkSocket.java
blob: 5aa64512dc82a2318192be0af28f5fc6717f4e48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net;

import android.net.LinkCapabilities;
import android.net.LinkProperties;
import android.net.LinkSocketNotifier;

import android.util.Log;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;

/** @hide */
public class LinkSocket extends Socket {
    private final static String TAG = "LinkSocket";
    private final static boolean DBG = true;

    /**
     * Default constructor
     */
    public LinkSocket() {
        if (DBG) log("LinkSocket() EX");
    }

    /**
     * Creates a new unconnected socket.
     * @param notifier a reference to a class that implements {@code LinkSocketNotifier}
     */
    public LinkSocket(LinkSocketNotifier notifier) {
        if (DBG) log("LinkSocket(notifier) EX");
    }

    /**
     * Creates a new unconnected socket usign the given proxy type.
     * @param notifier a reference to a class that implements {@code LinkSocketNotifier}
     * @param proxy the specified proxy for this socket
     * @throws IllegalArgumentException if the argument proxy is null or of an invalid type.
     * @throws SecurityException if a security manager exists and it denies the permission
     *                           to connect to the given proxy.
     */
    public LinkSocket(LinkSocketNotifier notifier, Proxy proxy) {
        if (DBG) log("LinkSocket(notifier, proxy) EX");
    }

    /**
     * @return the {@code LinkProperties} for the socket
     */
    public LinkProperties getLinkProperties() {
        if (DBG) log("LinkProperties() EX");
        return new LinkProperties();
    }

    /**
     * Set the {@code LinkCapabilies} needed for this socket.  If the socket is already connected
     * or is a duplicate socket the request is ignored and {@code false} will
     * be returned. A needs map can be created via the {@code createNeedsMap} static
     * method.
     * @param needs the needs of the socket
     * @return {@code true} if needs are successfully set, {@code false} otherwise
     */
    public boolean setNeededCapabilities(LinkCapabilities needs) {
        if (DBG) log("setNeeds() EX");
        return false;
    }

    /**
     * @return the LinkCapabilites set by setNeededCapabilities, empty if none has been set
     */
    public LinkCapabilities getNeededCapabilities() {
        if (DBG) log("getNeeds() EX");
        return null;
    }

    /**
     * @return all of the {@code LinkCapabilities} of the link used by this socket
     */
    public LinkCapabilities getCapabilities() {
        if (DBG) log("getCapabilities() EX");
        return null;
    }

    /**
     * Returns this LinkSockets set of capabilities, filtered according to
     * the given {@code Set}.  Capabilities in the Set but not available from
     * the link will not be reported in the results.  Capabilities of the link
     * but not listed in the Set will also not be reported in the results.
     * @param capabilities {@code Set} of capabilities requested
     * @return the filtered {@code LinkCapabilities} of this LinkSocket, may be empty
     */
    public LinkCapabilities getCapabilities(Set<Integer> capabilities) {
        if (DBG) log("getCapabilities(capabilities) EX");
        return new LinkCapabilities();
    }

    /**
     * Provide the set of capabilities the application is interested in tracking
     * for this LinkSocket.
     * @param capabilities a {@code Set} of capabilities to track
     */
    public void setTrackedCapabilities(Set<Integer> capabilities) {
        if (DBG) log("setTrackedCapabilities(capabilities) EX");
    }

    /**
     * @return the {@code LinkCapabilities} that are tracked, empty if none has been set.
     */
    public Set<Integer> getTrackedCapabilities() {
        if (DBG) log("getTrackedCapabilities(capabilities) EX");
        return new HashSet<Integer>();
    }

    /**
     * Connects this socket to the given remote host address and port specified
     * by dstName and dstPort.
     * @param dstName the address of the remote host to connect to
     * @param dstPort the port to connect to on the remote host
     * @param timeout the timeout value in milliseconds or 0 for infinite timeout
     * @throws UnknownHostException if the given dstName is invalid
     * @throws IOException if the socket is already connected or an error occurs
     *                     while connecting
     * @throws SocketTimeoutException if the timeout fires
     */
    public void connect(String dstName, int dstPort, int timeout)
            throws UnknownHostException, IOException, SocketTimeoutException {
        if (DBG) log("connect(dstName, dstPort, timeout) EX");
    }

    /**
     * Connects this socket to the given remote host address and port specified
     * by dstName and dstPort.
     * @param dstName the address of the remote host to connect to
     * @param dstPort the port to connect to on the remote host
     * @throws UnknownHostException if the given dstName is invalid
     * @throws IOException if the socket is already connected or an error occurs
     *                     while connecting
     */
    public void connect(String dstName, int dstPort)
            throws UnknownHostException, IOException {
        if (DBG) log("connect(dstName, dstPort, timeout) EX");
    }

    /**
     * Connects this socket to the given remote host address and port specified
     * by the SocketAddress with the specified timeout.
     * @deprecated Use {@code connect(String dstName, int dstPort, int timeout)}
     *             instead.  Using this method may result in reduced functionality.
     * @param remoteAddr the address and port of the remote host to connect to
     * @throws IllegalArgumentException if the given SocketAddress is invalid
     * @throws IOException if the socket is already connected or an error occurs
     *                     while connecting
     * @throws SocketTimeoutException if the timeout expires
     */
    @Override
    @Deprecated
    public void connect(SocketAddress remoteAddr, int timeout)
            throws IOException, SocketTimeoutException {
        if (DBG) log("connect(remoteAddr, timeout) EX DEPRECATED");
    }

    /**
     * Connects this socket to the given remote host address and port specified
     * by the SocketAddress.
     * TODO add comment on all these that the network selection happens during connect
     * and may take 30 seconds
     * @deprecated Use {@code connect(String dstName, int dstPort)}
     *             Using this method may result in reduced functionality.
     * @param remoteAddr the address and port of the remote host to connect to.
     * @throws IllegalArgumentException if the SocketAddress is invalid or not supported.
     * @throws IOException if the socket is already connected or an error occurs
     *                     while connecting
     */
    @Override
    @Deprecated
    public void connect(SocketAddress remoteAddr) throws IOException {
        if (DBG) log("connect(remoteAddr) EX DEPRECATED");
    }

    /**
     * Connect a duplicate socket socket to the same remote host address and port
     * as the original with a timeout parameter.
     * @param timeout the timeout value in milliseconds or 0 for infinite timeout
     * @throws IOException if the socket is already connected or an error occurs
     *                     while connecting
     */
    public void connect(int timeout) throws IOException {
        if (DBG) log("connect(timeout) EX");
    }

    /**
     * Connect a duplicate socket socket to the same remote host address and port
     * as the original.
     * @throws IOException if the socket is already connected or an error occurs
     *                     while connecting
     */
    public void connect() throws IOException {
        if (DBG) log("connect() EX");
    }

    /**
     * Closes the socket.  It is not possible to reconnect or rebind to this
     * socket thereafter which means a new socket instance has to be created.
     * @throws IOException if an error occurs while closing the socket
     */
    @Override
    public synchronized void close() throws IOException {
        if (DBG) log("close() EX");
    }

    /**
     * Request that a new LinkSocket be created using a different radio
     * (such as WiFi or 3G) than the current LinkSocket.  If a different
     * radio is available a call back will be made via {@code onBetterLinkAvail}.
     * If unable to find a better radio, application will be notified via
     * {@code onNewLinkUnavailable}
     * @see LinkSocketNotifier#onBetterLinkAvailable(LinkSocket, LinkSocket)
     * @param linkRequestReason reason for requesting a new link.
     */
    public void requestNewLink(LinkRequestReason linkRequestReason) {
        if (DBG) log("requestNewLink(linkRequestReason) EX");
    }

    /**
     * @deprecated LinkSocket will automatically pick the optimum interface
     *             to bind to
     * @param localAddr the specific address and port on the local machine
     *                  to bind to
     * @throws IOException always as this method is deprecated for LinkSocket
     */
    @Override
    @Deprecated
    public void bind(SocketAddress localAddr) throws UnsupportedOperationException {
        if (DBG) log("bind(localAddr) EX throws IOException");
        throw new UnsupportedOperationException("bind is deprecated for LinkSocket");
    }

    /**
     * Reason codes an application can specify when requesting for a new link.
     * TODO: need better documentation
     */
    public static final class LinkRequestReason {
        /** No constructor */
        private LinkRequestReason() {}

        /** This link is working properly */
        public static final int LINK_PROBLEM_NONE = 0;
        /** This link has an unknown issue */
        public static final int LINK_PROBLEM_UNKNOWN = 1;
    }

    /**
     * Debug logging
     */
    protected static void log(String s) {
        Log.d(TAG, s);
    }
}