Fix 'occurred' typo
[bpdyndnsd] / src / service_easydns.cpp
index ad33c85..d598d64 100644 (file)
@@ -7,7 +7,8 @@
  * @license GPLv2
 */
 
-#include "service_easydns.h"
+#include "service_easydns.hpp"
+#include "util.hpp"
 
 #include <time.h>
 #include <boost/foreach.hpp>
@@ -21,7 +22,7 @@ using namespace std;
 /**
  * Default Constructor, needed for object serialization.
  */
-SERVICE_EASYDNS::SERVICE_EASYDNS()
+ServiceEasydns::ServiceEasydns()
 {
 }
 
@@ -32,10 +33,10 @@ SERVICE_EASYDNS::SERVICE_EASYDNS()
  * @param _login The login name.
  * @param _password The corresponding password.
  */
-SERVICE_EASYDNS::SERVICE_EASYDNS(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)
+ServiceEasydns::ServiceEasydns(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(10);              // use default protocol value
+        set_update_interval(12);              // use default protocol value
     else
         set_update_interval(_update_interval);
 
@@ -44,6 +45,11 @@ SERVICE_EASYDNS::SERVICE_EASYDNS(const string& _protocol, const string& _hostnam
     else
         set_max_updates_within_interval(_max_updates_within_interval);
 
+    if ( _max_equal_updates_in_succession == -1 )
+        set_max_equal_updates_in_succession(2);
+    else
+        set_max_equal_updates_in_succession(_max_equal_updates_in_succession);
+
     if ( _dns_cache_ttl == -1 )
         set_dns_cache_ttl(1200);
     else
@@ -56,9 +62,7 @@ SERVICE_EASYDNS::SERVICE_EASYDNS(const string& _protocol, const string& _hostnam
     set_logger(_logger);
 
     // create http helper class
-    HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
-    HTTPHelp = _http_help;
-    _http_help.reset();
+    HTTPHelp = HTTPHelper::Ptr(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
 
     // extract domain part from hostname
     list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
@@ -74,7 +78,7 @@ SERVICE_EASYDNS::SERVICE_EASYDNS(const string& _protocol, const string& _hostnam
 /**
  * Default destructor
  */
-SERVICE_EASYDNS::~SERVICE_EASYDNS()
+ServiceEasydns::~ServiceEasydns()
 {
 }
 
@@ -84,10 +88,8 @@ SERVICE_EASYDNS::~SERVICE_EASYDNS()
  * @param domain_part The complete domain part.
  * @return Two_level_domain part if there is one or "" if not so.
  */
-string SERVICE_EASYDNS::get_two_level_tld(const string& domain_part) const
+string ServiceEasydns::get_two_level_tld(const string& domain_part) const
 {
-    // TODO: There is a list with all two level TLD's, use it
-
     // split the domain_part
     list<string> domain_tokens;
     ba::split(domain_tokens,domain_part,boost::is_any_of("."));
@@ -120,7 +122,7 @@ string SERVICE_EASYDNS::get_two_level_tld(const string& domain_part) const
  * @param domain_part The domain_part in which the hostname is located.
  * @return The assembled update url without IP.
  */
-string SERVICE_EASYDNS::assemble_base_url(const string& hostname, const string& two_level_tld) const
+string ServiceEasydns::assemble_base_url(const string& hostname, const string& two_level_tld) const
 {
     string base_url;
     if ( !two_level_tld.empty() )
@@ -148,7 +150,7 @@ string SERVICE_EASYDNS::assemble_base_url(const string& hostname, const string&
  * @param fqdn Hostname with domain part.
  * @return A list with 2 elements (first element is the hostname, second element the domain part), or a list with 1 element if the domain part couldn't be determined.
  */
-list<string> SERVICE_EASYDNS::separate_domain_and_host_part(const string& fqdn) const
+list<string> ServiceEasydns::separate_domain_and_host_part(const string& fqdn) const
 {
     list<string> splitted;
     ba::split(splitted,fqdn,boost::is_any_of("."));
@@ -181,44 +183,51 @@ list<string> SERVICE_EASYDNS::separate_domain_and_host_part(const string& fqdn)
  * @param ip IP Address to set.
  * @return 0 if all is fine, -1 otherwise.
  */
-int SERVICE_EASYDNS::perform_update(const std::string& ip)
+Service::UpdateErrorCode ServiceEasydns::perform_update(const std::string& ip)
 {
-    int ret_val = -1;
-
     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 easydns update errors
-    if ( http_status_code == 200 )
+    if ( HTTPHelp->is_initialized() )
     {
-        // Get the received http data.
-        string curl_data = HTTPHelp->get_curl_data();
+        long http_status_code = HTTPHelp->http_get(url);
+
+        get_logger()->print_http_status_code(url,http_status_code);
 
-        if ( curl_data == "NOERROR" )
-            ret_val = 0;
-        else if ( curl_data == "NOACCESS" )
-            get_logger()->print_http_not_authorized(url,get_login(),get_password());
+        // HTTP operation completed successful.
+        // Now we have to parse the data received by curl,
+        // cause http status code is not significant for easydns update errors
+        if ( http_status_code == 200 )
+        {
+            // Get the received http data.
+            string curl_data = HTTPHelp->get_curl_data();
+            string status_code = Util::parse_status_code(curl_data,"\n");
+
+            if ( status_code == "NOERROR" )
+            {
+                return UpdateOk;
+            }
+            else if ( status_code == "NOACCESS" )
+            {
+                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_httphelper_not_initialized();
+        HTTPHelp->re_initialize();
     }
 
-    return ret_val;
-}
-
-
-/**
- * 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 SERVICE_EASYDNS::serialize(Archive & ar, const unsigned int version)
-{
-    ar & boost::serialization::base_object<Service>(*this);
+    return GenericError;
 }