[title] sdb manager add
authorwonhyoung2.park <wonhyoung2.park@samsung.com>
Fri, 28 Oct 2011 04:43:15 +0000 (13:43 +0900)
committerwonhyoung2.park <wonhyoung2.park@samsung.com>
Fri, 28 Oct 2011 04:43:15 +0000 (13:43 +0900)
[Type]
[Module]
[Priority]
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]
[TestCase]

com.samsung.dynamicanalyzer/src/com/samsung/dynamicanalyzer/utils/SDBManager.java [new file with mode: 0644]

diff --git a/com.samsung.dynamicanalyzer/src/com/samsung/dynamicanalyzer/utils/SDBManager.java b/com.samsung.dynamicanalyzer/src/com/samsung/dynamicanalyzer/utils/SDBManager.java
new file mode 100644 (file)
index 0000000..8a0596c
--- /dev/null
@@ -0,0 +1,586 @@
+package com.samsung.dynamicanalyzer.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.List;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+class RecieveThread implements Runnable {
+       class AddBufferThread extends TimerTask {
+
+               @Override
+               public void run() {
+                       addBufferToList();
+               }
+       }
+
+       List<String> buffer;
+       Lock lock = new ReentrantLock();
+
+       public void addBufferToList() {
+               lock.lock();
+               if (null == buffer || 0 == buffer.size()) {
+                       lock.unlock();
+                       return;
+               }
+               //TODO remove 2 comment
+               //LogParser.getLogList().add(buffer);
+               buffer = new ArrayList<String>();
+               //LogParser.startLogParsingThread();
+               lock.unlock();
+
+       }
+       @Override
+       public void run() {
+
+               int cnt = 0;
+               Timer timer;
+               timer = new Timer();
+               AddBufferThread addBufferThread = new AddBufferThread();
+               Calendar date = Calendar.getInstance();
+               date.set(Calendar.MILLISECOND, 5);
+
+               try {
+                       buffer = new ArrayList<String>();
+                       timer.scheduleAtFixedRate(addBufferThread, date.getTime(), 1000);
+                       BufferedReader br = new BufferedReader(new InputStreamReader(
+                                       SDBManager.getSock().getInputStream()));
+
+                       String message = null;
+                       String[] msgs = null;
+                       while (null != (message = br.readLine())) {
+                               // System.out.println("client recieve : " + cnt + " : " +
+                               // message);
+
+                               msgs = message.split("|", 4);
+
+                               // //////// handle the message
+                               if (msgs[1].charAt(0) == '5') {
+                                       buffer.add(msgs[3]);
+                                       buffer.add(br.readLine());
+                               } else if (msgs[1].charAt(0) == '6') {
+
+                               } else if (msgs[1].charAt(0) == '7') {
+                                       SDBManager.getSock().close();
+                                       br.close();
+                                       timer.cancel();
+                                       addBufferToList();
+                                       //TODO change state
+                                       //SDBManager
+                                       //              .changeRecordingState(AnalyzerConstants.PERFORMANCEANALYZER_RECORD_START);
+                                       break;
+                               }
+
+                               // //////// Add buffer to list
+                               if (cnt == 100) {
+                                       //addBufferToList();
+                                       cnt = 0;
+                               } else {
+                                       cnt++;
+                               }
+
+                       }
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+}
+
+class SLPAppRunThread implements Runnable {
+
+       @Override
+       public void run() {
+               Runtime rt = Runtime.getRuntime();
+               Process ps = null;
+               try {
+                       SDBManager.setAppRunning(true);
+                       ps = rt.exec(SDBManager.getAppCommand());
+                       //TODO change state
+//                     SDBManager
+//                                     .changeRecordingState(AnalyzerConstants.PERFORMANCEANALYZER_RECORD_STOP);
+                       BufferedReader br = new BufferedReader(new InputStreamReader(
+                                       ps.getInputStream()));
+
+                       // String message;
+
+                       // while (null != (message = br.readLine())) {
+                       while (null != br.readLine()) {
+                               // System.out.println("run err message : " + message);
+                       }
+                       System.out.println(">>> app exit !!");
+                       br.close();
+
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+}
+
+public class SDBManager {
+       private static final String sdbPath = "/samsung-sdk/DynamicAnalysis/sdb";// TODO
+                                                                                                                                                               // fix
+       // path
+       private static String homePath = null;
+
+       public static String getHomePath() {
+               return homePath;
+       }
+
+       public static void setHomePath(String homePath) {
+               SDBManager.homePath = homePath;
+       }
+
+       public static String getSDBpath() {
+               return homePath + sdbPath;
+       }
+
+       private static String serial = null;
+
+       private static String appCommand = null;
+       private static Socket sock = null;
+       private static boolean isAppRunning = false;
+       private static String appName = null;
+
+       public static String getAppName() {
+               return appName;
+       }
+
+       public static void setAppName(String appName) {
+               SDBManager.appName = appName;
+       }
+
+       public static void setAppRunning(boolean isAppRunning) {
+               SDBManager.isAppRunning = isAppRunning;
+       }
+
+       public static boolean isAppRunning() {
+               return isAppRunning;
+       }
+
+       public static Socket getSock() {
+               return sock;
+       }
+
+       public static String getAppCommand() {
+               return appCommand;
+       }
+
+       public static String getSerial() {
+               return serial;
+       }
+
+       public static void setSerial(String serial) {
+               SDBManager.serial = serial;
+       }
+//TODO remove comment
+       /*
+       public static void changeRecordingState(final int state) {
+               Display.getDefault().asyncExec(new Runnable() {
+                       @Override
+                       public void run() {
+                               CommunicatorManager.getInstance().setRecordState(state);
+                       }
+               });
+       }
+       */
+       
+       
+
+       public static ArrayList<String> getDevices() {
+               ArrayList<String> deviceList = new ArrayList<String>();
+               if (null == homePath)
+                       getHomeEvn();
+               int cnt = 0;
+               try {
+                       Runtime rt = Runtime.getRuntime();
+                       Process ps = rt.exec(homePath + sdbPath + " devices");
+
+                       BufferedReader br = new BufferedReader(new InputStreamReader(
+                                       ps.getInputStream()));
+                       String message = null;
+                       String serial = null;
+                       while (!(message = br.readLine()).equals("")) {
+                               if (0 == cnt) {
+                                       cnt++;
+                                       continue;
+                               }
+                               if (-1 != message.indexOf("     ")) {
+                                       serial = message.substring(0, message.indexOf(" "));
+                                       deviceList.add(serial);
+                               }
+                       }
+                       br.close();
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+               return deviceList;
+       }
+
+       public static String getSDBPathWithSerial() {
+               if (null == sdbPath || null == serial) {
+                       return null;
+               }
+               return homePath + sdbPath + " -s " + serial;
+       }
+
+       public static ArrayList<String> getAppList() {
+               ArrayList<String> appList = new ArrayList<String>();
+
+               if (null == serial) {
+                       return null;
+               }
+               if (null == homePath)
+                       getHomeEvn();
+               try {
+                       Runtime rt = Runtime.getRuntime();
+                       Process ps = rt.exec(getSDBPathWithSerial()
+                                       + " shell ls /opt/apps -1");
+
+                       BufferedReader br = new BufferedReader(new InputStreamReader(
+                                       ps.getInputStream()));
+                       BufferedReader ebr = new BufferedReader(new InputStreamReader(
+                                       ps.getErrorStream()));
+                       String message = null;
+
+                       if (null != (message = ebr.readLine())) {
+                               serial = null;
+                               return null;
+                       }
+                       // String app = null;
+                       while (null != (message = br.readLine())) {
+                               appList.add(message);
+                       }
+                       br.close();
+                       ebr.close();
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+               return appList;
+       }
+
+       private static boolean isSoLoaded() {
+
+               if (null == serial) {
+                       return false;
+               }
+               if (null == homePath)
+                       getHomeEvn();
+               try {
+                       Runtime rt = Runtime.getRuntime();
+                       Process ps = rt.exec(getSDBPathWithSerial()
+                                       + " shell mkdir /home/root/.DynamicAnalysis");
+                       // System.out.println(homePath + getSDBPathWithSerial()
+                       // + " shell mkdir /home/root/.Dynamic/home/rootnalysis");
+                       BufferedReader br = new BufferedReader(new InputStreamReader(
+                                       ps.getInputStream()));
+                       BufferedReader ebr = new BufferedReader(new InputStreamReader(
+                                       ps.getErrorStream()));
+                       // String message = null;
+
+                       if (null != (ebr.readLine())) {
+                               System.out.println(">>>> no device (is loaded) !! ");
+                               serial = null;
+                               return false;
+                       }
+
+                       if (null == (br.readLine())) {
+                               System.out.println("not loaded");
+                               return false;
+                       } else {
+                               // System.out.println("loaded : " + message);
+                               return true;
+                       }
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+               return false;
+       }
+
+       private static boolean loadSo() {
+               boolean isLoaded = isSoLoaded();
+               if (null == serial) {
+                       return false;
+               } else if (!isLoaded) {
+                       // get monitor that user select
+                       // TODO remove comment
+
+                       try {
+                               Runtime rt = Runtime.getRuntime();
+                               // String message;
+                               StringBuilder command = new StringBuilder();
+                               command.append(getSDBPathWithSerial());
+                               command.append(" push ");
+                               command.append(homePath);
+                               String baseCommand = command.toString();
+                               // load helper so
+
+                               // command.append("/samsung-sdk/SDK/dynamic-analysis/libdaprobe.so /usr/lib");
+                               String loadHelperCommand = baseCommand
+                                               + "/samsung-sdk/DynamicAnalysis/lib/libdaprobe.so /usr/lib";
+                               Process ps = rt.exec(loadHelperCommand);
+
+                               BufferedReader ebr = new BufferedReader(new InputStreamReader(
+                                               ps.getErrorStream()));
+                               BufferedReader br = new BufferedReader(new InputStreamReader(
+                                               ps.getInputStream()));
+
+                               if (null == ebr.readLine()) {
+                                       System.out.println(">>>> no device (load helper)");
+                                       serial = null;
+                                       return false;
+                               }
+                               if (null != (br.readLine())) {
+                                       // System.out.println(">> load helper : " + message);
+                               }
+                               ebr.close();
+                               br.close();
+
+                               String loadScriptCommand = baseCommand
+                                               + "/samsung-sdk/DynamicAnalysis/Script/getsockstate.sh /home/root/.DynamicAnalysis";
+                               ps = rt.exec(loadScriptCommand);
+
+                               ebr = new BufferedReader(new InputStreamReader(
+                                               ps.getErrorStream()));
+                               br = new BufferedReader(new InputStreamReader(
+                                               ps.getInputStream()));
+
+                               if (null == ebr.readLine()) {
+                                       // System.out.println(">>>> no device (load script)) !! ");
+                                       serial = null;
+                                       return false;
+                               }
+                               if (null != (br.readLine())) {
+                                       // System.out.println(">> load script : " + message);
+                               }
+
+                               ebr.close();
+
+                               String loadMemorySoCommand = baseCommand
+                                               + "/samsung-sdk/DynamicAnalysis/lib/libdamemory.so /home/root/.DynamicAnalysis";
+                               ps = rt.exec(loadMemorySoCommand);
+
+                               ebr = new BufferedReader(new InputStreamReader(
+                                               ps.getErrorStream()));
+                               br = new BufferedReader(new InputStreamReader(
+                                               ps.getInputStream()));
+
+                               if (null == ebr.readLine()) {
+                                       // System.out.println(">>>> no device (load memory so)) !! ");
+                                       serial = null;
+                                       return false;
+                               }
+                               if (null != (br.readLine())) {
+                                       // System.out.println(">> load memory : " + message);
+                               }
+                               ebr.close();
+
+                               String loadNetstatCommand = baseCommand
+                                               + "/samsung-sdk/DynamicAnalysis/bin/netstat /home/root/.DynamicAnalysis";
+                               // System.out.println(command.toString());
+                               ps = rt.exec(loadNetstatCommand);
+
+                               ebr = new BufferedReader(new InputStreamReader(
+                                               ps.getErrorStream()));
+                               br = new BufferedReader(new InputStreamReader(
+                                               ps.getInputStream()));
+
+                               if (null == ebr.readLine()) {
+                                       // System.out.println(">>>> no device (load netstat)) !! ");
+                                       serial = null;
+                                       return false;
+                               }
+                               if (null != (br.readLine())) {
+                                       // System.out.println(">> load memory : " + message);
+                               }
+
+                               // load brobe so
+                               // TODO remove commant
+                               /*
+                                * List<Monitor> monitors = AnalyzerManager.getMonitors(); int
+                                * cnt = monitors.size(); for (int i = 0; i < cnt; i++) {
+                                * command.append(homePath);
+                                * command.append("/samsung-sdk/SDK/dynamic-analysis/da_");
+                                * command.append(monitors.get(i).getId());
+                                * command.append(".so /home/root/.DynamicAnalysis");
+                                * 
+                                * Process ps = rt.exec(command.toString());
+                                * 
+                                * // BufferedReader br = new BufferedReader( // new
+                                * InputStreamReader(ps.getInputStream())); BufferedReader ebr =
+                                * new BufferedReader( new
+                                * InputStreamReader(ps.getErrorStream())); String message =
+                                * null;
+                                * 
+                                * if (null != (message = ebr.readLine())) {
+                                * System.out.println(">>>> no device !! "); serial = null;
+                                * return false; }
+                                * 
+                                * // if (null == (message = br.readLine())) { //
+                                * System.out.println("not loaded"); // return false; // } else
+                                * { // System.out.println("loaded : " + message); // return
+                                * true; // } }
+                                */
+                               ebr.close();
+                       } catch (Exception e) {
+                               e.printStackTrace();
+                       }
+
+                       return true;
+               } else {
+                       return true;
+               }
+       }
+
+       public static boolean forward() {
+               if (!isSockCreated()) {
+                       System.out.println("forward : sock not yet created");
+                       return false;
+               }
+               if (null == homePath)
+                       getHomeEvn();
+
+               try {
+                       Runtime rt = Runtime.getRuntime();
+                       Process ps = rt.exec(getSDBPathWithSerial()
+                                       + " forward tcp:8000 tcp:8001");
+                       BufferedReader br = new BufferedReader(new InputStreamReader(
+                                       ps.getInputStream()));
+                       BufferedReader ebr = new BufferedReader(new InputStreamReader(
+                                       ps.getErrorStream()));
+                       String message = null;
+
+                       if (null != (message = ebr.readLine())) {
+                               System.out.println(">>>> no device (forward) !! ");
+                               serial = null;
+                               return false;
+                       }
+
+                       if (null == (message = br.readLine())) {
+                               return true;
+                       } else {
+                               System.out.println("error forward: " + message);
+                               return false;
+                       }
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+               return false;
+       }
+
+       private static String getShortAppName(String longAppName) {
+               String shortAppName = null;
+               int index = longAppName.lastIndexOf(".");
+               if (null != longAppName && -1 != longAppName.lastIndexOf(".")) {
+                       shortAppName = longAppName.substring(index + 1,
+                                       longAppName.length());
+               }
+               return shortAppName;
+       }
+
+       public static void runApplication() {
+               if (!loadSo()) {
+                       return;
+               }
+
+               StringBuilder ldpreload = new StringBuilder();
+               ldpreload
+                               .append("LD_PRELOAD=/opt/home/root/.DynamicAnalysis/libdamemory.so"); // TODO
+               // for문돌면서선택된모니터추가하도록수정
+
+               // .append("LD_PRELOAD=/opt/home/root/.DynamicAnalysis/libdamemory.so");
+
+               // run application
+               if (null == homePath)
+                       getHomeEvn();
+               appCommand = getSDBPathWithSerial() +  " shell " + ldpreload.toString()
+                               + " /opt/apps/" + appName + "/bin/" + getShortAppName(appName);
+
+               // TODO record button disable
+
+               Runnable runApp = new SLPAppRunThread();
+               Thread thread = new Thread(runApp);
+               thread.start();
+
+               return;
+
+               // get selected monitor & make command line (using LD_PRELOAD,
+               // LD_LIBRARY_PATH)
+
+       }
+
+       private static void getHomeEvn() {
+               // TODO need to confirm real SLP_SDK install path
+               homePath = System.getenv("HOME");
+       }
+
+       private static boolean isSockCreated() {
+
+               for (int i = 0; i < 20; i++) {
+                       if (!getSockState()) {
+                               try {
+                                       Thread.sleep(200);
+                               } catch (InterruptedException e) {
+                                       e.printStackTrace();
+                               }
+                       } else
+                               return true;
+               }
+
+               return false;
+       }
+
+       private static boolean getSockState() {
+               if (null == homePath)
+                       getHomeEvn();
+               try {
+                       Runtime rt = Runtime.getRuntime();
+                       Process ps = rt.exec(getSDBPathWithSerial()
+                                       + " shell /home/root/.DynamicAnalysis/getsockstate.sh");
+                       BufferedReader br = new BufferedReader(new InputStreamReader(
+                                       ps.getInputStream()));
+
+                       String message = null;
+                       while (null != (message = br.readLine())) {
+                               if (message.contains("LISTEN")) {
+                                       System.out.println("sock listen ok!");
+                                       return true;
+                               }
+                       }
+                       br.close();
+
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+               return false;
+       }
+
+       public static void createSocket() {
+               System.out.println("wh > host create sock");
+               try {
+                       sock = new Socket("localhost", 8000);
+                       if (null == sock) {
+                               System.out.println(">>> wh sock connection fail ! ");
+                       }
+               } catch (UnknownHostException e) {
+                       e.printStackTrace();
+               } catch (IOException e) {
+                       e.printStackTrace();
+               }
+       }
+
+       public static void createRecieveThread() {
+               Runnable rtr = new RecieveThread();
+               Thread rt = new Thread(rtr);
+               rt.start();
+       }
+
+}