import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
import java.lang.*;

public class CQE {
	static ComDB commDB = new ComDB();

	ServerSocket serverSocket;
	Socket clientSocket;

	public CQE() {
		serverSocket = null;
		clientSocket = null;

		try {
			serverSocket = new ServerSocket(5001);
			System.out.println("CQE Demon Processing...");

			while(true) {
				clientSocket = serverSocket.accept();
				//System.out.println("Port ==> " + clientSocket.getInetAddress());
				//System.out.println("Port ==> " + clientSocket.getLocalPort());
				System.out.println("Accept...");

				SerThread ser = new SerThread(clientSocket, commDB);
				ser.start();
			}
		} catch(Exception ex1) {
		} finally {
			System.out.println("CQE Demon Stop...");
			try {
				if(serverSocket != null) serverSocket.close();
			} catch(IOException ex2) {}
		}
	}

	public static void main(String[] args) throws Exception {
		CQE temp = new CQE();
	}

}

class SerThread extends Thread{
	Socket xsocket;
	ComDB xdata;
	DataOutputStream xsend = null;
	DataInputStream xrcvd = null;
	PrintStream xlog = null;
	PrintStream xfile = null;

	public SerThread(Socket client, ComDB commDB) {
		super();
		xsocket = client;
		xdata = commDB;
	}

	public void run() {
		String ackmsg = "TXSMS0000";
		//String ackmsg = "OK";
		String log, logname, filename;
		long fileno;

		SimpleDateFormat logfile = new SimpleDateFormat("yyMMddHH");
		SimpleDateFormat sdatetime = new SimpleDateFormat("yyMMddHHmmsss");
		SimpleDateFormat sdatetime1 = new SimpleDateFormat("yyMMddHHmmsss");
		logname = (logfile.format(new Date())).toString();
		//filename = (sdatetime.format(new Date())).toString();
		filename = (logfile.format(new Date())).toString();
		fileno = xdata.getFileNum();

		try {
	    		xsocket.setSoTimeout(2000);
			xlog = new PrintStream(new FileOutputStream("Log/" + logname + ".log", true));
			//xdata.addNumRcvd();
    			xfile = new PrintStream(new FileOutputStream("Data/" + filename + "_" + fileno + ".txt", true));
			xrcvd = new DataInputStream(xsocket.getInputStream());
			xsend = new DataOutputStream(xsocket.getOutputStream());

			log = "[in.] at " + sdatetime.format(new Date()) + " [" + fileno + "]";
			System.out.println(log);

			byte buff01[] = new byte[512];
			StringBuffer tmpbuff, outbuff;
			String line, line0;
			int idx;
			int len, sublen;
			int byte2, byte3;
			int byte6, byte7;
			int totbyte, totlen;
			int readcnt;
			int marklen;

			readcnt = 0;
			totbyte = 0;
			sublen = 0;
			totlen = 0;
			byte2 = 0;
			byte3 = 0;
			byte6 = 0;
			byte7 = 0;
			marklen = 0;

			tmpbuff = new StringBuffer("");
			outbuff = new StringBuffer("");

			while ((len = xrcvd.read(buff01, 0, 512)) != -1) {
				if (readcnt == 0) {
					readcnt = readcnt + 1;
					byte2 = (int) buff01[2] & 0xFF;
					byte3 = (int) buff01[3] & 0xFF;
					byte6 = (int) buff01[6] & 0xFF;
					byte7 = (int) buff01[7] & 0xFF;
					totbyte = (byte2 * 256) + byte3;
					//	
					//System.out.println("##" + byte2  + "--" + byte3 + "--" + totbyte + "--" + byte7 + "##");
				}
				//
				//System.out.println("readcnt**" + readcnt + "**");					
				line = new String(buff01, 0, len);
				//
				//System.out.println("line**" + line + "--" + len + "**");
				tmpbuff.append(line);
				//
				//System.out.println("tmpbuff**" + tmpbuff + "**");
				idx = -1;
				idx = line.indexOf("", idx + 1);
				//
				//System.out.println("indexOf**" + idx + "**");					
				sublen = sublen + len;
				//
				//System.out.println("##" + totbyte + "--" + sublen + "##");
				if (totbyte == sublen) {
					readcnt = 0;
					sublen = 0;
					//System.out.println("##" + byte7 + "##");
					if (byte7 == 48) {
						if (byte6 != 1) {
							tmpbuff.delete(0, 8);
						}
						outbuff.append(tmpbuff);
						xlog.println(sdatetime.format(new Date()) + "  ==> " + outbuff);
						xfile.println(outbuff);
						xsend.write(ackmsg.getBytes());
						xsend.flush();
					} else {
						marklen = tmpbuff.length();
						tmpbuff.deleteCharAt(marklen - 1);
						if (byte6 != 1) {
							tmpbuff.delete(0, 8);
						}
						outbuff.append(tmpbuff);
						//System.out.println("temp-->" + tmpbuff + "--");
						//System.out.println("out -->" + outbuff + "--");
						tmpbuff.delete(0, marklen + 1);
						xsend.write(ackmsg.getBytes());
						xsend.flush();
					}
				}
			}

		} catch(IOException ex1) {
			System.out.println("==>> Socket Thread Close...");
		} finally {
			try {
				log = "[out] at " + sdatetime.format(new Date()) + " [" + fileno + "]";
				System.out.println(log);
				xsocket.close();
				xlog.close();
				xfile.close();
				xsend.close();
				xrcvd.close();
			} catch(IOException ex2) {}
		}
	}
}

class ComDB {
	Vector vLog = new Vector();
	int nNumSend = 0;
	int nNumRcvd = 0;

	public synchronized void addNumRcvd() { nNumRcvd++; }
	public synchronized void addNumSend() { nNumSend++; }
	public synchronized long getNumRcvd() { return nNumRcvd; }
	public synchronized long getNumSend() { return nNumSend; }
	public synchronized long getFileNum() {
 		nNumRcvd++;
		return nNumRcvd;
 	}
	public synchronized void addLog(String msg) { vLog.add(msg); }
}
