KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
23 import java.net.Socket JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import com.maverick.multiplex.Channel;
28 import com.sslexplorer.agent.client.util.IOStreamConnector;
29 import com.sslexplorer.agent.client.util.IOStreamConnectorListener;
30 import com.sslexplorer.agent.client.util.TunnelConfiguration;
31
32 /**
33  * Represents a single <i>Local Tunnel Connection</i>, i.e. one connection
34  * that has been spawned from a {@link LocalTunnelServer}.
35  *
36  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
37  */

38 public class LocalTunnelConnection {
39
40     // Private instance variables
41
private Channel tunnel;
42     private Socket JavaDoc client;
43     private IOStreamConnector tx;
44     private IOStreamConnector rx;
45     private IOStreamListener rxStreamListener = new IOStreamListener(false);
46     private IOStreamListener txStreamListener = new IOStreamListener(false);
47     private Vector JavaDoc listeners;
48     private IOStreamConnectorListener txListener;
49     private IOStreamConnectorListener rxListener;
50     private LocalTunnelServer vpnConnectionListener;
51     private boolean hasStopped = false;
52     private boolean stopping;
53     // #ifdef DEBUG
54
org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LocalTunnelConnection.class);
55     // #endif
56

57     /**
58      * Constructor.
59      *
60      * @param vpnConnectionListener connection listener
61      * @param tunnel socket
62      * @param client client
63      * @param configuration tunnel configuration
64      * @param txListener
65      * @param rxListener
66      */

67     public LocalTunnelConnection(LocalTunnelServer vpnConnectionListener, Channel tunnel, Socket JavaDoc client, TunnelConfiguration configuration, IOStreamConnectorListener txListener, IOStreamConnectorListener rxListener) {
68         this.tunnel = tunnel;
69         this.client = client;
70         this.txListener = txListener;
71         this.rxListener = rxListener;
72         this.vpnConnectionListener = vpnConnectionListener;
73         this.listeners = new Vector JavaDoc();
74     }
75
76     /* (non-Javadoc)
77      * @see com.sslexplorer.vpn.base.VPNTunnel#getClientHost()
78      */

79     public String JavaDoc getClientHost() {
80         return client.getInetAddress().getHostName();
81     }
82
83     /* (non-Javadoc)
84      * @see com.sslexplorer.vpn.base.VPNTunnel#getVPNConnectionListener()
85      */

86     public LocalTunnelServer getVPNConnectionListener() {
87         return vpnConnectionListener;
88     }
89
90     /* (non-Javadoc)
91      * @see com.sslexplorer.vpn.base.VPNTunnel#start()
92      */

93     public void start() throws IOException JavaDoc {
94         try {
95             rx = new IOStreamConnector();
96             rx.addListener(rxListener);
97             rx.addListener(rxStreamListener);
98             rx.connect(tunnel.getInputStream(), client.getOutputStream());
99
100             tx = new IOStreamConnector();
101             tx.addListener(txListener);
102             tx.addListener(txStreamListener);
103             tx.connect(client.getInputStream(), tunnel.getOutputStream());
104
105             for (int i = 0; i < listeners.size(); i++)
106                 ((LocalTunnelConnectionEventListener) listeners.elementAt(i)).localTunnelConnectionStarted(this);
107             
108         } catch (Throwable JavaDoc ex) {
109             // #ifdef DEBUG
110
log.error("ActiveTunnel threads failed to start", ex);
111             // #endif
112

113             try {
114                 client.close();
115             } catch (Throwable JavaDoc e) {
116             }
117
118             try {
119                 tunnel.close();
120             } catch (Throwable JavaDoc e) {
121             }
122
123             throw new IOException JavaDoc(MessageFormat.format(Messages.getString("LocalTCPTunnel.tunnelFailedToStart"), new Object JavaDoc[] { ex.getMessage() })); //$NON-NLS-1$
124
}
125     }
126
127     /* (non-Javadoc)
128      * @see com.sslexplorer.vpn.base.VPNTunnel#stop()
129      */

130     public synchronized void stop() {
131
132         if(!hasStopped) {
133             stopping = true;
134             if (!rx.isClosed()) {
135                 rx.close();
136             }
137             if (!tx.isClosed()) {
138                 tx.close();
139             }
140             for (int i = 0; i < listeners.size(); i++)
141                 ((LocalTunnelConnectionEventListener) listeners.elementAt(i)).localTunnelConnectionStopped(this);
142         }
143         
144         try {
145             client.close();
146         } catch (IOException JavaDoc e) {
147         }
148
149         tunnel.close();
150
151         stopping = false;
152         hasStopped = true;
153     }
154
155     protected void addListener(LocalTunnelConnectionEventListener listener) {
156         if (listener != null)
157             listeners.addElement(listener);
158     }
159
160     class IOStreamListener implements IOStreamConnectorListener {
161         
162         private boolean send;
163         
164         public IOStreamListener(boolean send) {
165             this.send = send;
166         }
167
168         public void connectorClosed(IOStreamConnector connector) {
169             try {
170                 client.close();
171             } catch (IOException JavaDoc ex) {
172             }
173             try {
174                 tunnel.close();
175             } catch (Exception JavaDoc ex1) {
176             }
177             if (connector != rx) {
178                 rx.close();
179             }
180             if (connector != tx) {
181                 tx.close();
182             }
183             if(!stopping) {
184                 stop();
185             }
186         }
187
188         public void dataTransfered(byte[] buffer, int count) {
189             for (int i = listeners.size() - 1; i >= 0; i--)
190                 ((LocalTunnelConnectionEventListener) listeners.elementAt(i)).localTunnelConnectionDataTransferred(LocalTunnelConnection.this, buffer, count, send);
191         }
192     }
193
194 }
195
Popular Tags