package com.oim.core.socket;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import com.oim.adm.common.Content;
import com.oim.core.ServerDataContext;
import com.oim.server.service.MsgService;
import com.third.position.enqualcomm.msg.adaptor.MsgAdaptor;
import com.third.position.enqualcomm.msg.adaptor.YanQiangMsgAdaptor;
import com.third.position.enqualcomm.pojo.in.GpsPosition;
import com.third.position.enqualcomm.pojo.in.base.InMsg;
/**
*
* @ClassName: TCPServer
* @Description: TODO
* @author longjunping
* @date 2014-12-19 上午10:33:54
*
*/
public class TCPServer2 extends Thread {
private static Logger log = Logger.getLogger(TCPServer2.class);
private static int port = 8882;
private ExecutorService executorService;// 线程池
private final int POOL_SIZE = 10;// 单个CPU线程池大小
// 服务端socket
public static ServerSocket serverSocket = null;
public TCPServer2() {
try {
if (null == serverSocket) {
port = Integer.valueOf(Content.socket_port2);
serverSocket = new ServerSocket(port);
executorService = Executors.newFixedThreadPool(Runtime
.getRuntime().availableProcessors() * POOL_SIZE);
log.info(" TCPServer port:" + port);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (!this.isInterrupted()) {
try {
Socket socket = serverSocket.accept();
executorService.execute(new Handler2(socket, msgService));
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void closeSocketServer() {
try {
if (null != executorService && null != serverSocket
&& !serverSocket.isClosed()) {
serverSocket.close();
executorService.shutdown();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private MsgService msgService = (MsgService) ServerDataContext.getWac()
.getBean("msgService");
public MsgService getMsgService() {
return msgService;
}
public void setMsgService(MsgService msgService) {
this.msgService = msgService;
}
}
class Handler2 implements Runnable {
private static Logger log = Logger.getLogger(Handler2.class);
private static final int BUFFER_SIZE = 1024 * 1024;
private MsgService msgService;
private Socket socket;
public Handler2(Socket socket, MsgService msgService) {
this.socket = socket;
this.msgService = msgService;
}
public void run() {
try {
socket.setSoTimeout(60000);
System.setProperty("http.keepAlive", "false");
// 读取客户端数据
MsgAdaptor msg = new YanQiangMsgAdaptor();
InMsg imsg = new GpsPosition();
InputStream in = socket.getInputStream();
int len = in.available();
log.info("from client: " + len);
int i = 0, j = in.available(), k = 0;
byte[] maxBuffer = new byte[1024 * 64];
while ((i = in.read(maxBuffer)) != -1) {
k += i;
if (k >= j) {
break;
}
}
byte[] endodedData = new byte[k];
System.arraycopy(maxBuffer, 0, endodedData, 0, k);
imsg = msg.byte2Msg(endodedData);
log.info("-------------" + new String(endodedData));
log.info("from client last size: " + endodedData.length);
byte[] res = msgService.msgParse(imsg);
DataOutputStream dos = new DataOutputStream(
socket.getOutputStream());
dos.write(res);
dos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}