communication: substitute scheduleAtFixedRate for schedule
authorgiwoong.kim <giwoong.kim@samsung.com>
Sat, 19 Jan 2013 02:53:43 +0000 (11:53 +0900)
committergiwoong.kim <giwoong.kim@samsung.com>
Sat, 19 Jan 2013 02:53:43 +0000 (11:53 +0900)
substitute schedule function for scheduleAtFixedRate

Signed-off-by: GiWoong Kim <giwoong.kim@samsung.com>
tizen/src/skin/client/src/org/tizen/emulator/skin/comm/sock/SocketCommunicator.java
tizen/src/skin/maruskin_server.c [changed mode: 0644->0755]

index 7797d69..14d20e7 100644 (file)
@@ -41,9 +41,6 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -86,18 +83,19 @@ public class SocketCommunicator implements ICommunicator {
        }
 
        public static final int HEART_BEAT_INTERVAL = 1; //second
-       public static final int HEART_BEAT_EXPIRE = 10;
-       
+       public static final int HEART_BEAT_EXPIRE = 15;
+
        public final static int SCREENSHOT_WAIT_INTERVAL = 3; // milli-seconds
        public final static int SCREENSHOT_WAIT_LIMIT = 3000; // milli-seconds
        public final static int DETAIL_INFO_WAIT_INTERVAL = 1; // milli-seconds
        public final static int DETAIL_INFO_WAIT_LIMIT = 3000; // milli-seconds
-       
+
        public final static int MAX_SEND_QUEUE_SIZE = 100000;
-       
+
        private static int reqId;
-       
-       private Logger logger = SkinLogger.getSkinLogger( SocketCommunicator.class ).getLogger();
+
+       private Logger logger =
+                       SkinLogger.getSkinLogger(SocketCommunicator.class).getLogger();
 
        private EmulatorConfig config;
        private int uId;
