Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.common

File Nodes.java

 

Coverage histogram

../../../img/srcFileCovDistChart8.png
19% of files have more coverage

Code metrics

12
49
10
1
126
107
20
0.41
4.9
10
2

Classes

Class Line # Actions
Nodes 11 49 20
0.788732478.9%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    package io.sunshower.common;
2   
3    import java.net.InetAddress;
4    import java.net.NetworkInterface;
5    import java.net.SocketException;
6    import java.net.UnknownHostException;
7    import java.nio.ByteBuffer;
8    import java.util.*;
9    import java.util.concurrent.atomic.AtomicReference;
10   
 
11    public class Nodes {
12   
13    private static final Object lock = new Object();
14   
15    private static final long clock;
16   
17    private static volatile InetAddress localAddress;
18   
19    private static final Map<InetAddress, Long> addressCache;
20   
21    private static final AtomicReference<UUID> fallbackNodeId;
22   
 
23  1 toggle static {
24  1 addressCache = new HashMap<>();
25  1 fallbackNodeId = new AtomicReference<>(UUID.randomUUID());
26  1 clock = new Random(System.currentTimeMillis()).nextLong();
27    }
28   
 
29  2 toggle public static InetAddress localAddress() {
30  2 if (localAddress == null) {
31  1 synchronized (lock) {
32  1 if (localAddress == null) {
33  1 try {
34  1 localAddress = InetAddress.getLocalHost();
35    } catch (UnknownHostException ex) {
36  0 throw new IllegalStateException(ex);
37    }
38    }
39    }
40    }
41  2 return localAddress;
42    }
43   
 
44  40 toggle public static Set<NetworkInterface> getLocalInterfaces() {
45  40 try {
46  40 Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
47  40 final Set<NetworkInterface> results = new HashSet<>();
48   
49  120 while (networkInterfaces.hasMoreElements()) {
50  80 results.add(networkInterfaces.nextElement());
51    }
52  40 return results;
53    } catch (SocketException e) {
54  0 throw new NoSuchElementException("Failed to find local network interfaces");
55    }
56    }
57   
 
58  40 toggle public static NetworkInterface getIdentifiableInterface() {
59  40 return getLocalInterfaces()
60    .stream()
61    .filter(
62    t -> {
63  80 try {
64  80 return t.isUp() && !t.isLoopback();
65    } catch (SocketException e) {
66  0 return false;
67    }
68    })
69    .findAny()
70    .get();
71    }
72   
 
73  19 toggle public static byte[] getIdentifiableNodeHardwareAddress() {
74  19 return getIdentifiableNodeHardwareAddress(false);
75    }
76   
 
77  20 toggle public static byte[] getIdentifiableNodeHardwareAddress(boolean noFail) {
78  20 try {
79  20 return getIdentifiableInterface().getHardwareAddress();
80    } catch (SocketException e) {
81  0 if (noFail) {
82  0 final ByteBuffer buffer = ByteBuffer.allocate(16);
83  0 UUID uuid = fallbackNodeId.get();
84  0 long lsb = uuid.getLeastSignificantBits();
85  0 long msb = uuid.getMostSignificantBits();
86  0 buffer.putLong(msb);
87  0 buffer.putLong(lsb);
88  0 return buffer.array();
89    }
90  0 throw new IllegalStateException(e);
91    }
92    }
93   
 
94  19 toggle public static InetAddress getIdentifiableNetworkAddress() {
95  19 return getIdentifiableInterface().getInetAddresses().nextElement();
96    }
97   
 
98  1 toggle public static long localIdentity() {
99  1 return localIdentity(getIdentifiableInterface().getInetAddresses().nextElement());
100    }
101   
 
102  2 toggle public static long localIdentity(InetAddress address) {
103  2 long lsb = 0;
104  2 lsb |= (clock & 0x3f00000000000000L) >>> 56;
105  2 lsb |= 0x0000000000000080;
106  2 lsb |= (clock & 0x00ff000000000000L) >>> 48;
107  2 lsb |= localIdentityForAddress(address);
108  2 return lsb;
109    }
110   
 
111  2 toggle private static long localIdentityForAddress(InetAddress address) {
112  2 if (addressCache.containsKey(address)) {
113  1 return addressCache.get(address);
114    }
115   
116  1 byte[] hash =
117    Hashes.hashCode(Hashes.Algorithm.MD5)
118    .digest(ByteBuffer.wrap(address.toString().getBytes()));
119  1 long node = 0;
120  7 for (int i = 0; i < Math.min(6, hash.length); i++) {
121  6 node |= (0x00000000000000ff & (long) hash[i]) << (5 - i) * 8;
122    }
123  1 addressCache.put(address, node);
124  1 return node;
125    }
126    }