fried cake server ----我的油炸糕(1)

fried cake server(油炸糕) 是我前一段时间比较闲地时候,自己动手写的一个服务器,目标是做成类似于jboss一样的application server。不过目前做了几个月,还只是个不支持tagLib的jsp容器而已,好在支持jndi和datasource,加上不完善的jta……勉强能看得过……
没办法,上班也忙,下班也忙……断断续续写了好久,也仅仅是个雏形,一个人的力量毕竟是有限的
有兴趣的朋友们可以访问以下网址:
https://sourceforge.net/projects/freadcakeserver/
cvs 地址host:  freadcakeserver.cvs.sourceforge.net
       path:/cvsroot/freadcakeserver
可以匿名访问
在今后的日子里,我会逐渐的把代码贴出来,一边反省,一边重构……当前版本alpha build 1004,完善中……
今天是第一贴,放出两个核心类server.MyServer和server.SocketProcess
其实就是一个多路通道加上一个执行线程。
本来应该先发个设计图,可是类图经常变化,以后再发吧
/**
 * 整个服务器的核心,本身也是一个静态多路服务器的骨架
 * @author 刘宇航
 */
package server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Set;

import util.Constant;

public class MyServer
{
	public static final int setTimeOut = 8000;

	private  int port = 80;//默认端口

	Selector selector;
	ByteBuffer buffer = ByteBuffer.allocate(1024 * 8);

	/**
	 * 初始化一个server
	 * @throws Exception
	 */
	public  MyServer() throws Exception
	{
		init();
	}
	/**
	 * 实际初始化一个server
	 * 包括webLoader加载、解析appRuntime、注册通道等工作
	 * @throws Exception
	 */
	public void init() throws Exception
	{
		WebLoader webLoader = WebLoader.getInstance();
		webLoader.loadAll();// 这里已经隐含着将appRuntime初始化了
		port = (Integer)AppRunTime.getInstance().getProperty(Constant.SERVER_PORT); 
		ServerSocketChannel channel = ServerSocketChannel.open();
		channel.configureBlocking(false);
		channel.socket().bind(new InetSocketAddress(port));
		selector = Selector.open();
		channel.register(selector, SelectionKey.OP_ACCEPT);
	}

	/**
	 * 请不要误会,这并不是一个多线程的run()方法
	 * 另外,server只接受isAcceptable为真的请求
	 * 单向的消息请求暂不提供
	 * @throws Exception
	 */
	public void run() throws Exception
	{
		while (true)
		{
			int num = selector.select();
			if (num != 0)
			{
				Set<SelectionKey> set = selector.selectedKeys();
				Iterator<SelectionKey> it = set.iterator();

				while (it.hasNext())
				{
					SelectionKey key = it.next();

					if (key.isAcceptable())
					{
						accept((ServerSocketChannel) key.channel());
					}
					it.remove();
				}
			}
		}

	}
	
	
    /**
     * 对于isAcceptable的请求启动一个SoketProcess线程
     * @param serverSocket
     * @throws IOException
     */
	protected void accept(ServerSocketChannel serverSocket) throws IOException
	{
		SocketProcess pc = new SocketProcess(serverSocket);
		/*多线程模式---发布用*/
		pc.start();
		/*单线程模式---开发用*/
		//pc.run();
	}

    /**
     * 启动命令,发布版里使用bootstrap项目调用这个main方法,参数设置也挪至bootstrap项目中
      * @param args 暂无 待扩充
      * @throws Exception
     */
	public static void main(String args[]) throws Exception
	{
		long time = Calendar.getInstance().getTimeInMillis();
		MyServer server = new MyServer();
		time = Calendar.getInstance().getTimeInMillis()-time;
		System.out.println("服务器这正在监听端口"+AppRunTime.getInstance().getProperty(Constant.SERVER_PORT)+".....");
		System.out.println("启动成功,总耗时"+time+"毫秒.....");
		server.run();
	}
}

// end


