Exemplo n.º 1
0
        /// <summary>
        /// Constructs a poller that uses the given configuration.
        /// </summary>
        /// <param name="configuration">The configuration to use.</param>
        /// <param name="hamnetDbAccess">The HamnetDB access object to use. If null, a new instance will be requested from HamnetDB provider.</param>
        public HamnetDbPoller(IConfiguration configuration, IHamnetDbAccess hamnetDbAccess)
        {
            this.configuration = configuration;

            this.hamnetDbConfig = this.configuration.GetSection(HamnetDbProvider.HamnetDbSectionName);
            var rssiAquisitionConfig = this.configuration.GetSection(Program.RssiAquisitionServiceSectionKey);

            this.maximumSubnetCount = rssiAquisitionConfig.GetValue <int>("MaximumSubnetCount");
            if (this.maximumSubnetCount == 0)
            {
                // config returns 0 if not defined --> turn it to the reasonable "maximum" value
                this.maximumSubnetCount = int.MaxValue;
            }

            this.subnetStartOffset = rssiAquisitionConfig.GetValue <int>("SubnetStartOffset"); // will implicitly return 0 if not defined

            var bgpAquisitionConfig = this.configuration.GetSection(Program.BgpAquisitionServiceSectionKey);

            this.maximumHostCount = bgpAquisitionConfig.GetValue <int>("MaximumHostCount");
            if (this.maximumHostCount == 0)
            {
                // config returns 0 if not defined --> turn it to the reasonable "maximum" value
                this.maximumHostCount = int.MaxValue;
            }

            this.hostStartOffset = bgpAquisitionConfig.GetValue <int>("HostStartOffset"); // will implicitly return 0 if not defined

            this.cacheRefreshInterval = this.hamnetDbConfig.GetValue <TimeSpan>("CacheRefreshInterval");

            this.hamnetDbAccess = hamnetDbAccess ?? HamnetDbProvider.Instance.GetHamnetDbFromConfiguration(this.hamnetDbConfig);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Instantiate from connection string and an additional Disposer.
        /// </summary>
        /// <param name="cacheRefreshInterval">The interval at which the cache shall be refreshed.</param>
        /// <param name="underlyingHamnetDbAccess">The underlying IHamnetDbAccess implementation that is used to populate/refresh the cache.</param>
        /// <param name="usePreemtiveCachePopulation">
        /// A value indicating whether to preemptively populate the cache (in background).<br/>
        /// If <c>true</c> the cache will be populated in background at the given interval.<br/>
        /// If <c>false</c> the cache will be populated in foreground and only when needed.
        /// </param>
        public CachingHamnetDbAccessor(TimeSpan cacheRefreshInterval, IHamnetDbAccess underlyingHamnetDbAccess, bool usePreemtiveCachePopulation)
        {
            if (underlyingHamnetDbAccess == null)
            {
                throw new ArgumentNullException(nameof(underlyingHamnetDbAccess), "The underlying IHamnetDbAccess instance is null");
            }

            this.CacheRefreshInterval        = (cacheRefreshInterval < TimeSpan.Zero) ? TimeSpan.Zero : cacheRefreshInterval;
            this.underlyingHamnetDbAccess    = underlyingHamnetDbAccess;
            this.UsePreemtiveCachePopulation = usePreemtiveCachePopulation;

            if (this.UsePreemtiveCachePopulation)
            {
                if (this.CacheRefreshInterval >= MinimumCacheRefreshIntervalPreemtive)
                {
                    this.CacheRefreshInterval = this.CacheRefreshInterval - TimeSpan.FromSeconds(3); /* starting a bit before expiry */
                }
                else
                {
                    log.Warn($"Requested cache refresh interval in preemtive mode ({this.CacheRefreshInterval}) < minimum interval for preemtive mode ({MinimumCacheRefreshIntervalPreemtive}): Disabling preemtive mode !");
                    this.UsePreemtiveCachePopulation = false;
                }
            }

            this.cacheDataStore = new CacheDataStore(this.CacheRefreshInterval);

            if (this.UsePreemtiveCachePopulation)
            {
                log.Info($"Starting preemtive cache refresh at interval of {this.CacheRefreshInterval}");
                this.cacheRefreshTimer = new Timer(DoPreemtiveCacheRefresh, null, TimeSpan.Zero, this.CacheRefreshInterval);
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Instantiates the controller taking a logger.
 /// </summary>
 /// <param name="logger">The logger to use for logging.</param>
 /// <param name="configuration">The configuration settings.</param>
 /// <param name="dbContext">The databse context to use for retrieving the values to return.</param>
 /// <param name="hamnetDbAccess">The accessor to HamnetDB (needed to get coordinates for callsigns).</param>
 public ToolController(ILogger <RestController> logger, IConfiguration configuration, QueryResultDatabaseContext dbContext, IHamnetDbAccess hamnetDbAccess)
 {
     this.logger         = logger ?? throw new System.ArgumentNullException(nameof(logger), "The logger to use is null");
     this.configuration  = configuration ?? throw new System.ArgumentNullException(nameof(configuration), "The configuration to use is null");
     this.dbContext      = dbContext ?? throw new System.ArgumentNullException(nameof(dbContext), "The database context to take the data from is null");
     this.hamnetDbAccess = hamnetDbAccess ?? throw new System.ArgumentNullException(nameof(dbContext), "The HamnetDB access singleton is null");
 }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the KmlAction
 /// </summary>
 /// <param name="fromLocation">The from site location.</param>
 /// <param name="toLocation">The to site location.</param>
 /// <param name="fromUrlQueryQuerierOptions">The querier options.</param>
 /// <param name="hamnetDbAccess">The accessor to HamnetDB (needed to get coordinates for callsigns).</param>
 public KmlAction(ToolController.FromLocationFromQuery fromLocation, ToolController.ToLocationFromQuery toLocation, FromUrlQueryQuerierOptions fromUrlQueryQuerierOptions, IHamnetDbAccess hamnetDbAccess)
 {
     this.fromLocation = fromLocation;
     this.toLocation   = toLocation;
     this.fromUrlQueryQuerierOptions = fromUrlQueryQuerierOptions;
     this.hamnetDbAccess             = hamnetDbAccess;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the KmlAction
 /// </summary>
 /// <param name="fromCall">The from site callsign.</param>
 /// <param name="toCall">The to site callsign.</param>
 /// <param name="fromUrlQueryQuerierOptions">The querier options.</param>
 /// <param name="hamnetDbAccess">The accessor to HamnetDB (needed to get coordinates for callsigns).</param>
 public KmlAction(string fromCall, string toCall, FromUrlQueryQuerierOptions fromUrlQueryQuerierOptions, IHamnetDbAccess hamnetDbAccess)
 {
     this.hamnetDbAccess             = hamnetDbAccess;
     this.fromCall                   = fromCall;
     this.toCall                     = toCall;
     this.fromUrlQueryQuerierOptions = fromUrlQueryQuerierOptions;
 }
Exemplo n.º 6
0
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposedValue)
            {
                if (disposing)
                {
                    // TODO: verwalteten Zustand (verwaltete Objekte) entsorgen.
                    this.hamnetDbAccess?.Dispose();
                    this.hamnetDbAccess = null;
                }

                // TODO: nicht verwaltete Ressourcen (nicht verwaltete Objekte) freigeben und Finalizer weiter unten überschreiben.
                // TODO: große Felder auf Null setzen.

                this.disposedValue = true;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Construct for a specific host.
        /// </summary>
        /// <param name="network">The network to test in CIDR or IP/Netmask notation.</param>
        /// <param name="logger">The logger to use.</param>
        /// <param name="hamnetDbAccess">The service configuration.</param>
        /// <param name="querierOptions">The options to the Hamnet querier.</param>
        public NetworkTest(string network, ILogger logger, IHamnetDbAccess hamnetDbAccess, IQuerierOptions querierOptions)
        {
            if (string.IsNullOrWhiteSpace(network))
            {
                throw new ArgumentNullException(nameof(network), "Network to test is null, empty or white-space-only");
            }

            IPNetwork subnet = null;

            if (!IPNetwork.TryParse(network, out subnet))
            {
                throw new ArgumentException($"Specified network '{network}' is not a valid IP network specification", nameof(network));
            }

            this.logger         = logger;
            this.hamnetDbAccess = hamnetDbAccess ?? throw new ArgumentNullException(nameof(hamnetDbAccess), "Handle to the HamnetDB accessor is null");
            this.network        = subnet;
            this.querierOptions = querierOptions ?? new FromUrlQueryQuerierOptions();
        }
        /// <summary>
        /// Constructs taking the logger.
        /// </summary>
        /// <param name="logger">The logger to use.</param>
        /// <param name="configuration">The service configuration.</param>
        /// <param name="hamnetDbAccess">The singleton instance of the HamnetDB access handler.</param>
        /// <param name="retryFeasibleHandler">The handler to check whether retry is feasible.</param>
        public RssiAquisitionService(ILogger<RssiAquisitionService> logger, IConfiguration configuration, IHamnetDbAccess hamnetDbAccess, IFailureRetryFilteringDataHandler retryFeasibleHandler)
        {
            this.logger = logger ?? throw new ArgumentNullException(nameof(logger), "The logger has not been provided by DI engine");
            this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration), "The configuration has not been provided by DI engine");
            this.retryFeasibleHandler = retryFeasibleHandler ?? throw new ArgumentNullException(nameof(retryFeasibleHandler), "The retry feasible handler singleton has not been provided by DI engine");

            this.dataHandlers.Add(this.retryFeasibleHandler);
            this.dataHandlers.Add(new ResultDatabaseDataHandler(configuration, this.retryFeasibleHandler));

            this.hamnetDbPoller = new HamnetDbPoller(this.configuration, hamnetDbAccess ?? throw new ArgumentNullException(nameof(hamnetDbAccess), "The HamnetDB accessor singleton has not been provided by DI engine"));

            IConfigurationSection influxSection = configuration.GetSection(Program.InfluxSectionKey);
            if ((influxSection != null) && influxSection.GetChildren().Any())
            {
                this.dataHandlers.Add(new InfluxDatabaseDataHandler(configuration, this.hamnetDbPoller));
            }
            else
            {
                this.logger.LogInformation($"Influx database disabled: No or empty '{Program.InfluxSectionKey}' section in configuration");
            }

            this.dataHandlers = this.dataHandlers.OrderBy(h => h.Name).ToList();
        }
        /// <summary>
        /// Constructs taking the logger.
        /// </summary>
        /// <param name="logger">The logger to use.</param>
        /// <param name="configuration">The service configuration.</param>
        /// <param name="hamnetDbAccess">The singleton instance of the HamnetDB access handler.</param>
        /// <param name="retryFeasibleHandler">The handler to check whether retry is feasible.</param>
        public BgpAquisitionService(ILogger <BgpAquisitionService> logger, IConfiguration configuration, IHamnetDbAccess hamnetDbAccess, IFailureRetryFilteringDataHandler retryFeasibleHandler)
        {
            this.logger               = logger ?? throw new ArgumentNullException(nameof(logger), "The logger has not been provided by DI engine");
            this.configuration        = configuration ?? throw new ArgumentNullException(nameof(configuration), "The configuration has not been provided by DI engine");
            this.retryFeasibleHandler = retryFeasibleHandler ?? throw new ArgumentNullException(nameof(retryFeasibleHandler), "The retry feasible handler singleton has not been provided by DI engine");

            this.hamnetDbPoller = new HamnetDbPoller(this.configuration, hamnetDbAccess ?? throw new ArgumentNullException(nameof(hamnetDbAccess), "The HamnetDB accessor singleton has not been provided by DI engine"));

            this.dataHandlers.Add(this.retryFeasibleHandler);
            this.dataHandlers.Add(new ResultDatabaseDataHandler(configuration, this.retryFeasibleHandler));
            this.dataHandlers.Add(new InfluxDatabaseDataHandler(configuration, this.hamnetDbPoller));
            this.dataHandlers = this.dataHandlers.OrderBy(h => h.Name).ToList();
        }
Exemplo n.º 10
0
        /// <summary>
        /// Gets all unique <c>pairs</c> of hosts with monitoring flag set and which share the same subnet.<br/>
        /// Subnets with less than or more than two hosts will be discarded.
        /// </summary>
        /// <param name="hamnetDbAccess">The handle to access the database.</param>
        /// <param name="subnet">The subnet to return data for.</param>
        /// <returns>The dictionary mapping a subnet to its unique monitored host pair.</returns>
        public static IReadOnlyDictionary <IHamnetDbSubnet, IHamnetDbHosts> UniqueMonitoredHostPairsInSubnet(this IHamnetDbAccess hamnetDbAccess, IPNetwork subnet)
        {
            if (hamnetDbAccess == null)
            {
                throw new ArgumentNullException(nameof(hamnetDbAccess), "hamnetDbAccess to work with is null");
            }

            if (subnet == null)
            {
                throw new ArgumentNullException(nameof(subnet), "subnet search monitored hosts for is null");
            }

            var directSupportHamnetAccess = hamnetDbAccess as IDirectSupportOfHamnetDbAccessExtensions;

            if (directSupportHamnetAccess != null)
            {
                return(directSupportHamnetAccess.UniqueMonitoredHostPairsInSubnet(subnet));
            }

            var hosts      = hamnetDbAccess.QueryMonitoredHosts();
            var allSubnets = hamnetDbAccess.QuerySubnets();

            // filter out parents for which we have nested subnets
            var subnets = allSubnets.Where(s => !allSubnets.Any(a => !object.ReferenceEquals(s.Subnet, a.Subnet) && s.Subnet.Contains(a.Subnet)));

            var association = subnets.AssociateHosts(hosts);

            var uniquePairs = association
                              .Where(a => subnet.Contains(a.Key.Subnet) || subnet.Equals(a.Key.Subnet))
                              .Where(a => a.Value.Count == 2)
                              .ToDictionary(k => k.Key, v => v.Value);

            return(uniquePairs);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Gets all unique <c>pairs</c> of hosts with monitoring flag set and which share the same subnet.<br/>
        /// Subnets with less than or more than two hosts will be discarded.
        /// </summary>
        /// <param name="hamnetDbAccess">The handle to access the database.</param>
        /// <param name="allowDirectSupportClass">
        /// <c>true</c> if usage of IDirectSupportOfHamnetDbAccessExtensions is allowed (default).<br/>
        /// <c>false</c> if the usage is not allowed (usually only used when called from calls implementing IDirectSupportOfHamnetDbAccessExtensions).
        /// </param>
        /// <returns>The dictionary mapping a subnet to its unique monitored host pair.</returns>
        public static IReadOnlyDictionary <IHamnetDbSubnet, IHamnetDbHosts> UniqueMonitoredHostPairsInSameSubnet(this IHamnetDbAccess hamnetDbAccess, bool allowDirectSupportClass = true)
        {
            if (hamnetDbAccess == null)
            {
                throw new ArgumentNullException(nameof(hamnetDbAccess), "hamnetDbAccess to work with is null");
            }

            if (allowDirectSupportClass)
            {
                var directSupportHamnetAccess = hamnetDbAccess as IDirectSupportOfHamnetDbAccessExtensions;
                if (directSupportHamnetAccess != null)
                {
                    return(directSupportHamnetAccess.UniqueMonitoredHostPairsInSameSubnet());
                }
            }

            var hosts   = hamnetDbAccess.QueryMonitoredHosts();
            var subnets = hamnetDbAccess.QuerySubnets();

            var association = subnets.AssociateHosts(hosts);

            var uniquePairs = association.Where(a => a.Value.Count == 2).ToDictionary(k => k.Key, v => v.Value);

            return(uniquePairs);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Instantiates the controller taking a logger.
 /// </summary>
 /// <param name="logger">The logger to use for logging.</param>
 /// <param name="configuration">The configuration settings.</param>
 /// <param name="hamnetDbAccess">The handle to the HamnetDB accessor singleton.</param>
 public LinkTestController(ILogger <RestController> logger, IConfiguration configuration, IHamnetDbAccess hamnetDbAccess)
 {
     this.hamnetDbAccess = hamnetDbAccess ?? throw new System.ArgumentNullException(nameof(hamnetDbAccess), "The hamnetDbAccess to use is null");;
     this.logger         = logger ?? throw new System.ArgumentNullException(nameof(logger), "The logger to use is null");
     this.configuration  = configuration ?? throw new System.ArgumentNullException(nameof(configuration), "The configuration to use is null");
 }