Use enum for perform_update() return code. Made function protected
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 31 Jan 2011 14:27:22 +0000 (15:27 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 31 Jan 2011 14:27:22 +0000 (15:27 +0100)
18 files changed:
src/service.cpp
src/service.hpp
src/service_dhs.cpp
src/service_dhs.hpp
src/service_dyndns.cpp
src/service_dyndns.hpp
src/service_dyns.cpp
src/service_dyns.hpp
src/service_easydns.cpp
src/service_easydns.hpp
src/service_gnudip.cpp
src/service_gnudip.hpp
src/service_ods.cpp
src/service_ods.hpp
src/service_tzo.cpp
src/service_tzo.hpp
src/service_zoneedit.cpp
src/service_zoneedit.hpp

index e798a1d..1bab19b 100644 (file)
@@ -280,7 +280,7 @@ void Service::update(const string& ip, const time_t current_time, bool changed_t
     {
         Log->print_update_service(service_name);
 
-        if ( perform_update(ip) == 0 )
+        if ( perform_update(ip) == UpdateOk )
         {
             // if update was successful, we need to set the Lastupdated and ActualIP base member.
             set_last_update(current_time,ip);
index 5037883..82b765b 100644 (file)
@@ -61,6 +61,11 @@ private:
 
     Logger::Ptr Log;
 
+protected:
+
+    enum UpdateErrorCode { UpdateOk=0, GenericError=1, NoChange=2, Blocked=3 };
+    virtual UpdateErrorCode perform_update(const std::string& ip) = 0;
+
 public:
 
     typedef boost::shared_ptr<Service> Ptr;
@@ -69,8 +74,6 @@ public:
 
     virtual ~Service();
 
-    virtual int perform_update(const std::string& ip) = 0;
-
     void update(const std::string& ip, const time_t current_time, bool changed_to_online);
 
     bool update_allowed(const time_t current_time, bool changed_to_online, const std::string& ip_host);
index c757d54..27b9740 100644 (file)
@@ -138,7 +138,7 @@ list<string> ServiceDhs::separate_domain_and_host_part(const string& fqdn) const
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceDhs::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceDhs::perform_update(const std::string& ip)
 {
     string url = BaseUrl;
     url.append(ip);
@@ -161,7 +161,7 @@ int ServiceDhs::perform_update(const std::string& ip)
             if ( boost::regex_search(curl_data,expr_done) )
             {
                 // Update successful
-                return 0;
+                return UpdateOk;
             }
             else
             {
@@ -182,5 +182,6 @@ int ServiceDhs::perform_update(const std::string& ip)
         get_logger()->print_httphelper_not_initialized();
         HTTPHelp->re_initialize();
     }
-    return -1;
+
+    return GenericError;
 }
index b529a0d..26b87fd 100644 (file)
@@ -48,7 +48,7 @@ public:
 
     ~ServiceDhs();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 };
 
 #endif
index 0939d84..5d7b8b8 100644 (file)
@@ -100,7 +100,7 @@ string ServiceDyndns::assemble_base_url(const string& fqhn) const
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceDyndns::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceDyndns::perform_update(const std::string& ip)
 {
     string url = BaseUrl;
     url.append(ip);
@@ -109,7 +109,7 @@ int ServiceDyndns::perform_update(const std::string& ip)
     {
         get_logger()->print_httphelper_not_initialized();
         HTTPHelp->re_initialize();
-        return -1;
+        return GenericError;
     }
 
     // Perform curl operation on given url
@@ -127,19 +127,21 @@ int ServiceDyndns::perform_update(const std::string& ip)
 
         if ( curl_data.compare(0,4,"good") == 0 )
         {
-            return 0;
+            return UpdateOk;
         }
         else if (curl_data.compare(0,5,"nochg") == 0 )
         {
             // IP didn't change, this might indicate a problem at the
             // dyndns backend or another client is running elsewhere.
-            return 0;
+            get_logger()->print_update_failure(url, curl_data);
+            return UpdateOk;
         }
         else if (curl_data.compare(0,5,"abuse") == 0 )
         {
             // Account is blocked. Log it as a successful update
             // so the IP address will be "burnt" until it changes.
-            return 0;
+            get_logger()->print_update_failure(url, curl_data);
+            return UpdateOk;
         }
         else if ( curl_data == "badauth" )
         {
@@ -155,5 +157,5 @@ int ServiceDyndns::perform_update(const std::string& ip)
         get_logger()->print_update_failure(url,http_status_code);
     }
 
-    return -1;
+    return GenericError;
 }
index 2cbc541..33fded8 100644 (file)
@@ -47,7 +47,7 @@ public:
 
     ~ServiceDyndns();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 
 };
 
index 4bd14e8..b305637 100644 (file)
@@ -100,7 +100,7 @@ string ServiceDyns::assemble_base_url(const string& fqhn, const string& username
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceDyns::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceDyns::perform_update(const std::string& ip)
 {
     string url = BaseUrl;
     url.append(ip);
@@ -122,7 +122,7 @@ int ServiceDyns::perform_update(const std::string& ip)
 
             if ( status_code == "200" )
             {
-                return 0;
+                return UpdateOk;
             }
             else if ( status_code == "401" )
             {
@@ -143,5 +143,5 @@ int ServiceDyns::perform_update(const std::string& ip)
         get_logger()->print_httphelper_not_initialized();
         HTTPHelp->re_initialize();
     }
-    return -1;
+    return GenericError;
 }
index 0f33b38..035dc89 100644 (file)
@@ -46,7 +46,7 @@ public:
 
     ~ServiceDyns();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 };
 
 #endif
index 89d9a4e..161822c 100644 (file)
@@ -183,7 +183,7 @@ list<string> ServiceEasydns::separate_domain_and_host_part(const string& fqdn) c
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceEasydns::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceEasydns::perform_update(const std::string& ip)
 {
     string url = BaseUrl;
     url.append(ip);
@@ -205,7 +205,7 @@ int ServiceEasydns::perform_update(const std::string& ip)
 
             if ( status_code == "NOERROR" )
             {
-                return 0;
+                return UpdateOk;
             }
             else if ( status_code == "NOACCESS" )
             {
@@ -226,5 +226,6 @@ int ServiceEasydns::perform_update(const std::string& ip)
         get_logger()->print_httphelper_not_initialized();
         HTTPHelp->re_initialize();
     }
-    return -1;
+
+    return GenericError;
 }
index 7b8dad6..b554ffa 100644 (file)
@@ -50,7 +50,7 @@ public:
 
     ~ServiceEasydns();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 
 };
 
index e72ee5a..76b9420 100644 (file)
@@ -221,7 +221,7 @@ string ServiceGnudip::assemble_update_url(const string& salt, const string& curr
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceGnudip::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceGnudip::perform_update(const std::string& ip)
 {
     if ( HTTPHelp->is_initialized() )
     {
@@ -241,7 +241,7 @@ int ServiceGnudip::perform_update(const std::string& ip)
             if ( salt_time_sign.empty() )
             {
                 get_logger()->print_could_not_parse_received_data(curl_data);
-                return -1;
+                return GenericError;
             }
 
             // at this point we have salt, time and sign parsed successfully
@@ -271,12 +271,12 @@ int ServiceGnudip::perform_update(const std::string& ip)
             catch ( exception& e )
             {
                 get_logger()->print_exception_md5_sum(e.what());
-                return -1;
+                return GenericError;
             }
             catch ( ... )
             {
                 get_logger()->print_exception_md5_sum("Unknown exception");
-                return -1;
+                return GenericError;
             }
 
             // append "." and salt and compute md5 sum and get the HEX representation
@@ -291,12 +291,12 @@ int ServiceGnudip::perform_update(const std::string& ip)
             catch ( exception& e )
             {
                 get_logger()->print_exception_md5_sum(e.what());
-                return -1;
+                return GenericError;
             }
             catch ( ... )
             {
                 get_logger()->print_exception_md5_sum("Unknown exception");
-                return -1;
+                return GenericError;
             }
 
             // Now its time to issue the second http_get operation
@@ -314,7 +314,7 @@ int ServiceGnudip::perform_update(const std::string& ip)
                 string update_return_code = parse_return_code(curl_data);
                 if ( update_return_code == "0" )
                 {
-                    return 0;
+                    return UpdateOk;
                 }
                 else if ( update_return_code == "1" )
                 {
@@ -342,5 +342,5 @@ int ServiceGnudip::perform_update(const std::string& ip)
         get_logger()->print_httphelper_not_initialized();
         HTTPHelp->re_initialize();
     }
-    return -1;
+    return GenericError;
 }
index 4a1e5df..c2be8ed 100644 (file)
@@ -51,7 +51,7 @@ public:
 
     ~ServiceGnudip();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 
 };
 
index ba6df98..60d4354 100644 (file)
@@ -73,21 +73,21 @@ ServiceOds::~ServiceOds()
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceOds::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceOds::perform_update(const std::string& ip)
 {
     // First of all create a boost shared pointer to the NetHelper.
     NetHelper::Ptr connection(new NetHelper(get_logger()));
 
     // Then open the connection to the ODS update server.
     if (connection->open_connection(UpdateServer,Port) != 0)
-        return -1;
+        return GenericError;
 
     // Receive the server greeting.
     string server_greeting = connection->receive_data();
     if ( server_greeting.empty() )
     {
         connection->close_connection(); /*lint !e534 */
-        return -1;
+        return GenericError;
     }
 
     // Send login command
@@ -96,7 +96,7 @@ int ServiceOds::perform_update(const std::string& ip)
     if (connection->send_data(login.str()) != 0)
     {
         connection->close_connection(); /*lint !e534 */
-        return -1;
+        return GenericError;
     }
 
     // Receive login reply
@@ -104,7 +104,7 @@ int ServiceOds::perform_update(const std::string& ip)
     if ( login_reply.empty() )
     {
         connection->close_connection(); /*lint !e534 */
-        return -1;
+        return GenericError;
     }
     else
     {
@@ -115,14 +115,14 @@ int ServiceOds::perform_update(const std::string& ip)
             // Login failed
             get_logger()->print_service_not_authorized(UpdateServer,get_login(),get_password());
             connection->close_connection(); /*lint !e534 */
-            return -1;
+            return GenericError;
         }
         else if ( status_code != "225" )
         {
             // Undefined protocol error, Log login_reply
             get_logger()->print_undefined_protocol_error("ODS",login_reply);
             connection->close_connection(); /*lint !e534 */
-            return -1;
+            return GenericError;
         }
     }
 
@@ -139,14 +139,14 @@ int ServiceOds::perform_update(const std::string& ip)
         if (connection->send_data(delete_request.str()) != 0)
         {
             connection->close_connection(); /*lint !e534 */
-            return -1;
+            return GenericError;
         }
         // Get delete reply
         delete_reply = connection->receive_data();
         if ( delete_reply.empty() )
         {
             connection->close_connection(); /*lint !e534 */
-            return -1;
+            return GenericError;
         }
         del_count++;
         reply_code = Util::parse_status_code(delete_reply);
@@ -159,7 +159,7 @@ int ServiceOds::perform_update(const std::string& ip)
     if (connection->send_data(update_request.str()) != 0)
     {
         connection->close_connection(); /*lint !e534 */
-        return -1;
+        return GenericError;
     }
 
     // Receive update request server reply
@@ -167,7 +167,7 @@ int ServiceOds::perform_update(const std::string& ip)
     if ( update_reply.empty() )
     {
         connection->close_connection(); /*lint !e534 */
-        return -1;
+        return GenericError;
     }
     else
     {
@@ -177,11 +177,11 @@ int ServiceOds::perform_update(const std::string& ip)
         {
             get_logger()->print_undefined_protocol_error("ODS",update_reply);
             connection->close_connection(); /*lint !e534 */
-            return -1;
+            return GenericError;
         }
     }
 
     // Successfully updated host
     connection->close_connection(); /*lint !e534 */
-    return 0;
+    return UpdateOk;
 }
index 85141ca..1a948c1 100644 (file)
@@ -42,7 +42,7 @@ public:
 
     ~ServiceOds();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 };
 
 #endif
