Example #1
0
            public int Compare(ListViewItem lhs, ListViewItem rhs)
            {
                int result = Object.ReferenceEquals(lhs, rhs) ? 0 : -1;

                if (result != 0)
                {
                    var lhsClientAndSessions = (ClientAndSessions)lhs.Tag;
                    var rhsClientAndSessions = (ClientAndSessions)rhs.Tag;
                    var lhsClient            = lhsClientAndSessions.Client;
                    var rhsClient            = rhsClientAndSessions.Client;

                    switch (SortColumn)
                    {
                    case SortColumn.BytesSent:          result = Compare(lhsClientAndSessions.TotalBytesSent, rhsClientAndSessions.TotalBytesSent); break;

                    case SortColumn.Duration:           result = TimeSpan.Compare(lhsClientAndSessions.TotalDuration, rhsClientAndSessions.TotalDuration); break;

                    case SortColumn.FirstSeen:          result = DateTime.Compare(lhsClientAndSessions.FirstSession, rhsClientAndSessions.FirstSession); break;

                    case SortColumn.IpAddress:          result = _IPAddressComparer.Compare(lhsClient.Address, rhsClient.Address); break;

                    case SortColumn.IsLocal:            result = Compare(lhsClient.IsLocal, rhsClient.IsLocal); break;

                    case SortColumn.LastSeen:           result = DateTime.Compare(lhsClientAndSessions.LastSession, rhsClientAndSessions.LastSession); break;

                    case SortColumn.ReverseDns:         result = String.Compare(lhsClient.ReverseDns, rhsClient.ReverseDns); break;

                    case SortColumn.Sessions:           result = lhsClientAndSessions.Sessions.Count - rhsClientAndSessions.Sessions.Count; break;

                    default: throw new NotImplementedException();
                    }
                }

                return(Ascending ? result : -result);
            }
