用mina写了一个Memcache服务端的通讯模拟程序
我用mina写了一个Memcache服务端的通讯模拟程序使用的是 mina-core-2.0.0-M1.jar
一共3各类
HypTestServerHandler
MinaUtilRun 启动类
TimeServerHandler
欢迎和我交流着方面的问题。
public class TimeServerHandler extends IoHandlerAdapter {
public void exceptionCaught(IoSession session, Throwable t)
throws Exception { // 出现异常的时候调用.
t.printStackTrace();
session.close();
}
public void messageReceived(IoSession session, Object msg) throws Exception { // 接收客户端新的消息的时候调用.
String str = msg.toString();
if (str.trim().equalsIgnoreCase("quit")) {
session.close();
return;
}
Date date = new Date();
session.write(date.toString());
System.out.println("Message written...");
}
public void sessionCreated(IoSession session) throws Exception { // 当一个客户端连接到服务器的时候被调用.
System.out.println("Session created...");
if (session.getTransportType() == TransportType.SOCKET)
((SocketSessionConfig) session.getConfig())
.setReceiveBufferSize(2048);
session.setIdleTime(IdleStatus.BOTH_IDLE, 10);
}
}
public class MinaUtilRun {
private static final int PORT = 11211;
public static void test() throws Exception {
ByteBuffer.setUseDirectBuffers(false);
ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
IoAcceptor acceptor = new SocketAcceptor();
SocketAcceptorConfig cfg = new SocketAcceptorConfig(); //创建一个与SocketAcceptor相关联的配置对象.
cfg.getSessionConfig().setReuseAddress( true );
cfg.getFilterChain().addLast( "logger", new LoggingFilter() );
cfg.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new HypLineCodecFactory( Charset.forName( "GBK" ))));
//acceptor.bind( new InetSocketAddress(PORT), new TimeServerHandler(), cfg);//绑定端口,处理对象和配置对象.
acceptor.bind( new InetSocketAddress(PORT), new HypTestServerHandler(), cfg);//绑定端口,处理对象和配置对象.
System.out.println("MINA Time server started.");
}
/**
* @param args
*/
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class HypTestServerHandler extends IoHandlerAdapter {
public void exceptionCaught(IoSession session, Throwable t)
throws Exception { // 出现异常的时候调用.
t.printStackTrace();
session.close();
}
public void messageReceived(IoSession session, Object msg) throws Exception { // 接收客户端新的消息的时候调用.
String stat = (String) session.getAttribute("stat");
String str = msg.toString();
JavaUtil.debugPrint("receive:" + str);
if(stat.equals("GetCommand")){
if(str.equals("version")){
session.write("VERSION 1.2.2");
JavaUtil.debugPrint("response verison .... VERSION 1.2.2");
return;
}
if(str.startsWith("set ")){
String[] parts = str.split(" ");
int dataSize = Integer.parseInt(parts);
JavaUtil.debugPrint("dataSize:" + dataSize);
session.setAttribute("dataSize", dataSize);
session.setAttribute("readSize", 0);
session.setAttribute("stat", "readData");
session.setAttribute("dataBuf", new StringBuilder());
session.setAttribute("currentCmd", parts);
}
}else if(stat.equals("readData")){
int dataSize = (Integer)session.getAttribute("dataSize");
int readSize = (Integer)session.getAttribute("readSize");
StringBuilder buf = (StringBuilder)session.getAttribute("dataBuf");
if(buf.length() > 0){
buf.append("\r\n");
}
buf.append(str);
readSize += str.length() + 2;
session.setAttribute("readSize", readSize);
JavaUtil.debugPrint("read data:" + readSize + "/" +dataSize );
if(readSize == dataSize){
//处理命令
String[] parts = (String[])session.getAttribute("currentCmd");
System.out.println("buf.length():" + buf.length());
System.out.println(buf.toString());
session.write("STORED");
session.setAttribute("stat", "GetCommand");
}
}
//Date date = new Date();
//session.write(date.toString());
System.out.println("Message written...");
}
public void sessionCreated(IoSession session) throws Exception { // 当一个客户端连接到服务器的时候被调用.
System.out.println("Session created...");
if (session.getTransportType() == TransportType.SOCKET)
((SocketSessionConfig) session.getConfig())
.setReceiveBufferSize(2048);
session.setIdleTime(IdleStatus.BOTH_IDLE, 10);
session.setAttribute("stat", "GetCommand");
}
}
页:
[1]