KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > tunneling > TunnelInactivityMonitor


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.tunneling;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24
25 import com.sslexplorer.agent.client.Agent;
26
27 /**
28  * Monitors all active tunnels for inactivity, closing them when a certain
29  * number of milliseconds have elapsed without any date travelling over them.
30  * <p>
31  * This monitor must be started after the VPN client has been connected and will
32  * automatically stop when the client disconnects.
33  *
34  * @author Lee David Painter <a HREF="mailto: lee@3sp.com">&lt;lee@3sp.com&gt;</a>
35  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
36  */

37 public class TunnelInactivityMonitor extends Thread JavaDoc {
38
39     // Private instance variables
40
private Agent vpnClient;
41
42     // #ifdef DEBUG
43
static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(TunnelInactivityMonitor.class);
44
45     // #endif
46

47     /**
48      * Constructor.
49      *
50      * @param vpnClient client
51      */

52     public TunnelInactivityMonitor(Agent vpnClient) {
53         this.vpnClient = vpnClient;
54         setDaemon(true);
55
56     }
57
58     /*
59      * (non-Javadoc)
60      *
61      * @see java.lang.Thread#run()
62      */

63     public void run() {
64         // #ifdef DEBUG
65
log.info("Starting tunnel inactivity monitor (tunnels " + new Integer JavaDoc(vpnClient.getConfiguration().getTunnelInactivity())
66             + "ms, web forwards "
67             + new Integer JavaDoc(vpnClient.getConfiguration().getWebForwardInactivity())
68             + "ms)");
69         // #endif
70
while (true) {
71             try {
72                 Thread.sleep(30000);
73                 if (vpnClient.getState() == Agent.STATE_DISCONNECTED) {
74                     break;
75                 }
76                 // #ifdef DEBUG
77
log.info("Checking for tunnel inactivity"); //$NON-NLS-1$
78
// #endif
79

80                 // Hack to allow MSJVM access to the protected member
81
Hashtable JavaDoc activeListeners = vpnClient.getTunnelManager().getActiveLocalTunnels();
82
83                 synchronized (activeListeners) {
84                     long now = System.currentTimeMillis();
85                     for (Enumeration JavaDoc e = activeListeners.keys(); e.hasMoreElements();) {
86                         Integer JavaDoc id = (Integer JavaDoc) e.nextElement();
87                         LocalTunnelServer l = (LocalTunnelServer) activeListeners.get(id);
88                         try {
89                             if (l.isListening()) {
90                                 // Temporary single connect tunnels and
91
// permanent tunnels
92
if (vpnClient.getConfiguration().getTunnelInactivity() != 0 && (l.getTunnel().isPermanent() || (!l.getTunnel()
93                                                 .isPermanent() && l.getTunnel().isTemporarySingleConnect()))) {
94                                     if (now > (l.getDataLastTransferredTime() + vpnClient.getConfiguration().getTunnelInactivity())) {
95                                         // #ifdef DEBUG
96
log.info("Permanent / Temporary Single Connect tunnel " + id + " is out of date, closing."); //$NON-NLS-1$
97
// #endif
98
vpnClient.getTunnelManager().stopLocalTunnel(l.getId());
99                                     }
100
101                                 }
102                                 // Temporary tunnels that allow multiple
103
// connections (i.e.
104
// web forwards)
105
else if (vpnClient.getConfiguration().getWebForwardInactivity() != 0 && (!l.getTunnel()
106                                                 .isPermanent() && !l.getTunnel().isTemporarySingleConnect())) {
107                                     if (now > (l.getDataLastTransferredTime() + vpnClient.getConfiguration()
108                                                     .getWebForwardInactivity())) {
109                                         // #ifdef DEBUG
110
log.info("Temporary Multiple Connection (Webforward) tunnel " + id + " is out of date, closing."); //$NON-NLS-1$
111
// #endif
112
vpnClient.getTunnelManager().stopLocalTunnel(l.getId());
113                                     }
114                                 }
115                             }
116                         } catch (Throwable JavaDoc t) {
117                             // #ifdef DEBUG
118
log.error("Failed to check state of tunnel " + id, t); //$NON-NLS-1$
119
// #endif
120
}
121                     }
122                 }
123
124             } catch (Throwable JavaDoc t) {
125                 // #ifdef DEBUG
126
log.error("Failed to check state of tunnels", t); //$NON-NLS-1$
127
// #endif
128
}
129         }
130         // #ifdef DEBUG
131
log.error("Disconnected, so stopping tunnel inactivity monitor."); //$NON-NLS-1$
132
// #endif
133
}
134 }
Popular Tags