package org.tizen.dynamicanalysis.ide.eplugin.communication;
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.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
import org.eclipse.ui.PlatformUI;
import org.tizen.common.core.application.InstallPathConfig;
public class DAServerManager extends Thread {
private final int MAXBUFSIZE = 1024;
- private int port = 0;
+ private int idePort = 0;
private static DAServerManager instance;
private ServerSocket serverSocket = null;
serverSocket = setServePort();
if (serverSocket != null) {
- // Save Server port
- if (savePortFile() != true) {
- DALog.printLog("failed Save Server port"); //$NON-NLS-1$
- continue;
- }
-
// Set Active IDE to File
if (setActiveDAPlugin() != true) {
DALog.printLog("failed Set Active IDE to File "); //$NON-NLS-1$
- continue;
+ } else {
+ // Save Server port
+ if (savePortFile(true) != true) {
+ DALog.printLog("failed Save Server port"); //$NON-NLS-1$
+ continue;
+ }
}
+
// Communication IDE - DA
while (true) {
closeClientSokcet();
// Waiting Receive Message
waitingDataReceive();
+ if (savePortFile(true) != true) {
+ DALog.printLog("failed Save Server port"); //$NON-NLS-1$
+ continue;
+ }
DALog.printLog("Disconneced DA"); //$NON-NLS-1$
}
} else {
}
}
+ public boolean isAnotherIDEConnected() {
+ int saveFilePort = readActiveIdePort();
+ if (saveFilePort == -1) {
+ if (savePortFile(false) != true) {
+ DALog.printLog("failed Save Server port"); //$NON-NLS-1$
+ }
+ return false;
+ } else if (saveFilePort != -1 && saveFilePort == idePort) {
+ if (savePortFile(false) != true) {
+ DALog.printLog("failed Save Server port"); //$NON-NLS-1$
+ }
+ return false;
+ } else if (saveFilePort != -1 && saveFilePort != idePort) {
+ return true;
+ } else {
+ DALog.printLog("isAnotherIDEConnected - unknown status");
+ }
+ return false;
+ }
+
private boolean checkInstalledTizenSDK() {
String installPath = InstallPathConfig.getUserDataPath();
File logs = new File(installPath.toString());
ServerSocket serverSocket = null;
serverSocket = getServerSocket(0);
if (serverSocket != null) {
- port = serverSocket.getLocalPort();
- DALog.printLog("IDE port " + port); //$NON-NLS-1$
+ idePort = serverSocket.getLocalPort();
+ DALog.printLog("IDE port " + idePort); //$NON-NLS-1$
return serverSocket;
}
return serverSocket;
}
+ public String getPortFile() {
+ StringBuffer savePath = new StringBuffer();
+ savePath.append(InstallPathConfig.getUserDataPath())
+ .append(File.separatorChar).append(DANAIC_ANALYZER)
+ .append(File.separatorChar).append(SAVE);
+
+ File logs = new File(savePath.toString());
+ if (!logs.exists()) {
+ logs.mkdirs();
+ }
+
+ savePath.append(File.separatorChar).append(CURRENT_ACTIVE_IDE_PORT);
+ return savePath.toString();
+ }
+
// Save Current Port
- private boolean savePortFile() {
+ public boolean savePortFile(boolean isInitPort) {
boolean bSuccess = true;
try {
- StringBuffer savePath = new StringBuffer();
- savePath.append(InstallPathConfig.getUserDataPath())
- .append(File.separatorChar).append(DANAIC_ANALYZER)
- .append(File.separatorChar).append(SAVE);
-
- File logs = new File(savePath.toString());
- if (!logs.exists()) {
- logs.mkdirs();
- }
-
- savePath.append(File.separatorChar).append(CURRENT_ACTIVE_IDE_PORT);
-
- File file = new File(savePath.toString());
+ File file = new File(getPortFile());
FileOutputStream fos = null;
fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
- dos.writeInt(port);
+ if (isInitPort) {
+ dos.writeInt(-1);
+ } else {
+ dos.writeInt(idePort);
+ }
+
dos.flush();
bSuccess = true;
return bSuccess;
}
+ // read Current Port
+ private int readActiveIdePort() {
+ int port = -1;
+ FileInputStream fis = null;
+ DataInputStream dis = null;
+ try {
+ fis = new FileInputStream(getPortFile());
+ dis = new DataInputStream(fis);
+ try {
+ port = dis.readInt();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ if (null != fis) {
+ fis.close();
+ }
+ if (null != dis) {
+ dis.close();
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ } catch (FileNotFoundException e) {
+ DALog.printLog("DAServerManager - readActiveIdePort - FileNotFoundException");
+ } finally {
+ try {
+ if (null != fis) {
+ fis.close();
+ }
+ if (null != dis) {
+ dis.close();
+ }
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ return port;
+ }
+
// Set Active DA_Plugin
private boolean setActiveDAPlugin() {
File file = null;
+ FileLock fileLock = null;
FileChannel fchannel = null;
StringBuffer savePortPath = new StringBuffer();
DALog.printLog("DAServerManager - setActiveDAPlugin - FileNotFoundException");
}
try {
- fchannel.tryLock();
+ fileLock = fchannel.tryLock();
} catch (IOException e) {
DALog.printLog("DAServerManager - setActiveDAPlugin - IOException");
}
+ if (fileLock == null) {
+ return false;
+ }
return true;
}
public Boolean getRecvDAReadyStatus() {
return recvDAReadyStatus;
}
-
+
public void setRecvDAReadyStatus(boolean status) {
recvDAReadyStatus = status;
}