View Javadoc
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.meta;
17  
18  import org.fourthline.cling.model.ModelUtil;
19  import org.fourthline.cling.model.message.discovery.IncomingNotificationRequest;
20  import org.fourthline.cling.model.message.discovery.IncomingSearchResponse;
21  import org.fourthline.cling.model.types.UDN;
22  
23  import java.net.InetAddress;
24  import java.net.URL;
25  
26  /**
27   * Additional identifying information only relevant for discovered remote devices.
28   * <p>
29   * This information always includes the URL of the device's descriptor, and the
30   * local network interface address we should use in the future, because it is
31   * guaranteed to be reachable by this remote device (e.g. when we build a local
32   * callback URL).
33   * </p>
34   * <p>
35   * Optional is the remote hosts interface MAC hardware address. If we have it, we
36   * can use it to send Wake-On-LAN broadcasts if we think the remote host is not
37   * reachable and might be sleeping. (Useful for "stateless" reconnecting control
38   * points.)
39   * </p>
40   *
41   * @author Christian Bauer
42   */
43  public class RemoteDeviceIdentity extends DeviceIdentity {
44  
45      final private URL descriptorURL;
46      final private byte[] interfaceMacAddress;
47      final private InetAddress discoveredOnLocalAddress;
48  
49      public RemoteDeviceIdentity(UDN udn, RemoteDeviceIdentity template) {
50          this(udn, template.getMaxAgeSeconds(), template.getDescriptorURL(), template.getInterfaceMacAddress(), template.getDiscoveredOnLocalAddress());
51      }
52  
53      public RemoteDeviceIdentity(UDN udn, Integer maxAgeSeconds, URL descriptorURL, byte[] interfaceMacAddress, InetAddress discoveredOnLocalAddress) {
54          super(udn, maxAgeSeconds);
55          this.descriptorURL = descriptorURL;
56          this.interfaceMacAddress = interfaceMacAddress;
57          this.discoveredOnLocalAddress = discoveredOnLocalAddress;
58      }
59  
60      public RemoteDeviceIdentity(IncomingNotificationRequest notificationRequest) {
61          this(notificationRequest.getUDN(),
62               notificationRequest.getMaxAge(),
63               notificationRequest.getLocationURL(),
64               notificationRequest.getInterfaceMacHeader(),
65               notificationRequest.getLocalAddress()
66          );
67      }
68  
69      public RemoteDeviceIdentity(IncomingSearchResponse searchResponse) {
70          this(searchResponse.getRootDeviceUDN(),
71               searchResponse.getMaxAge(),
72               searchResponse.getLocationURL(),
73               searchResponse.getInterfaceMacHeader(),
74               searchResponse.getLocalAddress()
75          );
76      }
77  
78      public URL getDescriptorURL() {
79          return descriptorURL;
80      }
81  
82      public byte[] getInterfaceMacAddress() {
83          return interfaceMacAddress;
84      }
85  
86      public InetAddress getDiscoveredOnLocalAddress() {
87          return discoveredOnLocalAddress;
88      }
89  
90      public byte[] getWakeOnLANBytes() {
91          if (getInterfaceMacAddress() == null) return null;
92          byte[] bytes = new byte[6 + 16 * getInterfaceMacAddress().length];
93          for (int i = 0; i < 6; i++) {
94              bytes[i] = (byte) 0xff;
95          }
96          for (int i = 6; i < bytes.length; i += getInterfaceMacAddress().length) {
97              System.arraycopy(getInterfaceMacAddress(), 0, bytes, i, getInterfaceMacAddress().length);
98          }
99          return bytes;
100     }
101 
102     @Override
103     public String toString() {
104         // Performance optimization, so we don't have to wrap all log("foo " + device) calls with isLoggable
105 		if(ModelUtil.ANDROID_RUNTIME) {
106             return "(RemoteDeviceIdentity) UDN: " + getUdn() + ", Descriptor: " + getDescriptorURL();
107         }
108         return "(" + getClass().getSimpleName() + ") UDN: " + getUdn() + ", Descriptor: " + getDescriptorURL();
109     }
110 }