Search in sources :

Example 21 with ProtocolCodecFilter

use of org.apache.mina.filter.codec.ProtocolCodecFilter in project dubbo by alibaba.

the class MinaClient method doOpen.

@Override
protected void doOpen() throws Throwable {
    connectorKey = getUrl().toFullString();
    SocketConnector c = CONNECTORS.get(connectorKey);
    if (c != null) {
        connector = c;
    } else {
        // set thread pool.
        connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
        // config
        SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
        cfg.setThreadModel(ThreadModel.MANUAL);
        cfg.getSessionConfig().setTcpNoDelay(true);
        cfg.getSessionConfig().setKeepAlive(true);
        int timeout = getConnectTimeout();
        cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
        // set codec.
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
        CONNECTORS.put(connectorKey, connector);
    }
}
Also used : NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) SocketConnectorConfig(org.apache.mina.transport.socket.nio.SocketConnectorConfig) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) SocketConnector(org.apache.mina.transport.socket.nio.SocketConnector)

Example 22 with ProtocolCodecFilter

use of org.apache.mina.filter.codec.ProtocolCodecFilter in project vert.x by eclipse.

the class FakeDNSServer method start.

@Override
public void start() throws IOException {
    UdpTransport transport = new UdpTransport(ipAddress, port);
    setTransports(transport);
    acceptor = transport.getAcceptor();
    acceptor.setHandler(new DnsProtocolHandler(this, new RecordStore() {

        @Override
        public Set<ResourceRecord> getRecords(QuestionRecord question) throws DnsException {
            RecordStore actual = store;
            if (actual == null) {
                return Collections.emptySet();
            } else {
                return actual.getRecords(question);
            }
        }
    }) {

        @Override
        public void sessionCreated(IoSession session) throws Exception {
            // Use our own codec to support AAAA testing
            session.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new TestDnsProtocolUdpCodecFactory()));
        }

        @Override
        public void messageReceived(IoSession session, Object message) {
            if (message instanceof DnsMessage) {
                synchronized (FakeDNSServer.this) {
                    currentMessage.add((DnsMessage) message);
                }
            }
            super.messageReceived(session, message);
        }
    });
    // Allow the port to be reused even if the socket is in TIME_WAIT state
    ((DatagramSessionConfig) acceptor.getSessionConfig()).setReuseAddress(true);
    // Start the listener
    acceptor.bind();
}
Also used : UdpTransport(org.apache.directory.server.protocol.shared.transport.UdpTransport) QuestionRecord(org.apache.directory.server.dns.messages.QuestionRecord) DatagramSessionConfig(org.apache.mina.transport.socket.DatagramSessionConfig) DnsMessage(org.apache.directory.server.dns.messages.DnsMessage) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) ResourceRecord(org.apache.directory.server.dns.messages.ResourceRecord) DnsProtocolHandler(org.apache.directory.server.dns.protocol.DnsProtocolHandler) DnsException(org.apache.directory.server.dns.DnsException) IOException(java.io.IOException) RecordStore(org.apache.directory.server.dns.store.RecordStore) IoSession(org.apache.mina.core.session.IoSession)

Example 23 with ProtocolCodecFilter

use of org.apache.mina.filter.codec.ProtocolCodecFilter in project pancm_project by xuwujing.

the class MinaServer method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
/* 启动此类 提示服务端运行成功后
	 * Windows 命令 输入  telnet 127.0.0.1 3305
	 * 然后输入消息  message
	 * 消息为bye的时候关闭连接
	 * */
