/**
* 資料包內容解析
*
* @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);
}
}
}