Enhancements in BGP module
[netvirt.git] / bgpmanager / impl / src / main / java / org / opendaylight / netvirt / bgpmanager / thrift / client / BgpSyncHandle.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.netvirt.bgpmanager.thrift.client;
10
11 import java.io.IOException;
12 import java.net.Socket;
13 import java.net.SocketException;
14 import java.util.concurrent.atomic.AtomicInteger;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class BgpSyncHandle {
19     private static final Logger LOG = LoggerFactory.getLogger(BgpSyncHandle.class);
20     private int more = 1;
21
22     public static final int INITED = 1;
23     public static final int ITERATING = 2;
24     public static final int DONE = 3;
25     public static final int ABORTED = 4;
26     public static final int NEVER_DONE = 5;
27
28     public static final int DEFAULT_TCP_SOCK_SZ = 87380;    //default receive buffer size on linux > 2.4
29
30     private final AtomicInteger state = new AtomicInteger(NEVER_DONE);
31
32     public int getState() {
33         return state.get();
34     }
35
36     public int getMaxCount() {
37         //compute the max count of routes we would like to send
38         Socket skt = new Socket();
39         int sockBufSz = DEFAULT_TCP_SOCK_SZ;
40         try {
41             sockBufSz = skt.getReceiveBufferSize();
42         } catch (SocketException s) {
43             LOG.warn("Socket Exception while retrieving default socket buffer size");
44         }
45         try {
46             skt.close();
47         } catch (IOException e) {
48             LOG.warn("IO Exception while closing socket for retrieving default socket buffer size");
49         }
50         return sockBufSz / getRouteSize();
51     }
52
53     public int getRouteSize() {
54         //size of one update structure on the wire. ideally
55         //this should be computed; or thrift sure has a nice
56         //way to tell this to the applciation, but for the
57         //moment, we just use 8 bytes more than the size of
58         //the C struct.
59
60         return 168;
61     }
62
63     public int setState(int newState) {
64         return this.state.getAndSet(newState);
65     }
66
67     public int setMore(int localMore) {
68         this.more = localMore;
69         return this.more;
70     }
71 }