From 443ad84f0aee31231698ed82bc0a37a5a72b915c Mon Sep 17 00:00:00 2001 From: "Hyunjong,Pakr" Date: Mon, 8 Oct 2012 18:30:06 +0900 Subject: [PATCH] [Title] Refactoring Source [Desc.] IDE-DA Network, Delete Singleton Thread [Issue] Readmine 7009 --- .../ApplicationWorkbenchWindowAdvisor.java | 3 +- .../dynamicanalyzer/common/AnalyzerConstants.java | 1 + .../dynamicanalyzer/common/AnalyzerManager.java | 18 +- .../common/DASingletonCheckThread.java | 53 ----- .../common/DASingletonFocusManager.java | 42 ---- .../communicator/IDECommunicatorManager.java | 111 +++++++++-- .../communicator/IDECommunicatorThread.java | 214 ++++++--------------- .../listeners/TableTooltipListener.java | 2 +- 8 files changed, 176 insertions(+), 268 deletions(-) delete mode 100644 org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonCheckThread.java diff --git a/org.tizen.dynamicanalyzer.workbench/src/org/tizen/dynamicanalyzer/workbench/ApplicationWorkbenchWindowAdvisor.java b/org.tizen.dynamicanalyzer.workbench/src/org/tizen/dynamicanalyzer/workbench/ApplicationWorkbenchWindowAdvisor.java index 3f8f3a4..a21c343 100755 --- a/org.tizen.dynamicanalyzer.workbench/src/org/tizen/dynamicanalyzer/workbench/ApplicationWorkbenchWindowAdvisor.java +++ b/org.tizen.dynamicanalyzer.workbench/src/org/tizen/dynamicanalyzer/workbench/ApplicationWorkbenchWindowAdvisor.java @@ -43,7 +43,6 @@ import org.tizen.dynamicanalyzer.common.AnalyzerManager; import org.tizen.dynamicanalyzer.common.CommonConstants; import org.tizen.dynamicanalyzer.common.DASingletonFocusManager; import org.tizen.dynamicanalyzer.common.PathManager; -import org.tizen.dynamicanalyzer.communicator.IDECommunicatorManager; import org.tizen.dynamicanalyzer.communicator.ResourceCommunicator; import org.tizen.dynamicanalyzer.handlers.ExitHandler; import org.tizen.dynamicanalyzer.ui.toolbar.ToolbarArea; @@ -113,7 +112,7 @@ public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { ToolbarArea.getInstance().autoStart(); DASingletonFocusManager.setSaveFocusDAPid(); - IDECommunicatorManager.startCommunicationIDE(); + AnalyzerManager.startIDECommunicatorThread(); } public boolean preWindowShellClose() { diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerConstants.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerConstants.java index f59c46e..cf91f57 100644 --- a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerConstants.java +++ b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerConstants.java @@ -80,6 +80,7 @@ public class AnalyzerConstants { public static final String SLP_APP_RUN_THREAD = "SLP App Run Thread"; //$NON-NLS-1$ public static final String OPEN_TRACE_THREAD = "Open trace input thread"; //$NON-NLS-1$ public static final String INSERT_LOG_THREAD = "Insert log thread"; //$NON-NLS-1$ + public static final String IDE_COMMUNICATOR_THREAD = "IDE Communicator thread"; //$NON-NLS-1$ public static final String SEND_MESSAGE_THREAD = "Send message thread"; //$NON-NLS-1$ public static final String START_STOP_THREAD = "Start stop thread"; //$NON-NLS-1$ diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerManager.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerManager.java index 7a68e17..2b1ad37 100755 --- a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerManager.java +++ b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/AnalyzerManager.java @@ -33,6 +33,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; +import org.tizen.dynamicanalyzer.communicator.IDECommunicatorThread; import org.tizen.dynamicanalyzer.handlers.ClearHandler; import org.tizen.dynamicanalyzer.logparser.InsertLogQueue; import org.tizen.dynamicanalyzer.logparser.InsertLogThread; @@ -91,7 +92,8 @@ public class AnalyzerManager { private static Thread startStopThread = null; private static Thread openTraceInputThread = null; private static Thread insertLogThread = null; - + private static Thread IDEcommunicatorThread = null; + private static String startBinaryAddr = null; private static String endBinaryAddr = null; @@ -212,6 +214,7 @@ public class AnalyzerManager { stopLogQueueObservingThread(); stopInsertLogThread(); + stopIDEcommunicatorThread(); leakDetector = null; failedChecker = null; @@ -445,6 +448,19 @@ public class AnalyzerManager { } } + public static void startIDECommunicatorThread() { + if (null == IDEcommunicatorThread || !IDEcommunicatorThread.isAlive()) { + IDEcommunicatorThread = new Thread(null, new IDECommunicatorThread(), + AnalyzerConstants.IDE_COMMUNICATOR_THREAD); + IDEcommunicatorThread.start(); + } + } + public static void stopIDEcommunicatorThread() { + if (null != IDEcommunicatorThread && IDEcommunicatorThread.isAlive()) { + IDEcommunicatorThread.interrupt(); + } + } + public static LeakDetector getLeakDectector() { if (leakDetector == null) { diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonCheckThread.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonCheckThread.java deleted file mode 100644 index 68da69f..0000000 --- a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonCheckThread.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Dynamic Analyzer - * - * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. - * - * Contact: - * Hyunjong Park - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * - S-Core Co., Ltd - * - */ - -package org.tizen.dynamicanalyzer.common; - -import java.io.IOException; -import java.net.ServerSocket; -import java.net.SocketException; - -public class DASingletonCheckThread extends Thread { - private static final int DARunningWait = -1; - private static final int DARunningFalse = 0; - private static final int DARunningTrue = 1; - - static int portNumber = 6824; - public int bDARunning = DARunningWait; - - public void run() { - try { - portNumber += 12345; - bDARunning = DARunningFalse; - ServerSocket local = new ServerSocket(portNumber); - local.accept(); - } catch (SocketException e) { - bDARunning = DARunningTrue; - } catch (IOException e) { - e.printStackTrace(); - bDARunning = DARunningTrue; - } - } -} \ No newline at end of file diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonFocusManager.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonFocusManager.java index 27a454b..83cb780 100644 --- a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonFocusManager.java +++ b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/common/DASingletonFocusManager.java @@ -26,44 +26,17 @@ package org.tizen.dynamicanalyzer.common; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; - import org.tizen.dynamicanalyzer.nl.ConfigureLabels; import org.tizen.dynamicanalyzer.ui.toolbar.ConfigureManager; import org.tizen.dynamicanalyzer.utils.AnalyzerUtil; -class ThreadOutput extends Thread { - private InputStream _ips; - - public ThreadOutput(InputStream is) { - _ips = is; - } - - public void run() { - try { - InputStreamReader isr = new InputStreamReader(_ips); - BufferedReader br = new BufferedReader(isr); - @SuppressWarnings("unused") - String line; - while ((line = br.readLine()) != null) { - } - - } catch (IOException ioe) { - } finally { - - } - } -} - public class DASingletonFocusManager { private static String pid = null; private static String Processid = null; @@ -120,23 +93,8 @@ public class DASingletonFocusManager { process = runtime.exec(cmds); if (process != null) { - ThreadOutput gboInput = new ThreadOutput( - process.getInputStream()); - ThreadOutput gboError = new ThreadOutput( - process.getErrorStream()); - gboInput.start(); - gboError.start(); - - System.out.print(" process.waitFor() 1 \n"); process.waitFor(); - System.out.print(" process.waitFor() 2 \n"); - retValue = process.exitValue(); - while (true) { - if (!gboInput.isAlive() && !gboError.isAlive()) { - break; - } - } } } } catch (IOException e) { diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorManager.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorManager.java index 08b4123..85b3ca9 100644 --- a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorManager.java +++ b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorManager.java @@ -26,11 +26,26 @@ package org.tizen.dynamicanalyzer.communicator; +import java.io.BufferedWriter; +import java.io.DataInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.RandomAccessFile; +import java.net.Socket; +import java.net.UnknownHostException; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; + import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.tizen.dynamicanalyzer.common.AnalyzerConstants; import org.tizen.dynamicanalyzer.common.AnalyzerManager; +import org.tizen.dynamicanalyzer.common.AnalyzerPaths; +import org.tizen.dynamicanalyzer.common.CommonConstants; import org.tizen.dynamicanalyzer.common.DASingletonFocusManager; import org.tizen.dynamicanalyzer.nl.AnalyzerLabels; import org.tizen.dynamicanalyzer.resources.ImageResources; @@ -41,33 +56,35 @@ import org.tizen.dynamicanalyzer.utils.AnalyzerUtil; public class IDECommunicatorManager { - private static boolean isConnectedIDE = false; - private final static String IDE_DA_COMMUNICATION_TYPE_1 = "STATUS"; 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(); - communicatorThread.start(); + private static Socket socket = null; + private static int port = 0; + private static BufferedWriter writer = null; + + public static Socket getSocket(){ + return socket; } - - public static boolean getCurrentIDECommunicationStatus() { - return isConnectedIDE; + + public static void resetSocket(){ + socket = null; + writer = null; } - - public static void setCurrentIDECommunicationStatus(boolean status) { - isConnectedIDE = status; + + public static boolean isConnected() { + if (socket == null) { + return false; + } + return true; } - + // Management Receive Message - static void commandReceiveMessage(String msg) { - + public static void commandReceiveMessage(String msg) { String[] strMsg = msg.split(AnalyzerConstants.DATA_PARSING_TOKEN); - if (strMsg[0].equals(IDE_DA_COMMUNICATION_TYPE_1)) { sendDAStatus(); } else if (strMsg[0].equals(IDE_DA_COMMUNICATION_TYPE_2)) { @@ -84,7 +101,7 @@ public class IDECommunicatorManager { // Trace // Status - IDECommunicatorThread.sendMessageToIDE(sendMessageString( + sendMessageToIDE(sendMessageString( IDE_DA_COMMUNICATION_TYPE_1, IDE_DA_COMMUNICATION_TYPE_1_RECORD, "")); DASingletonFocusManager.setFocusDA(); @@ -95,7 +112,7 @@ public class IDECommunicatorManager { }); } else { // DA Stop Status - IDECommunicatorThread.sendMessageToIDE(sendMessageString( + sendMessageToIDE(sendMessageString( IDE_DA_COMMUNICATION_TYPE_1, IDE_DA_COMMUNICATION_TYPE_1_STOP, "")); } @@ -130,7 +147,7 @@ public class IDECommunicatorManager { public static void sendMessageSourceOpenIDE(final String path, final String line) { - IDECommunicatorThread.sendMessageToIDE(sendMessageString( + sendMessageToIDE(sendMessageString( IDE_DA_COMMUNICATION_TYPE_3, path, line)); } @@ -142,4 +159,60 @@ public class IDECommunicatorManager { warning.open(); } + public static boolean readActiveIdePort() { + boolean bSuccess = true; + // 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; + } + + public static 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 sendMessageToIDE( final String message ){ + + System.out.println("sendMessageOpenIDE " + message); + try { + if ( socket != null ) { + writer = new BufferedWriter(new OutputStreamWriter( + socket.getOutputStream())); + writer.write(message); + writer.flush(); + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } + return true; + } } diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorThread.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorThread.java index f3157df..cb87886 100644 --- a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorThread.java +++ b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/communicator/IDECommunicatorThread.java @@ -26,177 +26,91 @@ package org.tizen.dynamicanalyzer.communicator; -import java.io.BufferedWriter; -import java.io.DataInputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStreamWriter; import java.io.RandomAccessFile; -import java.net.Socket; -import java.net.UnknownHostException; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; -import org.tizen.dynamicanalyzer.common.AnalyzerPaths; -import org.tizen.dynamicanalyzer.common.CommonConstants; - -public class IDECommunicatorThread extends Thread { +import org.tizen.dynamicanalyzer.common.AnalyzerPaths; +public class IDECommunicatorThread implements Runnable { private final int MAXBUFSIZE = 1024; - private static Socket socket = null; - private int port = 0; - private static BufferedWriter writer = null; - + public void run() { + while (true) { - resetSocket(); - - // 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"); - 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) { + try { + IDECommunicatorManager.resetSocket(); + // Waiting Start IDE + System.out.println(" Waiting Start IDE"); + if (checkActiveIde() != true) { + System.out.println("Failed Check Active File Lock"); + continue; + } - String msg = new String(receiveBuffer); - IDECommunicatorManager.commandReceiveMessage(msg); - } else { + // Read IDE Port + if (IDECommunicatorManager.readActiveIdePort() != true) { + System.out.println("Failed Read Active IDE Port"); + continue; + } + + // Connect IDE Socket + if (IDECommunicatorManager.setIDESocket() != true) { + System.out.println("Failed Connect IDE Socket"); + continue; + } + System.out.println("Connect IDE"); + int readSize = 0; + byte receiveBuffer[] = new byte[MAXBUFSIZE]; + InputStream in = null; + while (true) { + try { + in = IDECommunicatorManager.getSocket() + .getInputStream(); + System.out.print("IDE recvMessage Wait\n"); + readSize = in.read(receiveBuffer); + if (readSize > 0) { + IDECommunicatorManager + .commandReceiveMessage(new String( + receiveBuffer)); + } else { + break; + } + } catch (IOException e) { + e.printStackTrace(); break; } - } catch (IOException e) { - e.printStackTrace(); - break; } + Thread.sleep(100); + System.out.println("Disconnect IDE"); + } catch (InterruptedException e) { + break; } - System.out.println("waiting IDE"); - IDECommunicatorManager.setCurrentIDECommunicationStatus(false); - } - } - - private void resetSocket(){ - socket = null; - writer = null; - } - - public static boolean isConnected() { - if (socket == null) { - return false; - } - return true; - } - - - private boolean readActiveIdePort() { - boolean bSuccess = true; - // 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() { - 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 sendMessageToIDE( final String message ){ - - System.out.println("sendMessageOpenIDE " + message); - try { - if ( IDECommunicatorManager.getCurrentIDECommunicationStatus() ) { - writer = new BufferedWriter(new OutputStreamWriter( - socket.getOutputStream())); - writer.write(message); - writer.flush(); - } - } catch (IOException e) { - e.printStackTrace(); - return false; - } - return true; - } - // Check Current IDE Status, File Lock by IDE - static boolean checkActiveIde() { - - while (true) { - - 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) { // Active IDE - return true; - } else { - fileLock.release(); - Thread.sleep(1000); + public static boolean checkActiveIde() { + while (true) { + 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) { // Active IDE + return true; + } else { + fileLock.release(); + Thread.sleep(1000); + } + } catch (Exception e) { + return false; } - } catch (Exception e) { - return false; - } + } } - } - - - - } diff --git a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/listeners/TableTooltipListener.java b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/listeners/TableTooltipListener.java index d96e1a0..469db23 100644 --- a/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/listeners/TableTooltipListener.java +++ b/org.tizen.dynamicanalyzer/src/org/tizen/dynamicanalyzer/listeners/TableTooltipListener.java @@ -406,7 +406,7 @@ public class TableTooltipListener implements Listener { } sl = getSourceLine(item); - if ( ( null == sl ) || (!sl.isValid()) || (IDECommunicatorManager.getCurrentIDECommunicationStatus() == false) ) { + if ( ( null == sl ) || (!sl.isValid()) || (IDECommunicatorManager.isConnected() == false) ) { createErrorPopup(AnalyzerLabels.SOURCE_NOT_EXIST); return; } -- 2.7.4