blob: 0c0872a78f74be343158d40cc571964e8956c2dc [file] [log] [blame]
Robert Greenwalt9ba9c582014-03-19 17:56:12 -07001/*
2 * Copyright (C) 2014 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 android.os.Parcelable;
20import android.os.Parcel;
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -070021import android.system.ErrnoException;
Erik Klined8959992015-06-05 17:37:45 +090022import android.system.Os;
23import android.system.OsConstants;
Robert Greenwalt9ba9c582014-03-19 17:56:12 -070024
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -070025import java.io.FileDescriptor;
Paul Jensen2d6f2652014-05-20 11:25:35 -040026import java.io.IOException;
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -070027import java.net.DatagramSocket;
Paul Jensen3e32a4b2014-05-13 19:05:13 -040028import java.net.InetAddress;
Paul Jensen2d6f2652014-05-20 11:25:35 -040029import java.net.InetSocketAddress;
Lorenzo Colittic473dc42014-07-18 01:34:19 +090030import java.net.MalformedURLException;
Robert Greenwalt85956262014-05-18 20:29:39 -070031import java.net.Socket;
Lorenzo Colittif0382892014-07-30 17:17:13 +090032import java.net.SocketAddress;
Paul Jensen32a58f02014-06-20 13:58:14 -040033import java.net.SocketException;
Paul Jensen3e32a4b2014-05-13 19:05:13 -040034import java.net.UnknownHostException;
Lorenzo Colittic473dc42014-07-18 01:34:19 +090035import java.net.URL;
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -070036import java.net.URLConnection;
Tobias Thierer851d8062016-06-01 20:51:45 +010037import java.util.Arrays;
38import java.util.List;
39import java.util.concurrent.TimeUnit;
Robert Greenwalt85956262014-05-18 20:29:39 -070040import javax.net.SocketFactory;
Robert Greenwalt9ba9c582014-03-19 17:56:12 -070041
Lorenzo Colitti0ed0a282014-08-28 17:15:40 -070042import com.android.okhttp.ConnectionPool;
Tobias Thierer851d8062016-06-01 20:51:45 +010043import com.android.okhttp.Dns;
Paul Jensen07ed0742014-09-08 14:59:09 -040044import com.android.okhttp.HttpHandler;
45import com.android.okhttp.HttpsHandler;
Lorenzo Colittic473dc42014-07-18 01:34:19 +090046import com.android.okhttp.OkHttpClient;
Neil Fuller50a01d82015-01-12 16:49:06 +000047import com.android.okhttp.OkUrlFactory;
48import com.android.okhttp.internal.Internal;
Lorenzo Colittic473dc42014-07-18 01:34:19 +090049
Robert Greenwalt9ba9c582014-03-19 17:56:12 -070050/**
Robert Greenwalt85956262014-05-18 20:29:39 -070051 * Identifies a {@code Network}. This is supplied to applications via
Robert Greenwalt6078b502014-06-11 16:05:07 -070052 * {@link ConnectivityManager.NetworkCallback} in response to the active
53 * {@link ConnectivityManager#requestNetwork} or passive
54 * {@link ConnectivityManager#registerNetworkCallback} calls.
Robert Greenwalt85956262014-05-18 20:29:39 -070055 * It is used to direct traffic to the given {@code Network}, either on a {@link Socket} basis
Paul Jensen0a363a32014-05-29 10:12:39 -040056 * through a targeted {@link SocketFactory} or process-wide via
Paul Jensen72db88e2015-03-10 10:54:12 -040057 * {@link ConnectivityManager#bindProcessToNetwork}.
Robert Greenwalt9ba9c582014-03-19 17:56:12 -070058 */
59public class Network implements Parcelable {
60
Robert Greenwalt85956262014-05-18 20:29:39 -070061 /**
62 * @hide
63 */
Robert Greenwalt9ba9c582014-03-19 17:56:12 -070064 public final int netId;
65
Lorenzo Colittic473dc42014-07-18 01:34:19 +090066 // Objects used to perform per-network operations such as getSocketFactory
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -070067 // and openConnection, and a lock to protect access to them.
Lorenzo Colitti5b37fa22014-07-29 22:51:27 +090068 private volatile NetworkBoundSocketFactory mNetworkBoundSocketFactory = null;
Tobias Thierer851d8062016-06-01 20:51:45 +010069 // mLock should be used to control write access to mConnectionPool and mDns.
Paul Jensen07ed0742014-09-08 14:59:09 -040070 // maybeInitHttpClient() must be called prior to reading either variable.
71 private volatile ConnectionPool mConnectionPool = null;
Tobias Thierer851d8062016-06-01 20:51:45 +010072 private volatile Dns mDns = null;
Erik Klined8959992015-06-05 17:37:45 +090073 private final Object mLock = new Object();
Paul Jensen2d6f2652014-05-20 11:25:35 -040074
Lorenzo Colitti0ed0a282014-08-28 17:15:40 -070075 // Default connection pool values. These are evaluated at startup, just
76 // like the OkHttp code. Also like the OkHttp code, we will throw parse
77 // exceptions at class loading time if the properties are set but are not
78 // valid integers.
79 private static final boolean httpKeepAlive =
80 Boolean.parseBoolean(System.getProperty("http.keepAlive", "true"));
81 private static final int httpMaxConnections =
82 httpKeepAlive ? Integer.parseInt(System.getProperty("http.maxConnections", "5")) : 0;
83 private static final long httpKeepAliveDurationMs =
84 Long.parseLong(System.getProperty("http.keepAliveDuration", "300000")); // 5 minutes.
85
Robert Greenwalt85956262014-05-18 20:29:39 -070086 /**
87 * @hide
88 */
Robert Greenwalt9ba9c582014-03-19 17:56:12 -070089 public Network(int netId) {
90 this.netId = netId;
91 }
92
Robert Greenwalt85956262014-05-18 20:29:39 -070093 /**
94 * @hide
95 */
Robert Greenwalt9ba9c582014-03-19 17:56:12 -070096 public Network(Network that) {
97 this.netId = that.netId;
98 }
99
Paul Jensen3e32a4b2014-05-13 19:05:13 -0400100 /**
101 * Operates the same as {@code InetAddress.getAllByName} except that host
102 * resolution is done on this network.
103 *
104 * @param host the hostname or literal IP string to be resolved.
105 * @return the array of addresses associated with the specified host.
106 * @throws UnknownHostException if the address lookup fails.
107 */
108 public InetAddress[] getAllByName(String host) throws UnknownHostException {
109 return InetAddress.getAllByNameOnNet(host, netId);
110 }
111
112 /**
113 * Operates the same as {@code InetAddress.getByName} except that host
114 * resolution is done on this network.
115 *
116 * @param host
117 * the hostName to be resolved to an address or {@code null}.
118 * @return the {@code InetAddress} instance representing the host.
119 * @throws UnknownHostException
120 * if the address lookup fails.
121 */
122 public InetAddress getByName(String host) throws UnknownHostException {
123 return InetAddress.getByNameOnNet(host, netId);
124 }
125
Robert Greenwalt85956262014-05-18 20:29:39 -0700126 /**
Paul Jensen2d6f2652014-05-20 11:25:35 -0400127 * A {@code SocketFactory} that produces {@code Socket}'s bound to this network.
128 */
129 private class NetworkBoundSocketFactory extends SocketFactory {
130 private final int mNetId;
131
132 public NetworkBoundSocketFactory(int netId) {
133 super();
134 mNetId = netId;
135 }
136
Lorenzo Colittif0382892014-07-30 17:17:13 +0900137 private Socket connectToHost(String host, int port, SocketAddress localAddress)
138 throws IOException {
Paul Jensen8ef34012014-05-27 10:19:39 -0400139 // Lookup addresses only on this Network.
140 InetAddress[] hostAddresses = getAllByName(host);
Lorenzo Colittif0382892014-07-30 17:17:13 +0900141 // Try all addresses.
142 for (int i = 0; i < hostAddresses.length; i++) {
Paul Jensen8ef34012014-05-27 10:19:39 -0400143 try {
Lorenzo Colittif0382892014-07-30 17:17:13 +0900144 Socket socket = createSocket();
145 if (localAddress != null) socket.bind(localAddress);
Paul Jensen8ef34012014-05-27 10:19:39 -0400146 socket.connect(new InetSocketAddress(hostAddresses[i], port));
Lorenzo Colittif0382892014-07-30 17:17:13 +0900147 return socket;
Paul Jensen8ef34012014-05-27 10:19:39 -0400148 } catch (IOException e) {
Lorenzo Colittif0382892014-07-30 17:17:13 +0900149 if (i == (hostAddresses.length - 1)) throw e;
Paul Jensen8ef34012014-05-27 10:19:39 -0400150 }
151 }
Lorenzo Colittif0382892014-07-30 17:17:13 +0900152 throw new UnknownHostException(host);
Paul Jensen8ef34012014-05-27 10:19:39 -0400153 }
154
Paul Jensen2d6f2652014-05-20 11:25:35 -0400155 @Override
156 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
Lorenzo Colittif0382892014-07-30 17:17:13 +0900157 return connectToHost(host, port, new InetSocketAddress(localHost, localPort));
Paul Jensen2d6f2652014-05-20 11:25:35 -0400158 }
159
160 @Override
161 public Socket createSocket(InetAddress address, int port, InetAddress localAddress,
162 int localPort) throws IOException {
163 Socket socket = createSocket();
164 socket.bind(new InetSocketAddress(localAddress, localPort));
165 socket.connect(new InetSocketAddress(address, port));
166 return socket;
167 }
168
169 @Override
170 public Socket createSocket(InetAddress host, int port) throws IOException {
171 Socket socket = createSocket();
172 socket.connect(new InetSocketAddress(host, port));
173 return socket;
174 }
175
176 @Override
177 public Socket createSocket(String host, int port) throws IOException {
Lorenzo Colittif0382892014-07-30 17:17:13 +0900178 return connectToHost(host, port, null);
Paul Jensen2d6f2652014-05-20 11:25:35 -0400179 }
180
181 @Override
182 public Socket createSocket() throws IOException {
183 Socket socket = new Socket();
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700184 bindSocket(socket);
Paul Jensen2d6f2652014-05-20 11:25:35 -0400185 return socket;
186 }
187 }
188
189 /**
Robert Greenwalt85956262014-05-18 20:29:39 -0700190 * Returns a {@link SocketFactory} bound to this network. Any {@link Socket} created by
191 * this factory will have its traffic sent over this {@code Network}. Note that if this
192 * {@code Network} ever disconnects, this factory and any {@link Socket} it produced in the
193 * past or future will cease to work.
194 *
195 * @return a {@link SocketFactory} which produces {@link Socket} instances bound to this
196 * {@code Network}.
197 */
Paul Jensen0a363a32014-05-29 10:12:39 -0400198 public SocketFactory getSocketFactory() {
Lorenzo Colitti5b37fa22014-07-29 22:51:27 +0900199 if (mNetworkBoundSocketFactory == null) {
200 synchronized (mLock) {
201 if (mNetworkBoundSocketFactory == null) {
202 mNetworkBoundSocketFactory = new NetworkBoundSocketFactory(netId);
203 }
Lorenzo Colittic473dc42014-07-18 01:34:19 +0900204 }
Paul Jensen2d6f2652014-05-20 11:25:35 -0400205 }
206 return mNetworkBoundSocketFactory;
Robert Greenwalt85956262014-05-18 20:29:39 -0700207 }
208
Paul Jensen07ed0742014-09-08 14:59:09 -0400209 // TODO: This creates a connection pool and host resolver for
Lorenzo Colitti0ed0a282014-08-28 17:15:40 -0700210 // every Network object, instead of one for every NetId. This is
211 // suboptimal, because an app could potentially have more than one
212 // Network object for the same NetId, causing increased memory footprint
213 // and performance penalties due to lack of connection reuse (connection
214 // setup time, congestion window growth time, etc.).
215 //
Paul Jensen07ed0742014-09-08 14:59:09 -0400216 // Instead, investigate only having one connection pool and host resolver
217 // for every NetId, perhaps by using a static HashMap of NetIds to
218 // connection pools and host resolvers. The tricky part is deciding when
219 // to remove a map entry; a WeakHashMap shouldn't be used because whether
220 // a Network is referenced doesn't correlate with whether a new Network
221 // will be instantiated in the near future with the same NetID. A good
222 // solution would involve purging empty (or when all connections are timed
223 // out) ConnectionPools.
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700224 private void maybeInitHttpClient() {
Paul Jensen07ed0742014-09-08 14:59:09 -0400225 synchronized (mLock) {
Tobias Thierer851d8062016-06-01 20:51:45 +0100226 if (mDns == null) {
227 mDns = new Dns() {
Paul Jensen07ed0742014-09-08 14:59:09 -0400228 @Override
Tobias Thierer851d8062016-06-01 20:51:45 +0100229 public List<InetAddress> lookup(String hostname) throws UnknownHostException {
230 return Arrays.asList(Network.this.getAllByName(hostname));
Paul Jensen07ed0742014-09-08 14:59:09 -0400231 }
232 };
233 }
234 if (mConnectionPool == null) {
235 mConnectionPool = new ConnectionPool(httpMaxConnections,
Tobias Thierer851d8062016-06-01 20:51:45 +0100236 httpKeepAliveDurationMs, TimeUnit.MILLISECONDS);
Lorenzo Colitti5b37fa22014-07-29 22:51:27 +0900237 }
Lorenzo Colitti5b37fa22014-07-29 22:51:27 +0900238 }
Lorenzo Colitti5b37fa22014-07-29 22:51:27 +0900239 }
240
241 /**
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700242 * Opens the specified {@link URL} on this {@code Network}, such that all traffic will be sent
243 * on this Network. The URL protocol must be {@code HTTP} or {@code HTTPS}.
244 *
245 * @return a {@code URLConnection} to the resource referred to by this URL.
246 * @throws MalformedURLException if the URL protocol is not HTTP or HTTPS.
247 * @throws IOException if an error occurs while opening the connection.
248 * @see java.net.URL#openConnection()
Lorenzo Colitti5b37fa22014-07-29 22:51:27 +0900249 */
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700250 public URLConnection openConnection(URL url) throws IOException {
Paul Jensen72db88e2015-03-10 10:54:12 -0400251 final ConnectivityManager cm = ConnectivityManager.getInstanceOrNull();
252 if (cm == null) {
253 throw new IOException("No ConnectivityManager yet constructed, please construct one");
254 }
Paul Jensene0bef712014-12-10 15:12:18 -0500255 // TODO: Should this be optimized to avoid fetching the global proxy for every request?
Paul Jensencee9b512015-05-06 07:32:40 -0400256 final ProxyInfo proxyInfo = cm.getProxyForNetwork(this);
Paul Jensene0bef712014-12-10 15:12:18 -0500257 java.net.Proxy proxy = null;
258 if (proxyInfo != null) {
259 proxy = proxyInfo.makeProxy();
260 } else {
261 proxy = java.net.Proxy.NO_PROXY;
262 }
263 return openConnection(url, proxy);
264 }
265
266 /**
267 * Opens the specified {@link URL} on this {@code Network}, such that all traffic will be sent
268 * on this Network. The URL protocol must be {@code HTTP} or {@code HTTPS}.
269 *
270 * @param proxy the proxy through which the connection will be established.
271 * @return a {@code URLConnection} to the resource referred to by this URL.
272 * @throws MalformedURLException if the URL protocol is not HTTP or HTTPS.
273 * @throws IllegalArgumentException if the argument proxy is null.
274 * @throws IOException if an error occurs while opening the connection.
275 * @see java.net.URL#openConnection()
Paul Jensene0bef712014-12-10 15:12:18 -0500276 */
277 public URLConnection openConnection(URL url, java.net.Proxy proxy) throws IOException {
278 if (proxy == null) throw new IllegalArgumentException("proxy is null");
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700279 maybeInitHttpClient();
280 String protocol = url.getProtocol();
Neil Fuller50a01d82015-01-12 16:49:06 +0000281 OkUrlFactory okUrlFactory;
282 // TODO: HttpHandler creates OkUrlFactory instances that share the default ResponseCache.
Paul Jensen07ed0742014-09-08 14:59:09 -0400283 // Could this cause unexpected behavior?
Paul Jensen07ed0742014-09-08 14:59:09 -0400284 if (protocol.equals("http")) {
Neil Fuller7014da72015-01-20 13:21:58 +0000285 okUrlFactory = HttpHandler.createHttpOkUrlFactory(proxy);
Paul Jensen07ed0742014-09-08 14:59:09 -0400286 } else if (protocol.equals("https")) {
Neil Fuller7014da72015-01-20 13:21:58 +0000287 okUrlFactory = HttpsHandler.createHttpsOkUrlFactory(proxy);
Paul Jensen07ed0742014-09-08 14:59:09 -0400288 } else {
Neil Fuller50a01d82015-01-12 16:49:06 +0000289 // OkHttp only supports HTTP and HTTPS and returns a null URLStreamHandler if
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700290 // passed another protocol.
291 throw new MalformedURLException("Invalid URL or unrecognized protocol " + protocol);
Lorenzo Colittic473dc42014-07-18 01:34:19 +0900292 }
Neil Fuller50a01d82015-01-12 16:49:06 +0000293 OkHttpClient client = okUrlFactory.client();
294 client.setSocketFactory(getSocketFactory()).setConnectionPool(mConnectionPool);
Tobias Thierer851d8062016-06-01 20:51:45 +0100295 // Let network traffic go via mDns
296 client.setDns(mDns);
Neil Fuller50a01d82015-01-12 16:49:06 +0000297
298 return okUrlFactory.open(url);
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700299 }
300
301 /**
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -0700302 * Binds the specified {@link DatagramSocket} to this {@code Network}. All data traffic on the
303 * socket will be sent on this {@code Network}, irrespective of any process-wide network binding
Paul Jensen72db88e2015-03-10 10:54:12 -0400304 * set by {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -0700305 * connected.
306 */
307 public void bindSocket(DatagramSocket socket) throws IOException {
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -0700308 // Query a property of the underlying socket to ensure that the socket's file descriptor
309 // exists, is available to bind to a network and is not closed.
310 socket.getReuseAddress();
Erik Klined8959992015-06-05 17:37:45 +0900311 bindSocket(socket.getFileDescriptor$());
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -0700312 }
313
314 /**
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700315 * Binds the specified {@link Socket} to this {@code Network}. All data traffic on the socket
316 * will be sent on this {@code Network}, irrespective of any process-wide network binding set by
Paul Jensen72db88e2015-03-10 10:54:12 -0400317 * {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected.
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700318 */
319 public void bindSocket(Socket socket) throws IOException {
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -0700320 // Query a property of the underlying socket to ensure that the socket's file descriptor
321 // exists, is available to bind to a network and is not closed.
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700322 socket.getReuseAddress();
Erik Klined8959992015-06-05 17:37:45 +0900323 bindSocket(socket.getFileDescriptor$());
Sreeram Ramachandrane15db7c2014-10-22 11:10:20 -0700324 }
325
Erik Klined8959992015-06-05 17:37:45 +0900326 /**
327 * Binds the specified {@link FileDescriptor} to this {@code Network}. All data traffic on the
328 * socket represented by this file descriptor will be sent on this {@code Network},
329 * irrespective of any process-wide network binding set by
330 * {@link ConnectivityManager#bindProcessToNetwork}. The socket must not be connected.
331 */
332 public void bindSocket(FileDescriptor fd) throws IOException {
333 try {
334 final SocketAddress peer = Os.getpeername(fd);
335 final InetAddress inetPeer = ((InetSocketAddress) peer).getAddress();
336 if (!inetPeer.isAnyLocalAddress()) {
337 // Apparently, the kernel doesn't update a connected UDP socket's
338 // routing upon mark changes.
339 throw new SocketException("Socket is connected");
340 }
341 } catch (ErrnoException e) {
342 // getpeername() failed.
343 if (e.errno != OsConstants.ENOTCONN) {
344 throw e.rethrowAsSocketException();
345 }
346 } catch (ClassCastException e) {
347 // Wasn't an InetSocketAddress.
348 throw new SocketException("Only AF_INET/AF_INET6 sockets supported");
349 }
350
351 final int err = NetworkUtils.bindSocketToNetwork(fd.getInt$(), netId);
Lorenzo Colitti9f1274b2014-08-21 11:45:54 -0700352 if (err != 0) {
353 // bindSocketToNetwork returns negative errno.
354 throw new ErrnoException("Binding socket to network " + netId, -err)
355 .rethrowAsSocketException();
356 }
Lorenzo Colittic473dc42014-07-18 01:34:19 +0900357 }
358
Erik Kline25f3b7b2015-03-05 15:13:37 +0900359 /**
360 * Returns a handle representing this {@code Network}, for use with the NDK API.
361 */
362 public long getNetworkHandle() {
363 // The network handle is explicitly not the same as the netId.
364 //
365 // The netId is an implementation detail which might be changed in the
366 // future, or which alone (i.e. in the absence of some additional
367 // context) might not be sufficient to fully identify a Network.
368 //
369 // As such, the intention is to prevent accidental misuse of the API
370 // that might result if a developer assumed that handles and netIds
371 // were identical and passing a netId to a call expecting a handle
372 // "just worked". Such accidental misuse, if widely deployed, might
373 // prevent future changes to the semantics of the netId field or
374 // inhibit the expansion of state required for Network objects.
375 //
376 // This extra layer of indirection might be seen as paranoia, and might
377 // never end up being necessary, but the added complexity is trivial.
378 // At some future date it may be desirable to realign the handle with
379 // Multiple Provisioning Domains API recommendations, as made by the
380 // IETF mif working group.
381 //
382 // The HANDLE_MAGIC value MUST be kept in sync with the corresponding
383 // value in the native/android/net.c NDK implementation.
Erik Klinee1a6cf22015-05-14 15:40:07 +0900384 if (netId == 0) {
385 return 0L; // make this zero condition obvious for debugging
386 }
Erik Kline25f3b7b2015-03-05 15:13:37 +0900387 final long HANDLE_MAGIC = 0xfacade;
388 return (((long) netId) << 32) | HANDLE_MAGIC;
389 }
390
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700391 // implement the Parcelable interface
392 public int describeContents() {
393 return 0;
394 }
395 public void writeToParcel(Parcel dest, int flags) {
396 dest.writeInt(netId);
397 }
398
399 public static final Creator<Network> CREATOR =
400 new Creator<Network>() {
401 public Network createFromParcel(Parcel in) {
402 int netId = in.readInt();
403
404 return new Network(netId);
405 }
406
407 public Network[] newArray(int size) {
408 return new Network[size];
409 }
410 };
Robert Greenwalt85956262014-05-18 20:29:39 -0700411
Robert Greenwalt2c7bf2c2014-06-23 16:07:43 -0700412 @Override
Robert Greenwalt85956262014-05-18 20:29:39 -0700413 public boolean equals(Object obj) {
414 if (obj instanceof Network == false) return false;
415 Network other = (Network)obj;
416 return this.netId == other.netId;
417 }
418
Robert Greenwalt2c7bf2c2014-06-23 16:07:43 -0700419 @Override
Robert Greenwalt85956262014-05-18 20:29:39 -0700420 public int hashCode() {
421 return netId * 11;
422 }
Robert Greenwalt2c7bf2c2014-06-23 16:07:43 -0700423
424 @Override
425 public String toString() {
426 return Integer.toString(netId);
427 }
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700428}