Exemple #1
0
        static void Main(string[] args)
        {
#if DEBUG
            //ObjectCache.Instance.Clear();
            SensorFactory.RegisterSensorBuilder <IcmpSensor>(ServiceProtocol.ICMP, (sfr) => new MockedSensor());
            SensorFactory.RegisterSensorBuilder <TcpPortSensor>(ServiceProtocol.TCP, (sfr) => new MockedSensor());
            SensorFactory.RegisterSensorBuilder <UdpPortSensor>(ServiceProtocol.UDP, (sfr) => new MockedSensor());
#else
            SensorFactory.RegisterSensorBuilder <IcmpSensor>(ServiceProtocol.ICMP, (sfr) => new IcmpSensor());
            SensorFactory.RegisterSensorBuilder <TcpPortSensor>(ServiceProtocol.TCP, (sfr) => new TcpPortSensor(sfr.PortNumber));
            SensorFactory.RegisterSensorBuilder <UdpPortSensor>(ServiceProtocol.UDP, (sfr) => new UdpPortSensor(sfr.PortNumber));
#endif
            var serviceRecords = ObjectCache.Instance.GetOrSetObject("ServiceFileRecordsRegistryContent", () => LoadServiceRecordsFromFile());

            ServiceFileRecordRegistry.Load(serviceRecords);
            ServiceFileRecordRegistry.AddServiceFileRecord(new ServiceFileRecord()
            {
                Name        = "echo-icmp",
                DisplayName = "ping",
                PortNumber  = 0,
                Protocol    = ServiceProtocol.ICMP
            });

            var serverRecords = ObjectCache.Instance.GetOrSetObject("ServerList", () => new[] {
                new ServerRecord()
                {
                    HostnameOrIpAddress = "127.0.0.1",
                    ServiceNames        = new[] { "echo-icmp", "microsoft-ds-tcp" }
                },
                new ServerRecord()
                {
                    HostnameOrIpAddress = "ic15.at",
                    ServiceNames        = new[]  { "echo-icmp", "http-tcp", "https-tcp" }
                },
                new ServerRecord()
                {
                    HostnameOrIpAddress = "8.8.8.8",
                    ServiceNames        = new[]  { "echo-icmp", "domain-tcp" }
                },
                new ServerRecord()
                {
                    HostnameOrIpAddress = "8.8.4.4",
                    ServiceNames        = new[]  { "echo-icmp", "domain-tcp" }
                }
            });

            foreach (var serverRecord in serverRecords)
            {
                servers.Add(serverRecord.HostnameOrIpAddress, new Server(serverRecord.HostnameOrIpAddress, serverRecord.ServiceNames));
            }

            Console.CursorVisible = false;

            int bufferWidth  = PreferedConsoleBufferWidth > Console.BufferWidth ? PreferedConsoleBufferWidth : Console.BufferWidth;
            int bufferHeight = PreferedConsoleBufferHeight > Console.BufferHeight ? PreferedConsoleBufferHeight : Console.BufferHeight;

            Console.SetBufferSize(bufferWidth, bufferHeight);
            try
            {
                Console.SetWindowSize(PreferedConsoleWindowWidth, PreferedConsoleWindowHeight);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex.Message);
                Console.WriteLine("== Continue With Enter ==");
                Console.ReadLine();
            }

            Console.Clear();
            buffer = new ConsoleBuffer(Console.BufferWidth, Console.BufferHeight, Console.BufferWidth, Console.BufferHeight);

            object bufferLock = new object();

            buffer.Draw("= == === ==== ===== =========================================== ====== ==== === == =", 0, 0, 15);
            buffer.Draw("= == === ==== ===== =      Fancy Console Host Monitor         = ====== ==== === == =", 0, 1, 15);
            buffer.Draw("= == === ==== ===== =========================================== ====== ==== === == =", 0, 2, 15);

            int offsetIndex = 4;
            foreach (var s in servers.Values)
            {
                var pos = offsetIndex;
                offsetIndex += s.Services.Count() + 2;

                buffer.Draw(string.Format("#### {0}", s.FullyQualifiedHostName), 0, pos, 15);

                offsets.Add(s.HostnameOrIpAddress, pos);
            }
            buffer.Print();

            Observable.Merge(
                servers.Values.Select(server => Observable.Interval(TimeSpan.FromSeconds(1.0)).Select(_ => server))
                )
            .Do(server => {
                lock (server)
                {
                    server.UpdateServices();
                }
            })
            .Do(server => {
                lock (server)
                {
                    RedrawServer(server);
                }
            })
            .Sample(TimeSpan.FromSeconds(0.5))
            .Do(_ => buffer.Print())
            .RunAsync(CancellationToken.None)
            ;

            Console.ReadKey();
        }