index f21c674..402ff44 100644 (file)
@@ -105,7 +105,7 @@ string ServiceTzo::assemble_base_url(const string& fqhn, const string& username,
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int ServiceTzo::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceTzo::perform_update(const std::string& ip)
 {
     string url = BaseUrl;
     url.append(ip);
@@ -127,7 +127,7 @@ int ServiceTzo::perform_update(const std::string& ip)
 
             if ( status_code == "200" )
             {
-                return 0;
+                return UpdateOk;
             }
             else if ( status_code == "401" )
             {
@@ -148,5 +148,6 @@ int ServiceTzo::perform_update(const std::string& ip)
         get_logger()->print_httphelper_not_initialized();
         HTTPHelp->re_initialize();
     }
-    return -1;
+
+    return GenericError;
 }
index c5b0bae..89f3615 100644 (file)
@@ -46,7 +46,7 @@ public:
 
     ~ServiceTzo();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 };
 
 #endif
index 16381b2..9641011 100644 (file)
@@ -97,9 +97,9 @@ string ServiceZoneedit::assemble_base_url(const string& fqhn) const
 /**
  * Performs the Service update.
  * @param ip IP Address to set.
- * @return 0 if all is fine, -1 otherwise.
+ * @return UpdateOk if all is fine, GenericError otherwise.
  */
-int ServiceZoneedit::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceZoneedit::perform_update(const std::string& ip)
 {
     string url = BaseUrl;
     url.append(ip);
@@ -121,7 +121,7 @@ int ServiceZoneedit::perform_update(const std::string& ip)
 
             if ( status_code == "200" )
             {
-                return 0;
+                return UpdateOk;
             }
             else
             {
@@ -142,5 +142,5 @@ int ServiceZoneedit::perform_update(const std::string& ip)
         get_logger()->print_httphelper_not_initialized();
         HTTPHelp->re_initialize();
     }
-    return -1;
+    return GenericError;
 }
index 836cc80..434b93b 100644 (file)
@@ -46,7 +46,7 @@ public:
 
     ~ServiceZoneedit();
 
-    int perform_update(const std::string& ip);
+    UpdateErrorCode perform_update(const std::string& ip);
 };
 
 #endif