Yet another nio framework for java

Sat Oct 11 14:25:00 CST 2008发表于BlogJava-首页技术区
项目名称:Yanf4j(Yet another nio framework for java)
项目地址: http://code.google.com/p/yanf4j/,当前版本0.30-beta1
协议:Apache License, Version 2.0
简单描述:
    有这么多nio框架了,为什么要another?重复造轮子也罢,这框架脱胎于一个服务器项目的网络层代码,期间参考了cindy、grizzly等nio框架的实现,加上自己的一些心得体会实现的。特点是简单、小巧、超轻量级。项目没有多大野心,目标是高效、简单地实现非阻塞模式的Server(TCP、UDP)并且保证不错的性能要求,不提供客户端API 和阻塞模式。如果你的项目需要实现一个socket server并且不希望用太重量级的框架,yanf4j是个不错的选择。

    例子,在source archive中带有例子,这里描述下tcp和udp的echo server的实现。
 一、先看TCP的Echo Server
1、实现处理handler,继承HandlerAdapter类,实现相应的回调方法,这与其他nio框架没啥区别:
import com.google.code.yanf4j.nio.Dispatcher;
import com.google.code.yanf4j.nio.Session;
import com.google.code.yanf4j.nio.impl.HandlerAdapter;
import com.google.code.yanf4j.nio.util.DispatcherFactory;

public class EchoHandler extends HandlerAdapter<String> {
    static boolean DEBUG = false;

    Dispatcher dispatcher = DispatcherFactory.newDispatcher(4);

    @Override
    public void onException(Session session, Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void onMessageSent(Session session, String t) {
        if (DEBUG)
            System.out.println("sent " + t + " to "
                    + session.getRemoteSocketAddress());
    }

    @Override
    public void onSessionStarted(Session session) {
        if (DEBUG)
            System.out.println("session started");
        session.setUseBlockingRead(true);
        session.setUseBlockingWrite(false);
    }

    public void onSessionCreated(Session session) {
        if (DEBUG)
            System.out.println(session.getRemoteSocketAddress().toString()
                    + " connected");
    }

    public void onSessionClosed(Session session) {
        if (DEBUG)
            System.out.println(session.getRemoteSocketAddress().toString()
                    + " disconnected");

    }

    public void onReceive(final Session session, final String msg) {
        if (DEBUG)
            System.out.println("recv:" + msg);
        if (msg != null)
            dispatcher.dispatch(new Runnable() {
                public void run() {

                    if (msg.equals("q"))
                        session.close();
                    session.send(msg);
                }
            });
    }

}


2、实现EchoServer,核心是TCPController类的使用:

        Configuration configuration = new Configuration();
        configuration.setStatisticsServer(true);
        configuration.setTcpSessionReadBufferSize(256 * 1024); // 设置读的缓冲区大小
        AbstractController controller = new TCPController(configuration,
                new StringCodecFactory());
        controller.setPort(8080); // 设置端口
        controller.setReadThreadCount(1); // 设置读线程数,通常为1
        controller.setReceiveBufferSize(16 * 1024); // 设置socket接收缓冲区大小
        controller.setReuseAddress(false); // 设置是否重用端口
        controller.setHandler(new EchoHandler()); // 设置handler
        controller.setHandleReadWriteConcurrently(true); // 设置是否允许读写并发处理
        controller.addStateListener(new ServerStateListener());
        controller.start();

Configuration 默认会在classpath查找yanf4j.properties属性文件,用于配置服务器属性,然而,你也看到,可以编码设置这些属性,具体参考wiki。

3、然后?没然后了,一个TCP的echo server已经搞定了,你可以telnet到8080端口试试了。

二、UDP的Echo server
1、handler,可以复用前面的EchoHandler
2、UDP的核心类是UDPController:

        Configuration configuration = new Configuration();
        configuration.setTcpPort(8090);
        configuration.setTcpReuseAddress(false);
        configuration.setStatisticsServer(true);
        configuration.setTcpNoDelay(true);
        configuration.setTcpReadThreadCount(1);
        configuration.setTcpRecvBufferSize(16 * 1024);
        UDPController controller = new UDPController(configuration);
        controller.setMaxDatagramPacketLength(1024);
        controller.setHandler(new EchoHandler());
        controller.start();

  更多细节,请参考项目主页上的wiki。




dennis 2008-10-11 14:25 发表评论
阅读全文...
 
本站相关内容:(RSS)

IEContentLoaded: Yet another DOMContentLoaded

Ajaxian

Hedger Wang has a different solution for the DOMContentLoaded issue that Dean Edwards and YUI also solves. The solution created an element and tries to scroll in it. If an error is thrown, the DOM isn't loaded yet, so we wait and try again later. PLAIN TEXT JAVASCRIPT:   (function (){         //check IE's proprietary DOM members     [...]

Yet another presentation submission

Looking at the Feedback I got for other Presentations Proposals I thought I indeed should submit general presentation focusing on Web Application performance tuning and explaining how you analyze performance and why do you do it this way, so here it is: Performance Analysis of MySQL powered Web Applications In this session we'll go beyond [...]

Yet another webcam software...

Yawcam is a webcam software for windows written in java. The main ideas for Yawcam are to keep it simple and easy to use but to include all the usual features. Yawcam is completely free to use

yet another stamp….

and a really great grande flatwhite (3.30pm) from Starbucks… made by Paul :) [IMG Coffee Rating][IMG Coffee Rating][IMG Coffee Rating][IMG Coffee Rating] by 4.30pm we were sitting Tags: Java Food, Coffee
互联网相关内容:
IEContentLoaded: Yet another DOMContentLoaded (2007年09月26日)
Yet another presentation submission (2007年10月31日)
Yet another webcam software... (2007年11月10日)
yet another stamp…. (2007年11月23日)
Yet Another Blog... (2007年12月12日)
Yet another closure proposal (2008年01月16日)
Yet another closure proposal (2008年01月16日)
Yet another closure proposal (2008年01月16日)
Yet Another Closures Proposal (2008年01月17日)