communication: some optimization for packet parsing 22/16722/1
authorGiWoong Kim <giwoong.kim@samsung.com>
Mon, 24 Feb 2014 06:33:01 +0000 (15:33 +0900)
committerGiWoong Kim <giwoong.kim@samsung.com>
Mon, 24 Feb 2014 08:37:08 +0000 (17:37 +0900)
In skin, take out the variable declaration from while loop.
To improve efficiency, delete a RecieveCommand class.

Change-Id: Icaaaee5a1d6c83000bcc91980fcff598fe842284
Signed-off-by: GiWoong Kim <giwoong.kim@samsung.com>
tizen/src/skin/client/src/org/tizen/emulator/skin/comm/ICommunicator.java
tizen/src/skin/client/src/org/tizen/emulator/skin/comm/sock/SocketCommunicator.java

index f16cf1aa7a2a926380f0422ebd8a2393a4077c12..231175a0a966a0bb1d547a68cfefecb79cb71791 100644 (file)
@@ -37,6 +37,24 @@ import org.tizen.emulator.skin.dbi.RotationNameType;
  *
  */
 public interface ICommunicator extends Runnable {
+       /* This values must match the QEMU definitions */
+       public final static short RECV_HEART_BEAT = 1;
+       public final static short RECV_SCREENSHOT_DATA = 2;
+       public final static short RECV_DETAIL_INFO_DATA = 3;
+       public final static short RECV_RAMDUMP_COMPLETED = 4;
+       public final static short RECV_BOOTING_PROGRESS = 5;
+       public final static short RECV_BRIGHTNESS_STATE = 6;
+       public final static short RECV_HOST_KBD_STATE = 8;
+       public final static short RECV_MULTI_TOUCH_STATE = 9;
+
+       public final static short RECV_SENSORD_STARTED = 800;
+       public final static short RECV_SDBD_STARTED = 801;
+       public final static short RECV_ECS_STARTED = 802;
+       public final static short RECV_DRAW_FRAME = 900;
+       public final static short RECV_DRAW_BLANK_GUIDE = 901;
+       public final static short RECV_EMUL_RESET = 998;
+       public final static short RECV_EMUL_SHUTDOWN = 999;
+
        public enum MouseButtonType {
                LEFT( (short)1 ),
                WHEEL( (short)2 ),
@@ -240,57 +258,6 @@ public interface ICommunicator extends Runnable {
                }
        }
 
-       public enum ReceiveCommand {
-               /* This values must match the QEMU definitions */
-
-               RECV_HEART_BEAT((short) 1),
-               RECV_SCREENSHOT_DATA((short) 2),
-               RECV_DETAIL_INFO_DATA((short) 3),
-               RECV_RAMDUMP_COMPLETED((short) 4),
-               RECV_BOOTING_PROGRESS((short) 5),
-               RECV_BRIGHTNESS_STATE((short) 6),
-               RECV_HOST_KBD_STATE((short) 8),
-               RECV_MULTI_TOUCH_STATE((short) 9),
-
-               RECV_SENSORD_STARTED((short) 800),
-               RECV_SDBD_STARTED((short) 801),
-               RECV_ECS_STARTED((short) 802),
-               RECV_DRAW_FRAME((short) 900),
-               RECV_DRAW_BLANK_GUIDE((short) 901),
-               RECV_EMUL_RESET((short) 998),
-               RECV_EMUL_SHUTDOWN((short) 999);
-
-               private short value;
-
-               ReceiveCommand(short value) {
-                       this.value = value;
-               }
-
-               public short value() {
-                       return this.value;
-               }
-
-               public static ReceiveCommand getValue(String val) {
-                       ReceiveCommand[] values = ReceiveCommand.values();
-                       for (int i = 0; i < values.length; i++) {
-                               if (values[i].value == Short.parseShort(val)) {
-                                       return values[i];
-                               }
-                       }
-                       throw new IllegalArgumentException(val);
-               }
-
-               public static ReceiveCommand getValue(short val) {
-                       ReceiveCommand[] values = ReceiveCommand.values();
-                       for (int i = 0; i < values.length; i++) {
-                               if (values[i].value == val) {
-                                       return values[i];
-                               }
-                       }
-                       throw new IllegalArgumentException(Integer.toString(val));
-               }
-       }
-
        public void sendToQEMU(SendCommand command, ISendData data, boolean urgent);
 
        public void terminate();
index 1e67dfaafaabdd7a3144701ca698868a2a02063e..05493a30720f6f8ecec119fb6347c31bceb8742e 100755 (executable)
@@ -57,12 +57,17 @@ import org.tizen.emulator.skin.image.ImageRegistry.ResourceImageName;
 import org.tizen.emulator.skin.log.SkinLogger;
 import org.tizen.emulator.skin.util.IOUtil;
 
-
 /**
  * 
  * 
  */
 public class SocketCommunicator implements ICommunicator {
+       private static class PacketHeader {
+               int reqId; /* id */
+               short cmd; /* command */
+               int length; /* data length */
+       }
+
        public static class DataTranfer {
                private boolean isTransferState;
                private byte[] receivedData;
@@ -189,6 +194,7 @@ public class SocketCommunicator implements ICommunicator {
 
                sendThread = new Thread("sendThread") {
                        List<SkinSendData> list = new ArrayList<SkinSendData>();
+                       SkinSendData sendData = null;
 
                        @Override
                        public void run() {
@@ -202,7 +208,6 @@ public class SocketCommunicator implements ICommunicator {
                                                        }
                                                }
 
-                                               SkinSendData sendData = null;
                                                while (true) {
                                                        sendData = sendQueue.poll();
                                                        if (null != sendData) {
@@ -263,31 +268,24 @@ public class SocketCommunicator implements ICommunicator {
                        heartbeatTimer.schedule(heartbeatExecutor, 1, HEART_BEAT_INTERVAL * 1000);
                }
 
+               PacketHeader header = new PacketHeader();
+
                while (true) {
                        if (isTerminated) {
                                break;
                        }
 
                        try {
-                               int reqId = sockInputStream.readInt();
-                               short cmd = sockInputStream.readShort();
-                               int length = sockInputStream.readInt();
+                               header.reqId = sockInputStream.readInt();
+                               header.cmd = sockInputStream.readShort();
+                               header.length = sockInputStream.readInt();
 
                                if (logger.isLoggable(Level.FINE)) {
-                                       logger.fine("[Socket] read - reqId:" + reqId +
-                                                       ", command:" + cmd + ", dataLength:" + length);
+                                       logger.fine("[Socket] read - reqId:" + header.reqId +
+                                                       ", command:" + header.cmd + ", dataLength:" + header.length);
                                }
 
-                               ReceiveCommand command = null;
-
-                               try {
-                                       command = ReceiveCommand.getValue(cmd);
-                               } catch (IllegalArgumentException e) {
-                                       logger.severe("unknown command:" + cmd);
-                                       continue;
-                               }
-
-                               switch (command) {
+                               switch (header.cmd) {
                                case RECV_HEART_BEAT: {
                                        resetHeartbeatCount();
 
@@ -300,13 +298,13 @@ public class SocketCommunicator implements ICommunicator {
                                }
                                case RECV_SCREENSHOT_DATA: {
                                        logger.info("received SCREENSHOT_DATA from QEMU");
-                                       receiveData(sockInputStream, screenShotDataTransfer, length);
+                                       receiveData(sockInputStream, screenShotDataTransfer, header.length);
 
                                        break;
                                }
                                case RECV_DETAIL_INFO_DATA: {
                                        logger.info("received DETAIL_INFO_DATA from QEMU");
-                                       receiveData(sockInputStream, detailInfoTransfer, length);
+                                       receiveData(sockInputStream, detailInfoTransfer, header.length);
 
                                        break;
                                }
@@ -319,12 +317,12 @@ public class SocketCommunicator implements ICommunicator {
                                        //logger.info("received BOOTING_PROGRESS from QEMU");
 
                                        resetDataTransfer(miscDataTransfer);
-                                       receiveData(sockInputStream, miscDataTransfer, length);
+                                       receiveData(sockInputStream, miscDataTransfer, header.length);
 
                                        byte[] receivedData = getReceivedData(miscDataTransfer);
                                        if (null != receivedData) {
                                                String strLayer = new String(receivedData, 0, 1, "UTF-8");
-                                               String strValue = new String(receivedData, 1, length - 2, "UTF-8");
+                                               String strValue = new String(receivedData, 1, header.length - 2, "UTF-8");
 
                                                int layer = 0;
                                                int value = 0;
@@ -345,11 +343,11 @@ public class SocketCommunicator implements ICommunicator {
                                        //logger.info("received BRIGHTNESS_STATE from QEMU");
 
                                        resetDataTransfer(miscDataTransfer);
-                                       receiveData(sockInputStream, miscDataTransfer, length);
+                                       receiveData(sockInputStream, miscDataTransfer, header.length);
 
                                        byte[] receivedData = getReceivedData(miscDataTransfer);
                                        if (null != receivedData) {
-                                               String strValue = new String(receivedData, 0, length - 1, "UTF-8");
+                                               String strValue = new String(receivedData, 0, header.length - 1, "UTF-8");
 
                                                int value = 1;
                                                try {
@@ -371,11 +369,11 @@ public class SocketCommunicator implements ICommunicator {
                                        logger.info("received HOST_KBD_STATE from QEMU");
 
                                        resetDataTransfer(miscDataTransfer);
-                                       receiveData(sockInputStream, miscDataTransfer, length);
+                                       receiveData(sockInputStream, miscDataTransfer, header.length);
 
                                        byte[] receivedData = getReceivedData(miscDataTransfer);
                                        if (null != receivedData) {
-                                               String strValue = new String(receivedData, 0, length - 1, "UTF-8");
+                                               String strValue = new String(receivedData, 0, header.length - 1, "UTF-8");
 
                                                int value = 1;
                                                try {
@@ -397,11 +395,11 @@ public class SocketCommunicator implements ICommunicator {
                                        logger.info("received MULTI_TOUCH_STATE from QEMU");
 
                                        resetDataTransfer(miscDataTransfer);
-                                       receiveData(sockInputStream, miscDataTransfer, length);
+                                       receiveData(sockInputStream, miscDataTransfer, header.length);
 
                                        byte[] receivedData = getReceivedData(miscDataTransfer);
                                        if (null != receivedData) {
-                                               String strValue = new String(receivedData, 0, length - 1, "UTF-8");
+                                               String strValue = new String(receivedData, 0, header.length - 1, "UTF-8");
 
                                                int value = 1;
                                                try {
@@ -483,7 +481,7 @@ public class SocketCommunicator implements ICommunicator {
                                        break;
                                }
                                default: {
-                                       logger.severe("Unknown command from QEMU. command : " + cmd);
+                                       logger.severe("Unknown command from QEMU. command : " + header.cmd);
                                        break;
                                }
                                }
@@ -619,6 +617,7 @@ public class SocketCommunicator implements ICommunicator {
                        } else {
                                byte[] byteData = data.serialize();
                                length = (short) byteData.length;
+
                                dataOutputStream.writeShort(length);
                                dataOutputStream.write(byteData);
                        }