Fix 'occurred' typo
[bpdyndnsd] / src / service_dyns.cpp
index 00e85fc..c831296 100644 (file)
@@ -7,7 +7,8 @@
  * @license GPLv2
 */
 
-#include "service_dyns.h"
+#include "service_dyns.hpp"
+#include "util.hpp"
 
 #include <time.h>
 #include <boost/foreach.hpp>
@@ -32,7 +33,7 @@ ServiceDyns::ServiceDyns()
  * @param _login The login name.
  * @param _password The corresponding password.
  */
-ServiceDyns::ServiceDyns(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port)
+ServiceDyns::ServiceDyns(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int _max_equal_updates_in_succession, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port)
 {
     if ( _update_interval == -1 )        // If _update_interval is default po::option_desc (not specified via config)
         set_update_interval(5);              // use default protocol value
@@ -56,9 +57,7 @@ ServiceDyns::ServiceDyns(const string& _protocol, const string& _hostname, const
     set_logger(_logger);
 
     // create http helper class
-    HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port));
-    HTTPHelp = _http_help;
-    _http_help.reset();
+    HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port));
 
     BaseUrl = assemble_base_url(get_hostname(),get_login(),get_password());
 }
@@ -101,66 +100,50 @@ 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);
 
-    long http_status_code = HTTPHelp->http_get(url);
-
-    get_logger()->print_http_status_code(url,http_status_code);
-
-    // HTTP operation completed successful.
-    // Now we have to parse the data received by curl,
-    // cause http status code is not significant for dyns update errors
-    if ( http_status_code == 200 )
+    if ( HTTPHelp->is_initialized() )
     {
-        // Get the received http data and parse the status code.
-        string curl_data = HTTPHelp->get_curl_data();
-        string status_code = parse_status_code(curl_data);
+        long http_status_code = HTTPHelp->http_get(url);
 
-        if ( status_code == "200" )
-        {
-            return 0;
-        }
-        else if ( status_code == "401" )
+        get_logger()->print_http_status_code(url,http_status_code);
+
+        // HTTP operation completed successful.
+        // Now we have to parse the data received by curl,
+        // cause http status code is not significant for dyns update errors
+        if ( http_status_code == 200 )
         {
-            get_logger()->print_http_not_authorized(url,get_login(),get_password());
+            // Get the received http data and parse the status code.
+            string curl_data = HTTPHelp->get_curl_data();
+            string status_code = Util::parse_status_code(curl_data);
+
+            if ( status_code == "200" )
+            {
+                return UpdateOk;
+            }
+            else if ( status_code == "401" )
+            {
+                get_logger()->print_service_not_authorized(url,get_login(),get_password());
+                return NotAuth;
+            }
+            else
+            {
+                get_logger()->print_update_failure(url,curl_data);
+                return UpdateError;
+            }
         }
         else
         {
-            get_logger()->print_update_failure(url,curl_data);
+            get_logger()->print_update_failure(url,http_status_code);
         }
     }
     else
     {
-        get_logger()->print_update_failure(url,http_status_code);
+        get_logger()->print_httphelper_not_initialized();
+        HTTPHelp->re_initialize();
     }
-
-    return -1;
-}
-
-
-/**
- * Get the status code from the returned http data
- * @param data The returned http data.
- * @return The status code.
- */
-string ServiceDyns::parse_status_code(const string& data) const
-{
-    list<string> tokens;
-    ba::split(tokens,data,boost::is_any_of(" "));
-    return tokens.front();
-}
-
-
-/**
- * Serialize function needed by boost/serialization to define which members should be stored as the object state.
- * @param ar Archive
- * @param version Version
- */
-template<class Archive>
-void ServiceDyns::serialize(Archive & ar, const unsigned int version)
-{
-    ar & boost::serialization::base_object<Service>(*this);
+    return GenericError;
 }