用socket实现的文件服务器(5)

数据包解析器设计
本文介绍了一种数据包解析器的设计与实现方法,该解析器能够处理多种类型的论坛操作请求,如读取主题帖、回帖、发帖等,并通过特定的分隔符对请求进行解析。

/**
 * 数据包内容解析
 *
 * @author HongSoft
 */
public class PacketParserThread extends Thread
{
    private Socket socket;

    private BufferedReader in;

    private PrintWriter out;

    public PacketParserThread(Socket s) throws IOException
    {
        socket = s;
        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                .getOutputStream())), true);
        start();
    }

    public void run()
    {
        try
        {
            String str = in.readLine();
            if (str == null || str.length() == 0)
            {
                // do nothing
            }
            else
            {
               doParse(str,out);             
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    /**
     * 中间全部用ox1e分割
     * @param str
     * @param out
     */
    public static void doParse(String str,PrintWriter out)
    {
        String[] strs=str.split("/0x1e");     
        if("0".equals(strs[0]))//读主题贴
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            out.println(ForumFileOperator.getTopicContent(forumId,topicId));
        }
        else if("1".equals(strs[0]))//读回帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            List list=ForumFileOperator.getReplyList(forumId,topicId);
            //在这里进行写出工作
            Iterator it=list.iterator();
            while(it.hasNext())
            {
                out.println(it.next().toString());
            }
        }
        else if("2".equals(strs[0]))//发帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.addTopic(forumId,topicId,strs[3]);
        }
        else if("3".equals(strs[0]))//续帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.resumeTopic(forumId,topicId,strs[3]);
        }
        else if("4".equals(strs[0]))//回帖
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.addReply(forumId,topicId,strs[3],strs[4],strs[5]);
        }
        else if("5".equals(strs[0]))//删主题贴
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            ForumFileOperator.deleteTopic(forumId,topicId);
        }
        else if("6".equals(strs[0]))//删回贴中的某一个
        {
            int forumId=Integer.parseInt(strs[1]);
            int topicId=Integer.parseInt(strs[2]);
            int replyId=Integer.parseInt(strs[3]);
            ForumFileOperator.deleteReply(forumId,topicId,replyId);
        }
      
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值