Ejemplo n.º 1
0
        public static async Task DiscoverAsync()
        {
            Discoverer discoverer = new Discoverer();

            discoverer.AgentFound += DiscovererAgentFound;
            Console.WriteLine("v1 discovery");
            await discoverer.DiscoverAsync(VersionCode.V1, new IPEndPoint(IPAddress.Broadcast, 161), new OctetString("public"), 6000);

            Console.WriteLine("v2 discovery");
            await discoverer.DiscoverAsync(VersionCode.V2, new IPEndPoint(IPAddress.Broadcast, 161), new OctetString("public"), 6000);

            Console.WriteLine("v3 discovery");
            await discoverer.DiscoverAsync(VersionCode.V3, new IPEndPoint(IPAddress.Broadcast, 161), null, 6000);
        }
Ejemplo n.º 2
0
        public async void TestDiscovererAsync()
        {
            var engine = CreateEngine();

            engine.Listener.ClearBindings();
            var serverEndPoint = new IPEndPoint(IPAddress.Any, Port.NextId);

            engine.Listener.AddBinding(serverEndPoint);
            engine.Start();

            var timeout = 1000;
            var wait    = 60 * timeout;

            try
            {
                var signal     = new AutoResetEvent(false);
                var discoverer = new Discoverer();
                discoverer.AgentFound += (sender, args)
                                         =>
                {
                    Assert.True(args.Agent.Address.ToString() != "0.0.0.0");
                    signal.Set();
                };
                await discoverer.DiscoverAsync(VersionCode.V1, new IPEndPoint(IPAddress.Broadcast, serverEndPoint.Port),
                                               new OctetString("public"), timeout);

                Assert.True(signal.WaitOne(wait));

                signal.Reset();
                await discoverer.DiscoverAsync(VersionCode.V2, new IPEndPoint(IPAddress.Broadcast, serverEndPoint.Port),
                                               new OctetString("public"), timeout);

                Assert.True(signal.WaitOne(wait));

                signal.Reset();
                await discoverer.DiscoverAsync(VersionCode.V3, new IPEndPoint(IPAddress.Broadcast, serverEndPoint.Port),
                                               null, timeout);

                Assert.True(signal.WaitOne(wait));
            }
            finally
            {
                if (SnmpMessageExtension.IsRunningOnWindows)
                {
                    engine.Stop();
                }
            }
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var discoverer = new Discoverer();

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            try
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    var agents = new List <string>();

                    void OnAgentFound(object sender, AgentFoundEventArgs args)
                    {
                        agents.Add(args.Agent.Address.ToString());
                    };

                    discoverer.AgentFound += OnAgentFound;
                    discoverer.AgentFound += OnAgentFound;
                    await discoverer.DiscoverAsync(VersionCode.V3, new IPEndPoint(IPAddress.Broadcast, 161), new OctetString("public"), 5000);

                    var discoveredAgents = agents.Select(x =>
                                                         new AgentDeviceInfo(x, VersionCode.V3.ToString())).ToArray();

                    var currentAgents  = _activeAgentsCache.GetActiveDevices();
                    var newAgents      = discoveredAgents.Except(currentAgents);
                    var inactiveAgents = currentAgents.Except(discoveredAgents);

                    foreach (var newAgent in newAgents)
                    {
                        await _agentsHubContext.Clients.All.SendAsync("NEW-AGENT-DISCOVERED", newAgent, cancellationToken);

                        _activeAgentsCache.AddOrUpdateDevice(newAgent);
                        Console.WriteLine($"New agent discovered: {newAgent}");
                    }

                    foreach (var inactiveAgent in inactiveAgents)
                    {
                        await _agentsHubContext.Clients.All.SendAsync("AGENT-DISCONNECTED", inactiveAgent, cancellationToken);

                        _activeAgentsCache.DeleteDevice(inactiveAgent);
                        Console.WriteLine($"Agent ({inactiveAgent}) became inactive");
                    }

                    await Task.Delay(5000, cancellationToken);
                }
            }
            catch (TaskCanceledException)
            {
            }
            finally
            {
                _activeAgentsCache.Clear();
            }
        }
Ejemplo n.º 4
0
        public void TestDiscovererAsyncV3()
        {
            var engine = CreateEngine();

            engine.Listener.ClearBindings();
            var serverEndPoint = new IPEndPoint(IPAddress.Any, Port.NextId);

            engine.Listener.AddBinding(serverEndPoint);
            engine.Start();

            var timeout = 1000;
            var wait    = 60 * timeout;

            try
            {
                var signal     = new AutoResetEvent(false);
                var ending     = new AutoResetEvent(false);
                var discoverer = new Discoverer();
                discoverer.AgentFound += (sender, args)
                                         =>
                {
                    Assert.True(args.Agent.Address.ToString() != "0.0.0.0");
                    signal.Set();
                };

                var source = Observable.Defer(async() =>
                {
                    await discoverer.DiscoverAsync(VersionCode.V3, new IPEndPoint(IPAddress.Broadcast, serverEndPoint.Port),
                                                   null, timeout);
                    var result = signal.WaitOne(wait);
                    if (!result)
                    {
                        throw new Messaging.TimeoutException();
                    }

                    return(Observable.Return(result));
                })
                             .RetryWithBackoffStrategy(
                    retryCount: 4,
                    retryOnError: e => e is Messaging.TimeoutException
                    );

                source.Subscribe(result =>
                {
                    Assert.True(result);
                    ending.Set();
                });
                Assert.True(ending.WaitOne(60 * wait));
            }
            finally
            {
                if (SnmpMessageExtension.IsRunningOnWindows)
                {
                    engine.Stop();
                }
            }
        }
Ejemplo n.º 5
0
        private async Task <HashSet <string> > DiscoverAsync()
        {
            var addresses = new HashSet <string>();
            var endPoint  = new IPEndPoint(IPAddress.Broadcast, 161);

            try
            {
                var discoverer = new Discoverer();
                discoverer.AgentFound += (sender, e) => { addresses.Add(e.Agent.Address.ToString()); };

                await discoverer.DiscoverAsync(VersionCode.V1, endPoint, _community, _timeOut);

                await discoverer.DiscoverAsync(VersionCode.V2, endPoint, _community, _timeOut);

                await discoverer.DiscoverAsync(VersionCode.V3, endPoint, null, _timeOut);
            }
            catch (Exception e)
            {
                WriteWarning($"Could not complete the request: {e.Message}");
            }

            return(addresses);
        }