2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lifx.internal.util;
15 import static org.openhab.binding.lifx.internal.LifxBindingConstants.PACKET_INTERVAL;
17 import java.util.List;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.CopyOnWriteArrayList;
21 import java.util.concurrent.locks.ReentrantLock;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.lifx.internal.fields.MACAddress;
28 * The {@link LifxThrottlingUtil} is a utility class that regulates the frequency at which messages/packets are
29 * sent to LIFX lights. The LIFX LAN Protocol Specification states that lights can process up to 20 messages per second,
32 * @author Karel Goderis - Initial contribution
33 * @author Wouter Born - Deadlock fix
36 public final class LifxThrottlingUtil {
38 private LifxThrottlingUtil() {
39 // hidden utility class constructor
43 * Tracks when the last packet was sent to a LIFX light. The packet is sent after obtaining the lock and before
46 private static class LifxLightCommunicationTracker {
48 private long timestamp;
50 private ReentrantLock lock = new ReentrantLock();
56 public void unlock() {
57 // When iterating over all trackers another thread may have inserted this object so this thread may not
58 // have a lock on it. When the thread does not have the lock, it also did not send a packet.
59 if (lock.isHeldByCurrentThread()) {
60 timestamp = System.currentTimeMillis();
65 public long getTimestamp() {
71 * A separate list of trackers is maintained when locking all lights in case of a broadcast. Iterators of
72 * {@link ConcurrentHashMap}s may behave non-linear when inserts take place to obtain more concurrency. When the
73 * iterator of {@code values()} of {@link #macTrackerMapping} is used for locking all lights, it could sometimes
76 private static List<LifxLightCommunicationTracker> trackers = new CopyOnWriteArrayList<>();
78 private static Map<MACAddress, LifxLightCommunicationTracker> macTrackerMapping = new ConcurrentHashMap<>();
80 public static void lock(@Nullable MACAddress mac) throws InterruptedException {
82 LifxLightCommunicationTracker tracker = getOrCreateTracker(mac);
84 waitForNextPacketInterval(tracker.getTimestamp());
90 private static LifxLightCommunicationTracker getOrCreateTracker(MACAddress mac) {
91 LifxLightCommunicationTracker tracker = macTrackerMapping.get(mac);
92 if (tracker == null) {
93 // for better performance only synchronize when necessary
94 synchronized (trackers) {
95 // another thread may just have added a tracker in this synchronized block, so reevaluate
96 tracker = macTrackerMapping.get(mac);
97 if (tracker == null) {
98 tracker = new LifxLightCommunicationTracker();
99 trackers.add(tracker);
100 macTrackerMapping.put(mac, tracker);
107 private static void waitForNextPacketInterval(long timestamp) throws InterruptedException {
108 long timeToWait = Math.max(PACKET_INTERVAL - (System.currentTimeMillis() - timestamp), 0);
109 if (timeToWait > 0) {
110 Thread.sleep(timeToWait);
114 public static void unlock(@Nullable MACAddress mac) {
116 LifxLightCommunicationTracker tracker = macTrackerMapping.get(mac);
117 if (tracker != null) {
125 public static void lock() throws InterruptedException {
127 for (LifxLightCommunicationTracker tracker : trackers) {
129 lastStamp = Math.max(lastStamp, tracker.getTimestamp());
131 waitForNextPacketInterval(lastStamp);
134 public static void unlock() {
135 for (LifxLightCommunicationTracker tracker : trackers) {