Merge "Simplified inheritance within stateful module."
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / SessionRIBsOut.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11
12 import java.util.concurrent.atomic.AtomicBoolean;
13
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
15
16 final class SessionRIBsOut extends AbstractAdjRIBsOut implements Runnable {
17     private final AtomicBoolean scheduled = new AtomicBoolean(false);
18     private final BGPSessionImpl session;
19
20     SessionRIBsOut(final BGPSessionImpl session) {
21         this.session = Preconditions.checkNotNull(session);
22     }
23
24     @Override
25     protected void wantWrite() {
26         if (scheduled.compareAndSet(false, true)) {
27             session.schedule(this);
28         }
29     }
30
31     @Override
32     protected boolean writePDU(final Update pdu) {
33         session.writeAndFlush(pdu);
34         return session.isWritable();
35     }
36
37     @Override
38     public void run() {
39         process();
40     }
41 }