blob: c4a17c98f18d73fe4f37bc43a1d95c127fc5468f [file] [log] [blame]
David K. Bainbridged77028f2017-08-01 12:47:55 -07001/*
Brian O'Connor4d084702017-08-03 22:45:58 -07002 * Copyright 2017-present Open Networking Foundation
David K. Bainbridged77028f2017-08-01 12:47:55 -07003 *
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 */
developere400c582020-03-24 19:42:08 +010016package org.opencord.igmpproxy.impl;
ke han81a38b92017-03-10 18:41:44 +080017
18import com.google.common.collect.Maps;
19
20import java.util.Iterator;
21import java.util.Map;
22import java.util.Set;
23
24/**
25 * Implement the timer for igmp state machine.
26 */
27public 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}