1 /*
2 * Copyright (C) 2013 4th Line GmbH, Switzerland
3 *
4 * The contents of this file are subject to the terms of either the GNU
5 * Lesser General Public License Version 2 or later ("LGPL") or the
6 * Common Development and Distribution License Version 1 or later
7 * ("CDDL") (collectively, the "License"). You may not use this file
8 * except in compliance with the License. See LICENSE.txt for more
9 * information.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 */
15
16 package org.fourthline.cling.model;
17
18 import java.net.InetAddress;
19 import java.net.URI;
20 import java.net.URL;
21
22 /**
23 * The IP address/port, MAC address, and URI path of a (network) location.
24 * <p>
25 * Used when sending messages about local devices and services to
26 * other UPnP participants on the network, such as where our device/service
27 * descriptors can be found or what callback address to use for event message
28 * delivery. We also let them know our MAC hardware address so they
29 * can wake us up from sleep with Wake-On-LAN if necessary.
30 * </p>
31 *
32 * @author Christian Bauer
33 */
34 public class Location {
35
36 protected final NetworkAddress networkAddress;
37 protected final String path;
38 protected final URL url;
39
40 public Location(NetworkAddress networkAddress, String path) {
41 this.networkAddress = networkAddress;
42 this.path = path;
43 this.url = createAbsoluteURL(networkAddress.getAddress(), networkAddress.getPort(), path);
44 }
45
46 public NetworkAddress getNetworkAddress() {
47 return networkAddress;
48 }
49
50 public String getPath() {
51 return path;
52 }
53
54 @Override
55 public boolean equals(Object o) {
56 if (this == o) return true;
57 if (o == null || getClass() != o.getClass()) return false;
58
59 Location location = (Location) o;
60
61 if (!networkAddress.equals(location.networkAddress)) return false;
62 if (!path.equals(location.path)) return false;
63
64 return true;
65 }
66
67 @Override
68 public int hashCode() {
69 int result = networkAddress.hashCode();
70 result = 31 * result + path.hashCode();
71 return result;
72 }
73
74 /**
75 * @return An HTTP URL with the address, port, and path of this location.
76 */
77 public URL getURL() {
78 return url;
79 }
80
81 // Performance optimization on Android
82 private static URL createAbsoluteURL(InetAddress address, int localStreamPort, String path) {
83 try {
84 return new URL("http", address.getHostAddress(), localStreamPort, path);
85 } catch (Exception ex) {
86 throw new IllegalArgumentException("Address, port, and URI can not be converted to URL", ex);
87 }
88 }
89 }