You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
227 lines
8.0 KiB
Java
227 lines
8.0 KiB
Java
package center.online;
|
|
|
|
import java.io.IOException;
|
|
import java.net.ServerSocket;
|
|
import java.net.SocketException;
|
|
import java.sql.Connection;
|
|
import java.sql.SQLException;
|
|
|
|
import com.util.Log4JLogger;
|
|
import com.util.Properties;
|
|
|
|
import center.online.process.DefaultFareReadInfo;
|
|
import center.online.process.FareAdjustInfo;
|
|
import center.online.process.FareTerminalInfo;
|
|
import center.online.process.RFRepayInfo;
|
|
import center.online.process.RFTerminalInfo;
|
|
/**
|
|
* 남산 3호로부터 실시간 데이터 수신
|
|
* @author jckim
|
|
* @since JDK 1.4.1
|
|
* @version 0.1, 2007-11-01 초기 작성
|
|
*/
|
|
public class OnServer03 extends OnCommon {
|
|
|
|
private String sServerCode = "03";
|
|
|
|
public OnServer03(Properties config) {
|
|
this.config = config;
|
|
}
|
|
|
|
/**
|
|
* startup
|
|
*/
|
|
public void startup() {
|
|
// Logger Create
|
|
try {
|
|
String sLogDir = config.getProperty( "log.dir" );
|
|
this.logger = new Log4JLogger( sLogDir + "OnServer_"+sServerCode+".log" );
|
|
// 로그 레벨 지정
|
|
String sLogLevel = config.getProperty( "log.level" );
|
|
if( sLogLevel != null ) this.logLevel = Integer.parseInt( sLogLevel );
|
|
} catch ( Exception e ) {
|
|
System.out.println( "로거를 생성하지 못했습니다.\n" + e.getMessage() );
|
|
System.exit(1);
|
|
}
|
|
|
|
try{
|
|
this.port = Integer.parseInt(config.getProperty( "server_"+sServerCode+".port" ));
|
|
} catch(Exception e) {
|
|
logger.logFail("ERROR : 서버 포트번호가 지정되지 않았습니다.");
|
|
System.exit(1);
|
|
}
|
|
// db driver setting
|
|
String driver = null;
|
|
try{
|
|
driver = config.getProperty("db.driver");
|
|
if( driver == null ) throw new Exception ();
|
|
}catch(Exception e){
|
|
System.err.println("ERROR : DB driver가 지정되지 않았습니다.");
|
|
System.exit(1);
|
|
}
|
|
try{
|
|
Class.forName(driver);
|
|
}catch(ClassNotFoundException cnfe){
|
|
System.err.println("ClassNotFoundException : " + driver);
|
|
System.exit(1);
|
|
}
|
|
}
|
|
/**
|
|
* 실행
|
|
*/
|
|
public void run() {
|
|
try{
|
|
serverSocket = new ServerSocket(port);
|
|
|
|
while (true) {
|
|
try {
|
|
sock = serverSocket.accept();
|
|
sock.setSoTimeout(TIME_OUT);
|
|
|
|
// logInfo(sock.toString()); // 접근 IP check
|
|
reader = sock.getInputStream();
|
|
writer = sock.getOutputStream();
|
|
execute();
|
|
} catch (SocketException se ) {
|
|
logFail( se.getMessage() );
|
|
} catch (IOException ie ) {
|
|
logFail( ie );
|
|
} catch(Exception ex ){
|
|
logFail( ex );
|
|
} finally{
|
|
try { if(reader!=null) reader.close(); } catch (Exception e) {}
|
|
try { if(writer!=null) writer.close(); } catch (Exception e) {}
|
|
try { if(sock!=null) sock.close(); } catch (Exception e) {}
|
|
}
|
|
}
|
|
} catch(Exception e) {
|
|
System.out.println("Exception port:"+port);
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 온라인 메시지 수신 로직
|
|
* @throws IOException
|
|
* @throws Exception
|
|
*/
|
|
public void execute() throws IOException, Exception{
|
|
String sReturn = null;
|
|
Connection conn = null;
|
|
String sMsgComm = null;
|
|
String sMsgData = null;
|
|
String sMsgCode = null;
|
|
String sMsgSize = null;
|
|
|
|
try {
|
|
FareTerminalInfo fareTerminal = new FareTerminalInfo();
|
|
FareAdjustInfo fareAdjust = new FareAdjustInfo();
|
|
RFTerminalInfo rfTerminal = new RFTerminalInfo();
|
|
DefaultFareReadInfo defaultFareRead = new DefaultFareReadInfo();
|
|
RFRepayInfo rfRepayInfo = new RFRepayInfo();
|
|
|
|
conn = this.getConnection();
|
|
|
|
while (true) {
|
|
if (checkSTX() == false) return; //continue;
|
|
sMsgComm = recvMessage(10);
|
|
sMsgCode = sMsgComm.substring(0, 3);
|
|
sMsgSize = sMsgComm.substring(3, 7);
|
|
//String sRtnCode = sMsgComm.substring(7,10);
|
|
|
|
int nSize;
|
|
try {
|
|
nSize = Integer.parseInt(sMsgSize);
|
|
} catch (Exception e) {
|
|
sReturn = "800"; // 전문 오류
|
|
throw e;
|
|
}
|
|
|
|
sMsgData = recvMessage(Integer.parseInt(sMsgSize));
|
|
if (checkETX() == false) {
|
|
logFail("전송 레코드의 끝표시값이 이상합니다.");
|
|
return ; //break;
|
|
}
|
|
|
|
try {
|
|
if( sMsgCode.equals("001")) // Fare_Terminal_Info
|
|
sReturn = fareTerminal.applyData(sMsgData, logger, conn);
|
|
else if( sMsgCode.equals("002")) // Fare_Adjust_Info
|
|
sReturn = fareAdjust.applyData(sMsgData, logger, conn);
|
|
else if( sMsgCode.equals("003")) // RF_Terminal_Info
|
|
sReturn = rfTerminal.applyData(sMsgData, logger, conn);
|
|
else if( sMsgCode.equals("004")) // Default_Fare_Read_Info
|
|
sReturn = defaultFareRead.applyData(sMsgData, logger, conn);
|
|
else if( sMsgCode.equals("005")) // RF_Repay_INFO
|
|
sReturn = rfRepayInfo.applyData(sMsgData, logger, conn);
|
|
else
|
|
sReturn = "800"; // 전문 오류
|
|
} catch (SQLException ex) {
|
|
int ERROR_CODE = ex.getErrorCode();
|
|
if(ERROR_CODE==1){
|
|
sReturn = "001"; // 무결성 제약 조건 ERROR
|
|
}else{
|
|
sReturn = "900"; // SQLException
|
|
logFail("[ERROR 900] " + ex.getMessage());
|
|
logFail("[ERROR 900] " + sMsgData );
|
|
logFail(ex);
|
|
}
|
|
}
|
|
|
|
sendMessage( makeMessage(sMsgCode, 0 ,sReturn, "") );
|
|
}
|
|
} catch (Exception ex) {
|
|
sendMessage( makeMessage( sMsgCode, 0 ,"999", "") );
|
|
logFail("[ERROR 999] " + ex.getMessage());
|
|
logFail("[ERROR 999] [" + sMsgComm + sMsgData +"]");
|
|
logFail(ex);
|
|
throw ex;
|
|
} finally {
|
|
try { if( conn != null ) conn.close(); } catch(Exception e) {}
|
|
}
|
|
}
|
|
/**
|
|
* Help 메시지 출력
|
|
*/
|
|
public static void showHelp(String sClass) {
|
|
System.err.println("Command :");
|
|
System.err.println("\tjava" + " -Donline.conf=CONFIG_FILE " + sClass);
|
|
System.err.println("");
|
|
}
|
|
/**
|
|
* main
|
|
*/
|
|
public static void main(String[] args) {
|
|
try {
|
|
if(args.length != 0){
|
|
showHelp("center.online.OnServer03");
|
|
System.exit(0);
|
|
}
|
|
|
|
String sConfFile = System.getProperty("online.conf");
|
|
if (sConfFile == null) {
|
|
showHelp("center.online.OnServer03");
|
|
System.exit(0);
|
|
}
|
|
|
|
Properties config = new Properties();
|
|
try {
|
|
config.load(sConfFile);
|
|
}
|
|
catch (IOException ie) {
|
|
System.err.println("환경파일을 읽어 들일 수 없습니다.");
|
|
System.exit(0);
|
|
}
|
|
|
|
OnServer03 server = new OnServer03(config);
|
|
server.startup();
|
|
server.run();
|
|
}
|
|
catch (Exception ex) {
|
|
System.err.println("데몬을 구동하는데 실패했습니다.");
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
|
|
}
|