Refine codes using an enum with std atomic in DevicePresence
authorcoderhyme <jhyo.kim@samsung.com>
Wed, 19 Aug 2015 15:34:59 +0000 (00:34 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Fri, 21 Aug 2015 00:19:14 +0000 (00:19 +0000)
DevicePresence::state, a variable to keep the state of the class, is an enum type. It needs to be declared as an atomic to make it thread-safe.
std::atomic with user-defined type is not available on GCC 4.6.3, therefore std::atomic_int is used instead. The enum is scoped enum, which means it is enum class. It means it is not interchangeable with int implictly.
That's why a separate method is declared to set the value from int value. Plus, the values is controlled in the class, so it is safe to cast to int and vice versa.

Change-Id: I2689c550af293c41a9757646924f210e7fccab74
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2244
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
service/resource-encapsulation/src/resourceBroker/include/BrokerTypes.h
service/resource-encapsulation/src/resourceBroker/include/DevicePresence.h
service/resource-encapsulation/src/resourceBroker/src/DevicePresence.cpp

index b974da0..8ef780f 100644 (file)
@@ -64,7 +64,7 @@ namespace OIC
          * REQUESTED   - It means that broker receives the request for presence checking
          * LOST_SIGNAL - In case that 'subscribeCB' function receives the message except 'OK'
          */
-        enum DEVICE_STATE
+        enum class DEVICE_STATE
         {
             ALIVE = 0,
             REQUESTED,
index e5bc141..cfb7bc9 100644 (file)
@@ -49,7 +49,8 @@ namespace OIC
 
             bool isEmptyResourcePresence() const;
             const std::string getAddress() const;
-            DEVICE_STATE getDeviceState() const;
+            DEVICE_STATE getDeviceState() const noexcept;
+
         private:
             std::list<ResourcePresence * > resourcePresenceList;
 
@@ -69,6 +70,8 @@ namespace OIC
             void changeAllPresenceMode(BROKER_MODE mode);
             void subscribeCB(OCStackResult ret,const unsigned int seq, const std::string& Hostaddress);
             void timeOutCB(TimerID id);
+
+            void setDeviceState(DEVICE_STATE);
         };
     } // namespace Service
 } // namespace OIC
index f338979..aade9e9 100644 (file)
@@ -27,7 +27,7 @@ namespace OIC
     {
         DevicePresence::DevicePresence()
         {
-            state = DEVICE_STATE::REQUESTED;
+            setDeviceState(DEVICE_STATE::REQUESTED);
 
             presenceTimerHandle = 0;
             isRunningTimeOut = false;
@@ -69,11 +69,17 @@ namespace OIC
             presenceTimerHandle
             = presenceTimer.post(BROKER_DEVICE_PRESENCE_TIMEROUT, pTimeoutCB);
         }
-        DEVICE_STATE DevicePresence::getDeviceState() const
+
+        DEVICE_STATE DevicePresence::getDeviceState() const noexcept
         {
-            int ret=state;
-            return (DEVICE_STATE)ret;
+            return static_cast< DEVICE_STATE >(state.load());
         }
+
+        void DevicePresence::setDeviceState(DEVICE_STATE newState)
+        {
+            state = static_cast< int >(newState);
+        }
+
         const std::string DevicePresence::getAddress() const
         {
             OC_LOG_V(DEBUG, BROKER_TAG, "getAddress()");
@@ -131,7 +137,7 @@ namespace OIC
                 case OC_STACK_CONTINUE:
                 {
                     OC_LOG_V(DEBUG, BROKER_TAG, "SEQ# %d",seq);
-                    state = DEVICE_STATE::ALIVE;
+                    setDeviceState(DEVICE_STATE::ALIVE);
                     OC_LOG_V(DEBUG, BROKER_TAG, "device state : %d",
                             (int)getDeviceState());
                     changeAllPresenceMode(BROKER_MODE::DEVICE_PRESENCE_MODE);
@@ -147,14 +153,14 @@ namespace OIC
                 case OC_STACK_PRESENCE_TIMEOUT:
                 case OC_STACK_PRESENCE_DO_NOT_HANDLE:
                 {
-                    state = DEVICE_STATE::LOST_SIGNAL;
+                    setDeviceState(DEVICE_STATE::LOST_SIGNAL);
                     changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
                     break;
                 }
                 default:
                 {
                     OC_LOG_V(DEBUG, BROKER_TAG, "Presence Lost Signal because unknown type");
-                    state = DEVICE_STATE::LOST_SIGNAL;
+                    setDeviceState(DEVICE_STATE::LOST_SIGNAL);
                     changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
                     break;
                 }
@@ -169,7 +175,7 @@ namespace OIC
 
             OC_LOG_V(DEBUG, BROKER_TAG,
                     "Timeout execution. will be discard after receiving cb message");
-            state = DEVICE_STATE::LOST_SIGNAL;
+            setDeviceState(DEVICE_STATE::LOST_SIGNAL);
             changeAllPresenceMode(BROKER_MODE::NON_PRESENCE_MODE);
 
             isRunningTimeOut = false;