David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 4d08470 | 2017-08-03 22:45:58 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
David K. Bainbridge | d77028f | 2017-08-01 12:47:55 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame^] | 16 | package org.opencord.igmpproxy.impl; |
ke han | 81a38b9 | 2017-03-10 18:41:44 +0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | |
| 20 | import java.util.Iterator; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | /** |
| 25 | * Implement the timer for igmp state machine. |
| 26 | */ |
| 27 | public final class IgmpTimer { |
| 28 | |
| 29 | public static final int INVALID_TIMER_ID = 0; |
| 30 | public static int timerId = INVALID_TIMER_ID + 1; |
| 31 | private static Map<Integer, SingleTimer> igmpTimerMap = Maps.newConcurrentMap(); |
| 32 | |
| 33 | private IgmpTimer(){ |
| 34 | |
| 35 | } |
| 36 | private static int getId() { |
| 37 | return timerId++; |
| 38 | } |
| 39 | |
| 40 | public static int start(SingleStateMachine machine, int timeOut) { |
| 41 | int id = getId(); |
| 42 | igmpTimerMap.put(id, new SingleTimer(machine, timeOut)); |
| 43 | return id; |
| 44 | } |
| 45 | |
| 46 | public static int reset(int oldId, SingleStateMachine machine, int timeOut) { |
| 47 | igmpTimerMap.remove(new Integer(oldId)); |
| 48 | int id = getId(); |
| 49 | igmpTimerMap.put(new Integer(id), new SingleTimer(machine, timeOut)); |
| 50 | return id; |
| 51 | } |
| 52 | |
| 53 | public static void cancel(int id) { |
| 54 | igmpTimerMap.remove(new Integer(id)); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | static void timeOut1s() { |
| 59 | Set mapSet = igmpTimerMap.entrySet(); |
| 60 | Iterator itr = mapSet.iterator(); |
| 61 | while (itr.hasNext()) { |
| 62 | Map.Entry entry = (Map.Entry) itr.next(); |
| 63 | SingleTimer single = (SingleTimer) entry.getValue(); |
| 64 | if (single.timeOut > 0) { |
| 65 | single.timeOut--; |
| 66 | } else { |
| 67 | single.machine.timeOut(); |
| 68 | itr.remove(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static class SingleTimer { |
| 74 | |
| 75 | public int timeOut; // unit is 1 second |
| 76 | public SingleStateMachine machine; |
| 77 | |
| 78 | public SingleTimer(SingleStateMachine machine, int timeOut) { |
| 79 | this.machine = machine; |
| 80 | this.timeOut = timeOut; |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | } |