[Title] fix. ide-da communicator
authorHyunjong,park <phjwithyou.park@samsung.com>
Tue, 1 Oct 2013 02:11:37 +0000 (11:11 +0900)
committerHyunjong,park <phjwithyou.park@samsung.com>
Tue, 1 Oct 2013 02:11:37 +0000 (11:11 +0900)
[Desc.] change server-client
[Issue] -

org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicator.java

index ceae51b..3633fc6 100644 (file)
 package org.tizen.dynamicanalyzer.communicator;
 
 import java.io.BufferedWriter;
-import java.io.DataInputStream;
+import java.io.DataOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
 import java.io.RandomAccessFile;
+import java.net.ServerSocket;
 import java.net.Socket;
-import java.net.UnknownHostException;
 import java.nio.channels.FileChannel;
 import java.nio.channels.FileLock;
+import java.util.ArrayList;
 
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Display;
@@ -72,83 +73,80 @@ public class IDECommunicator implements Runnable {
        private static final int MSG_LENGTH = 4;
        private static final int APPNAME_LENGTH = 20;
 
-       private static Socket socket = null;
-       private int port = 0;
        private static BufferedWriter writer = null;
        private static Thread instance;
 
-       private File file = null;
-       private FileLock fileLock = null;
-       private FileChannel fchannel = null;
-
-       private static boolean openWelcomeDlg = false;
-       private static Boolean waitingWelcomeDlg = false;
+       private static Boolean openWelcomeDlg = Boolean.FALSE;
+       private static Boolean waitingWelcomeDlg = Boolean.FALSE;
+       
+       private ServerSocket daServerSocket = null;
+       private static Socket lastCommunicationIdeSocket = null;
+       private static ArrayList<Socket> ideClientSocketArray = new ArrayList<Socket>();
+       private int serverPort = 0;
 
 
        public void run() {
-               while (true) {
-                       try {
-                               resetSocket();
-                               // Waiting Start IDE
-                               System.out.println(" Waiting Start IDE"); //$NON-NLS-1$
+               daServerSocket = setServePort(); // Server Bind
+               if (daServerSocket != null) {
+                       if (savePortToFile() != true) { // Save Server port
+                               System.out.println("failed Save Server port");
+                               errorMessageBox();
+                               return;
+                       }
+
+                       if (setReadyCommunication() != true) {
+                               System.out.println("failed socket File Lock");
+                               errorMessageBox();
+                               return;
+                       }
+
+                       while (true) {
                                try {
-                                       file = new File(AnalyzerPaths.IDE_ACTIVE_PORT_PATH);
-                                       fchannel = new RandomAccessFile(file, "rw").getChannel();//$NON-NLS-1$
-                               } catch (FileNotFoundException e1) {
-                                       e1.printStackTrace();
-                                       closeFileLockData();
+                                       System.out.println("Waiting IDE Client : "
+                                                       + ideClientSocketArray.size());
+                                       lastCommunicationIdeSocket = daServerSocket.accept();
+                                       ideClientSocketArray.add(lastCommunicationIdeSocket);
+                                       System.out.println("Connected IDE : "
+                                                       + ideClientSocketArray.size());
+                               } catch (IOException e) {
+                                       e.printStackTrace();
                                        errorMessageBox();
-                                       return;
-                               }
-                               while (true) {
-                                       try {
-                                               fileLock = fchannel.tryLock();
-                                               if (fileLock == null) { // Active IDE
-                                                       break;
-                                               } else {
-                                                       fileLock.release();
-                                                       Thread.sleep(1000);
-                                               }
-                                       } catch (IOException e) {
-                                               e.printStackTrace();
-                                               closeFileLockData();
-                                               errorMessageBox();
-                                               return;
-                                       }
+                                       break;
                                }
-                               closeFileLockData();
-                               // Read IDE Port
-                               if (readActiveIdePort() != true) {
-                                       System.out.println("Failed Read Active IDE Port");//$NON-NLS-1$
-                                       continue;
-                               }
-                               // Connect IDE Socket
-                               if (setIDESocket() != true) {
-                                       System.out.println("Failed Connect IDE Socket");//$NON-NLS-1$
-                                       continue;
-                               }
-                               System.out.println("Connect IDE");//$NON-NLS-1$
-                               int readSize = 0;
-                               byte receiveBuffer[] = new byte[MAXBUFSIZE];
-                               InputStream in = null;
-                               while (true) {
-                                       try {
-                                               in = getSocket().getInputStream();
-                                               System.out.print("IDE recvMessage Wait\n");//$NON-NLS-1$
-                                               readSize = in.read(receiveBuffer);
-                                               if (readSize > 0) {
-                                                       parsingReceiveMessage(new String(receiveBuffer));
-                                               } else {
-                                                       break;
+
+                               Thread acceptThread = new Thread("IDEAcceptThread") {//$NON-NLS-1$
+                                       int readSize = 0;
+                                       byte receiveBuffer[] = new byte[MAXBUFSIZE];
+                                       InputStream in = null;
+
+                                       public void run() {
+                                               Socket clientSocket = lastCommunicationIdeSocket;
+                                               while (true) {
+                                                       try {
+                                                               in = clientSocket.getInputStream();
+                                                               System.out.println("IDE recvMessage Wait : "
+                                                                               + ideClientSocketArray.size());
+                                                               readSize = in.read(receiveBuffer);
+                                                               if (readSize > 0) {
+                                                                       lastCommunicationIdeSocket = clientSocket;
+                                                                       parsingReceiveMessage(new String(
+                                                                                       receiveBuffer));
+                                                               } else {
+                                                                       System.out.println("Disconnected IDE : "
+                                                                                       + ideClientSocketArray.size());
+                                                                       clientSocket.close();
+                                                                       ideClientSocketArray.remove(clientSocket);
+                                                                       break;
+                                                               }
+                                                       } catch (IOException e) {
+                                                               e.printStackTrace();
+                                                               break;
+                                                       }
                                                }
-                                       } catch (IOException e) {
-                                               e.printStackTrace();
-                                               break;
                                        }
-                               }
-                               System.out.println("Disconnect IDE");//$NON-NLS-1$
-                       } catch (InterruptedException e) {
-                               break;
+                               };
+                               acceptThread.start();
+                               System.out.println("Start IDE AcceptThread");
                        }
                }
        }
@@ -161,17 +159,7 @@ public class IDECommunicator implements Runnable {
                });
        }
 
-       private void closeFileLockData() {
-               if (file != null) {
-                       file = null;
-               }
-               AnalyzerUtil.tryClose(fchannel);
-               if (fileLock != null) {
-                       fileLock = null;
-               }
-       }
-
-       public static void startIDECommunicatorThread() {
+       public synchronized static void startIDECommunicatorThread() {
                if (null == instance || !instance.isAlive()) {
                        instance = new Thread(null, new IDECommunicator(),
                                        AnalyzerConstants.IDE_COMMUNICATOR_THREAD);
@@ -185,20 +173,12 @@ public class IDECommunicator implements Runnable {
                }
        }
 
-       private Socket getSocket() {
-               return socket;
-       }
-
-       private void resetSocket() {
-               socket = null;
-               writer = null;
-       }
-
        public static boolean isConnected() {
-               if (socket == null) {
+               if (ideClientSocketArray.size() < 1) {
                        return false;
+               } else {
+                       return true;
                }
-               return true;
        }
 
        public static void sendCodeInfoToIDE(final String path, final String line) {
@@ -208,9 +188,9 @@ public class IDECommunicator implements Runnable {
 
        public static boolean sendMessageToIDE(final String message) {
                try {
-                       if (socket != null) {
+                       if (lastCommunicationIdeSocket != null) {
                                writer = new BufferedWriter(new OutputStreamWriter(
-                                               socket.getOutputStream()));
+                                               lastCommunicationIdeSocket.getOutputStream()));
                                writer.write(message);
                                writer.flush();
                        }
@@ -343,63 +323,103 @@ public class IDECommunicator implements Runnable {
                warning.open();
        }
 
-       private boolean readActiveIdePort() {
-               boolean bSuccess = true;
-               // read port from current_active_ide_port
-               FileInputStream fis = null;
-               DataInputStream dis = null;
-               try {
-                       File logs = new File(AnalyzerPaths.IDE_ACTIVE_SAVE_PORT_PATH);
-                       if (!logs.exists()) {
-                               bSuccess = false;
-                       }
-                       fis = new FileInputStream(AnalyzerPaths.IDE_ACTIVE_SAVE_PORT_PATH);
-                       dis = new DataInputStream(fis);
-                       try {
-                               port = dis.readInt();
-                               if(port < 0){
-                                       port *= -1;
-                               }
-                       } catch (IOException e) {
-                               e.printStackTrace();
-                               bSuccess = false;
-                       } finally {
-                               AnalyzerUtil.tryClose(fis, dis);
-
-                       }
-               } catch (FileNotFoundException e) {
-                       e.printStackTrace();
-                       bSuccess = false;
-               } finally {
-                       AnalyzerUtil.tryClose(fis,dis);
-               }
-               return bSuccess;
-       }
-
-       private boolean setIDESocket() {
-               try {
-                       socket = new Socket(CommonConstants.LOCAL_HOST, port);
-               } catch (UnknownHostException e) {
-                       e.printStackTrace();
-                       socket = null;
-                       return false;
-               } catch (IOException e) {
-                       e.printStackTrace();
-                       socket = null;
-                       return false;
-               }
-               return true;
-       }
-
-       public static boolean isOpenWelcomeDlg() {
+       public static Boolean isOpenWelcomeDlg() {
                return openWelcomeDlg;
        }
 
        public static void setOpenWelcomeDlg(boolean data) {
-               openWelcomeDlg = data;
+               if(data){
+                       openWelcomeDlg = Boolean.TRUE;
+               }else{
+                       openWelcomeDlg = Boolean.FALSE;
+               }
        }
 
        public static Boolean getWaitingWelcomeDlg() {
                return waitingWelcomeDlg;
        }
+       
+       // Find Available Port, Save Current Port, Set Active DA_Plugin
+               private ServerSocket setServePort() {
+                       ServerSocket serverSocket = null;
+                       serverSocket = getServerSocket(0);
+                       if (serverSocket != null) {
+                               serverPort = serverSocket.getLocalPort();
+                               System.out.println("IDE-DA Server port " + serverPort); //$NON-NLS-1$
+
+                               return serverSocket;
+                       } else {
+                               System.out.println("failed Set Serve Socket"); //$NON-NLS-1$
+                       }
+                       return serverSocket;
+               }
+
+               // Find Available Port
+               private ServerSocket getServerSocket(final int port) {
+                       ServerSocket serverSocket = null;
+                       try {
+                               serverSocket = new ServerSocket(0);
+                       } catch (IOException e) {
+                               serverSocket = null;
+                               e.printStackTrace();
+                       }
+                       return serverSocket;
+               }
+
+               // Save Current Port
+               public boolean savePortToFile() {
+                       boolean bSuccess = true;
+                       FileOutputStream fos = null;
+                       DataOutputStream dos = null;
+                       try {
+                               File file = new File(AnalyzerPaths.IDE_ACTIVE_SAVE_PORT_PATH);
+                               fos = new FileOutputStream(file);
+                               dos = new DataOutputStream(fos);
+                               dos.writeInt(serverPort);
+                               dos.flush();
+                               bSuccess = true;
+                       } catch (FileNotFoundException e) {
+                               bSuccess = false;
+                               e.printStackTrace();
+                       } catch (IOException e) {
+                               bSuccess = false;
+                               e.printStackTrace();
+                       } finally{
+                               AnalyzerUtil.tryClose(fos,dos);
+                       }
+                       return bSuccess;
+               }
+
+               public boolean setReadyCommunication() {
+                       File file = null;
+                       FileLock fileLock = null;
+                       FileChannel fchannel = null;
+                       boolean setFileLock = false;
+                       try {
+                               file = new File(AnalyzerPaths.IDE_ACTIVE_PORT_PATH);
+                               fchannel = new RandomAccessFile(file, "rw").getChannel();//$NON-NLS-1$
+                               fileLock = fchannel.tryLock();
+                       } catch (FileNotFoundException e1) {
+                               e1.printStackTrace();
+                               errorMessageBox();
+                               return false;
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                               errorMessageBox();
+                       } finally {
+                               if (null == fileLock) {
+                                       setFileLock = false;
+                                       if (null != fchannel) {
+                                               try {
+                                                       fchannel.close();
+                                               } catch (IOException e) {
+                                                       e.printStackTrace();
+                                               }
+                                       }
+                               } else {
+                                       setFileLock = true;
+                               }
+                       }
+                       return setFileLock;
+               }
 }