Java Code Examples for org.apache.mina.core.future.ConnectFuture#addListener()

The following examples show how to use org.apache.mina.core.future.ConnectFuture#addListener() . 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: MinaNetClient.java    From Lealone-Plugins with Apache License 2.0 6 votes vote down vote up
@Override
protected void createConnectionInternal(NetNode node, AsyncConnectionManager connectionManager,
        AsyncCallback<AsyncConnection> ac) {
    InetSocketAddress inetSocketAddress = node.getInetSocketAddress();
    ConnectFuture future = connector.connect(inetSocketAddress);
    future.addListener(f -> {
        if (f.isDone()) {
            IoSession session = future.getSession();
            MinaWritableChannel writableChannel = new MinaWritableChannel(session);
            AsyncConnection conn;
            if (connectionManager != null) {
                conn = connectionManager.createConnection(writableChannel, false);
            } else {
                conn = new TcpClientConnection(writableChannel, this);
            }
            conn.setInetSocketAddress(inetSocketAddress);
            conn = addConnection(inetSocketAddress, conn);
            ac.setAsyncResult(conn);
        }
    });
}
 
Example 2
Source File: NioSession.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
NioSession(AbstractIoService service, SocketChannel channel, ConnectFuture future) {
	this.service = service;
	this.channel = channel;
	config = new SessionConfigImpl(service);
	IoSessionDataStructureFactory factory = service.getSessionDataStructureFactory();
	attributes = factory.getAttributeMap(this);
	writeRequestQueue = factory.getWriteRequestQueue(this);
	if (future != null) {
		// DefaultIoFilterChain will notify the future. (We support ConnectFuture only for now).
		setAttribute(DefaultIoFilterChain.SESSION_CREATED_FUTURE, future);
		// In case that ConnectFuture.cancel() is invoked before setSession() is invoked,
		// add a listener that closes the connection immediately on cancellation.
		future.addListener((ConnectFuture cf) -> {
			if (cf.isCanceled())
				closeNow();
		});
	}
}
 
Example 3
Source File: StreamBaseDevice.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public synchronized void connect ()
{
    if ( isConnected () )
    {
        logger.info ( "Already connected" );
        return;
    }

    if ( this.connector == null )
    {
        this.connector = new NioSocketConnector ();
        this.connector.setHandler ( this );
        if ( Boolean.getBoolean ( "org.eclipse.scada.da.server.io.common.trace" ) )
        {
            this.connector.getFilterChain ().addLast ( "logger", new LoggingFilter () );
        }
        setupConnector ( this.connector );
    }

    final ConnectFuture cu = this.connector.connect ( this.address );
    cu.addListener ( new IoFutureListener<ConnectFuture> () {

        @Override
        public void operationComplete ( final ConnectFuture future )
        {
            try
            {
                future.getSession ();
            }
            catch ( final Throwable e )
            {
                StreamBaseDevice.this.fireConnectionFailed ( e );
            }
        }
    } );
}
 
Example 4
Source File: MinaClient.java    From JobX with Apache License 2.0 5 votes vote down vote up
@Override
public Response sentSync(final Request request) throws Exception {
    final ConnectFuture connect = super.getConnect(request);
    if (connect != null && connect.isConnected()) {
        RpcFuture rpcFuture = new RpcFuture(request);
        //写数据
        connect.addListener(new AbstractClient.FutureListener(rpcFuture));
        IoSession session = connect.getSession();
        session.write(request);
        return rpcFuture.get();
    } else {
        throw new IllegalArgumentException("[JobX] MinaRPC channel not active. request id:" + request.getId());
    }
}
 
Example 5
Source File: MinaClient.java    From JobX with Apache License 2.0 5 votes vote down vote up
@Override
public void sentAsync(final Request request, final InvokeCallback callback) throws Exception {
    final ConnectFuture connect = super.getConnect(request);
    if (connect != null && connect.isConnected()) {
        RpcFuture rpcFuture = new RpcFuture(request,callback);
        connect.addListener(new AbstractClient.FutureListener(rpcFuture));
        connect.getSession().write(request);
    } else {
        throw new IllegalArgumentException("[JobX] MinaRPC invokeAsync channel not active. request id:" + request.getId());
    }
}
 
Example 6
Source File: MinaClient.java    From JobX with Apache License 2.0 5 votes vote down vote up
@Override
public void sentOneWay(final Request request) throws Exception {
    ConnectFuture connect = super.getConnect(request);
    if (connect != null && connect.isConnected()) {
        RpcFuture rpcFuture = new RpcFuture(request);
        connect.addListener(new AbstractClient.FutureListener(rpcFuture));
        connect.getSession().write(request);
    } else {
        throw new IllegalArgumentException("[JobX] MinaRPC channel not active. request id:" + request.getId());
    }
}
 
Example 7
Source File: TftpServerProtocolHandler.java    From tftp4j with Apache License 2.0 4 votes vote down vote up
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
    TftpPacket packet = (TftpPacket) message;

    SocketAddress address = session.getRemoteAddress();
    LOG.info("Address is " + address);

    switch (packet.getOpcode()) {
        case RRQ: {
            TftpRequestPacket request = (TftpRequestPacket) packet;
            TftpData source = provider.open(request.getFilename());
            if (source == null) {
                session.write(new TftpErrorPacket(address, TftpErrorCode.FILE_NOT_FOUND), address);
                session.close(false);
            } else {
                final NioDatagramConnector connector = new NioDatagramConnector();
                TftpTransfer transfer = new TftpReadTransfer(address, source, request.getBlockSize());
                TftpTransferProtocolHandler handler = new TftpTransferProtocolHandler(connector, transfer);
                connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TftpProtocolCodecFactory()));
                connector.getFilterChain().addLast("logger-packet", new LoggingFilter("tftp-transfer-packet"));
                connector.setHandler(handler);
                ConnectFuture future = connector.connect(address);
                future.addListener(handler);
            }
            break;
        }
        case WRQ: {
            session.write(new TftpErrorPacket(address, TftpErrorCode.PERMISSION_DENIED), address);
            session.close(false);
            break;
        }
        case ACK: {
            break;
        }
        case DATA: {
            LOG.warn("Unexpected TFTP " + packet.getOpcode() + " packet: " + packet);
            session.write(new TftpErrorPacket(address, TftpErrorCode.ILLEGAL_OPERATION), address);
            session.close(false);
            break;
        }
        case ERROR: {
            TftpErrorPacket error = (TftpErrorPacket) packet;
            LOG.error("Received TFTP error packet: " + error);
            session.close(true);
            break;
        }
    }
}