Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [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 | |
| 17 | package org.onosproject.bgp.controller.impl; |
| 18 | |
| 19 | import java.util.Timer; |
| 20 | import java.util.TimerTask; |
| 21 | |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | /** |
| 26 | * Implement sending keepalive message to connected peer periodically based on negotiated holdtime. |
| 27 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 28 | public class BgpKeepAliveTimer { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 29 | |
| 30 | private Timer keepAliveTimer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 31 | private BgpChannelHandler handler; |
| 32 | private static final Logger log = LoggerFactory.getLogger(BgpKeepAliveTimer.class); |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Gets keepalive timer object. |
| 36 | * |
| 37 | * @return keepAliveTimer keepalive timer. |
| 38 | */ |
| 39 | public Timer getKeepAliveTimer() { |
| 40 | return keepAliveTimer; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Initialize timer to send keepalive message periodically. |
| 45 | * |
| 46 | * @param h channel handler |
| 47 | * @param seconds time interval. |
| 48 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 49 | public BgpKeepAliveTimer(BgpChannelHandler h, int seconds) { |
Shashikanth VH | 6de20d3 | 2015-10-09 12:04:13 +0530 | [diff] [blame] | 50 | this.handler = h; |
| 51 | this.keepAliveTimer = new Timer(); |
| 52 | this.keepAliveTimer.schedule(new SendKeepAlive(), 0, seconds * 1000); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Send keepalive message to connected peer on schedule. |
| 57 | */ |
| 58 | class SendKeepAlive extends TimerTask { |
| 59 | @Override |
| 60 | public void run() { |
| 61 | log.debug("Sending periodic KeepAlive"); |
| 62 | |
| 63 | try { |
| 64 | // Send keep alive message |
| 65 | handler.sendKeepAliveMessage(); |
| 66 | handler.getBgpPacketStats().addOutPacket(); |
| 67 | } catch (Exception e) { |
| 68 | log.info("Exception occured while sending keepAlive message" + e.toString()); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |