Ejemplo n.º 1
0
        /// <summary>Method which when a StatsConnection is established requests Interface statistics.</summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="connectionStatus">The <see cref="StatsConnection"/>'s new <see cref="StatsConnection.ConnectionStatus"/>.</param>
        private void Connection_ConnectionStatusChanged(object sender, StatsConnection.ConnectionStatus connectionStatus)
        {
            // The sender should be a StatsConnection so that we can interact with that StatsConnection instance.
            StatsConnection statsConnection = sender as StatsConnection;

            if (statsConnection == null)
            {
                return;
            }

            // Specifically what did the ConnectionStatus change to?
            switch (connectionStatus)
            {
            // It was previously not connected and now it is.
            case StatsConnection.ConnectionStatus.Connected:

                // Compose a subscription request message.
                SubscriptionRequest subscriptionRequest = new SubscriptionRequest
                {
                    Subscribe = new Subscription[] { new Subscription()
                                                     {
                                                         name = SubscriptionMessageType.Interfaces
                                                     } },
                    SessionID = statsConnection.SessionID
                };

                // Ask for events to be delivered.
                statsConnection.SubscribeForEvents(subscriptionRequest);
                break;
            }
        }
Ejemplo n.º 2
0
        static void Main()
        {
            // Set the window title to something a bit more interesting.
            if (!Console.IsOutputRedirected)
            {
                Console.Title = "LogFeed V0.1";
            }

            // Check the credentials are provided in the application's configuration file.
            if (ConfigurationManager.AppSettings["Username"] == null || ConfigurationManager.AppSettings["Password"] == null || ConfigurationManager.AppSettings["Host"] == null)
            {
                Console.WriteLine("Program cannot start, some credentials were missing in the program's configuration file.");

                // Exit the application.
                Environment.Exit(1610);
            }

            // The WebClient allows us to get a valid SessionID to then use with the StatsConnection.
            using (WebClient webClient = new WebClient("https://" + ConfigurationManager.AppSettings["Host"] + "/"))
            {
                // This method will be invoked each time the timer has elapsed.
                sessionHeartbeatTimer.Elapsed += (s, a) => webClient.Heartbeat();

                // Ignore TLS certificate errors if there is a ".crt" file present that matches this host.
                webClient.AllowLocalCertificates();

                // Login to the router.
                webClient.Login(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);

                // Share a valid SessionID with a new StatsConnection object.
                statsConnection = new StatsConnection(webClient.SessionID);

                // Ignore TLS certificate errors if there is a ".crt" file present that matches this host.
                statsConnection.AllowLocalCertificates();

                // Connect to the router.
                statsConnection.ConnectAsync(new Uri("wss://" + ConfigurationManager.AppSettings["Host"] + "/ws/stats"));

                // Setup an event handler for when data is received.
                statsConnection.DataReceived += Connection_DataReceived;

                // Setup an event handler for when the connection state changes.
                statsConnection.ConnectionStatusChanged += Connection_ConnectionStatusChanged;

                // We want the user (and the program itself) to be able to choose to exit.
                Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs eventArgs)
                {
                    // Mark as handled by us, as we will want to clean up.
                    eventArgs.Cancel = true;

                    // Signal to the program that the user wishes to quit.
                    WantToQuit.Set();
                };

                // Wait for something (user requested to quit, program finished..) to signal we should resume.
                WantToQuit.WaitOne();
            }
        }
Ejemplo n.º 3
0
 private void UploadUsageStatsAsync()
 {
     try
     {
         Thread.Sleep(3000);
         var asc = new StatsConnection(Settings, Repository);
         asc.UploadStatistics(App.APP_VERSION);
     }
     catch (Exception e)
     {
         App.Logger.Error("Main", "Fatal error in UploadUsageStatsAsync", e);
     }
 }
Ejemplo n.º 4
0
        /// <summary>Method which dynamically adds some series of data to the chart and connects to the EdgeOS device once the form is ready.</summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">THe <see cref="EventArgs"/> instance containing the event data.</param>
        private void FormBandwidthChart_Load(object sender, EventArgs e)
        {
            // Check the credentials are provided in the application's configuration file.
            if (ConfigurationManager.AppSettings["Username"] == null || ConfigurationManager.AppSettings["Password"] == null || ConfigurationManager.AppSettings["Host"] == null)
            {
                MessageBox.Show("Program cannot start, some credentials were missing in the program's configuration file.", "Missing Credentials", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            // We want to control the ordering of some of the series so we can control the colours.
            for (int count = 0; count < NumberOfEthInterfaces; count++)
            {
                bandwidthChart.Series.Add(new Series("eth" + count + "Rx")
                {
                    ChartArea = "ChartAreaRx", ChartType = SeriesChartType.StackedColumn, Color = paletteColors[count]
                });
                bandwidthChart.Series.Add(new Series("eth" + count + "Tx")
                {
                    ChartArea = "ChartAreaTx", ChartType = SeriesChartType.StackedColumn, Color = paletteColors[count]
                });
            }

            // This method will be invoked each time the timer has elapsed.
            sessionHeartbeatTimer.Elapsed += (s, a) => webClient.Heartbeat();

            // The WebClient allows us to get a valid SessionID to then use with the StatsConnection.
            webClient = new WebClient("https://" + ConfigurationManager.AppSettings["Host"] + "/");

            // Ignore TLS certificate errors if there is a ".crt" file present that matches this host.
            webClient.AllowLocalCertificates();

            // Login to the Router.
            webClient.Login(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"]);

            // Share a valid SessionID with a new StatsConnection object.
            statsConnection = new StatsConnection(webClient.SessionID);

            // Ignore TLS certificate errors if there is a ".crt" file present that matches this host.
            statsConnection.AllowLocalCertificates();

            // Connect to the router.
            statsConnection.ConnectAsync(new Uri("wss://" + ConfigurationManager.AppSettings["Host"] + "/ws/stats"));

            // Setup an event handler for when data is received.
            statsConnection.DataReceived += Connection_DataReceived;

            // Setup an event handler for when the connection state changes.
            statsConnection.ConnectionStatusChanged += Connection_ConnectionStatusChanged;
        }
Ejemplo n.º 5
0
        /// <summary>Method which when a StatsConnection is established requests Interface statistics.</summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="connectionStatus">The <see cref="StatsConnection"/>'s new <see cref="StatsConnection.ConnectionStatus"/>.</param>
        private static void Connection_ConnectionStatusChanged(object sender, StatsConnection.ConnectionStatus connectionStatus)
        {
            // The sender should be a StatsConnection so that we can interact with that StatsConnection instance.
            StatsConnection statsConnection = sender as StatsConnection;

            if (statsConnection == null)
            {
                return;
            }

            // Specifically what did the ConnectionStatus change to?
            switch (connectionStatus)
            {
            // It was previously not connected and now it is.
            case StatsConnection.ConnectionStatus.Connected:

                // Compose a subscription request message.
                SubscriptionRequest subscriptionRequest = new SubscriptionRequest
                {
                    Subscribe = new Subscription[] { new Subscription()
                                                     {
                                                         name = SubscriptionMessageType.LogFeed
                                                     } },
                    SessionID = statsConnection.SessionID
                };

                // Ask for events to be delivered.
                statsConnection.SubscribeForEvents(subscriptionRequest);

                // Start the heartbeat timer.
                sessionHeartbeatTimer.Enabled = true;

                break;

            // The router has disconnected (usually due to session expiry due to lack of heartbeats).
            case StatsConnection.ConnectionStatus.DisconnectedByHost:

                // Stop the heartbeat timer.
                sessionHeartbeatTimer.Enabled = false;

                // Just close this program.
                WantToQuit.Set();

                break;
            }
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Connections()
        {
            StatsConnection ret = await statsHandler.StatsConnection();

            return(Ok(ret));
        }