da642576ae9fbe756fb284ef2dbd5bf8a4884429
[vpnservice.git] / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / bgpmanager / thrift / client / implementation / 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.bgpmanager.thrift.client.implementation;
10
11 import java.io.IOException;
12 import java.net.Socket;
13 import java.net.SocketException;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 public class BgpSyncHandle {
18     private static BgpSyncHandle handle = null;
19     private static final Logger LOGGER = LoggerFactory.getLogger(BgpSyncHandle.class);
20     private int more;
21     private int state;
22
23     public static final int INITED = 1;
24     public static final int ITERATING = 2;
25     public static final int DONE = 3;
26     public static final int ABORTED = 4;
27     public static final int NEVER_DONE = 5;
28
29     public static final int DEFAULT_TCP_SOCK_SZ = 87380;    //default receive buffer size on linux > 2.4
30
31     private BgpSyncHandle() {
32         more = 1;
33         state = NEVER_DONE;
34     }
35
36     public static synchronized BgpSyncHandle getInstance() {
37        if (handle == null) {
38            handle = new BgpSyncHandle();
39        }
40        return handle;
41     }
42
43     public synchronized int getState() {
44        return state;
45     }
46
47     public int getMaxCount() {
48         //compute the max count of routes we would like to send
49         Socket skt = new Socket();
50         int sockBufSz = DEFAULT_TCP_SOCK_SZ;
51         try {
52             sockBufSz = skt.getReceiveBufferSize();
53         } catch (SocketException s) {
54             LOGGER.warn("Socket Exception while retrieving default socket buffer size");
55         }
56         try {
57             skt.close();
58         } catch (IOException e) {
59             LOGGER.warn("IO Exception while closing socket for retrieving default socket buffer size");
60         }
61         return sockBufSz/getRouteSize();
62     }
63
64     public int getRouteSize() {
65        //size of one update structure on the wire. ideally
66        //this should be computed; or thrift sure has a nice
67        //way to tell this to the applciation, but for the
68        //moment, we just use 8 bytes more than the size of
69        //the C struct.
70
71        return 96;
72     }
73
74     public int setState(int state) {
75        int retval = this.state;
76        this.state = state;
77        return retval;
78     }
79
80     public int setMore(int more) {
81        int retval = this.more;
82        this.more = more;
83        return retval;
84     }
85 }
86
87
88