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

The following examples show how to use org.apache.mina.core.future.IoFutureListener. 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: WriteRequestFilter.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {

    final IoEvent e = new IoEvent(IoEventType.WRITE, session, writeRequest);

    if (queueHandler.accept(this, e)) {
        nextFilter.filterWrite(session, writeRequest);
        WriteFuture writeFuture = writeRequest.getFuture();
        if (writeFuture == null) {
            return;
        }

        // We can track the write request only when it has a future.
        queueHandler.offered(this, e);
        writeFuture.addListener(new IoFutureListener<WriteFuture>() {
            public void operationComplete(WriteFuture future) {
                queueHandler.polled(WriteRequestFilter.this, e);
            }
        });
    }
}
 
Example #2
Source File: ClientBaseConnection.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private synchronized void startConnect ( final InetAddress address )
{
    logger.debug ( "Start connection to {}", address );

    setState ( ConnectionState.CONNECTING, null );
    this.connectFuture = this.connector.connect ( new InetSocketAddress ( address, this.connectionInformation.getSecondaryTarget () ) );
    logger.trace ( "Returned from connect call" );
    this.connectFuture.addListener ( new IoFutureListener<ConnectFuture> () {

        @Override
        public void operationComplete ( final ConnectFuture future )
        {
            handleConnectComplete ( future );
        }
    } );
    logger.trace ( "Future listener registered" );
}
 
Example #3
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 发送对象的底层入口. 可带监听器,并返回WriteFuture
 */
public static WriteFuture write(IoSession session, Object obj, IoFutureListener<?> listener)
{
	if (session.isClosing() || obj == null)
		return null;
	IoFilterChain ifc = session.getFilterChain();
	WriteFuture wf = new DefaultWriteFuture(session);
	if (listener != null)
		wf.addListener(listener);
	DefaultWriteRequest dwr = new DefaultWriteRequest(obj, wf);
	synchronized (session)
	{
		ifc.fireFilterWrite(dwr);
	}
	return wf;
}
 
Example #4
Source File: IoServiceListenerSupport.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Close all the sessions
 * TODO disconnectSessions.
 *
 */
private void disconnectSessions() {
    if (!(service instanceof IoAcceptor)) {
        // We don't disconnect sessions for anything but an Acceptor
        return;
    }

    if (!((IoAcceptor) service).isCloseOnDeactivation()) {
        return;
    }

    Object lock = new Object();
    IoFutureListener<IoFuture> listener = new LockNotifyingListener(lock);

    for (IoSession s : managedSessions.values()) {
        s.close(true).addListener(listener);
    }

    try {
        synchronized (lock) {
            while (!managedSessions.isEmpty()) {
                lock.wait(500);
            }
        }
    } catch (InterruptedException ie) {
        // Ignored
    }
}
 
Example #5
Source File: ClientSideIoHandler.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
@Override
   public void sessionOpened(IoSession session) throws Exception {

	logger.debug("sessionOpened");

	//Store session to MAP
       MINASession mSession = new MINASession(serviceName, session);
       logConfigurator.registerLogger(mSession, serviceName);
	proxyService.addSession(session, mSession);

	connector.connect(remoteAddress).addListener(new IoFutureListener<ConnectFuture>() {
		@Override
           public void operationComplete(ConnectFuture future) {
			try {
				future.getSession().setAttribute(OTHER_IO_SESSION, session);
				session.setAttribute(OTHER_IO_SESSION, future.getSession());
				IoSession session2 = future.getSession();
				session2.resumeRead();
				session2.resumeWrite();
			} catch (RuntimeIoException e) {
				session.close(true);
			} finally {
				session.resumeRead();
				session.resumeWrite();
			}
		}
	});
}
 
Example #6
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 #7
Source File: WebSocketConnection.java    From red5-websocket with Apache License 2.0 5 votes vote down vote up
/**
 * Sends the handshake response.
 * 
 * @param wsResponse
 */
public void sendHandshakeResponse(HandshakeResponse wsResponse) {
    log.debug("Writing handshake on session: {}", session.getId());
    // create write future
    handshakeWriteFuture = session.write(wsResponse);
    handshakeWriteFuture.addListener(new IoFutureListener<WriteFuture>() {

        @Override
        public void operationComplete(WriteFuture future) {
            IoSession sess = future.getSession();
            if (future.isWritten()) {
                // handshake is finished
                log.debug("Handshake write success! {}", sess.getId());
                // set completed flag
                sess.setAttribute(Constants.HANDSHAKE_COMPLETE);
                // set connected state on ws connection
                if (connected.compareAndSet(false, true)) {
                    try {
                        // send queued packets
                        queue.forEach(entry -> {
                            sess.write(entry);
                            queue.remove(entry);
                        });
                    } catch (Exception e) {
                        log.warn("Exception draining queued packets on session: {}", sess.getId(), e);
                    }
                }
            } else {
                log.warn("Handshake write failed from: {} to: {}", sess.getLocalAddress(), sess.getRemoteAddress());
            }
        }

    });
}
 
Example #8
Source File: DefaultWriteRequest.java    From jane with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public WriteFuture removeListener(IoFutureListener<?> listener) {
	throw new IllegalStateException("you can't remove a listener from a dummy future");
}
 
