[Title]apply checking multi instance
authormunkyu.im <munkyu.im@samsung.com>
Thu, 22 Mar 2012 15:07:47 +0000 (00:07 +0900)
committermunkyu.im <munkyu.im@samsung.com>
Thu, 22 Mar 2012 15:07:47 +0000 (00:07 +0900)
[Type]
[Module]
[Priority]
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]
[TestCase]

dist/linux/emulator-manager.jar [deleted file]
dist/win32/emulator-manager.jar [deleted file]
src/org/tizen/emulator/manager/tool/Launcher.java
src/org/tizen/emulator/manager/vms/VMProcess.java

diff --git a/dist/linux/emulator-manager.jar b/dist/linux/emulator-manager.jar
deleted file mode 100644 (file)
index b8c585d..0000000
Binary files a/dist/linux/emulator-manager.jar and /dev/null differ
diff --git a/dist/win32/emulator-manager.jar b/dist/win32/emulator-manager.jar
deleted file mode 100644 (file)
index 5fd4a27..0000000
Binary files a/dist/win32/emulator-manager.jar and /dev/null differ
index e869524..5c35cab 100644 (file)
@@ -2,7 +2,6 @@ package org.tizen.emulator.manager.tool;
 
 import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -13,6 +12,7 @@ import org.eclipse.swt.widgets.MessageBox;
 import org.eclipse.swt.widgets.Shell;
 import org.tizen.emulator.manager.EmulatorManager;
 import org.tizen.emulator.manager.vms.EmulatorVMs;
+import org.tizen.emulator.manager.vms.VMProcess;
 import org.tizen.emulator.manager.vms.VMsProperty;
 
 public class Launcher {
@@ -25,46 +25,14 @@ public class Launcher {
                String kvm = "";
                boolean exists = false;
                int portNo; 
-               String  StrPortNo = "";
-               int c;
-
-               //check if there is another Emulator instance with the same name
-               String PortPath = new FileIO().getTizenVmsArchPath() + File.separator + property.getName() + File.separator + ".port";
-               File file = new File(PortPath);
-               exists  = file.exists();
-               if(exists) {
-                       try {
-                               FileReader in = new FileReader(PortPath); 
-                               while((c=in.read()) != -1) {
-                                       StrPortNo = StrPortNo + (char)c;
-                               }
-                               in.close();
-                       } catch(Exception e1) {
-                               e1.printStackTrace();
-                       }
-
-                       //FIXME
-                       /*
-                       Socket so = null;
-                       try {
-                               Socket so = new Socket("localhost", Integer.parseInt(StrPortNo) +1 );
-                               so.close();
-                       } catch (UnknownHostException e) {
-                               // TODO Auto-generated catch block
-                       //      e.printStackTrace();
-                       } catch (IOException e) {
-                               // means portNo not in use
-                               MessageBox alert = new MessageBox(shell, SWT.OK);
-                               alert.setMessage("Can not launch this target: " + property.getName()
-                                               + "\n※ Predictable Causes are below."
-                                               +"\n1. Virtual target with the same name is running now."
-                                               + "\n2. Emulator might be terminated abnormally before."
-                                               + "\nIf case 1, exit existing virtual target and re-launch."
-                                               + "\nIf case 2, delete the port info file: " + PortPath);
-                               alert.open();
-                               return;
-                       }
-                       */
+        
+   VMProcess vmprocess = new VMProcess();
+               if(!vmprocess.checkIfRunning(property))
+               {
+                       MessageBox alert = new MessageBox(shell, SWT.OK);
+                       alert.setMessage("Can not execute emulator.\n Emulator instance with the same name is running now.");
+                       alert.open();
+                       return;
                }
 
                //check available port number.
@@ -123,7 +91,6 @@ public class Launcher {
                                        + height 
                                        + "-32@60"
                                        + " dpi=" + property.getConfiguration().getDevice().getDisplay().getDensity().getValue() 
-                                       + " root=/dev/vda rw"
                                        + " ip=10.0.2.16::10.0.2.2:255.255.255.0::eth0:none 5 virtio-9p");
                        }
                        else {
@@ -133,7 +100,6 @@ public class Launcher {
                                                + height 
                                                + "-32@60"
                                                + " dpi=" + property.getConfiguration().getDevice().getDisplay().getDensity().getValue() 
-                                               + " root=/dev/vda rw"
                                                + " ip=10.0.2.16::10.0.2.2:255.255.255.0::eth0:none 5");
                        }
 
@@ -207,15 +173,7 @@ public class Launcher {
                        cmd.add("-rtc");
                        cmd.add("base=utc");
                        cmd.add(kvm);
-                       /*
-                       if (System.getProperty("os.name").toLowerCase().indexOf("linux") > -1 && property.getConfiguration().getUsability().isHwVirtualization()) {
-                               File fi = new File("/dev/kvm");
-                               exists = fi.exists();
-                               if(exists) {
-                                       cmd.add("-enable-kvm");
-                               }
-                       }
-                       */
+                       
                        //      cmd.add("-redir");
                        //      cmd.add("tcp:1202:10.0.2.16:22");
                        //System.out.println(cmd);
index c22c183..4f53b3c 100644 (file)
@@ -1,10 +1,12 @@
 package org.tizen.emulator.manager.vms;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -32,6 +34,58 @@ public class VMProcess {
                errorMessage = "Throw unknown error";
        }
 
+// check if the emulator instance with the same name is running now.
+       public boolean checkIfRunning(VMsProperty property)
+       {
+               int isLinux = System.getProperty("os.name").toLowerCase().indexOf("linux");
+               int isWindows = System.getProperty("os.name").toLowerCase().indexOf("linux");
+               
+               try { 
+                            String line;
+               List<String> cmd = new ArrayList<String>();
+               if(isLinux > -1) {
+                       cmd.add("/bin/ps");
+                       cmd.add("-ef");
+                                       }
+               else if(isWindows > -1) {
+                       cmd.add("tasklist");
+                       cmd.add("/V");
+                       cmd.add("/FI");
+                       cmd.add("\"IMAGENAME eq emulator-x86\"");
+                                       }
+                       
+              ProcessBuilder pb = new ProcessBuilder(cmd);
+                                               Process p = pb.start();
+                                               BufferedReader stdOut = new BufferedReader(new InputStreamReader(p.getInputStream()));
+                                               
+                                               if(isLinux > -1) {
+                                                       while ((line = stdOut.readLine()) != null) {
+                                                                                                       if(line.contains(FileIO.getInstance().getVirtualTargetPath(property.getName()))) {
+                                                                               //                              System.out.println("exist in"+ line);
+                                                                                                               stdOut.close();
+                                                                                                               return false; 
+                                                                                                       }
+                                                       }       
+                                               }
+                                               else if(isWindows > -1) {
+                                                       while ((line = stdOut.readLine()) != null) {
+                                                               if(line.contains(property.getName()+ ":261")) {
+                                                                       //System.out.println("exist in"+ line);
+                                                                       stdOut.close();
+                                                                       return false; 
+                                                               }
+                                                       }
+                                               }               
+                                               stdOut.close();
+                                               System.out.println("not exist anywhere");
+                                               return true;                                                     
+           } catch (Exception err) { 
+               err.printStackTrace();
+               return false;
+           }  
+       }
+
+
        public VMsProperty modify(VMsProperty property, VMPropertyValue oldVM, VMPropertyValue newVM) {
                if (!newVM.resolution.equals(oldVM.resolution)) {
                        isModify = true;