import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import org.tizen.sdblib.exception.TimeoutException;
import org.tizen.sdblib.service.SyncResult;
+/**
+ * @author greatim
+ *
+ */
public class Communicator30 extends BaseCommunicator {
static final boolean PRINT_CONTROL_LOG_TOFILE = true;
@Override
public HostResult startTrace() {
+ DeviceInfo curDev = GlobalInformation.getCurrentDeviceInfo();
+ if (curDev == null) {
+ return HostResult.ERR_DEVICE_IS_NULL;
+ }
+
DACommunicator.setRunning(true);
UserSpaceInst userSpaceInst = getUserSpaceInst();
Logger.performance("TEST", "Start Trace", "Set application inst");
Thread dataThread = new Thread(null, new DataChannelThread(),
AnalyzerConstants.COMMUNICATOR_RECEIVE_THREAD);
// start message send
- HostResult result = handleControlMessage(GlobalInformation.getCurrentDeviceInfo(), ret);
+ HostResult result = handleControlMessage(curDev, ret);
Logger.performance("TEST", "Start Trace", "Make and Send start message");
// FIX
byte[] preMsg = null;
DeviceInfo curDevice = GlobalInformation.getCurrentDeviceInfo();
+ if (curDevice == null) {
+ return HostResult.ERR_DEVICE_IS_NULL;
+ }
if (AnalyzerConstants.RUNNING_PROCESS.equals(selectedPkg.getPackageId())) {
Map<Integer, String> selProcess = selectedPkg.getMainApp().getRunningProcesses();
// send config message
byte[] config = ByteUtil.toBytes(AnalyzerConstants.MSG_CONFIG);
RunTimeConfiguration rt = new RunTimeConfiguration();
- rt.setFeatures(SettingDataManager.getInstance().getConfiguration(devInfo), SettingDataManager
- .getInstance().getPreConfiguration(devInfo));
-// TODO : check and remove
-// rt.setSystemTracePeriod(ConfigureManager.getInstance().getSystemPeriod());
-// rt.setSamplingPeriod(ConfigureManager.getInstance().getSamplingPeriod());
-
+ rt.setFeatures(SettingDataManager.getInstance().getConfiguration(devInfo),
+ SettingDataManager.getInstance().getPreConfiguration(devInfo));
+ // TODO : check and remove
+ // rt.setSystemTracePeriod(ConfigureManager.getInstance().getSystemPeriod());
+ // rt.setSamplingPeriod(ConfigureManager.getInstance().getSamplingPeriod());
+
TargetData target = SettingDataManager.getInstance().getConnectedTarget();
- int systemPeriod = target.getSelectedFeatureValue(ConfigureLabels.SYSTEM_INFOMATION_SAMPLING_PERIODIC);
+ int systemPeriod = target
+ .getSelectedFeatureValue(ConfigureLabels.SYSTEM_INFOMATION_SAMPLING_PERIODIC);
if (systemPeriod < 0) {
systemPeriod = target.getAvailableFeature(
ConfigureLabels.SYSTEM_INFOMATION_SAMPLING_PERIODIC).getDefaultValue();
}
- int samplingPeriod = target.getSelectedFeatureValue(ConfigureLabels.FUNCTION_PROFILING_SAMPLING_PERIODIC);
+ int samplingPeriod = target
+ .getSelectedFeatureValue(ConfigureLabels.FUNCTION_PROFILING_SAMPLING_PERIODIC);
if (samplingPeriod < 0) {
samplingPeriod = target.getAvailableFeature(
ConfigureLabels.FUNCTION_PROFILING_SAMPLING_PERIODIC).getDefaultValue();
byte[] stop = ByteUtil.toBytes(AnalyzerConstants.MSG_STOP_SWAP);
int length = 0;
byte[] stopMsg = ByteUtil.getByte(stop, length);
- HostResult result = handleControlMessage(GlobalInformation.getCurrentDeviceInfo(),
- stopMsg);
+ HostResult result = handleControlMessage(stopMsg);
if (result.isSuccess()) {
if (isCorrectAck(MSG_STOP_ACK, result)) {
}
}
- @Override
- public HostResult handleControlMessage(String message) {
- return HostResult.SUCCESS;
+ public HostResult handleControlMessage(byte[] message) {
+ DeviceInfo curDev = GlobalInformation.getCurrentDeviceInfo();
+ if (curDev != null) {
+ return handleControlMessage(curDev, message);
+ } else {
+ return HostResult.ERR_DEVICE_IS_NULL;
+ }
}
public HostResult handleControlMessage(DeviceInfo device, byte[] message) {
+ if (device != null) {
+ return handleControlMessage(device, message, true,
+ AnalyzerConstants.CONTROL_SOCKET_TIMEOUT);
+ } else {
+ return HostResult.ERR_DEVICE_IS_NULL;
+ }
+ }
+
+ /**
+ * send host message to manager by control socket, and receive ack message.
+ *
+ * @param device
+ * target device to send message
+ * @param message
+ * message to send
+ * @param waitack
+ * whether receive ack message or not
+ * @param timeout
+ * when wait for ack message, time limit of waiting by millisec
+ * @return HostResult
+ */
+ public HostResult handleControlMessage(DeviceInfo device, byte[] message, boolean waitack,
+ int timeout) {
HostResult result = null;
Socket controlSock = device.getControlSock();
printWriter.printf("%02x ", message[k]);
printWriter.printf("\n");
}
- controlSock.getOutputStream().write(message);
- Logger.debug("wait for ack... [send message : " + ByteUtil.toInt(message) + " ]");
- int readsize = -1;
- byte[] cbuf = new byte[DACommunicator.READ_BUFFER_SIZE];
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ synchronized (controlSock) {
+ controlSock.getOutputStream().write(message);
+ if (!waitack) {
+ return HostResult.SUCCESS;
+ }
+
+ Logger.debug("wait for ack of message [" + ByteUtil.toInt(message) + "]");
- Logger.debug("blocked");
- blocked = true;
+ int readsize = -1;
+ byte[] cbuf = new byte[DACommunicator.READ_BUFFER_SIZE];
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- readsize = controlSock.getInputStream().read(cbuf, 0, CONTROL_HEADER_SIZE);
- if (readsize == CONTROL_HEADER_SIZE) {
- baos.write(cbuf, 0, readsize);
- int payloadsize = ByteUtil.toInt(cbuf, INT_SIZE);
+ Logger.debug("blocked");
+ blocked = true;
+
+ int oldtimeout = controlSock.getSoTimeout();
+ controlSock.setSoTimeout(timeout);
+ InputStream in = controlSock.getInputStream();
- do {
- readsize = controlSock.getInputStream().read(cbuf);
+ readsize = in.read(cbuf, 0, CONTROL_HEADER_SIZE);
+ if (readsize == CONTROL_HEADER_SIZE) {
baos.write(cbuf, 0, readsize);
+ int payloadsize = ByteUtil.toInt(cbuf, INT_SIZE);
- payloadsize -= readsize;
- } while (payloadsize > 0);
- blocked = false;
-
- readsize = baos.size();
- cbuf = baos.toByteArray();
- if (PRINT_CONTROL_LOG_TOFILE && printWriter != null) {
- printWriter.printf("recv :");
- for (int k = 0; k < readsize; k++)
- printWriter.printf("%02x ", cbuf[k]);
- printWriter.printf("\n");
+ do {
+ readsize = in.read(cbuf);
+ baos.write(cbuf, 0, readsize);
+
+ payloadsize -= readsize;
+ } while (payloadsize > 0);
+ blocked = false;
+ Logger.debug("unblocked");
}
- Logger.debug("unblocked");
- if (readsize > 0) {
- byte[] buf = new byte[readsize];
- System.arraycopy(cbuf, 0, buf, 0, readsize);
- result = HostResult.SUCCESS;
- int ackId = ByteUtil.toInt(buf);
+ controlSock.setSoTimeout(oldtimeout);
+ }
- // for debug
- System.out.print("ack id : ");
- AnalyzerUtil.printHexdecimal(ackId);
- Logger.debug();
+ int readsize = baos.size();
+ byte[] cbuf = baos.toByteArray();
+ if (PRINT_CONTROL_LOG_TOFILE && printWriter != null) {
+ printWriter.printf("recv :");
+ for (int k = 0; k < readsize; k++)
+ printWriter.printf("%02x ", cbuf[k]);
+ printWriter.printf("\n");
+ }
- result.setRet(buf);
- } else {
- result = HostResult.ERR_CONTROL_SOCKET_CONNECTION_CLOSED;
- result.setMessage(result.getMessage());
- }
+ if (readsize > 0) {
+ byte[] buf = new byte[readsize];
+ System.arraycopy(cbuf, 0, buf, 0, readsize);
+ result = HostResult.SUCCESS;
+ int ackId = ByteUtil.toInt(buf);
+
+ // for debug
+ System.out.print("ack id : ");
+ AnalyzerUtil.printHexdecimal(ackId);
+ Logger.debug();
+
+ result.setRet(buf);
} else {
- blocked = false;
- result = HostResult.ERR_EXCEPTION_OCCURRED;
- result.setMessage("Failed to read Ack Header");
+ result = HostResult.ERR_RECEIVE_ACK_FAIL;
+ result.setMessage(result.getMessage());
}
+ } else {
+ result = HostResult.ERR_CONTROL_SOCKET_CONNECTION_CLOSED;
+ result.setMessage("Control socket connection is closed");
}
} catch (SocketException e) {
result = HostResult.ERR_EXCEPTION_OCCURRED;
public HostResult sendScreenShotRequest() {
byte[] msg = ByteUtil.getByte(AnalyzerConstants.MSG_GET_SCREENSHOT, 0);
- HostResult result = HostResult.SUCCESS;
- Socket controlSock = GlobalInformation.getCurrentDeviceInfo().getControlSock();
- try {
- if (null != controlSock && !controlSock.isClosed()) {
- controlSock.getOutputStream().write(msg);
- } else {
- Logger.debug("control socket is closed");
- result = HostResult.ERR_CONTROL_SOCKET_CONNECTION_CLOSED;
- }
- } catch (SocketException e) {
- result = HostResult.ERR_EXCEPTION_OCCURRED;
- result.setMessage(e.getMessage());
- e.printStackTrace();
- return result;
- } catch (SocketTimeoutException e) {
- result = HostResult.ERR_EXCEPTION_OCCURRED;
- result.setMessage(e.getMessage());
- e.printStackTrace();
- return result;
- } catch (IOException e) {
- result = HostResult.ERR_EXCEPTION_OCCURRED;
- result.setMessage(e.getMessage());
- e.printStackTrace();
- return result;
- } finally {
- blocked = false;
- }
- return result;
+ return handleControlMessage(GlobalInformation.getCurrentDeviceInfo(), msg, false, 0);
}
public HostResult sendSWAPMessage(int messageId, List<BinarySettingData> settings) {
+ DeviceInfo curDev = GlobalInformation.getCurrentDeviceInfo();
+ if (curDev == null) {
+ return HostResult.ERR_DEVICE_IS_NULL;
+ }
+
byte[] msg = new byte[0];
int count = settings.size();
int failedCount = 0;
msg = ByteUtil.getByte(msg, length, libInst);
// ByteUtils.printByteArrayForStart(msg);
- HostResult result = handleControlMessage(GlobalInformation.getCurrentDeviceInfo(), msg);
+ HostResult result = handleControlMessage(curDev, msg);
int ackMsg = AnalyzerConstants.MSG_SWAP_INST_ADD_ACK;
if (messageId == AnalyzerConstants.MSG_SWAP_INST_REMOVE) {
}
public Map<Integer, String> sendProcessAddInfoMessage(List<Integer> pids) {
+ DeviceInfo curDev = GlobalInformation.getCurrentDeviceInfo();
+ if (curDev == null) {
+ return null;
+ }
+
Map<Integer, String> infoMap = null;
int pidcount = pids.size();
int length = sendbin.length;
byte[] msg = ByteUtil.getByte(AnalyzerConstants.MSG_GET_PROCESS_ADD_INFO, length,
sendbin);
- HostResult result = handleControlMessage(GlobalInformation.getCurrentDeviceInfo(), msg);
+ HostResult result = handleControlMessage(curDev, msg, true,
+ AnalyzerConstants.PROC_ADD_INFO_TIMEOUT);
if (result.isSuccess()
&& isCorrectAck(AnalyzerConstants.MSG_GET_PROCESS_ADD_INFO_ACK, result)) {
int length = sendBin.length;
byte[] msg = ByteUtil.getByte(AnalyzerConstants.MSG_BINARY_INFO, length, sendBin);
- HostResult result = handleControlMessage(GlobalInformation.getCurrentDeviceInfo(), msg);
+ HostResult result = handleControlMessage(msg);
if (!result.isSuccess() || !isCorrectAck(MSG_BINARY_INFO_ACK, result)) {
return HostResult.ERR_BIN_INFO_GET_FAIL;