KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > channel > LocalAcceptThread


1
2 package ch.ethz.ssh2.channel;
3
4 import java.io.IOException JavaDoc;
5 import java.net.InetSocketAddress JavaDoc;
6 import java.net.ServerSocket JavaDoc;
7 import java.net.Socket JavaDoc;
8
9 /**
10  * LocalAcceptThread.
11  *
12  * @author Christian Plattner, plattner@inf.ethz.ch
13  * @version $Id: LocalAcceptThread.java,v 1.8 2006/10/20 20:50:24 cplattne Exp $
14  */

15 public class LocalAcceptThread extends Thread JavaDoc implements IChannelWorkerThread
16 {
17     ChannelManager cm;
18     String JavaDoc host_to_connect;
19     int port_to_connect;
20
21     final ServerSocket JavaDoc ss;
22
23     public LocalAcceptThread(ChannelManager cm, int local_port, String JavaDoc host_to_connect, int port_to_connect)
24             throws IOException JavaDoc
25     {
26         this.cm = cm;
27         this.host_to_connect = host_to_connect;
28         this.port_to_connect = port_to_connect;
29
30         ss = new ServerSocket JavaDoc(local_port);
31     }
32
33     public LocalAcceptThread(ChannelManager cm, InetSocketAddress JavaDoc localAddress, String JavaDoc host_to_connect,
34             int port_to_connect) throws IOException JavaDoc
35     {
36         this.cm = cm;
37         this.host_to_connect = host_to_connect;
38         this.port_to_connect = port_to_connect;
39
40         ss = new ServerSocket JavaDoc();
41         ss.bind(localAddress);
42     }
43
44     public void run()
45     {
46         try
47         {
48             cm.registerThread(this);
49         }
50         catch (IOException JavaDoc e)
51         {
52             stopWorking();
53             return;
54         }
55
56         while (true)
57         {
58             Socket JavaDoc s = null;
59
60             try
61             {
62                 s = ss.accept();
63             }
64             catch (IOException JavaDoc e)
65             {
66                 stopWorking();
67                 return;
68             }
69
70             Channel cn = null;
71             StreamForwarder r2l = null;
72             StreamForwarder l2r = null;
73
74             try
75             {
76                 /* This may fail, e.g., if the remote port is closed (in optimistic terms: not open yet) */
77
78                 cn = cm.openDirectTCPIPChannel(host_to_connect, port_to_connect, s.getInetAddress().getHostAddress(), s
79                         .getPort());
80
81             }
82             catch (IOException JavaDoc e)
83             {
84                 /* Simply close the local socket and wait for the next incoming connection */
85
86                 try
87                 {
88                     s.close();
89                 }
90                 catch (IOException JavaDoc ignore)
91                 {
92                 }
93
94                 continue;
95             }
96
97             try
98             {
99                 r2l = new StreamForwarder(cn, null, null, cn.stdoutStream, s.getOutputStream(), "RemoteToLocal");
100                 l2r = new StreamForwarder(cn, r2l, s, s.getInputStream(), cn.stdinStream, "LocalToRemote");
101             }
102             catch (IOException JavaDoc e)
103             {
104                 try
105                 {
106                     /* This message is only visible during debugging, since we discard the channel immediatelly */
107                     cn.cm.closeChannel(cn, "Weird error during creation of StreamForwarder (" + e.getMessage() + ")",
108                             true);
109                 }
110                 catch (IOException JavaDoc ignore)
111                 {
112                 }
113
114                 continue;
115             }
116
117             r2l.setDaemon(true);
118             l2r.setDaemon(true);
119             r2l.start();
120             l2r.start();
121         }
122     }
123
124     public void stopWorking()
125     {
126         try
127         {
128             /* This will lead to an IOException in the ss.accept() call */
129             ss.close();
130         }
131         catch (IOException JavaDoc e)
132         {
133         }
134     }
135 }
136
Popular Tags