org.apache.mina.core.service.IoAcceptor Java Examples

The following examples show how to use org.apache.mina.core.service.IoAcceptor. 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: MinaTimeServer.java    From frameworkAggregate with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {

		IoAcceptor acceptor = new NioSocketAcceptor();
		// 这个过滤器用来记录所有的信息,比如创建session(会话),接收消息,发送消息,关闭会话等
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		// 用来转换二进制或协议的专用数据到消息对象中
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));

		// 实时处理客户端的连接和请求
		acceptor.setHandler(new TimeServerHandler());
		acceptor.getSessionConfig().setReadBufferSize(2048);
		// 方法将定时调用一次会话,保持空闲状态。来设定时间间隔。
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		acceptor.bind(new InetSocketAddress(PORT));
	}
 
Example #2
Source File: MinaTimeServer.java    From java-tutorial with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    // 创建服务器端的监听器对象
    IoAcceptor acceptor = new NioSocketAcceptor();
    // 增加日志过滤器:用于日志存储
    acceptor.getFilterChain().addLast("logger", new LoggingFilter());
    // 增加消息编码过滤器,采用UTF-8编码
    acceptor.getFilterChain().addLast("codec",
            new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
    // 设置具体的事物逻辑处理器
    acceptor.setHandler(new TimeServerHandler());
    // 设置IoSession的一些属性
    acceptor.getSessionConfig().setReadBufferSize(2048);
    acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
    // 设置服务器监听的端口
    acceptor.bind(new InetSocketAddress(PORT));
}
 
Example #3
Source File: CalculatorServer.java    From mina with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
        IoAcceptor acceptor = new NioSocketAcceptor();
//        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, IDELTIMEOUT);

        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.getFilterChain().addLast("myfliter", new MyFilter());
        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new CommandCodecFactory("UTF-8")));

        KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();
        KeepAliveFilter kaf = new KeepAliveFilter(kamfi, IdleStatus.BOTH_IDLE);
        /** 是否回发 */
        kaf.setForwardEvent(true);
        acceptor.getFilterChain().addLast("heart", kaf);

        acceptor.setHandler(new CalculatorHandler());
        acceptor.bind(new InetSocketAddress(PORT));

        log.debug("socket通信服务端已启动,端口是" + PORT);
    }
 
Example #4
Source File: SslFilter.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * An extended toString() method for sessions.
 * If the SSL handshake is not yet completed, we will print (ssl) in small caps.
 * Once it's completed, we will use SSL capitalized.
 */
public static String getSessionInfo(IoSession session) {
	StringBuilder sb = new StringBuilder();
	sb.append(session.getService() instanceof IoAcceptor ? "session server" : "session client");
	sb.append('[').append(session.getId()).append(']');

	SslHandler sslHandler = (SslHandler)session.getAttribute(SSL_HANDLER);
	if (sslHandler == null)
		sb.append("(no sslEngine)");
	else if (sslHandler.isOutboundDone())
		sb.append("(done)");
	else
		sb.append(sslHandler.isHandshakeComplete() ? "(SSL)" : "(SSL...)");

	return sb.toString();
}
 
Example #5
Source File: SslFilter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * An extended toString() method for sessions. If the SSL handshake
 * is not yet completed, we will print (ssl) in small caps. Once it's
 * completed, we will use SSL capitalized.
 */
/* no qualifier */String getSessionInfo(IoSession session) {
    StringBuilder sb = new StringBuilder();

    if (session.getService() instanceof IoAcceptor) {
        sb.append("Session Server");

    } else {
        sb.append("Session Client");
    }

    sb.append('[').append(session.getId()).append(']');

    SslHandler handler = (SslHandler) session.getAttribute(SSL_HANDLER);

    if (handler == null) {
        sb.append("(no sslEngine)");
    } else if (isSslStarted(session)) {
        if (handler.isHandshakeComplete()) {
            sb.append("(SSL)");
        } else {
            sb.append("(ssl...)");
        }
    }

    return sb.toString();
}
 
Example #6
Source File: AbstractIoSession.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public SocketAddress getServiceAddress() {
    IoService service = getService();
    if (service instanceof IoAcceptor) {
        return ((IoAcceptor) service).getLocalAddress();
    }

    return getRemoteAddress();
}
 
Example #7
Source File: NetworkHelper.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public static Set<ConnectionInformation> getLocalInterfaces ( final IoAcceptor acceptor, final ConnectionInformation connectionInformation ) throws SocketException
{
    final Set<ConnectionInformation> result = new HashSet<ConnectionInformation> ();

    for ( final InetSocketAddress address : getLocalAddresses ( acceptor ) )
    {
        result.add ( cloneFill ( address.getAddress (), connectionInformation ) );
    }

    return result;
}
 
Example #8
Source File: NetworkHelper.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public static Set<InetSocketAddress> getLocalAddresses ( final IoAcceptor acceptor ) throws SocketException
{
    final Set<InetSocketAddress> result = new HashSet<InetSocketAddress> ();

    for ( final SocketAddress address : acceptor.getLocalAddresses () )
    {
        logger.info ( "Bound to: {}", address );
        if ( ! ( address instanceof InetSocketAddress ) )
        {
            continue;
        }

        final InetSocketAddress socketAddress = (InetSocketAddress)address;
        if ( socketAddress.getAddress ().isAnyLocalAddress () )
        {
            final int port = socketAddress.getPort ();

            final Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces ();
            while ( interfaces.hasMoreElements () )
            {
                final NetworkInterface networkInterface = interfaces.nextElement ();

                for ( final InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses () )
                {
                    result.add ( new InetSocketAddress ( interfaceAddress.getAddress (), port ) );
                }
            }
        }
        else
        {
            result.add ( socketAddress );
        }

    }

    return result;
}
 
Example #9
Source File: MinaTimeServer.java    From javabase with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
	IoAcceptor acceptor = new NioSocketAcceptor();
	acceptor.getFilterChain().addLast("logger", new LoggingFilter());
	acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));// 指定编码过滤器
	acceptor.setHandler(new TimeServerHandler());// 指定业务逻辑处理器

	//读写 通道均在3 秒内无任何操作就进入空闲状态
	acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 3);

	acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));// 设置端口号
	acceptor.bind();// 启动监听
}
 
Example #10
Source File: HelloTcpServer.java    From mina-examples with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	IoAcceptor acceptor = new NioSocketAcceptor(); //TCP Acceptor
	acceptor.getFilterChain().addLast("logging", new LoggingFilter());
	acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
	acceptor.getFilterChain().addLast("mdc", new MdcInjectionFilter());
	acceptor.setHandler(new HelloServerHandler());
	acceptor.getSessionConfig().setReadBufferSize(2048);
	acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
	acceptor.bind(new InetSocketAddress(PORT));
}