net_check_logic.h 2.26 KB
//
//  net_check_logic.h
//
//  Created by cairui01 on 18/11/9.
//  Copyright (c) 2018年 baidu. All rights reserved.
//
#ifndef SDT_SRC_NET_CHECK_LOGIC_H_
#define SDT_SRC_NET_CHECK_LOGIC_H_

#include <list>
#include <string>
#include "netchecker_profile.h"

namespace apirequest {
    namespace sdt {
    
class NetSource;

class NetCheckLogic {
  public:
    NetCheckLogic();
    ~NetCheckLogic();

    bool UpdateShortLinkInfo(unsigned int _continue_count, bool isSuccess, int32_t checkCount, long long minCheckTimeSpan, bool isIncrement, std::map<std::string, CheckResultProfile*>& resultMap);
    void reset();
    
    //  是否开启DNS Query容错机制
    void EnableDnsQueryFailOver(bool dns_query_fail_over) {
        enable_dns_query_failover_ = dns_query_fail_over;
    }
    
    //  在绑定网卡链路探测时, 指定网卡名称、DNS服务器IP、需要Ping的目标IP
    bool BindInterface(const std::string& interface_name,
                       const std::string& dns_server_ip,
                       const std::string& ping_ip) {
        if (interface_name.empty()) {
            return false;
        }
        interface_name_ = interface_name;
        dns_server_ip_ = dns_server_ip;
        ping_ip_ = ping_ip;
        enable_bind_interface_ = true;
        printf("\n%s interface_name: %s, dns_server_ip: %s, ping_ip: %s\n",
               SDT_ALTER_INTERFACE_LOG,
               interface_name_.c_str(), dns_server_ip_.c_str(), ping_ip_.c_str());
        return true;
    }

  private:
    void __StartNetCheck(std::map<std::string, CheckResultProfile*>& resultMap);
    bool __ShouldNetCheck(unsigned int _continues_count, bool isSuccess, int32_t checkCount, long long minCheckTimeSpan, bool isIncrement);
    const std::string __GetDomainOrIpForPing(const std::string& domain, const CheckResultProfile *dnsProfile);

  private:
    unsigned long long last_netcheck_time_;
    //阶梯递增参数
    int increment_steps_;
    long long check_time_span_increment_step_; //1min
    //斐波那契数列
    int fib1_;
    int fib2_;
    bool enable_dns_query_failover_;
    
    //  绑定网卡探测
    bool enable_bind_interface_;
    std::string interface_name_;
    std::string dns_server_ip_;
    std::string ping_ip_;

};

    }
}

#endif // SDT_SRC_NET_CHECK_LOGIC_H_