org.apache.mina.core.future.DefaultConnectFuture Java Examples

The following examples show how to use org.apache.mina.core.future.DefaultConnectFuture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProxyConnector.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Connects to the specified <code>address</code>.  If communication starts
 * successfully, events are fired to the connector's <code>handler</code>.
 * 
 * @param remoteAddress the remote address to connect to
 * @param localAddress the local address
 * @param sessionInitializer the session initializer
 * @return {@link ConnectFuture} that will tell the result of the connection attempt
 */
@SuppressWarnings("unchecked")
@Override
protected ConnectFuture connect0(final SocketAddress remoteAddress, final SocketAddress localAddress,
        final IoSessionInitializer<? extends ConnectFuture> sessionInitializer) {
    if (!proxyIoSession.isReconnectionNeeded()) {
        // First connection
        IoHandler handler = getHandler();
        if (!(handler instanceof AbstractProxyIoHandler)) {
            throw new IllegalArgumentException("IoHandler must be an instance of AbstractProxyIoHandler");
        }

        connector.setHandler(handler);
        future = new DefaultConnectFuture();
    }

    ConnectFuture conFuture = connector.connect(proxyIoSession.getProxyAddress(), new ProxyIoSessionInitializer(
            sessionInitializer, proxyIoSession));

    // If proxy does not use reconnection like socks the connector's 
    // future is returned. If we're in the middle of a reconnection
    // then we send back the connector's future which is only used
    // internally while <code>future</code> will be used to notify
    // the user of the connection state.
    if (proxyIoSession.getRequest() instanceof SocksProxyRequest || proxyIoSession.isReconnectionNeeded()) {
        return conFuture;
    }

    return future;
}
 
Example #2
Source File: NioSocketConnector.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public ConnectFuture connect(InetSocketAddress remoteAddress, InetSocketAddress localAddress) {
	if (isDisposing())
		throw new IllegalStateException("disposed connector");
	if (remoteAddress == null)
		throw new IllegalArgumentException("null remoteAddress");
	if (getHandler() == null)
		throw new IllegalStateException("null handler");

	SocketChannel channel = null;
	try {
		channel = SocketChannel.open();

		int receiveBufferSize = getSessionConfig().getReceiveBufferSize();
		if (receiveBufferSize > 65535)
			channel.socket().setReceiveBufferSize(receiveBufferSize);

		if (localAddress != null) {
			try {
				channel.socket().bind(localAddress);
			} catch (IOException ioe) {
				// Add some info regarding the address we try to bind to the message
				throw new IOException("error while binding on " + localAddress + "\noriginal message: " + ioe.getMessage(), ioe);
			}
		}

		channel.configureBlocking(false);
		if (channel.connect(remoteAddress)) {
			ConnectFuture future = new DefaultConnectFuture();
			processor.add(new NioSession(this, channel, future));
			return future;
		}
	} catch (Exception e) {
		close(channel);
		return DefaultConnectFuture.newFailedFuture(e);
	}

  		return connect0(channel, true);
}