View Javadoc

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  package org.indi.reactor;
19  
20  import java.io.IOException;
21  import java.net.InetSocketAddress;
22  import java.nio.channels.ClosedChannelException;
23  import java.nio.channels.SelectionKey;
24  import java.nio.channels.SocketChannel;
25  
26  /**
27   * EventHandler for active connection estabishment. The Connector regesters with
28   * the reactor asking to estabish a connection to a particular server during
29   * construction. It called by the reactor and establishes the connection. It
30   * creates a new EventHandler using the given EventHandlerFactory and associates
31   * it with the newly created connection. After completing this work is
32   * unregisters with the reactor.
33   * 
34   * @author Dirk Hünniger
35   */
36  public class Connector extends SimpleEventHandler {
37      /**
38       * The EventHandlerFactory used to create new EventHandlers to be
39       * associated with the newly created connection
40       */
41      protected EventHandlerFactory factory;
42  
43      /**
44       * Class Constructor
45       * 
46       * @param r
47       *                The reactor to register with
48       * @param f
49       *                The factory used to create a new EventHandler for the
50       *                newly created connection
51       * @param host
52       *                The host to connect to
53       * @param port
54       *                The port to connect to
55       * @throws IOException
56       */
57      public Connector(Reactor r, EventHandlerFactory f, String host, int port)
58  	    throws IOException {
59  	super(r, SocketChannel.open());
60  	this.factory = f;
61  	SocketChannel socket = (SocketChannel) channel();
62  	socket.configureBlocking(false);
63  	socket.connect(new InetSocketAddress(host, port));
64  	register(SelectionKey.OP_CONNECT);
65      }
66  
67      /**
68       * Callback method called by the reactor when the new connection is
69       * ready to be established.
70       */
71      @Override
72      final public void onConnect() throws java.io.IOException {
73  	SocketChannel socket = (SocketChannel) channel();
74  	socket.finishConnect();
75  	EventHandler h = this.factory.produce(this.reactor, socket);
76  	onConnected(h);
77  	this.reactor.unregister(this);
78      }
79  
80      /**
81       * Callback method called when the new connection has been established
82       * and a new EvendHandler has be created and associated with it
83       * 
84       * @param h
85       *                The new EventHandler associated with the new
86       *                connection
87       * @throws ClosedChannelException
88       * @throws IOException
89       */
90      public void onConnected(EventHandler h) throws ClosedChannelException,
91  	    IOException {
92      }
93  }