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.server;
20
21 import java.io.IOException;
22 import java.nio.channels.ClosedChannelException;
23 import java.nio.channels.SelectionKey;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Queue;
27
28 import org.indi.reactor.EventHandler;
29 import org.indi.reactor.EventHandlerFactory;
30 import org.indi.reactor.Reactor;
31
32 /**
33 * Acceptor used for passive connection estabishment. Initializes incomming
34 * client connections.
35 *
36 * @author Dirk Hünniger
37 *
38 */
39 public class Acceptor extends org.indi.reactor.Acceptor {
40 /**
41 * Queue to pass messages that have been received from the client to the
42 * device drivers
43 */
44 private final Queue<java.lang.Object> toDriverQueue;
45 /**
46 * Colletion of all clientHandlers. Each Client handler represents a
47 * connection to a single client.
48 */
49 private final Collection<ClientHandler> clientHandlers = new ArrayList<ClientHandler>();
50
51 /**
52 * Class constructor
53 *
54 * @param r
55 * The reactor to register with
56 * @param f
57 * The EventHandlerFactory used to procduce a new
58 * clientHandler for each new connection
59 * @param port
60 * The port to listen for incomming connections
61 * @param toDriverQueue
62 * A queue to pass messages that have been received from the
63 * client to the device drivers.
64 * @throws IOException
65 */
66 Acceptor(Reactor r, EventHandlerFactory f, int port,
67 Queue<java.lang.Object> toDriverQueue) throws IOException {
68 super(r, f, port);
69 this.toDriverQueue = toDriverQueue;
70 // TODO Auto-generated constructor stub
71 }
72
73 /**
74 * Called when a new connection has been estabished and new ClientHanlder
75 * has been created and associated with that connection.
76 */
77 @Override
78 public void onAccepted(EventHandler h) throws ClosedChannelException,
79 IOException {
80 ClientHandler ch = (ClientHandler) h;
81 h.register(SelectionKey.OP_READ);
82 ch.setQueue(this.toDriverQueue);
83 this.clientHandlers.add(ch);
84 }
85
86 /**
87 *
88 * @return the collection of currently know ClientHandlers
89 */
90 public Collection<ClientHandler> getClientHandlers() {
91 return this.clientHandlers;
92 }
93 }