package common;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* Build key value used to identify this view-controller/client to the
* model/server as hex representation of last 3 bytes (least significant
* bytes) of IP address giving a six character key. This is a static class
* and cannot be instantiated. Instead use IpDtaQKey.getDqKey().
* @author Bill Blalock
*
*/
public class IpDtaQKey {
private static IpDtaQKey INSTANCE;
private static String dqKey;
/**
* Private constructor. Constructor only invoked by DtaQKey.getDqKey().
* <p>
* Build key value used to identify this view-controller/client to the
* model/server as hex representation of last 3 bytes (least significant
* bytes) of IP address giving a six character key.
* @return Six character key used to identify this view-controller/client
* to the model/server.
*/
private IpDtaQKey() {
try {
InetAddress addr = InetAddress.getLocalHost();
// Get IP Address
byte[] ipAddr
= addr.getAddress();
// Convert to hex
StringBuffer
sb = new StringBuffer();
for (int i = 0; i < ipAddr.length;
i++) {
if( ipAddr[i]>= 0 & ipAddr[i] < 16 )
sb.append("0");
sb.append(Integer.toHexString(((int) ipAddr[i]) & 0xFF));
}
String hexIpAddr
= sb.toString();
// return least 3 significant
bytes, 6 character hex
dqKey = hexIpAddr.toUpperCase().substring(2);
} catch (UnknownHostException e) {
dqKey = "FFFFFF";
}
}
/**
* Get key used to identify this view-controller/client to the model/server.
* @return Key used to identify this view-controller/client to the model/server.
* "FFFFFF" indicate that class was unable to build a key from the IP address.
*/
public static String getDqKey() {
if( INSTANCE == null )
INSTANCE = new IpDtaQKey();
return dqKey;
}
}