Example #9
Source File: DefaultWriteRequest.java    From jane with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public WriteFuture addListener(IoFutureListener<?> listener) {
	throw new IllegalStateException("you can't add a listener to a dummy future");
}
 
Example #10
Source File: ClientPool.java    From gameserver with Apache License 2.0 4 votes vote down vote up
@Override
public void setStatListener(IoFutureListener<IoFuture> statListener) {
	this.statListener = statListener;
}
 
Example #11
Source File: ClientPool.java    From gameserver with Apache License 2.0 4 votes vote down vote up
@Override
public IoFutureListener<IoFuture> getStatListener() {
	return statListener;
}
 
Example #12
Source File: SimpleClient.java    From gameserver with Apache License 2.0 4 votes vote down vote up
@Override
public void setStatListener(IoFutureListener<IoFuture> statListener) {
	this.statListener = statListener;
}
 
Example #13
Source File: SimpleClient.java    From gameserver with Apache License 2.0 4 votes vote down vote up
@Override
public IoFutureListener<IoFuture> getStatListener() {
	return statListener;
}
 
Example #14
Source File: HttpGameHandler.java    From gameserver with Apache License 2.0 4 votes vote down vote up
/**
	 * Receive the http request. Check its request uri.
	 */
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		HttpMessage httpMessage = (HttpMessage)message;
		try {
			if ( httpMessage.getRequestUri() != null ) {
				String variable = null;
				if ( httpMessage.getRequestUri().indexOf('?') >= 0 ) {
					variable = substring(httpMessage.getRequestUri(), "?", null);
				}
				String uri = substring(httpMessage.getRequestUri(), null, HTTP);
				logger.debug("Request uri: {}, variable: {}", uri, variable);
				byte[] configData = NOT_FOUND_MSG;
				if ( uri.startsWith("/verifyemail") ) {
					VerifyEmailProcessor.getInstance().process(uri, variable, httpMessage);
				} else if ( uri.startsWith("/offlinepush") ) {
					Collection<String> offlineMessages = MailMessageManager.getInstance().
							listSimplePushMessages(variable);
					if ( offlineMessages != null ) {
						StringBuilder buf = new StringBuilder(200);
						for ( String offline : offlineMessages ) {
							buf.append(offline).append('\n');
						}
						httpMessage.setResponseContentType(HttpGameHandler.CONTENT_TYPE);
						httpMessage.setResponseContent(buf.toString().getBytes(Constant.charset));
					} else {
						httpMessage.setResponseContentType(HttpGameHandler.CONTENT_TYPE);
						httpMessage.setResponseContent(new byte[0]);
					}
				} else if ( uri.startsWith("/cmcccharge") ) {
					httpMessage = CMCCCharge.cmccChargeNotif(httpMessage, variable);
				} else if ( uri.startsWith("/dangle") ) {
					httpMessage = DangleCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/xiaomi") ) {
					httpMessage = XiaomiCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/changyou") ) {
					httpMessage = ChangyouCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/lianxiang") ) {
					httpMessage = LegendCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/xinqihd") ) {
					httpMessage = XinqihdCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/baoruan") ) {
					httpMessage = BaoruanCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/uc") ) {
					httpMessage = UCCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/huawei") ) {
					httpMessage = HuaweiCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/kupai") ) {
					httpMessage = KupaiCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/91") ) {
					httpMessage = Wanglong91Charge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/oppo") ) {
					httpMessage = OppoCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/shenzhoufu") ) {
					httpMessage = ShenZhouFuCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/yeepay") ) {
					httpMessage = YeepayCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/anzhi") ) {
					httpMessage = AnzhiCharge.chargeNotify(httpMessage, variable);
				} else if ( uri.startsWith("/mobage/tmptoken") ) {
					httpMessage = MobageCharge.getInstance().processTmpToken(httpMessage, variable);
				} else if ( uri.startsWith("/mobage/accesstoken") ) {
					httpMessage = MobageCharge.getInstance().processAccessToken(httpMessage, variable);
				} else if ( uri.startsWith("/mobage/commit") ) {
					httpMessage = MobageCharge.getInstance().processTransaction(httpMessage, variable);
				} else {
					httpMessage.setResponseContent(configData);
				}
			} else {
				httpMessage.setResponseContent(NOT_FOUND_MSG);
			}
		} catch (Throwable e1) {
			e1.printStackTrace();
		}
		WriteFuture future = session.write(httpMessage);
		// We can wait for the client to close the socket.
		future.addListener(IoFutureListener.CLOSE);
//		GameContext.getInstance().writeResponse(session, httpMessage);
	}
 
Example #15
Source File: DefaultWriteRequest.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public WriteFuture removeListener(IoFutureListener<?> listener) {
    throw new IllegalStateException("You can't add a listener to a dummy future.");
}
 
Example #16
Source File: DefaultWriteRequest.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public WriteFuture addListener(IoFutureListener<?> listener) {
    throw new IllegalStateException("You can't add a listener to a dummy future.");
}
 
Example #17
Source File: Client.java    From gameserver with Apache License 2.0 2 votes vote down vote up
/**
 * @param statListener the statListener to set
 */
public abstract void setStatListener(IoFutureListener<IoFuture> statListener);
 
Example #18
Source File: Client.java    From gameserver with Apache License 2.0 2 votes vote down vote up
/**
 * @return the statListener
 */
public abstract IoFutureListener<IoFuture> getStatListener();