package com.asb.adapter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.asb.minimediation.constDefs.ConstDefs.CityCode;
import com.asb.minimediation.entity.AsbTrapAlarmEntity;
import com.asb.minimediation.util.INMSLog;
public class ServerSocketTask implements Runnable {
Logger adapterlog = INMSLog.getInstance().getAdapterLog();
Logger recvlog = INMSLog.getInstance().getReceiveLog();
private static PrintWriter out = null;
private static BufferedReader in = null;
int count = 0;
boolean socketFlag = true;
StringBuffer synchBuffer = new StringBuffer();
StringBuffer alarmBuffer = new StringBuffer();
boolean alarmStartFlag = false;
boolean alarmEndFlag = false;
boolean synchStartFlag = false;
boolean synchEndFlag = false;
public static synchronized PrintWriter getOutStream(){
return out;
}
/**
* 为每个socket初始化接收告警的参数*/
public void initParams(){
synchBuffer = new StringBuffer();
alarmBuffer = new StringBuffer();
alarmStartFlag = false;
alarmEndFlag = false;
synchStartFlag = false;
synchEndFlag = false;
}
public void run() {
try {
ServerSocket ss = new ServerSocket(Integer.parseInt(YiYangAdapter.adapter.socketPort.trim()));
adapterlog.info("ServerSocketTask::run >>wait for socket connect...");
while(true){
Socket socket = ss.accept();
adapterlog.info("ServerSocketTask::run >>socket connected...");
adapterlog.info("ServerSocketTask::run >>remote socket port:" + socket.getPort());
adapterlog.info("ServerSocketTask::run >>local socket port:" + socket.getLocalPort());
socketFlag = true;
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "gb2312"));
out = new PrintWriter(socket.getOutputStream());
socket.setSoTimeout(60*1000);//设置超时,若一分钟没有收到任何报文,抛出SocketTimeoutException
String lineString = "";
new Thread(new SendData()).start();
do{
try{
lineString = in.readLine();
adapterlog.info("ServerSocketTask::run >>" + lineString);
count = 0;
if(lineString != null) {
processLineString(lineString);
}
} catch (SocketTimeoutException ste) {
adapterlog.warn("time out one min...");
out.println("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
out.println("<root type=\"Heartbeat\">");
out.println("</root>");
out.flush();
count++;
if(lineString.indexOf("Heartbeat") == -1 && count == 3){
adapterlog.warn("断开socket");
if(in != null)
in.close();
if(in != null)
out.close();
if(in != null)
socket.close();
socketFlag = false;
initParams();
}
} catch (SocketException se){
adapterlog.info("ServerSocketTask::run >>" + se.toString());
if(in != null)
in.close();
if(in != null)
out.close();
if(in != null)
socket.close();
socketFlag = false;
initParams();
}
} while (socketFlag && lineString != null);
}
} catch (IOException e) {
adapterlog.info("ServerSocketTask::run >>" + e.toString());
}
}
public void processLineString(String lineString){
/**
* 接收告警*/
if(lineString.indexOf("<AlarmStart>") != -1){
alarmStartFlag = true;
alarmEndFlag = false;
}
if(lineString.indexOf("</AlarmStart>") != -1){
alarmStartFlag = false;
alarmEndFlag = true;
alarmBuffer.append(lineString + "\n");
processAlarm(alarmBuffer.toString());
alarmBuffer = new StringBuffer();
}
if(alarmStartFlag && !alarmEndFlag){
alarmBuffer.append(lineString + "\n");
}
/**
* 同步当前告警*/
if(lineString.trim().indexOf("<?xml encoding=\"gbk\"?>") != -1){
synchStartFlag = true;
synchEndFlag = false;
}
if(synchStartFlag && !synchEndFlag && lineString.trim().indexOf("</root>") != -1){
synchBuffer.append(lineString + "\n");
if(synchBuffer.toString().indexOf("synCurrent") != -1) {
processSynchAlarm(synchBuffer.toString());
adapterlog.info("收到同步当前告警返回报文"+synchBuffer.toString());
}
if(synchBuffer.toString().indexOf("Heartbeat") != -1)
adapterlog.info("收到心跳确认报文");
synchBuffer = new StringBuffer();
synchEndFlag = true;
synchStartFlag = false;
}
if(synchStartFlag && !synchEndFlag){
synchBuffer.append(lineString + "\n");
}
}
public void processAlarm(String alarm){
}
public void processSynchAlarm(String synchAlarm){
adapterlog.info("processSynchAlarm>> synchAlarm:"+synchAlarm);
String filename = synchAlarm.substring(synchAlarm.indexOf("$")+1,synchAlarm.indexOf("</ftpfile>"));
filename = filename.replaceAll("//", "/");
adapterlog.info("processSynchAlarm>> filename:"+filename);
Ftp ftp = new Ftp("","","",filename);
try {
if(ftp.connect()) {
adapterlog.info("connect to success...");
ftp.readFile("testlog.txt");
adapterlog.info("read success...");
ftp.disConnect();
}
} catch (IOException e) {
adapterlog.error("ServerSocketTask::processSynchAlarm>> "+e.toString());
} catch (Exception e) {
adapterlog.error("ServerSocketTask::processSynchAlarm>> "+e.toString());
}
}
public String transSeverity(String s) {
}
public String transCity(String cityName){
}
// public static void main(String[] args){
// ServerSocketTask task = new ServerSocketTask();
// task.run();
// }
}
class SendData implements Runnable {
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(10*1000);
YiYangAdapter.adapter.synchAlarm();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}