blob: 524ac4c1c8f8613d13ccbe75569f0429e577a5e5 [file] [log] [blame]
Shashikanth VH6de20d32015-10-09 12:04:13 +05301/*
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
17package org.onosproject.bgp.controller.impl;
18
19import java.util.Timer;
20import java.util.TimerTask;
21
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25/**
26 * Implement sending keepalive message to connected peer periodically based on negotiated holdtime.
27 */
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053028public class BgpKeepAliveTimer {
Shashikanth VH6de20d32015-10-09 12:04:13 +053029
30 private Timer keepAliveTimer;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031 private BgpChannelHandler handler;
32 private static final Logger log = LoggerFactory.getLogger(BgpKeepAliveTimer.class);
Shashikanth VH6de20d32015-10-09 12:04:13 +053033
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 VH5dd8dbe2015-11-26 13:22:18 +053049 public BgpKeepAliveTimer(BgpChannelHandler h, int seconds) {
Shashikanth VH6de20d32015-10-09 12:04:13 +053050 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}