MINA 开发入门实例--用于FLEX的843端口

 最近由于公司业务需要,开始学习mima框架 ,贴上一个简单的入门实例.

     该实例主要实现的功能是:

开启一个843监听端口,其作用是为FLEX的安全沙箱机制提供策略文件(可不管这是干什么的)。


WebFlex843Server类

package com.demo.service843;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class WebFlex843Server {

	private final static Logger logger = Logger.getLogger(WebFlex843Server.class);
	//端口号		
	private final static int PORT = 843;

	public void startWf843Server() {

		// 非阻塞的
		IoAcceptor acceptor = new NioSocketAcceptor();

		// 过滤器,客服端上传上来的信息必须以">"结尾;且服务器端下发的信息末尾自动加上">"
		acceptor.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset
						.forName("UTF-8"), ">", ">")));

		// 绑定逻辑处理器
		acceptor.setHandler(new WebFlex843ServerHandler());

		// 设置缓冲区大小和读写通道10秒内无操作进入空闲状态
		acceptor.getSessionConfig().setReadBufferSize(2048);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);

		// 绑定端口
		try {
			acceptor.bind(new InetSocketAddress(PORT));
		} catch (IOException e) {
			logger.info("异常:WebFlex服务器绑定端口号:" + PORT + ",出现异常");
			return;
		}

		logger.info("WebFlxe843服务器已启动,监听端口" + PORT + " ...");
	}	

FlexServerHandler 类

package com.demo.service843;

import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class WebFlex843ServerHandler extends IoHandlerAdapter {

	private final static Logger logger = Logger
			.getLogger(WebFlex843ServerHandler.class);

	/**
	 * AS要接收\0结尾的字符串
	 */
	private static final String SECURITY_FILE = "<?xml version=\"1.0\"?>"
			+ "<cross-domain-policy>"
			+ "<site-control permitted-cross-domain-policies=\"all\"/>"
			+ "<allow-access-from domain=\"*\" to-ports=\"*\" />"
			+ "</cross-domain-policy>\0";

	@Override
	public void sessionOpened(IoSession session) {
		logger.info("发现用户登录843服务器");
	}

	/**
	 * Trap exceptions.
	 */
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		cause.printStackTrace();
		session.close(true);
		if (cause.getMessage().indexOf("远程主机强迫关闭") >= 0) {
			logger.info("侦测到:本地Web-Flex的Client的Session关闭,自动关闭其连接的session");
		}
	}

	/**
	 * If the message is 'quit', we exit by closing the session. Otherwise, we
	 * return the current date.
	 */
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		String msg = message.toString();
		
		if (msg.length() < 0)
			return;

		if (msg.contains("<policy-file-request/>")) {
			session.write(SECURITY_FILE);
		} else
			session.write(SECURITY_FILE);

	}

	/**
	 * On idle, we just write a message on the console
	 */
	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		System.out.println("IDLE " + session.getIdleCount(status));
	}

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值