1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/BidiStreamProxy.java,v 1.1.2.2 2004/02/22 18:21:18 olegk Exp $
3    * $Revision: 1.1.2.2 $
4    * $Date: 2004/02/22 18:21:18 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient.server;
33  
34  import java.io.InputStream;
35  import java.io.OutputStream;
36  
37  /***
38   * Pumps data between a pair of input / output streams. Used to
39   * connect the two ends (left and right) of a bidirectional communication channel.
40   * Instances of this class are thread safe.
41   * 
42   * @author Ortwin Glueck
43   */
44  class BidiStreamProxy {
45      private StreamProxy leftToRight, rightToLeft;
46      private int state = 0;
47      
48      /***
49       * Sets up a new connection between two peers (left and right)
50       * @param leftIn input channel of the left peer
51       * @param leftOut output channel of the left peer
52       * @param rightIn input channel of the right peer
53       * @param rightOut output channel of the right peer
54       */
55      public BidiStreamProxy(InputStream leftIn, OutputStream leftOut, InputStream rightIn, OutputStream rightOut) {
56          leftToRight = new StreamProxy(leftIn, rightOut);
57          rightToLeft = new StreamProxy(rightIn, leftOut);
58      }
59      
60      /***
61       * Starts pumping the information from left to right and vice versa.
62       * This is performed asynchronously so this method returns immediately.
63       */
64      public synchronized void start() {
65          if (state != 0) throw new IllegalStateException("Can not start twice");
66          leftToRight.start();
67          rightToLeft.start();
68          state = 1;
69      }
70      
71      /***
72       * Aborts the communication between the peers and releases all resources.
73       * Note: The method does not wait for the pump threads to terminate.
74       */
75      public synchronized void abort() {
76          if (leftToRight != null) leftToRight.abort();
77          if (rightToLeft != null)  rightToLeft.abort();
78          leftToRight = null;
79          rightToLeft = null;
80      }
81      
82      /***
83       * Blocks until all data has been copied. Basically calls the 
84       * join method on the pump thread.
85       * @throws InterruptedException
86       */
87      public void block() throws InterruptedException {
88      	if (state != 1) throw new IllegalStateException("Can not block before started");
89      	leftToRight.block();
90      	rightToLeft.block();
91      }
92  }