[Title] Refactoring Network IDE - DA
authorHyunjong,Pakr <phjwithyou.park@samsung.com>
Tue, 25 Sep 2012 05:37:19 +0000 (14:37 +0900)
committerHyunjong,Pakr <phjwithyou.park@samsung.com>
Tue, 25 Sep 2012 05:37:19 +0000 (14:37 +0900)
[Type] Refactoring
[Module] DAManager
[Priority] normal
[CQ#]
[Redmine#] 7009, 7020
[Problem]
[Cause]
[Solution]
[TestCase]

org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorManager.java
org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorThread.java

index f2dacb4..2f87abb 100644 (file)
@@ -49,6 +49,9 @@ public class IDECommunicatorManager {
        private final static String IDE_DA_COMMUNICATION_TYPE_2 = "RUN";
        private final static String IDE_DA_COMMUNICATION_TYPE_3 = "SOURCE";
        
+       private final static String IDE_DA_COMMUNICATION_TYPE_1_RECORD = "record";
+       private final static String IDE_DA_COMMUNICATION_TYPE_1_STOP = "stop";
+       
 
        public static void startCommunicationIDE(){
                IDECommunicatorThread communicatorThread = new IDECommunicatorThread();
@@ -68,9 +71,9 @@ public class IDECommunicatorManager {
 
                String[] strMsg = msg.split(AnalyzerConstants.DATA_PARSING_TOKEN);
 
-               if (strMsg[0].equals( IDE_DA_COMMUNICATION_TYPE_1 )) {//$NON-NLS-1$
+               if (strMsg[0].equals( IDE_DA_COMMUNICATION_TYPE_1 )) {
                        sendDAStatus();
-               } else if (strMsg[0].equals( IDE_DA_COMMUNICATION_TYPE_2 )) {//$NON-NLS-1$
+               } else if (strMsg[0].equals( IDE_DA_COMMUNICATION_TYPE_2 )) {
                        traceApplication(strMsg);
                } else {
                        System.out.println(" wrong receive message ");
@@ -79,37 +82,36 @@ public class IDECommunicatorManager {
 
        // Receive Message DA Status , Send DA Status
        private static void sendDAStatus() {
-               boolean bDAPlay = false;
                String state = AnalyzerUtil.getRecordState();
                if (state.equals(RecordStateSourceProvider.RECORD_RECORDING)) { // DA
                                                                                                                                                // Trace
                                                                                                                                                // Status
-                       bDAPlay = true;
-                       StringBuffer sendMsg = new StringBuffer();
-                       sendMsg.append( IDE_DA_COMMUNICATION_TYPE_1 )//$NON-NLS-1$
-                                       .append(AnalyzerConstants.DATA_PARSING_TOKEN)
-                                       .append("play")//$NON-NLS-1$
-                                       .append(AnalyzerConstants.DATA_PARSING_TOKEN).append("");//$NON-NLS-1$
-                       IDECommunicatorThread.sendMessageToIDE(sendMsg.toString());
-               } else { // DA Stop Status
-                       StringBuffer sendMsg = new StringBuffer();
-                       sendMsg.append( IDE_DA_COMMUNICATION_TYPE_1 )//$NON-NLS-1$
-                                       .append(AnalyzerConstants.DATA_PARSING_TOKEN)
-                                       .append("stop")//$NON-NLS-1$
-                                       .append(AnalyzerConstants.DATA_PARSING_TOKEN).append("");//$NON-NLS-1$
-                       IDECommunicatorThread.sendMessageToIDE(sendMsg.toString());
-               }
-
-               if (bDAPlay == true) {
+                       
+                       IDECommunicatorThread.sendMessageToIDE( sendMessageString(IDE_DA_COMMUNICATION_TYPE_1, IDE_DA_COMMUNICATION_TYPE_1_RECORD,"" ) );
                        DASingletonFocusManager.setFocusDA();
                        Display.getDefault().syncExec(new Runnable() {
                                public void run() {
                                        popupMessage(AnalyzerLabels.RE_TRACE_FORM_IDE);
                                }
                        });
+                       
+               } else { // DA Stop Status
+                       IDECommunicatorThread.sendMessageToIDE( sendMessageString(IDE_DA_COMMUNICATION_TYPE_1, IDE_DA_COMMUNICATION_TYPE_1_STOP,"" ) );
                }
        }
 
+       private static String sendMessageString( String first, String second, String third ){
+               
+               StringBuffer sendMsg = new StringBuffer();
+               sendMsg.append( first )
+                               .append(AnalyzerConstants.DATA_PARSING_TOKEN)
+                               .append( second )
+                               .append(AnalyzerConstants.DATA_PARSING_TOKEN)
+                               .append( third );
+               return sendMsg.toString();
+               
+       }
+       
        // trace Application
        private static void traceApplication(String[] strMsg) {
 
@@ -126,13 +128,8 @@ public class IDECommunicatorManager {
                DASingletonFocusManager.setFocusDA();
        }
 
-       
        public static void sendMessageSourceOpenIDE( final String path , final String line ) {
-               StringBuffer msg = new StringBuffer();
-               msg.append( IDE_DA_COMMUNICATION_TYPE_3 ).append( AnalyzerConstants.DATA_PARSING_TOKEN )//$NON-NLS-1$
-               .append(path).append( AnalyzerConstants.DATA_PARSING_TOKEN )
-               .append( line );
-               IDECommunicatorThread.sendMessageToIDE( msg.toString() );
+               IDECommunicatorThread.sendMessageToIDE( sendMessageString( IDE_DA_COMMUNICATION_TYPE_3, path, line ) );
        }
 
        private static void popupMessage(final String message) {
index 7dcc034..f3157df 100644 (file)
@@ -45,7 +45,7 @@ import org.tizen.dynamicanalyzer.common.CommonConstants;
 
 public class IDECommunicatorThread extends Thread {
 
-
+       private final int MAXBUFSIZE = 1024;
        private static Socket socket = null;
        private int port = 0;
        private static BufferedWriter writer = null;
@@ -53,15 +53,50 @@ public class IDECommunicatorThread extends Thread {
        public void run() {
                while (true) {
                        resetSocket();
-                       acceptIDE(); // Waiting Connect IDE
-                       IDECommunicatorManager.setCurrentIDECommunicationStatus( true ); 
+                                               
+                       // Waiting Start IDE
+                       System.out.println(" Waiting Start IDE");
+                       if ( checkActiveIde() != true ) {
+                               System.out.println("Failed Set Active File Lock");
+                               continue;
+                       }
+                       
+                       // Read IDE Port
+                       if ( readActiveIdePort() != true ) {
+                               System.out.println("Failed Set Active File Lock");
+                               continue;
+                       }
+                       
+                       // Connect IDE Socket
+                       if (setIDESocket() != true) {
+                               System.out.println("Failed Connect IDE Socket");
+                               continue;
+                       }
+                       
                        System.out.println("Connect IDE");
-                       if (setIDESocket() == true) {
-                               receiveApplicationInfoFromIDE(); // Call Receive Thread, Waiting
-                                                                                                       // DisConnect IDE
+                       IDECommunicatorManager.setCurrentIDECommunicationStatus(true);
+                       while (true) {
+                               try {
+                                       int readSize = 0;
+                                       byte receiveBuffer[] = null;
+                                       receiveBuffer = new byte[MAXBUFSIZE];
+                                       InputStream in = socket.getInputStream();
+                                       System.out.print("IDE recvMessage Wait\n");
+                                       readSize = in.read(receiveBuffer);
+                                       if (readSize > 0) {
+
+                                               String msg = new String(receiveBuffer);
+                                               IDECommunicatorManager.commandReceiveMessage(msg);
+                                       } else {
+                                               break;
+                                       }
+                               } catch (IOException e) {
+                                       e.printStackTrace();
+                                       break;
+                               }
                        }
                        System.out.println("waiting IDE");
-                       IDECommunicatorManager.setCurrentIDECommunicationStatus( false );
+                       IDECommunicatorManager.setCurrentIDECommunicationStatus(false);
                }
        }
        
@@ -78,25 +113,30 @@ public class IDECommunicatorThread extends Thread {
        }
        
        
-       private boolean acceptIDE() {
+       private boolean readActiveIdePort() {
                boolean bSuccess = true;
-
-               while (true) {
-                       // read port from current_active_ide_port
-                       port = readActiveIdePort();
-                       if (port > 0) {
-                               System.out.println("port :" + port);
-                               break;
-                       } else {
-               //              System.out.println("waiting IDE");
-                               try {
-                                       Thread.sleep(1000);
-                               } catch (InterruptedException e) {
-                                       e.printStackTrace();
-                               }
+               // read port from current_active_ide_port
+               try {
+                       File logs = new File(AnalyzerPaths.IDE_ACTIVE_SAVE_PORT_PATH);
+                       if (!logs.exists()) {
+                               bSuccess = false;
                        }
+                       FileInputStream fis = new FileInputStream(
+                                       AnalyzerPaths.IDE_ACTIVE_SAVE_PORT_PATH);
+                       DataInputStream dis = new DataInputStream(fis);
+                       try {
+                               port = dis.readInt();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                               bSuccess = false;
+                       }
+
+               } catch (FileNotFoundException e) {
+                       e.printStackTrace();
+                       bSuccess = false;
                }
                return bSuccess;
+
        }
        
        private boolean setIDESocket() {
@@ -130,78 +170,33 @@ public class IDECommunicatorThread extends Thread {
                }
                return true;
        }
-       
-       private boolean receiveApplicationInfoFromIDE() {
-               boolean bSuccess = true;
-               final int MAXBUFSIZE = 1024;
 
-               int readSize = 0;
-               byte receiveBuffer[] = null;
+       // Check Current IDE Status, File Lock by IDE
+       static boolean checkActiveIde() {
 
                while (true) {
+                       
+                       File file = null;
+                       FileLock fileLock = null;
+                       FileChannel fchannel = null;
                        try {
-                               receiveBuffer = new byte[MAXBUFSIZE];
-                               InputStream in = socket.getInputStream();
-                               System.out.print("IDE recvMessage Wait\n");
-                               readSize = in.read(receiveBuffer);
-                               if (readSize > 0) {
-
-                                       String msg = new String(receiveBuffer);
-                                       IDECommunicatorManager.commandReceiveMessage(msg);
+                               file = new File(AnalyzerPaths.IDE_ACTIVE_PORT_PATH);
+                               fchannel = new RandomAccessFile(file, "rw").getChannel();
+                               fileLock = fchannel.tryLock();
+                               if (fileLock == null) { // Active IDE
+                                       return true; 
                                } else {
-                                       break;
+                                       fileLock.release();
+                                       Thread.sleep(1000);
                                }
-                       } catch (IOException e) {
-                               e.printStackTrace();
-                               break;
-                       }
-               }
-               return bSuccess;
-       }
-
-       // Check Current IDE Status, File Lock by IDE
-       static boolean checkActiveIde() {
-
-               
-               File file = null;
-               FileLock fileLock = null;
-               FileChannel fchannel = null;
-               try {
-                       file = new File( AnalyzerPaths.IDE_ACTIVE_PORT_PATH );
-                       fchannel = new RandomAccessFile(file, "rw").getChannel();
-                       fileLock = fchannel.tryLock();
-                       if (fileLock == null) {
-                               return true; // Active IDE
-                       } else {
-                               fileLock.release();
+                       } catch (Exception e) {
                                return false;
-                               //
                        }
-               } catch (Exception e) {
-                       return true;
+
                }
        }
        
-       static int readActiveIdePort() {
-               if ( checkActiveIde() == true) {
-                       try {                           
-                               FileInputStream fis = new FileInputStream( AnalyzerPaths.IDE_ACTIVE_SAVE_PORT_PATH );
-                               DataInputStream dis = new DataInputStream(fis);
-                               try {
-                                       return dis.readInt();
-                               } catch (IOException e) {
-                                       e.printStackTrace();
-                                       return 0;
-                               }
 
-                       } catch (FileNotFoundException e) {
-                               e.printStackTrace();
-                               return 0;
-                       }
-               } else {
-                       return 0;
-               }
-       }
        
        
 }