ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame^] | 1 | package org.opencord.igmpproxy; |
| 2 | |
| 3 | import com.google.common.collect.Maps; |
| 4 | |
| 5 | import java.util.Iterator; |
| 6 | import java.util.Map; |
| 7 | import java.util.Set; |
| 8 | |
| 9 | /** |
| 10 | * Implement the timer for igmp state machine. |
| 11 | */ |
| 12 | public final class IgmpTimer { |
| 13 | |
| 14 | public static final int INVALID_TIMER_ID = 0; |
| 15 | public static int timerId = INVALID_TIMER_ID + 1; |
| 16 | private static Map<Integer, SingleTimer> igmpTimerMap = Maps.newConcurrentMap(); |
| 17 | |
| 18 | private IgmpTimer(){ |
| 19 | |
| 20 | } |
| 21 | private static int getId() { |
| 22 | return timerId++; |
| 23 | } |
| 24 | |
| 25 | public static int start(SingleStateMachine machine, int timeOut) { |
| 26 | int id = getId(); |
| 27 | igmpTimerMap.put(id, new SingleTimer(machine, timeOut)); |
| 28 | return id; |
| 29 | } |
| 30 | |
| 31 | public static int reset(int oldId, SingleStateMachine machine, int timeOut) { |
| 32 | igmpTimerMap.remove(new Integer(oldId)); |
| 33 | int id = getId(); |
| 34 | igmpTimerMap.put(new Integer(id), new SingleTimer(machine, timeOut)); |
| 35 | return id; |
| 36 | } |
| 37 | |
| 38 | public static void cancel(int id) { |
| 39 | igmpTimerMap.remove(new Integer(id)); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | static void timeOut1s() { |
| 44 | Set mapSet = igmpTimerMap.entrySet(); |
| 45 | Iterator itr = mapSet.iterator(); |
| 46 | while (itr.hasNext()) { |
| 47 | Map.Entry entry = (Map.Entry) itr.next(); |
| 48 | SingleTimer single = (SingleTimer) entry.getValue(); |
| 49 | if (single.timeOut > 0) { |
| 50 | single.timeOut--; |
| 51 | } else { |
| 52 | single.machine.timeOut(); |
| 53 | itr.remove(); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static class SingleTimer { |
| 59 | |
| 60 | public int timeOut; // unit is 1 second |
| 61 | public SingleStateMachine machine; |
| 62 | |
| 63 | public SingleTimer(SingleStateMachine machine, int timeOut) { |
| 64 | this.machine = machine; |
| 65 | this.timeOut = timeOut; |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | } |