KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > requesthandler > test > TestFullDuplexTunnel


1 package com.sslexplorer.requesthandler.test;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.InterruptedIOException JavaDoc;
6 import java.io.OutputStream JavaDoc;
7 import java.util.Random JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import com.sslexplorer.boot.RequestHandlerTunnel;
13 import com.sslexplorer.boot.Util;
14
15 public class TestFullDuplexTunnel implements RequestHandlerTunnel {
16
17     
18     private InputStream JavaDoc _in;
19     private OutputStream JavaDoc _out;
20     private Thread JavaDoc thread;
21     
22     static Log log = LogFactory.getLog(TestFullDuplexTunnel.class);
23     
24     public TestFullDuplexTunnel() {
25         
26     }
27     
28     
29     public void close() {
30         
31
32     }
33
34     /* ------------------------------------------------------------ */
35     /** handle method.
36      * This method is called by the HttpConnection.handleNext() method if
37      * this HttpTunnel has been set on that connection.
38      * The default implementation of this method copies between the HTTP
39      * socket and the socket passed in the constructor.
40      * @param in
41      * @param out
42      */

43     public void tunnel(InputStream JavaDoc in, OutputStream JavaDoc out)
44     {
45         
46         log.info("Starting new full duplex test tunnel");
47         
48         Copy copy= new Copy();
49
50         try
51         {
52             thread= Thread.currentThread();
53             _in = in;
54             _out = out;
55             copy.start();
56             copydata(new RandomInputStream(), out);
57         }
58         catch (Exception JavaDoc e)
59         {
60             log.error("Error from full duplex test thread", e);
61         }
62         finally
63         {
64             Util.closeStream(_out);
65             Util.closeStream(_in);
66             copy.interrupt();
67             
68             log.info("Full duplex test therad is exiting");
69         }
70     }
71
72     /* ------------------------------------------------------------ */
73     private void copydata(InputStream JavaDoc in, OutputStream JavaDoc out) throws java.io.IOException JavaDoc
74     {
75         long timestamp= 0;
76         while (true)
77         {
78             try
79             {
80                 Util.copy(in, out);
81                 timestamp= 0;
82                 return;
83             }
84             catch (InterruptedIOException JavaDoc e)
85             {
86
87             }
88         }
89     }
90
91     /* ------------------------------------------------------------ */
92     /* ------------------------------------------------------------ */
93     /** Copy thread.
94      * Helper thread to copy from the HTTP input to the sockets output
95      */

96     private class Copy extends Thread JavaDoc
97     {
98         public void run()
99         {
100             try
101             {
102                 copydata(_in, new RandomOutputStream());
103             }
104             catch (Exception JavaDoc e)
105             {
106 // log.error("Error from full duplex test thread (2nd Thread)", e);
107
}
108             finally
109             {
110                 try
111                 {
112                     Util.closeStream(_out);
113                     Util.closeStream(_in);
114                     thread.interrupt();
115                 }
116                 catch (Exception JavaDoc e)
117                 {
118                 }
119             }
120             
121             log.info("Exiting run method of Copy thread");
122         }
123     }
124
125     class RandomInputStream extends InputStream JavaDoc {
126         
127         Random JavaDoc rnd = new Random JavaDoc();
128         
129         public int read() {
130             return rnd.nextInt(255);
131         }
132         
133         public int read(byte[] buf, int off, int len) throws IOException JavaDoc {
134             
135             rnd.nextBytes(buf);
136             
137             return len;
138         }
139     }
140     
141     
142     class RandomOutputStream extends OutputStream JavaDoc {
143
144         Random JavaDoc rnd = new Random JavaDoc();
145         
146         public void write(int b) {
147             rnd.nextInt();
148         }
149         
150         public void write(byte[] buf, int off, int len) {
151             
152             rnd.nextBytes(buf);
153         }
154     }
155 }
156
Popular Tags