package server;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import project.ProjectInfo;
import util.Constant;
import util.HttpError;
import util.ServerMessage;
import webHttp.HttpRequestImpl;
import webHttp.HttpResponseImpl;
import webHttp.RequestHanler;
import webHttp.SessionPool;
import exceptions.HttpException;
import executor.BaseExecutor;
import executor.Executor;
/**
 * 双线通信线程
 * 解释: 双线通信指,既接收请求,又返回响应
 * 相对应的还有 单向发送通讯 和 单向接受通讯
 * 后两个可以用于jms等功能的实现
 * 目前尚未考虑实现
 * @author 刘宇航
 *
 */
public class SocketProcess extends Thread {
	ServerSocketChannel serverSocket = null;

	/**
	 * 初始化一个线程
	 * @param serverSocket 必须保证双线通信
	 */
	public SocketProcess(ServerSocketChannel serverSocket) {
		this.serverSocket = serverSocket;
	}

	/**
	 * 从serverSocket中获取数据
	 * 然后判断是资源文件还是servlet
	 * 最有发送正确的信息给客户端
	 */
	public void run() {
		ByteBuffer buffer = ByteBuffer.allocate(1024 * 8);
		SocketChannel channel = null;
			// 一些读取数据的工作
			try{
			channel = serverSocket.accept();
			if (channel == null || channel.read(buffer) <= 0) return;
			}catch(Exception e){e.printStackTrace();return;}
			//初始化元素
			String requestInfo = new String(buffer.array(), 0, buffer.position());
			HttpRequestImpl request = RequestHanler.getHttpRequest(requestInfo);
			HttpServletResponse response = new HttpResponseImpl(channel);
			//项目解析
			AppRunTime runtime = AppRunTime.getInstance();
			String[] uri = request.getContextPath().split(Constant.WEB_SP);
			ProjectInfo project = runtime.getProjectMap().get(uri[1]);
	
			if(project==null)
			{
				sendExceptionMessage(channel,new HttpException(HttpError.ERROR_404));
				closeChannel(buffer,channel);
			}
			else
			{
			SessionPool sessionPool = SessionPool.getInstance(project.getId());
			HttpSession session = sessionPool.getSession(channel.socket().getInetAddress().getHostAddress());
			request.bind(session);
			// 执行资源
			try {
			Executor exe  = new BaseExecutor(project);
			exe.forward(request, response);		
			} 
			//返回错误到页面
			catch (Exception e) {
				sendExceptionMessage(channel,e);
			}
			// 至少要反回空串
			closeChannel(buffer,channel);
			}
		
	}
	/**
	 * 追加一个空格并关闭通道
	 * @param buffer
	 * @param channel
	 */
	private void closeChannel(ByteBuffer buffer, SocketChannel channel){
		buffer.clear();
		if (!channel.isOpen()) return ;
		buffer.put(" ".getBytes());
		buffer.flip();
		try {
			channel.write(buffer);
			channel.finishConnect();
			channel.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 向客户端浏览器报告错误,比如error404 page not found之类的
	 * @param channel
	 * @param message
	 */
	private void sendExceptionMessage(SocketChannel channel ,Exception message){
		/*创建缓冲区*/
		ByteBuffer buffer = ByteBuffer.allocate(1024 * 8);
		/*获得错误消息堆栈*/
		StackTraceElement[]stackTraces =  message.getStackTrace();
		/*设置消息头*/
		buffer.put(message.toString().getBytes());
		/*设置堆栈体*/
		for(StackTraceElement trace:stackTraces)
		{
			buffer.put(trace.toString().getBytes());
			buffer.put(Constant.STR_NEXT_LINE.getBytes());
		}
		/*发送到浏览器*/
		try {
		buffer.flip();
		channel.write(buffer);
		} catch (IOException e) {
			System.err.println(ServerMessage.connection_exception);
		}
		buffer.clear();
		/*本机报错*/
		message.printStackTrace();
	}
}

// end

呵呵,先发这么多,以后的慢慢补上

你可能感兴趣的:(java,多线程,socket,浏览器,cvs)