View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: ChannelManager.java,v 1.6 2005/09/03 23:11:42 mat007 Exp $
29   */
30  
31  package jtge.engine.io;
32  
33  import java.util.Iterator;
34  import java.util.Vector;
35  
36  /***
37   * Maintains a link between inputs and outputs.
38   * <p>
39   * A channel is simply the association of an input and an output.
40   * <p>
41   * The manager closes both input and ouput when one of them is closed.
42   *
43   * @author Mathieu Champlon
44   * @version $Revision: 1.6 $ $Date: 2005/09/03 23:11:42 $
45   */
46  public class ChannelManager implements ISessionManager
47  {
48      private final Vector inputs;
49      private final Vector outputs;
50      private final Vector views;
51  
52      /***
53       * Create a channel manager.
54       */
55      public ChannelManager()
56      {
57          inputs = new Vector();
58          outputs = new Vector();
59          views = new Vector();
60      }
61  
62      /***
63       * Register a channel observer.
64       *
65       * @param observer the observer
66       */
67      public final void register( final IChannelObserver observer )
68      {
69          if( observer != null )
70              views.add( observer );
71      }
72  
73      /***
74       * Register a status observer.
75       *
76       * @param observer the observer
77       */
78      public final void register( final IStatusObserver observer )
79      {
80          if( observer != null )
81              views.add( new ChannelObserverAdapter( observer ) );
82      }
83  
84      private void connected( final IInput input, final IOutput output )
85      {
86          final Iterator iterator = views.iterator();
87          while( iterator.hasNext() )
88              ((IChannelObserver)iterator.next()).connected( input, output );
89      }
90  
91      private void disconnected( final IInput input, final IOutput output )
92      {
93          final Iterator iterator = views.iterator();
94          while( iterator.hasNext() )
95              ((IChannelObserver)iterator.next()).disconnected( input, output );
96      }
97  
98      /***
99       * {@inheritDoc}
100      */
101     public final void opened( final IInput input, final IOutput output )
102     {
103         if( input == null )
104             throw new IllegalArgumentException( "argument 'input' is null" );
105         if( output == null )
106             throw new IllegalArgumentException( "argument 'output' is null" );
107         inputs.add( input );
108         outputs.add( output );
109         connected( input, output );
110     }
111 
112     /***
113      * {@inheritDoc}
114      */
115     public final void closed( final IInput input )
116     {
117         remove( inputs.indexOf( input ) );
118     }
119 
120     /***
121      * {@inheritDoc}
122      */
123     public final void closed( final IOutput output )
124     {
125         remove( outputs.indexOf( output ) );
126     }
127 
128     private void remove( final int index )
129     {
130         if( index != -1 )
131         {
132             final IInput input = (IInput)inputs.remove( index );
133             final IOutput output = (IOutput)outputs.remove( index );
134             input.close();
135             output.close();
136             disconnected( input, output );
137         }
138     }
139 
140     /***
141      * Close all inputs and outputs.
142      */
143     public final void close()
144     {
145         Iterator iterator = ((Vector)outputs.clone()).iterator();
146         while( iterator.hasNext() )
147             ((IOutput)iterator.next()).close();
148         iterator = ((Vector)inputs.clone()).iterator();
149         while( iterator.hasNext() )
150             ((IInput)iterator.next()).close();
151     }
152 }