⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 protocolparserimpl.java

📁 Java实现的Socket框架
💻 JAVA
字号:
/**
 * Project:ms4j (Message Server for Java)
 * Date:2007-3-13 
 * Authoer : jobsun@gmail.com
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 */
package com.sunjob.ms4j.server.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketAddress;
import java.util.HashMap;

import com.sunjob.ms4j.server.MsgProcesser;
import com.sunjob.ms4j.server.MsgRequest;
import com.sunjob.ms4j.server.MsgResponse;
import com.sunjob.ms4j.server.ProtocolParser;
import com.sunjob.ms4j.util.IOUtil;

/**
 * @author sunjb
 * 
 */
public class ProtocolParserImpl implements ProtocolParser {

	private HashMap context = null;

	public ProtocolParserImpl() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunjob.ms4j.server.MsgTranProtocol#getStatusText(int)
	 */
	public String getStatusText(int code) {
		String str = null;
		switch (code) {
		case 20:
			str = "正常";
			break;
		case 41:
			str = "网络异常";
			break;
		case 43:
			str = "用户名或密码错误";
			break;
		case 44:
			str = "无效的请求";
			break;
		case 50:
			str = "后台处理出现异常";
			break;
		case 60:
			str = "消息服务器异常";
			break;
		}
		return str;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunjob.ms4j.server.MsgTranProtocol#parse(java.io.InputStream,
	 *      java.io.OutputStream)
	 */
	public void parse(SocketAddress address, InputStream is, OutputStream os)
			throws Exception {
		byte[] breq = IOUtil.read(is);
		byte[] bName = null;
		if (breq.length > 256) {
			bName = new byte[256];
			System.arraycopy(breq, 0, bName, 0, 256);
		} else
			bName = breq;
		String strHeader = new String(bName);
		int i = strHeader.indexOf(" ");
		if (i <= 0)
			throw new Exception("Error Request Header.");
		String name = strHeader.substring(0, i);
		MsgProcesser mp = (MsgProcesser) this.context.get(name);
		ByteArrayInputStream bais = new ByteArrayInputStream(breq);
		bais.skip(i + 1);
		MsgRequest request = new MsgRequestImpl(bais);
		MsgResponse response = new MsgResponseImpl(os);
		mp.process(request, response);
	}

	public void setContext(HashMap context) {
		this.context = context;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -