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: NetworkAcceptor.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.ServerSocket;
35 import java.net.Socket;
36 import jtge.engine.io.IAcceptor;
37 import jtge.engine.io.IConnectionObserver;
38 import jtge.engine.io.IInput;
39 import jtge.engine.io.IInputOutputFactory;
40 import jtge.engine.io.IOutput;
41 import jtge.engine.io.InputOutputException;
42
43 /***
44 * Implements a network acceptor.
45 * <p>
46 * This acceptor provides a blocking open and a non-blocking close.
47 *
48 * @author Mathieu Champlon
49 * @version $Revision: 1.2 $ $Date: 2005/09/10 11:26:47 $
50 */
51 public class NetworkAcceptor implements IAcceptor
52 {
53 private final IInputOutputFactory factory;
54 private final IConnectionObserver observer;
55 private final int port;
56 private ServerSocket listenSocket;
57 private boolean isClosed;
58
59 /***
60 * Create a network acceptor.
61 *
62 * @param factory the input/output factory
63 * @param observer the connection observer
64 * @param port the listening port
65 */
66 public NetworkAcceptor( final IInputOutputFactory factory, final IConnectionObserver observer, 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( port < 0 )
73 throw new IllegalArgumentException( "argument 'port' is < 0" );
74 this.factory = factory;
75 this.observer = observer;
76 this.port = port;
77 this.isClosed = false;
78 }
79
80 /***
81 * {@inheritDoc}
82 */
83 public final void open()
84 {
85 try
86 {
87 synchronized( this )
88 {
89 if( !isClosed() )
90 listenSocket = new ServerSocket( port );
91 }
92 while( !isClosed() )
93 {
94 final Socket socket = listenSocket.accept();
95 final IInput input = factory.create( socket.getInputStream() );
96 final IOutput output = factory.create( socket.getOutputStream() );
97 observer.opened( input, output );
98 }
99 }
100 catch( IOException e )
101 {
102 if( !isClosed() )
103 throw new InputOutputException( e );
104 }
105 }
106
107 /***
108 * {@inheritDoc}
109 */
110 public final void close()
111 {
112 try
113 {
114 synchronized( this )
115 {
116 isClosed = true;
117 if( listenSocket != null )
118 listenSocket.close();
119 }
120 }
121 catch( IOException e )
122 {
123 throw new InputOutputException( e );
124 }
125 }
126
127 private boolean isClosed()
128 {
129 return isClosed;
130 }
131 }