Example #2
0
        public void Compare3()
        {
            IPAddressComparer comparer = IPAddressComparer.Default as IPAddressComparer;

            try
            {
                int result = comparer.Compare(IPAddress.Loopback, null);
            }
            catch (ArgumentNullException e)
            {
                if (e.ParamName == "y")
                {
                    Assert.IsTrue(true);
                }
                else
                {
                    Assert.Fail(e.Message);
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            try
            {
                int result = comparer.Compare(null, IPAddress.Loopback);
            }
            catch (ArgumentNullException e)
            {
                if (e.ParamName == "x")
                {
                    Assert.IsTrue(true);
                }
                else
                {
                    Assert.Fail(e.Message);
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
Example #3
0
        public void Compare4()
        {
            IPAddressComparer comparer = IPAddressComparer.Default as IPAddressComparer;
            //Assert.IsTrue(comparer.Compare(null, IPAddress.Parse("128.0.0.0")) == -1);
            //Assert.IsTrue(comparer.Compare(IPAddress.Parse("128.0.0.0"), null) == 1);

            object x = null;
            object y = null;

            Assert.IsTrue(comparer.Compare(x, y) == 0);
        }
        public void Describe_Airport_Formats_Airport_Correctly()
        {
            var worksheet = new ExcelWorksheetData(TestContext);

            var comparer = new IPAddressComparer();
            var lhs = worksheet.String("LHS") == null ? null : IPAddress.Parse(worksheet.String("LHS"));
            var rhs = worksheet.String("RHS") == null ? null : IPAddress.Parse(worksheet.String("RHS"));
            var expectedResult = worksheet.Int("Result");

            var result = comparer.Compare(lhs, rhs);
            if(expectedResult < 0)      Assert.IsTrue(result < 0);
            else if(expectedResult > 0) Assert.IsTrue(result > 0);
            else                        Assert.AreEqual(0, result);
        }
Example #5
0
        public void Compare6()
        {
            IPAddressComparer comparer = new IPAddressComparer();
            object            x        = "127.0.0.0";
            object            y        = "128.0.0.0";

            Assert.IsTrue(comparer.Compare(x, y) < 0);
            Assert.IsTrue(comparer.Compare(y, x) > 0);
            Assert.IsTrue(comparer.Compare(x, x) == 0);

            x = "1567890";
            y = "1890329";

            Assert.IsTrue(comparer.Compare(x, y) < 0);
            Assert.IsTrue(comparer.Compare(y, x) > 0);
            Assert.IsTrue(comparer.Compare(x, x) == 0);

            x = String.Empty;
            y = String.Empty;

            Assert.IsTrue(comparer.Compare(x, "127.0.0.0") == -1);
            Assert.IsTrue(comparer.Compare("127.0.0.0", y) == 1);
            Assert.IsTrue(comparer.Compare(x, y) == 0);
        }
Example #6
0
            public int Compare(ListViewItem lhs, ListViewItem rhs)
            {
                int result = Object.ReferenceEquals(lhs, rhs) ? 0 : -1;

                if (result != 0)
                {
                    ClientAndSession lhsClientAndSession = (ClientAndSession)lhs.Tag;
                    ClientAndSession rhsClientAndSession = (ClientAndSession)rhs.Tag;
                    LogClient        lhsClient           = lhsClientAndSession.Client;
                    LogClient        rhsClient           = rhsClientAndSession.Client;
                    LogSession       lhsSession          = lhsClientAndSession.Session;
                    LogSession       rhsSession          = rhsClientAndSession.Session;

                    switch (SortColumn)
                    {
                    case SortColumn.AudioBytesSent:     result = Compare(lhsSession.AudioBytesSent, rhsSession.AudioBytesSent); break;

                    case SortColumn.BytesSent:          result = Compare(lhsSession.TotalBytesSent, rhsSession.TotalBytesSent); break;

                    case SortColumn.Duration:           result = TimeSpan.Compare(lhsSession.Duration, rhsSession.Duration); break;

                    case SortColumn.HtmlBytesSent:      result = Compare(lhsSession.HtmlBytesSent, rhsSession.HtmlBytesSent); break;

                    case SortColumn.ImageBytesSent:     result = Compare(lhsSession.ImageBytesSent, rhsSession.ImageBytesSent); break;

                    case SortColumn.IpAddress:          result = _IPAddressComparer.Compare(lhsClient.Address, rhsClient.Address); break;

                    case SortColumn.IsLocal:            result = Compare(lhsClient.IsLocal, rhsClient.IsLocal); break;

                    case SortColumn.JsonBytesSent:      result = Compare(lhsSession.JsonBytesSent, rhsSession.JsonBytesSent); break;

                    case SortColumn.OtherBytesSent:     result = Compare(lhsSession.OtherBytesSent, rhsSession.OtherBytesSent); break;

                    case SortColumn.Requests:           result = Compare(lhsSession.CountRequests, rhsSession.CountRequests); break;

                    case SortColumn.StartTime:          result = DateTime.Compare(lhsSession.StartTime, rhsSession.StartTime); break;

                    default:                            throw new NotImplementedException();
                    }
                }

                return(Ascending ? result : -result);
            }
Example #7
0
        public void IPEqualsIPShould()
        {
            IPAddress ip1 = IPAddress.Parse("192.168.0.1");
            IPAddress ip2 = IPAddress.Parse("192.168.0.1");
            IPAddress ip3 = IPAddress.Parse("192.168.0.2");

            IPAddressComparer comparer = new IPAddressComparer();
            //Test equals method in IPAddressComparer
            bool isAddressEqual  = comparer.Equals(ip1, ip2);
            bool isAddressEqual2 = comparer.Equals(ip2, ip3);

            Assert.True(isAddressEqual);
            Assert.False(isAddressEqual2);

            //Test compare method in IPAddressComparer
            int compare = comparer.Compare(ip1, ip3);

            Assert.Equal(-1, compare);
        }
Example #8
0
        public void Compare2()
        {
            IPAddressComparer comparer = IPAddressComparer.Default as IPAddressComparer;

            Assert.IsTrue(comparer.Compare(IPAddress.Loopback, IPAddress.Parse("128.0.0.0")) < 0);
            Assert.IsTrue(comparer.Compare(IPAddress.Parse("128.0.0.0"), IPAddress.Loopback) > 0);
            Assert.IsTrue(comparer.Compare(IPAddress.Loopback, IPAddress.Loopback) == 0);

            IPAddress x = new IPAddress(1067890); // 114.75.16.0
            IPAddress y = new IPAddress(1890329); // 25.216.28.0

            Assert.IsTrue(comparer.Compare(x, y) > 0);
            Assert.IsTrue(comparer.Compare(y, x) < 0);
            Assert.IsTrue(comparer.Compare(x, x) == 0);
        }
Example #9
0
        public void Compare1()
        {
            IPAddressComparer comparer = IPAddressComparer.Default as IPAddressComparer;
            string            x        = "127.0.0.0";
            string            y        = "128.0.0.0";

            Assert.IsTrue(comparer.Compare(x, y) < 0);
            Assert.IsTrue(comparer.Compare(y, x) > 0);
            Assert.IsTrue(comparer.Compare(x, x) == 0);

            x = "1067890"; // 0.16.75.114
            y = "1890329"; // 0.28.216.25.0

            Assert.IsTrue(comparer.Compare(x, y) < 0);
            Assert.IsTrue(comparer.Compare(y, x) > 0);
            Assert.IsTrue(comparer.Compare(x, x) == 0);
        }
        public void Describe_Airport_Formats_Airport_Correctly()
        {
            var worksheet = new ExcelWorksheetData(TestContext);

            var comparer       = new IPAddressComparer();
            var lhs            = worksheet.String("LHS") == null ? null : IPAddress.Parse(worksheet.String("LHS"));
            var rhs            = worksheet.String("RHS") == null ? null : IPAddress.Parse(worksheet.String("RHS"));
            var expectedResult = worksheet.Int("Result");

            var result = comparer.Compare(lhs, rhs);

            if (expectedResult < 0)
            {
                Assert.IsTrue(result < 0);
            }
            else if (expectedResult > 0)
            {
                Assert.IsTrue(result > 0);
            }
            else
            {
                Assert.AreEqual(0, result);
            }
        }
Example #11
0
        public override CollectedData OnAcquire()
        {
            // Find the IP addresses to ping. This will typically provide the IP addresses
            // of the devices being monitored, and can also provide a subnet to ping, and
            // also any extra addresses to ping.
            IPAddressRequest request = new IPAddressRequest("PingCollector");

            RequestBus.Instance.MakeRequest(request);
            if (request.IsHandled == false)
            {
                return(null);
            }

            ListData <PingResult>             d              = new ListData <PingResult>(Context);
            List <Tuple <IPAddress, string> > to_ping        = new List <Tuple <IPAddress, string> >();
            Dictionary <string, string>       ip_to_name_map = new Dictionary <string, string>();

            request.IPAddresses.ForEach(i => ip_to_name_map[i.Item1] = i.Item2);

            // See if a full subnet ping was requested
            foreach (string s in request.Subnets)
            {
                if (IPAddress.TryParse(s, out IPAddress subnet))
                {
                    byte[] ping_addr = subnet.GetAddressBytes();

                    // Collect all the pingable IP addresses on the specified subnet.
                    // 0 and 255 are reserved, so no need to ping them.
                    for (byte i = 1; i < 255; ++i)
                    {
                        ping_addr[3] = i;
                        IPAddress addr = new IPAddress(ping_addr);

                        // Get the name of the device, if we happen to know it
                        string name = string.Empty;
                        ip_to_name_map.TryGetValue(addr.ToString(), out name);

                        to_ping.Add(Tuple.Create(addr, name));
                    }
                }
            }

            // Now put in the other ip addresses that are being monitored.
            // Add them to the list of IPs to ping.
            foreach (Tuple <string, string> ip in request.IPAddresses)
            {
                try
                {
                    if (IPAddress.TryParse(ip.Item1, out IPAddress addr))
                    {
                        to_ping.Add(Tuple.Create(addr, ip.Item2));
                    }
                }
                catch (Exception)
                {
                }
            }

            // Remove any duplictes IP addresses that might have gotten in there
            IPAddressComparer c = new IPAddressComparer();

            to_ping.Sort((a, b) => c.Compare(a.Item1, b.Item1));
            to_ping = to_ping.Distinct().ToList();

            // Create some # of threads for concurrent pinging. The number of threads
            // will be around request.NumPingers.
            ManualResetEvent reset_event = new ManualResetEvent(false);
            int           thread_count   = 0;
            List <Thread> threads        = new List <Thread>();
            List <List <Tuple <IPAddress, string> > > chunks = to_ping.ChunkBy(to_ping.Count / request.NumPingers);

            foreach (List <Tuple <IPAddress, string> > addrs in chunks)
            {
                Thread t = new Thread(
                    () =>
                {
                    Stopwatch watch = Stopwatch.StartNew();

                    List <Tuple <IPAddress, string> > local_addrs = addrs;
                    foreach (Tuple <IPAddress, string> addr in local_addrs)
                    {
                        PingResult result = addr.Item1.PingIt(addr.Item2);
                        lock (d.Data)
                            d.Data.Add(result);
                    }

                    Trace.WriteLine(string.Format("Pinging {0} addrs took {1} ms", local_addrs.Count, watch.ElapsedMilliseconds));

                    if (Interlocked.Decrement(ref thread_count) <= 0)
                    {
                        reset_event.Set();
                    }
                });
                threads.Add(t);
            }

            if (threads.Count > 0)
            {
                try
                {
                    thread_count = threads.Count;
                    threads.ForEach(t => t.Start());

                    reset_event.WaitOne();
                }
                catch (Exception)
                {
                }
                finally
                {
                }

                Dictionary <string, string> ip_to_mac = GetIPAddrsAndMacAddresses();
                foreach (PingResult pr in d.Data)
                {
                    string ip = pr.Address.ToString();
                    if (ip_to_mac.TryGetValue(ip, out string mac))
                    {
                        pr.MAC = mac;
                    }
                }
            }

            d.Data.Sort((a, b) => c.Compare(a.Address, b.Address));
            return(new CollectedData(Context, true, d));
        }
Example #12
0
        public void Compare6()
        {
            IPAddressComparer comparer = new IPAddressComparer();
            object x = "127.0.0.0";
            object y = "128.0.0.0";

            Assert.IsTrue(comparer.Compare(x, y) < 0);
            Assert.IsTrue(comparer.Compare(y, x) > 0);
            Assert.IsTrue(comparer.Compare(x, x) == 0);

            x = "1567890";
            y = "1890329";

            Assert.IsTrue(comparer.Compare(x, y) < 0);
            Assert.IsTrue(comparer.Compare(y, x) > 0);
            Assert.IsTrue(comparer.Compare(x, x) == 0);

            x = String.Empty;
            y = String.Empty;

            Assert.IsTrue(comparer.Compare(x, "127.0.0.0") == -1);
            Assert.IsTrue(comparer.Compare("127.0.0.0", y) == 1);
            Assert.IsTrue(comparer.Compare(x, y) == 0);
        }
Example #13
0
        public void Compare5()
        {
            IPAddressComparer comparer = IPAddressComparer.Default as IPAddressComparer;

            try
            {
                comparer.Compare("1067890", "abc123");
            }
            catch (FormatException)
            {
                Assert.IsTrue(true);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            try
            {
                comparer.Compare("abc123", "1067890");
            }
            catch (FormatException)
            {
                Assert.IsTrue(true);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            try
            {
                comparer.Compare("1067890", String.Empty);
            }
            catch (FormatException)
            {
                Assert.IsTrue(true);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            try
            {
                comparer.Compare(String.Empty, "1067890");
            }
            catch (FormatException)
            {
                Assert.IsTrue(true);
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            try
            {
                comparer.Compare("1067890", null);
            }
            catch (ArgumentNullException e)
            {
                if (e.ParamName == "y")
                {
                    Assert.IsTrue(true);
                }
                else
                {
                    Assert.Fail(e.Message);
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }

            try
            {
                comparer.Compare(null, "1067890");
            }
            catch (ArgumentNullException e)
            {
                if (e.ParamName == "x")
                {
                    Assert.IsTrue(true);
                }
                else
                {
                    Assert.Fail(e.Message);
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
Example #14
0
 public void Equal()
 {
     Assert.AreEqual(0, comparer.Compare(IpAddress.Parse("192.168.1.254"), IpAddress.Parse("192.168.1.254")));
 }