public static void main(String[] args) {
    IoAcceptor ia = null;
    try {
        // 创建一个非堵塞的server端Socket
        ia = new NioSocketAcceptor();
        // 创建 协议编码解码过滤器ProtocolCodecFilter
        // ProtocolCodecFilter pf=new ProtocolCodecFilter(new TextLineCodecFactory(Charset
        // .forName("UTF-8"),
        // LineDelimiter.WINDOWS.getValue(),
        // LineDelimiter.WINDOWS.getValue()));
        // 设置端口
        InetSocketAddress pt = new InetSocketAddress(PORT);
        // 设置过滤器(使用Mina提供的文本换行符编解码器)
        ia.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
        // 设置读取数据的缓存区大小
        ia.getSessionConfig().setReadBufferSize(2048);
        // 读写通道10秒内无操作进入空闲状态
        ia.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
        // 绑定逻辑处理器
        ia.setHandler(new MinaServerHandler());
        // 绑定端口
        ia.bind(pt);
        logger.info("服务端启动成功...端口号为:" + PORT);
    } catch (Exception e) {
        logger.error("服务器的异常..." + e);
        e.printStackTrace();
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ObjectSerializationCodecFactory(org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory) IoAcceptor(org.apache.mina.core.service.IoAcceptor) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) NioSocketAcceptor(org.apache.mina.transport.socket.nio.NioSocketAcceptor)

Example 24 with ProtocolCodecFilter

use of org.apache.mina.filter.codec.ProtocolCodecFilter in project zm-mailbox by Zimbra.

the class NioServer method start.

/**
 * Starts the server. Binds the server port and starts the connection
 * handler. Optionally adds an SSLFilter if ssl is enabled.
 *
 * @throws IOException if an I/O error occured while starting the server
 */
@Override
public void start() {
    ServerConfig sc = getConfig();
    DefaultIoFilterChainBuilder fc = acceptor.getFilterChain();
    if (sc.isSslEnabled()) {
        fc.addFirst("ssl", newSSLFilter());
    }
    fc.addLast("executer", executorFilter);
    fc.addLast("logger", new NioLoggingFilter(this, false));
    fc.addLast("codec", new ProtocolCodecFilter(getProtocolCodecFactory()));
    for (IoFilter filter : FILTERS.get(getClass())) {
        // insert custom filters
        fc.addLast(filter.getClass().getName(), filter);
    }
    acceptor.getSessionConfig().setBothIdleTime(sc.getMaxIdleTime());
    acceptor.getSessionConfig().setWriteTimeout(sc.getWriteTimeout());
    acceptor.setHandler(new NioHandlerDispatcher(this));
    try {
        acceptor.bind();
    } catch (Throwable e) {
        Zimbra.halt(getName() + " failed to start", e);
    }
    getLog().info("Starting %s on %s", getName(), acceptor.getLocalAddress());
}
Also used : DefaultIoFilterChainBuilder(org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder) IoFilter(org.apache.mina.core.filterchain.IoFilter) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter)

Example 25 with ProtocolCodecFilter

use of org.apache.mina.filter.codec.ProtocolCodecFilter in project MtgDesktopCompanion by nicho92.

the class ConsoleServer method start.

@Override
public void start() throws IOException {
    acceptor = new NioSocketAcceptor();
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName(getString("ENCODING")))));
    acceptor.getSessionConfig().setReadBufferSize(Integer.parseInt(getString("BUFFER-SIZE")));
    acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, Integer.parseInt(getString("IDLE-TIME")));
    acceptor.setHandler(new MTGConsoleHandler());
    acceptor.bind(new InetSocketAddress(Integer.parseInt(getString("SERVER-PORT"))));
    logger.info("Server started on port " + getString("SERVER-PORT"));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) NioSocketAcceptor(org.apache.mina.transport.socket.nio.NioSocketAcceptor) TextLineCodecFactory(org.apache.mina.filter.codec.textline.TextLineCodecFactory) MTGConsoleHandler(org.magic.console.MTGConsoleHandler)

Aggregations

ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)27 InetSocketAddress (java.net.InetSocketAddress)15 NioSocketAcceptor (org.apache.mina.transport.socket.nio.NioSocketAcceptor)9 IOException (java.io.IOException)6 IoSession (org.apache.mina.core.session.IoSession)4 TextLineCodecFactory (org.apache.mina.filter.codec.textline.TextLineCodecFactory)4 DnsProtocolHandler (org.apache.directory.server.dns.protocol.DnsProtocolHandler)3 UdpTransport (org.apache.directory.server.protocol.shared.transport.UdpTransport)3 ObjectSerializationCodecFactory (org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory)3 DatagramSessionConfig (org.apache.mina.transport.socket.DatagramSessionConfig)3 SocketAcceptor (org.apache.mina.transport.socket.nio.SocketAcceptor)3 SocketAcceptorConfig (org.apache.mina.transport.socket.nio.SocketAcceptorConfig)3 NamedThreadFactory (com.alibaba.dubbo.common.utils.NamedThreadFactory)2 Charset (java.nio.charset.Charset)2 DnsException (org.apache.directory.server.dns.DnsException)2 NamedThreadFactory (org.apache.dubbo.common.utils.NamedThreadFactory)2 DefaultIoFilterChainBuilder (org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder)2 ConnectFuture (org.apache.mina.core.future.ConnectFuture)2 IoAcceptor (org.apache.mina.core.service.IoAcceptor)2 IoConnector (org.apache.mina.core.service.IoConnector)2