SocketClient public class SocketClient extends Thread {// Fieldsprivate Socket socket;// socket套接字private volatile static SocketClient socketClient;// socketClientprivate byte[] byteArrayData = new byte[1024];private static SimpleDate
public class SocketClient extends Thread {
// Fields
private Socket socket;// socket套接字
private volatile static SocketClient socketClient;// socketClient
private byte[] byteArrayData = new byte[1024];
private static SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
private Answer answer;// GCS服务器返回结果实体类
// Private Constructor
private SocketClient() {
super();
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>SocketClient初始化完成");
// 初始化SocketClient建立与GCS系统的Socke连接
socket = new Socket();
InetSocketAddress serverAddress = new InetSocketAddress(
Constant.SERVER_IP, Constant.SERVER_PORT); // 创建socket
try {
socket.connect(serverAddress);
socket.setKeepAlive(true);
this.start();
// // 启动心跳包任务
Timer timer = new Timer();
timer.schedule(new DeviceWatchdogTask(socket), 1000, 5000);
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>初始化GCS服务器连接成功,数据读取线程已启动");
} catch (IOException e) {
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>初始化GCS服务器连接失败,失败原因:" + e.getMessage());
}
}
// Get SocketClient
public static SocketClient getSocketClient() {
if (socketClient == null) {
synchronized (SocketClient.class) {
if (socketClient == null) {
socketClient = new SocketClient();
}
}
}
return socketClient;
}
// Thread Run
@Override
public void run() {
while (true) {
// Socket 连接正常读取数据
if (!Constant.isSocketClosed) {
this.receive();
} else {// Socket连接异常,断开重连
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>GCS服务器Socket连接已断开,重新连接中...");
socket = new Socket();
InetSocketAddress serverAddress = new InetSocketAddress(
Constant.SERVER_IP, Constant.SERVER_PORT); // 创建socket
try {
socket.connect(serverAddress);
socket.setKeepAlive(true);
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>重新连接GCS服务器成功,继续读取数据");
} catch (IOException e) {
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>重新连接GCS服务器失败,失败原因:" + e.getMessage());
}
}
}
}
/**
* 接收GCS服务器发送的数据消息
*
* @return String 数据字符串
*/
public String receive() {
try {
socket.getInputStream().read(byteArrayData);
String[] resultArray = GcsUtil.getMessage(byteArrayData);
String cf = resultArray[2];
String cc = resultArray[3];
String jd = resultArray[4];
/* 判断业务类型,进行对应处理 */
if (cf.equals("1")) {// GCS服务器请求
if (cc.equals(Constant.CommandCode.Device_Watchdog_Request
.toString())) {// 心跳处理
String jsonData = new DeviceWatchDogAnswer().toString();
System.out.println(jsonData);
socket.getOutputStream().write(
GcsUtil.getSendByteArray(Constant.ANSWER_FLAG,
Constant.CommandCode.Device_Watchdog_Answer
.getCode(), jsonData));
socket.getOutputStream().flush();
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>Receive&Send:GCS服务器心跳请求,已回复");
}
} else {// GCS服务器响应
// Device-Watchdog-Answer 心跳处理
if (cc.equals(Constant.CommandCode.Device_Watchdog_Answer
.toString())) {
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>Receive:GBS心跳请求响应");
}
// Business-Handle-Answer 业务受理
else if (cc.equals(Constant.CommandCode.Business_Handle_Answer
.toString())) {
answer = JsonUtils.readValue(jd, Answer.class);
String sessionId = answer.getSessionId();
int requestCode = answer.getResultCode();
int recordType = answer.getRecordType();
if (Constant.ResultCode.SUCCESS.getCode() == requestCode) {// 成功响应
System.out.println("响应成功");
} else {// 响应失败
String failureReason = Constant.ResultCode
.getDetail(requestCode);
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>Receive:("
+ Constant.RecordType.getName(recordType)
+ ")响应失败,失败原因:" + failureReason);
}
}
// Settlement-Charge-Request 结费处理
else if (cc
.equals(Constant.CommandCode.Settlement_Charge_Answer
.toString())) {
}
// Price-Charge-Answer 批价处理
else if (cc.equals(Constant.CommandCode.Price_Charge_Answer
.toString())) {
}
// 约定之外的为止CommandCode
else {
}
}
return null;
} catch (IOException e) {
System.out.println("[" + format.format(new Date())
+ "]【GBS】=>Receive:接收GCS服务器发送的数据消息失败,失败原因:"
+ e.getMessage());
return null;
}
}
// Property accessors
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
}
