1 /*
2 Copyright (C) 2007 Dirk Huenniger
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 package org.indi.reactor;
20
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.nio.channels.ClosedChannelException;
24 import java.nio.channels.SelectableChannel;
25 import java.nio.channels.SelectionKey;
26 import java.nio.channels.ServerSocketChannel;
27
28 /**
29 * EventHandler for passive connection establishment. The Acceptor is registered
30 * with the reactor during construction. Whenever a new client connects, on the
31 * specified port a new EventHandler is created using the given
32 * EventHandlerFactory and associated with the newly created connection, as well
33 * as registered with the reactor.
34 *
35 * @author Dirk Hünniger
36 */
37 public class Acceptor extends SimpleEventHandler {
38 /**
39 * The EventHandlerFactory used to produce new EventHandlers for new
40 * connections
41 */
42 protected EventHandlerFactory factory;
43
44 /**
45 * Class Constructor
46 *
47 * @param r
48 * the reactor the Acceptor is to be registered with
49 * @param f
50 * the EventHandlerFactory to be used for the creation of
51 * new EventHandlers for new connections
52 * @param port
53 * The port to listen for new connections
54 * @throws IOException
55 */
56 protected Acceptor(Reactor r, EventHandlerFactory f, int port)
57 throws IOException {
58 super(r, ServerSocketChannel.open());
59 ServerSocketChannel serverSocket = (ServerSocketChannel) this.channel;
60 serverSocket.socket().bind(new InetSocketAddress(port));
61 serverSocket.configureBlocking(false);
62 this.factory = f;
63 register(SelectionKey.OP_ACCEPT);
64 }
65
66 /**
67 * Callback method called by the reactor when a new client is ready to
68 * be accepted. Implemented here.
69 */
70 @Override
71 final public void onAccept() throws java.io.IOException {
72 SelectableChannel ch = ((ServerSocketChannel) this.channel).accept();
73 EventHandler h = this.factory.produce(this.reactor, ch);
74 ch.configureBlocking(false);
75 onAccepted(h);
76 }
77
78 /**
79 * Callbackmethod called after the connection has been established and a
80 * new EventHandler has been created and set up to interact with this
81 * new conenction
82 *
83 * @param h
84 * The newly created EventHandler
85 * @throws ClosedChannelException
86 * @throws IOException
87 */
88 public void onAccepted(EventHandler h) throws ClosedChannelException,
89 IOException {
90 }
91 }