ITeye博客

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

hongsoft · 2005-09-28 20:44 · Markdown

/**
 * 数据包内容解析
 *
 * @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);
        }
      
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。