package mclab;



import java.net.*;
import java.io.*; 


public class DBThread extends Thread{

	private Socket connectionSocket;

	private static final int AP_REGISTRATION_REQUEST = 0;
	private static final int AP_REGISTRATION_RESPONSE = 1;
	private static final int AP_STATE_UPDATE_REQUEST = 2;
	private static final int AP_STATE_UPDATE_RESPONSE = 3;
	private static final int AP_LIST_REQUEST = 4;
	private static final int AP_LIST_RESPONSE = 5;
	private static final int AP_TERMINATE_REQUEST = 6;
	private static final int AP_TERMINATE_RESPONSE = 7;


	private static final int AP_ID = 0;
	private static final int AP_IP = 1;
	private static final int AP_SSID = 2;
	private static final int AP_DESCRIPTION = 3;
	private static final int AP_PASSWORD = 4;
	private static final int AP_HIDE = 5;
	private static final int AP_CHANNEL = 6;

	private static final int END_OF_MESSAGE = 99;

	private static final String delimiter = "|";



	public DBThread(Socket connectionSocket) {
		this.connectionSocket = connectionSocket;
	}
	protected String MakeMessage(int mType, String param) {
		return String.format("%d|%s|99", mType, param);
	}
	public int byteToint(byte[] arr){
		return (arr[0] & 0xff)<<24 | (arr[1] & 0xff)<<16 |
				(arr[2] & 0xff)<<8 | (arr[3] & 0xff);
	}
	


	@Override
	public void run() {
		try {

			System.out.println("Start");

			while(true)
			{
				InputStream in = connectionSocket.getInputStream();
				BufferedInputStream br = new BufferedInputStream(in);

				OutputStream out = connectionSocket.getOutputStream();
				BufferedOutputStream bo = new BufferedOutputStream(out);



				byte[] recv = new byte[1024];
				int length = br.read(recv);


				String message = new String(recv, 0, length);
				System.out.println(message);

				String parser[] = message.split(delimiter);
				int msg_type = Integer.parseInt(parser[0]);

				String sendMessage = null;

				/*
				 * processing by msg_type.
				 * make message to send ack.
				 */
				switch(msg_type) {

				case AP_REGISTRATION_REQUEST:
					//INSERT DB
					sendMessage = MakeMessage(AP_REGISTRATION_RESPONSE, "");
					break;
				case AP_REGISTRATION_RESPONSE:
					break;
				case AP_STATE_UPDATE_REQUEST:
					//UPDATE DB
					sendMessage = MakeMessage(AP_STATE_UPDATE_RESPONSE, "");
					break;
				case AP_STATE_UPDATE_RESPONSE:
					break;
				case AP_LIST_REQUEST:
					/*
					 * SELECT DB
					 * MAKE MESSAGE
					 */
					sendMessage = MakeMessage(AP_LIST_RESPONSE, "");
					break;
				case AP_LIST_RESPONSE:
					break;
				case AP_TERMINATE_REQUEST:
					br.close();
					bo.close();
					connectionSocket.close();
					return;
				default:
					break;
				}

				byte[] send = sendMessage.getBytes();

				bo.write(send);
				bo.flush();



			}





		}catch(Exception e) {

		}
	}

}

@@ -49,16 +49,32 @@
 .io.*; %0A
+import mclab.*;%0A
 %0A%0Apublic
@@ -139,16 +139,301 @@
 nSocket;
+%0A%09%0A%09public static final String MYSQL_DRIVER = %22com.mysql.jdbc.Driver%22;%0A%09public static final String MYSQL_URL = %22jdbc:mysql://163.180.118.44:3306/AP_Information?%22%0A%09public static final String MYSQL_USER = %22%22;%0A%09public static final String MYSQL_PASSWORD = %22%22;%0A%09%0A%09private MySQLdb db = null;
 %0A%0A%09priva
@@ -853,293 +853,8 @@
 7;%0A%0A
-%0A%09private static final int AP_ID = 0;%0A%09private static final int AP_IP = 1;%0A%09private static final int AP_SSID = 2;%0A%09private static final int AP_DESCRIPTION = 3;%0A%09private static final int AP_PASSWORD = 4;%0A%09private static final int AP_HIDE = 5;%0A%09private static final int AP_CHANNEL = 6;%0A%0A
 %09pri
@@ -939,16 +939,18 @@
 iter = %22
+%5C%5C
 %7C%22;%0A%0A%0A%0A%09
@@ -1036,19 +1036,93 @@
 Socket;%0A
+%09%09db = new MySQLdb(MYSQL_DRIVER,MYSQL_URL, MYSQL_USER, MYSQL_PASSWORD);%0A
 %09%7D%0A
+%09%0A
 %09protect
@@ -1223,16 +1223,18 @@
 am);%0A%09%7D%0A
+%09%0A
 %09public 
@@ -1362,11 +1362,8 @@
 %0A%09%7D%0A
-%09%0A%0A
 %0A%09@O
@@ -1690,17 +1690,16 @@
 out);%0A%0A%0A
-%0A
 %09%09%09%09byte
@@ -1861,16 +1861,28 @@
 parser%5B%5D
+;%0A%09%09%09%09parser
  = messa
@@ -1902,16 +1902,21 @@
 miter);%0A
+%09%09%09%09%0A
 %09%09%09%09int 
@@ -1955,16 +1955,89 @@
 er%5B0%5D);%0A
+%09%09%09%09%0A%09%09%09%09%0A%09%09%09%09AP ap = new AP(parser);%0A%09%09%09%09//AP ap = parseAP(parser);%0A%09%09%09%09
 %0A%09%09%09%09Str
@@ -2207,19 +2207,22 @@
 %09%09%09%09
-//INSERT DB
+db.insert(ap);
 %0A%09%09%09
@@ -2382,19 +2382,22 @@
 %09%09%09%09
-//UPDATE DB
+db.update(ap);
 %0A%09%09%09

add insert, update, delete