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: NetworkConnector.java,v 1.2 2005/09/10 11:26:47 mat007 Exp $
29   */
30  
31  package jtge.engine.net;
32  
33  import java.io.IOException;
34  import java.net.Socket;
35  import jtge.engine.io.IConnectionObserver;
36  import jtge.engine.io.IConnector;
37  import jtge.engine.io.IInput;
38  import jtge.engine.io.IInputOutputFactory;
39  import jtge.engine.io.IOutput;
40  import jtge.engine.io.InputOutputException;
41  
42  /***
43   * Implements a network connector.
44   * <p>
45   * This connector provides a blocking open.
46   *
47   * @author Mathieu Champlon
48   * @version $Revision: 1.2 $ $Date: 2005/09/10 11:26:47 $
49   */
50  public class NetworkConnector implements IConnector
51  {
52      private final IInputOutputFactory factory;
53      private final IConnectionObserver observer;
54      private final String host;
55      private final int port;
56  
57      /***
58       * Create a network connector.
59       *
60       * @param factory the input/output factory
61       * @param observer the connection observer
62       * @param host the server host name
63       * @param port the listening port
64       */
65      public NetworkConnector( final IInputOutputFactory factory, final IConnectionObserver observer, final String host,
66              final int port )
67      {
68          if( factory == null )
69              throw new IllegalArgumentException( "argument 'factory' is null" );
70          if( observer == null )
71              throw new IllegalArgumentException( "argument 'observer' is null" );
72          if( host == null )
73              throw new IllegalArgumentException( "argument 'host' is null" );
74          if( port < 0 )
75              throw new IllegalArgumentException( "argument 'port' is < 0" );
76          this.factory = factory;
77          this.observer = observer;
78          this.host = host;
79          this.port = port;
80      }
81  
82      /***
83       * {@inheritDoc}
84       */
85      public final void open()
86      {
87          try
88          {
89              final Socket socket = new Socket( host, port );
90              final IInput input = factory.create( socket.getInputStream() );
91              final IOutput output = factory.create( socket.getOutputStream() );
92              observer.opened( input, output );
93          }
94          catch( IOException e )
95          {
96              throw new InputOutputException( e );
97          }
98      }
99  }