alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.pim.impl; |
| 17 | |
| 18 | import org.jboss.netty.util.HashedWheelTimer; |
| 19 | |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 22 | /** |
| 23 | * PIM Timer used for PIM Neighbors. |
| 24 | */ |
| 25 | public final class PIMTimer { |
| 26 | |
| 27 | private static volatile HashedWheelTimer timer; |
| 28 | |
| 29 | // Ban public construction |
| 30 | private PIMTimer() { |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns the singleton hashed-wheel timer. |
| 35 | * |
| 36 | * @return hashed-wheel timer |
| 37 | */ |
| 38 | public static HashedWheelTimer getTimer() { |
| 39 | if (PIMTimer.timer == null) { |
| 40 | initTimer(); |
| 41 | } |
| 42 | return PIMTimer.timer; |
| 43 | } |
| 44 | |
| 45 | // Start the PIM timer. |
| 46 | private static synchronized void initTimer() { |
| 47 | if (PIMTimer.timer == null) { |
| 48 | |
| 49 | // Create and start a new hashed wheel timer, if it does not exist. |
| 50 | HashedWheelTimer hwTimer = new HashedWheelTimer(); |
| 51 | hwTimer.start(); |
| 52 | PIMTimer.timer = hwTimer; |
| 53 | } |
| 54 | } |
Rusty Eddy | 9542164 | 2015-10-21 17:22:13 -0700 | [diff] [blame] | 55 | |
| 56 | public static void start() { |
| 57 | if (PIMTimer.timer == null) { |
| 58 | getTimer(); |
| 59 | } |
| 60 | checkNotNull(timer); |
| 61 | timer.start(); |
| 62 | } |
| 63 | |
| 64 | public static void stop() { |
| 65 | if (PIMTimer.timer == null) { |
| 66 | // No need to stop |
| 67 | return; |
| 68 | } |
| 69 | checkNotNull(timer); |
| 70 | timer.stop(); |
| 71 | } |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 72 | } |