@@ -112,7 +110,8 @@ public class SocketCommunicator implements ICommunicator {
        private boolean isTerminated;
        private boolean isSensorDaemonStarted;
        private boolean isRamdump;
-       private ScheduledExecutorService heartbeatExecutor;
+       private TimerTask heartbeatExecutor;
+       private Timer heartbeatTimer;
 
        private DataTranfer screenShotDataTransfer;
        private DataTranfer detailInfoTransfer;
@@ -140,7 +139,8 @@ public class SocketCommunicator implements ICommunicator {
                this.progressDataTransfer.maxWaitTime = SCREENSHOT_WAIT_LIMIT;
 
                this.heartbeatCount = new AtomicInteger(0);
-               this.heartbeatExecutor = Executors.newSingleThreadScheduledExecutor();
+               //this.heartbeatExecutor = Executors.newSingleThreadScheduledExecutor();
+               this.heartbeatTimer = new Timer();
 
                try {
 
@@ -239,25 +239,22 @@ public class SocketCommunicator implements ICommunicator {
 
                boolean ignoreHeartbeat = config.getArgBoolean( ArgsConstants.TEST_HEART_BEAT_IGNORE );
 
-               if ( ignoreHeartbeat ) {
-                       logger.info( "Ignore Skin heartbeat." );
+               if (ignoreHeartbeat) {
+                       logger.info("Ignore Skin heartbeat.");
                } else {
-
-                       heartbeatExecutor.scheduleAtFixedRate( new Runnable() {
-
+                       heartbeatExecutor = new TimerTask() {
                                @Override
                                public void run() {
-
                                        increaseHeartbeatCount();
 
-                                       if ( isHeartbeatExpired() ) {
+                                       if (isHeartbeatExpired()) {
+                                               logger.info("heartbeat was expired");
                                                terminate();
                                        }
-
                                }
+                       };
 
-                       }, 0, HEART_BEAT_INTERVAL, TimeUnit.SECONDS );
-
+                       heartbeatTimer.schedule(heartbeatExecutor, 1, HEART_BEAT_INTERVAL * 1000);
                }
 
                while ( true ) {
@@ -554,12 +551,12 @@ public class SocketCommunicator implements ICommunicator {
 
        public byte[] getReceivedData( DataTranfer dataTranfer ) {
 
-               if( null == dataTranfer ) {
+               if (null == dataTranfer) {
                        return null;
                }
-               
-               synchronized ( dataTranfer ) {
-                       
+
+               synchronized (dataTranfer) {
+
                        int count = 0;
                        byte[] receivedData = null;
                        long sleep = dataTranfer.sleep;
@@ -612,8 +609,9 @@ public class SocketCommunicator implements ICommunicator {
 
        private void increaseHeartbeatCount() {
                int count = heartbeatCount.incrementAndGet();
-               if ( logger.isLoggable( Level.FINE ) ) {
-                       logger.fine( "HB count : " + count );
+
+               if (logger.isLoggable(Level.FINE)) {
+                       logger.info("HB count : " + count);
                }
        }
 
@@ -622,30 +620,33 @@ public class SocketCommunicator implements ICommunicator {
        }
 
        private void resetHeartbeatCount() {
-               heartbeatCount.set( 0 );
+               heartbeatCount.set(0);
+
+               if (logger.isLoggable(Level.FINE)) {
+                       logger.info("HB count reset");
+               }
        }
 
        @Override
        public void terminate() {
-               
                isTerminated = true;
-               
-               if ( null != heartbeatExecutor ) {
-                       heartbeatExecutor.shutdownNow();
-               }
-               
-               if( null != sendQueue ) {
-                       synchronized ( sendQueue ) {
+               logger.info("terminated");
+
+               if (null != sendQueue) {
+                       synchronized (sendQueue) {
                                sendQueue.notifyAll();
                        }
                }
-               
-               IOUtil.closeSocket( socket );
-               
-               synchronized ( this ) {
+
+               if (null != heartbeatTimer) {
+                       heartbeatTimer.cancel();
+               }
+
+               IOUtil.closeSocket(socket);
+
+               synchronized (this) {
                        skin.shutdown();
                }
-               
        }
 
        public void resetSkin( EmulatorSkin skin ) {
@@ -661,7 +662,7 @@ class SkinSendData {
        private SendCommand command;
        private ISendData data;
 
-       public SkinSendData( SendCommand command, ISendData data ) {
+       public SkinSendData(SendCommand command, ISendData data) {
                this.command = command;
                this.data = data;
        }
old mode 100644 (file)
new mode 100755 (executable)
index 8e3e2d3..9a93d81
@@ -150,8 +150,6 @@ static int is_started_heartbeat = 0;
 static int stop_heartbeat = 0;
 static int recv_heartbeat_count = 0;
 static pthread_t thread_id_heartbeat;
-static pthread_mutex_t mutex_heartbeat = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t cond_heartbeat = PTHREAD_COND_INITIALIZER;
 static pthread_mutex_t mutex_recv_heartbeat_count = PTHREAD_MUTEX_INITIALIZER;
 
 static int skin_argc = 0;
@@ -1141,30 +1139,28 @@ static void* do_heart_beat(void* args)
     int need_restart_skin_client = 0;
     int shutdown = 0;
 
-    int booting_handicap_cnt = 0;
-
-    struct timeval current;
-    struct timespec ts_heartbeat;
+    unsigned int booting_handicap_cnt = 0;
+    unsigned int hb_interval = HEART_BEAT_INTERVAL * 1000;
 
     while ( 1 ) {
-        gettimeofday( &current, NULL );
-
         if (booting_handicap_cnt < 5) {
             booting_handicap_cnt++;
-            ts_heartbeat.tv_sec = current.tv_sec +
-                (HEART_BEAT_INTERVAL * 10);
+
+#ifdef CONFIG_WIN32
+            Sleep(hb_interval * 10); /* 10sec */
+#else
+            usleep(hb_interval * 1000 * 10);
+#endif
         } else {
-            ts_heartbeat.tv_sec = current.tv_sec + HEART_BEAT_INTERVAL;
+#ifdef CONFIG_WIN32
+            Sleep(hb_interval); /* 1sec */
+#else
+            usleep(hb_interval * 1000);
+#endif
         }
 
-        ts_heartbeat.tv_nsec = current.tv_usec * 1000;
-
-        pthread_mutex_lock( &mutex_heartbeat );
-        pthread_cond_timedwait( &cond_heartbeat, &mutex_heartbeat, &ts_heartbeat );
-        pthread_mutex_unlock( &mutex_heartbeat );
-
-        if ( stop_heartbeat ) {
-            INFO( "[HB] stop heart beat.\n" );
+        if (stop_heartbeat) {
+            INFO("[HB] stop heart beat.\n");
             break;
         }
 
@@ -1176,7 +1172,7 @@ static void* do_heart_beat(void* args)
                 send_fail_count = 0;
             }
         } else {
-            // fail to get socket in accepting or client is not yet accepted.
+            /* fail to get socket in accepting or client is not yet accepted */
             send_fail_count++;
             TRACE( "[HB] client socket is NULL yet.\n" );
         }
@@ -1296,10 +1292,5 @@ static int start_heart_beat(void)
 
 static void stop_heart_beat(void)
 {
-    pthread_mutex_lock(&mutex_heartbeat);
-
     stop_heartbeat = 1;
-    pthread_cond_signal(&cond_heartbeat);
-
-    pthread_mutex_unlock(&mutex_heartbeat);
 }