Remove devices when API returns 401
authorKyle Fuller <redacted>
Sat, 2 Jul 2016 10:24:06 +0000 (11:24 +0100)
committerKyle Fuller <redacted>
Sat, 2 Jul 2016 10:24:06 +0000 (11:24 +0100)
palaver.cpp

index 67326d6a19078fd4cfb3c514d0d576bba0d31acc..372f0c20759419546b82d4a546c91ab47f89c469 100644 (file)
@@ -126,6 +126,10 @@ public:
                }
        }
 
+       virtual void HandleStatusCode(unsigned int status) {
+
+       }
+
        void ReadLine(const CString& sData) {
                CString sLine = sData;
                sLine.TrimRight("\r\n");
@@ -141,6 +145,8 @@ public:
                                        DEBUG("Palaver: Successfully send notification ('" << uStatus << "')");
                                }
 
+                               HandleStatusCode(uStatus);
+
                                m_eState = Headers;
                                break;
                        }
@@ -190,6 +196,18 @@ private:
        CString m_sHostname;
 };
 
+class PLVHTTPNotificationSocket : public PLVHTTPSocket {
+public:
+       PLVHTTPNotificationSocket(CModule *pModule, const CString &sToken, const CString &sMethod, const CString &sURL, MCString &mcsHeaders, const CString &sContent) : PLVHTTPSocket(pModule, sMethod, sURL, mcsHeaders, sContent) {
+               m_sToken = sToken;
+       }
+
+       virtual void HandleStatusCode(unsigned int status);
+
+private:
+       CString m_sToken;
+};
+
 class CDevice {
 public:
        CDevice(const CString &sToken) {
@@ -673,7 +691,7 @@ public:
                }
                sJSON += "}";
 
-               PLVHTTPSocket *pSocket = new PLVHTTPSocket(&module, "POST", GetPushEndpoint(), mcsHeaders, sJSON);
+               PLVHTTPSocket *pSocket = new PLVHTTPNotificationSocket(&module, GetToken(), "POST", GetPushEndpoint(), mcsHeaders, sJSON);
                module.AddSocket(pSocket);
        }
 
@@ -686,7 +704,7 @@ public:
 
                        CString sJSON = "{\"badge\": 0}";
 
-                       PLVHTTPSocket *pSocket = new PLVHTTPSocket(&module, "POST", GetPushEndpoint(), mcsHeaders, sJSON);
+                       PLVHTTPSocket *pSocket = new PLVHTTPNotificationSocket(&module, GetToken(), "POST", GetPushEndpoint(), mcsHeaders, sJSON);
                        module.AddSocket(pSocket);
 
                        m_uiBadge = 0;
@@ -899,6 +917,21 @@ public:
                return pDevice;
        }
 
+       bool RemoveDeviceWithToken(const CString& sToken) {
+               for (std::vector<CDevice*>::iterator it = m_vDevices.begin();
+                               it != m_vDevices.end(); ++it) {
+                       CDevice& device = **it;
+
+                       if (device.GetToken().Equals(sToken)) {
+                               m_vDevices.erase(it);
+                               Save();
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
 #pragma mark - Serialization
 
        CString GetConfigPath() const {
@@ -1151,6 +1184,15 @@ private:
        std::vector<CDevice*> m_vDevices;
 };
 
+void PLVHTTPNotificationSocket::HandleStatusCode(unsigned int status) {
+       if (status == 401) {
+               if (CPalaverMod *pModule = dynamic_cast<CPalaverMod *>(m_pModule)) {
+                       DEBUG("palaver: Removing device");
+                       pModule->RemoveDeviceWithToken(m_sToken);
+               }
+       }
+}
+
 template<> void TModInfo<CPalaverMod>(CModInfo& Info) {
        Info.SetWikiPage("palaver");
 }
git clone https://git.99rst.org/PROJECT