
package org.upeng.mail.net.server;


import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;


import org.upeng.mail.net.util.AESUtil;

import org.upeng.mail.net.util.NetConfit;

import org.upeng.mail.net.util.NetLoger;

import org.upeng.mail.process.InstructionProcessor;

/**
* <ul>
* <li>Title:[服务器处理类]</li>
* <li>Description: [type of functional description]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-9-5]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/

public
class ServerHandler implements Runnable

{
/** SOCKET */

private Socket socket;
/** 缓冲写入 */

private BufferedWriter bw;
/** 缓冲读入 */

private BufferedReader br;
/** 指令处理缓存 */

private InstructionProcessor processor;
/**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param socket
*/

public
ServerHandler(Socket socket)

{

this.socket
=
socket;

processor
=
new InstructionProcessor();

}

public
void run()

{

try

{

//
创建缓冲读写

br
=
new BufferedReader(new InputStreamReader(socket

.getInputStream()));

bw
=
new BufferedWriter(new OutputStreamWriter(socket

.getOutputStream()));

//
客户端发来的消息

String msg
=
null
;

while
(true)

{

msg
=
readLine();

//
这里是远程商品,发向远程

processor.process(getRemoteIp(), msg);

write(NetConfit.tcp_oparator_success);

}

}

catch (IOException e)

{

e.printStackTrace();

}

finally

{

freeSource();

}

}
/**
* <ul>
* <li>Description:[写数据]</li>
* <li>Created by [Huyvanpull] [2011-9-5]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @param msg
* @throws IOException
*/

private void write(String msg) throws IOException

{

//
判断写入的消息是否合法

if
(msg
==
null
||
"".equals(msg))

{

return
;

}

//
处理消息,加上部首, 且加密数据,防止非法者抓包

String encodeMsg
=
NetConfit.tcp_msg_head
+
msg;

encodeMsg
=
AESUtil.encrypt(encodeMsg, NetConfit.password);

//
写数据

bw.write(encodeMsg);

bw.newLine();

bw.flush();

//
记录日志

NetLoger.logSnd(getRemoteIp(), socket.getPort(), msg);

}
/**
* <ul>
* <li>Description:[读数据]</li>
* <li>Created by [Huyvanpull] [2011-9-5]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @return
* @throws IOException
*/

private String readLine() throws IOException

{

String decodeMsg
=
"";

//
读取一行数据,并判断数据是否非法

String endcodeMsg
=
br.readLine();

if
(endcodeMsg
==
null
||
"".equals(endcodeMsg))

{

return
decodeMsg;

}

//
解密数据,并判断数据是否是合法的

decodeMsg
=
AESUtil.decrypt(endcodeMsg, NetConfit.password);

if
(decodeMsg.length()
<
5

||
!NetConfit.tcp_msg_head.equals(decodeMsg.
substring
(
0
,
4
)))

{

//
如果消息不合法

return
"";

}

else

{

decodeMsg
=
decodeMsg.
substring
(
4
);

}

//
写入日志

NetLoger.logRsv(getRemoteIp(), socket.getPort(), decodeMsg);

//
返回结果

return
decodeMsg;

}
/**
* <ul>
* <li>Description:[得到远程IP]</li>
* <li>Created by [Huyvanpull] [2011-9-7]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @return
*/

private String getRemoteIp()

{

return
socket.getInetAddress().getHostAddress().toString();

}
/**
* <ul>
* <li>Description:[释放资源]</li>
* <li>Created by [Huyvanpull] [2011-9-6]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/

private void freeSource()

{

try

{

//
释放BufferWriter

if
(bw
!=
null
)

{

bw.
close
();

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

try

{

//
释放BufferReader

if
(br
!=
null
)

{

br.
close
();

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

try

{

//
释放Socket

if
(socket
!=
null
)

{

socket.
close
();

}

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}