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