Commit ac588cf1 authored by David Kline's avatar David Kline Committed by GitHub
Browse files

Merge pull request #26 from Microsoft/HymanMatt

Remove ResourceManager class and add system perf web socket
parents 3973af37 b6515b28
Loading
Loading
Loading
Loading
+21 −52
Original line number Diff line number Diff line
@@ -15,80 +15,49 @@ namespace TestApp
    public class ParameterHelper
    {
        /// <summary>
        /// List for storing parsed command line parameters as key value pairs.
        /// Help Flag identifier string
        /// </summary>
        private Dictionary<string, string> parameters = new Dictionary<string, string>();
        public static readonly string HelpFlag = "?";

        /// <summary>
        /// List for storing parsed command line flags.
        /// Verbose Flag identifier string
        /// </summary>
        private List<string> flags = new List<string>();
        public static readonly string VerboseFlag = "v";

        /// <summary>
        /// Gets Help Flag identifier string
        /// Operation type identifier string
        /// </summary>
        public static string HelpFlag
        {
            get
            {
                return "?";
            }
        }
        public static readonly string Operation = "op";

        /// <summary>
        /// Gets Verbose Flag identifier string
        /// Device Identifier identifier string
        /// </summary>
        public static string VerboseFlag
        {
            get
            {
                return "v";
            }
        }
        public static readonly string IpOrHostname = "ip";

        /// <summary>
        /// Gets Operation type identifier string
        /// WDP Username identifier string
        /// </summary>
        public static string Operation
        {
            get
            {
                return "op";
            }
        }
        public static readonly string WdpUser = "user";

        /// <summary>
        /// Gets Device Identifier identifier string
        /// WDP Password identifier string
        /// </summary>
        public static string IpOrHostname
        {
            get
            {
                return "ip";
            }
        }
        public static readonly string WdpPassword = "pwd";

        /// <summary>
        /// Gets WDP Username identifier string
        /// Listen (use web socket) identifier string
        /// </summary>
        public static string WdpUser
        {
            get
            {
                return "user";
            }
        }
        public static readonly string Listen = "listen";

        /// <summary>
        /// Gets WDP Password identifier string
        /// List for storing parsed command line parameters as key value pairs.
        /// </summary>
        public static string WdpPassword
        {
            get
            {
                return "pwd";
            }
        }
        private Dictionary<string, string> parameters = new Dictionary<string, string>();

        /// <summary>
        /// List for storing parsed command line flags.
        /// </summary>
        private List<string> flags = new List<string>();

        /// <summary>
        /// Helper for getting a parameter value for a key
+112 −16
Original line number Diff line number Diff line
@@ -25,12 +25,22 @@ namespace TestApp
        /// <summary>
        /// Event used to indicate that the running processes on the device have been received.
        /// </summary>
        private ManualResetEvent processesReceived = new ManualResetEvent(false);
        private ManualResetEvent runningProcessesReceived = new ManualResetEvent(false);

        /// <summary>
        /// The security key to use when connecting to the network access point.
        /// The running processes on the device.
        /// </summary>
        private DevicePortal.DeviceProcesses deviceProcesses = null;
        private DevicePortal.RunningProcesses runningProcesses = null;

        /// <summary>
        /// Event used to indicate that the system perf on the device have been received.
        /// </summary>
        private ManualResetEvent systemPerfReceived = new ManualResetEvent(false);

        /// <summary>
        /// The system perf of the device.
        /// </summary>
        private DevicePortal.SystemPerformanceInformation systemPerf = null;

        /// <summary>
        /// Operation types
@@ -61,6 +71,11 @@ namespace TestApp
            /// List processes operation
            /// </summary>
            ListProcessesOperation,

            /// <summary>
            /// Get the system performance operation
            /// </summary>
            GetSystemPerfOperation,
        }

        /// <summary>
@@ -109,6 +124,16 @@ namespace TestApp
                return;
            }

            bool listen = false;
            if (parameters.HasParameter(ParameterHelper.Listen))
            {
                bool parsedValue = false;
                if (bool.TryParse(parameters.GetParameterValue(ParameterHelper.Listen), out parsedValue))
                {
                    listen = parsedValue;
                }
            }

            DevicePortal portal = new DevicePortal(new DevicePortalConnection(parameters.GetParameterValue(ParameterHelper.IpOrHostname), parameters.GetParameterValue(ParameterHelper.WdpUser), parameters.GetParameterValue(ParameterHelper.WdpPassword)));

            Task connectTask = portal.Connect(null, null, false);
@@ -150,24 +175,71 @@ namespace TestApp
            }
            else if (operation == OperationType.ListProcessesOperation)
            {
                portal.ProcessesMessageReceived += app.ProcessesReceivedHandler;
                DevicePortal.RunningProcesses deviceProcesses = null;
                if (listen)
                {
                    portal.RunningProcessesMessageReceived += app.ProcessesReceivedHandler;

                Task startListeningForProcessesTask = portal.StartListeningForProcesses();
                    Task startListeningForProcessesTask = portal.StartListeningForRunningProcesses();
                    startListeningForProcessesTask.Wait();

                app.processesReceived.WaitOne();
                    app.runningProcessesReceived.WaitOne();

                Task stopListeningForProcessesTask = portal.StopListeningForProcesses();
                    Task stopListeningForProcessesTask = portal.StopListeningForRunningProcesses();
                    stopListeningForProcessesTask.Wait();

                foreach (DevicePortal.ProcessInfo process in app.deviceProcesses.Processes)
                    deviceProcesses = app.runningProcesses;
                }
                else
                {
                    if (!string.IsNullOrEmpty(process.ImageName))
                    Task<DevicePortal.RunningProcesses> getRunningProcessesTask = portal.GetRunningProcesses();
                    deviceProcesses = getRunningProcessesTask.Result;
                }

                foreach (DevicePortal.DeviceProcessInfo process in deviceProcesses.Processes)
                {
                    if (!string.IsNullOrEmpty(process.Name))
                    {
                        Console.WriteLine(process.ImageName);
                        Console.WriteLine(process.Name);
                    }
                }
            }
            else if (operation == OperationType.GetSystemPerfOperation)
            {
                DevicePortal.SystemPerformanceInformation systemPerformanceInformation = null;
                if (listen)
                {
                    portal.SystemPerfMessageReceived += app.SystemPerfReceivedHandler;

                    Task startListeningForSystemPerfTask = portal.StartListeningForSystemPerf();
                    startListeningForSystemPerfTask.Wait();

                    app.systemPerfReceived.WaitOne();

                    Task stopListeningForSystemPerfTask = portal.StopListeningForRunningProcesses();
                    stopListeningForSystemPerfTask.Wait();

                    systemPerformanceInformation = app.systemPerf;
                }
                else
                {
                    Task<DevicePortal.SystemPerformanceInformation> getRunningProcessesTask = portal.GetSystemPerf();
                    systemPerformanceInformation = getRunningProcessesTask.Result;
                }

                Console.WriteLine("Available Pages: " + systemPerformanceInformation.AvailablePages);
                Console.WriteLine("Commit Limit: " + systemPerformanceInformation.CommitLimit);
                Console.WriteLine("Commited Pages: " + systemPerformanceInformation.CommittedPages);
                Console.WriteLine("CPU Load: " + systemPerformanceInformation.CpuLoad);
                Console.WriteLine("IoOther Speed: " + systemPerformanceInformation.IoOtherSpeed);
                Console.WriteLine("IoRead Speed: " + systemPerformanceInformation.IoReadSpeed);
                Console.WriteLine("IoWrite Speed: " + systemPerformanceInformation.IoWriteSpeed);
                Console.WriteLine("Non-paged Pool Pages: " + systemPerformanceInformation.NonPagedPoolPages);
                Console.WriteLine("Paged Pool Pages: " + systemPerformanceInformation.PagedPoolPages);
                Console.WriteLine("Page Size: " + systemPerformanceInformation.PageSize);
                Console.WriteLine("Total Installed Kb: " + systemPerformanceInformation.TotalInstalledKb);
                Console.WriteLine("Total Pages: " + systemPerformanceInformation.TotalPages);
            }
        }

        /// <summary>
@@ -197,10 +269,18 @@ namespace TestApp
            {
                return OperationType.ListProcessesOperation;
            }
            else if (operation.Equals("systemPerf", StringComparison.InvariantCultureIgnoreCase))
            {
                return OperationType.GetSystemPerfOperation;
            }

            throw new Exception("Unknown Operation Type. Supported operations are the following:\n" +
                "info\n" +
                "xbluser\n");
                "xbluser\n" +
                "install\n" +
                "reboot\n" +
                "processes\n" +
                "systemPerf\n");
        }

        /// <summary>
@@ -210,12 +290,28 @@ namespace TestApp
        /// <param name="args">The event data.</param>
        private void ProcessesReceivedHandler(
            object sender,
            WebSocketMessageReceivedEventArgs<DevicePortal.DeviceProcesses> args)
            WebSocketMessageReceivedEventArgs<DevicePortal.RunningProcesses> args)
        {
            if (args.Message != null)
            {
                this.runningProcesses = args.Message;
                this.runningProcessesReceived.Set();
            }
        }

        /// <summary>
        /// Handler for the SystemPerfMessageReceived event.
        /// </summary>
        /// <param name="sender">The object sending the event.</param>
        /// <param name="args">The event data.</param>
        private void SystemPerfReceivedHandler(
            object sender,
            WebSocketMessageReceivedEventArgs<DevicePortal.SystemPerformanceInformation> args)
        {
            if (args.Message != null)
            {
                this.deviceProcesses = args.Message;
                this.processesReceived.Set();
                this.systemPerf = args.Message;
                this.systemPerfReceived.Set();
            }
        }
    }
+230 −2
Original line number Diff line number Diff line
@@ -19,12 +19,43 @@ namespace Microsoft.Tools.WindowsDevicePortal
        /// <summary>
        /// API for getting all running processes
        /// </summary>
        public static readonly string RunningProcessApi = "api/resourcemanager/processes";
        private static readonly string RunningProcessApi = "api/resourcemanager/processes";

        /// <summary>
        /// API for getting system performance
        /// </summary>
        public static readonly string SystemPerfApi = "api/resourcemanager/systemperf";
        private static readonly string SystemPerfApi = "api/resourcemanager/systemperf";

#if !WINDOWS_UWP
        /// <summary>
        /// Web socket to get running processes.
        /// </summary>
        private WebSocket<RunningProcesses> deviceProcessesWebSocket;

        /// <summary>
        /// Web socket to get the system perf of the device.
        /// </summary>
        private WebSocket<SystemPerformanceInformation> systemPerfWebSocket;

        /// <summary>
        /// Gets or sets the running processes message received handler.
        /// </summary>
        public WebSocketMessageReceivedEventHandler<RunningProcesses> RunningProcessesMessageReceived
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the system perf message received handler.
        /// </summary>
        public WebSocketMessageReceivedEventHandler<SystemPerformanceInformation> SystemPerfMessageReceived
        {
            get;
            set;
        }

#endif // !WINDOWS_UWP

        /// <summary>
        /// Gets the collection of processes running on the device.
@@ -35,6 +66,48 @@ namespace Microsoft.Tools.WindowsDevicePortal
            return await this.Get<RunningProcesses>(RunningProcessApi);
        }

#if !WINDOWS_UWP
        /// <summary>
        /// Starts listening for the running processes on the device with them being returned via the RunningProcessesMessageReceived event handler.
        /// </summary>
        /// <returns>Task for connecting to the websocket but not for listening to it.</returns>
        public async Task StartListeningForRunningProcesses()
        {
            if (this.deviceProcessesWebSocket == null)
            {
                this.deviceProcessesWebSocket = new WebSocket<RunningProcesses>(this.deviceConnection, this.ServerCertificateValidation);

                this.deviceProcessesWebSocket.WebSocketMessageReceived += this.RunningProcessesReceivedHandler;
            }
            else
            {
                if (this.deviceProcessesWebSocket.IsListeningForMessages)
                {
                    return;
                }
            }

            await this.deviceProcessesWebSocket.OpenConnection(RunningProcessApi);

            // Do not await on the actual listening.
            Task listenTask = this.deviceProcessesWebSocket.StartListeningForMessages();
        }

        /// <summary>
        /// Stop listening for the running processes on the device.
        /// </summary>
        /// <returns>Task for stop listening for processes and disconnecting from the websocket .</returns>
        public async Task StopListeningForRunningProcesses()
        {
            if (this.deviceProcessesWebSocket == null || !this.deviceProcessesWebSocket.IsListeningForMessages)
            {
                return;
            }

            await this.deviceProcessesWebSocket.CloseConnection();
        }
#endif // !WINDOWS_UWP

        /// <summary>
        /// Gets system performance information for the device.
        /// </summary>
@@ -44,8 +117,115 @@ namespace Microsoft.Tools.WindowsDevicePortal
            return await this.Get<SystemPerformanceInformation>(SystemPerfApi);
        }

#if !WINDOWS_UWP
        /// <summary>
        /// Starts listening for the system performance information for the device with it being returned via the SystemPerfMessageReceived event handler.
        /// </summary>
        /// <returns>Task for connecting to the websocket but not for listening to it.</returns>
        public async Task StartListeningForSystemPerf()
        {
            if (this.systemPerfWebSocket == null)
            {
                this.systemPerfWebSocket = new WebSocket<SystemPerformanceInformation>(this.deviceConnection, this.ServerCertificateValidation);

                this.systemPerfWebSocket.WebSocketMessageReceived += this.SystemPerfMessageReceived;
            }
            else
            {
                if (this.systemPerfWebSocket.IsListeningForMessages)
                {
                    return;
                }
            }

            await this.systemPerfWebSocket.OpenConnection(SystemPerfApi);

            // Do not await on the actual listening.
            Task listenTask = this.systemPerfWebSocket.StartListeningForMessages();
        }

        /// <summary>
        /// Stop listening for the system performance information for the device.
        /// </summary>
        /// <returns>Task for stop listening for system perf and disconnecting from the websocket .</returns>
        public async Task StopListeningForSystemPerf()
        {
            if (this.systemPerfWebSocket == null || !this.systemPerfWebSocket.IsListeningForMessages)
            {
                return;
            }

            await this.systemPerfWebSocket.CloseConnection();
        }

        /// <summary>
        /// Handler for the processes received event that passes the event to the RunningProcessesMessageReceived handler.
        /// </summary>
        /// <param name="sender">The object sending the event.</param>
        /// <param name="args">The event data.</param>
        private void RunningProcessesReceivedHandler(
            object sender,
            WebSocketMessageReceivedEventArgs<RunningProcesses> args)
        {
            if (args.Message != null)
            {
                this.RunningProcessesMessageReceived?.Invoke(
                            this,
                            args);
            }
        }

        /// <summary>
        /// Handler for the system performance information received event that passes the event to the SystemPerfMessageReceived handler.
        /// </summary>
        /// <param name="sender">The object sending the event.</param>
        /// <param name="args">The event data.</param>
        private void SystemPerfReceivedHandler(
            object sender,
            WebSocketMessageReceivedEventArgs<SystemPerformanceInformation> args)
        {
            if (args.Message != null)
            {
                this.SystemPerfMessageReceived?.Invoke(
                            this,
                            args);
            }
        }

#endif // !WINDOWS_UWP
        #region Device contract

        /// <summary>
        /// Object representing the process version.
        /// </summary>
        [DataContract]
        public class ProcessVersion
        {
            /// <summary>
            /// Gets or sets the major version number
            /// </summary>
            [DataMember(Name = "Major")]
            public uint Major { get; set; }

            /// <summary>
            /// Gets or sets the minor version number
            /// </summary>
            [DataMember(Name = "Minor")]
            public uint Minor { get; set; }

            /// <summary>
            /// Gets or sets the build number
            /// </summary>
            [DataMember(Name = "Build")]
            public uint Build { get; set; }

            /// <summary>
            /// Gets or sets the revision number
            /// </summary>
            [DataMember(Name = "Revision")]
            public uint Revision { get; set; }
        }

        /// <summary>
        /// Process Info
        /// </summary>
@@ -100,6 +280,54 @@ namespace Microsoft.Tools.WindowsDevicePortal
            [DataMember(Name = "WorkingSetSize")]
            public uint WorkingSet { get; set; }

            /// <summary>
            /// Gets or sets package working set
            /// </summary>
            [DataMember(Name = "PrivateWorkingSet")]
            public double PrivateWorkingSet { get; set; }

            /// <summary>
            /// Gets or sets session id
            /// </summary>
            [DataMember(Name = "SessionId")]
            public uint SessionId { get; set; }

            /// <summary>
            /// Gets or sets total commit
            /// </summary>
            [DataMember(Name = "TotalCommit")]
            public double TotalCommit { get; set; }

            /// <summary>
            /// Gets or sets virtual size
            /// </summary>
            [DataMember(Name = "VirtualSize")]
            public double VirtualSize { get; set; }

            /// <summary>
            /// Gets or sets a value indicating whether or not the process is running
            /// </summary>
            [DataMember(Name = "IsRunning")]
            public bool IsRunning { get; set; }

            /// <summary>
            /// Gets or sets publisher
            /// </summary>
            [DataMember(Name = "Publisher")]
            public string Publisher { get; set; }

            /// <summary>
            /// Gets or sets version
            /// </summary>
            [DataMember(Name = "Version")]
            public ProcessVersion Version { get; set; }

            /// <summary>
            /// Gets or sets a value indicating whether or not the package is a XAP package
            /// </summary>
            [DataMember(Name = "IsXAP")]
            public bool IsXAP { get; set; }

            /// <summary>
            /// String representation of a process
            /// </summary>
+1 −2
Original line number Diff line number Diff line
@@ -17,10 +17,9 @@
    <Compile Include="$(MSBuildThisFileDirectory)Core\Etw.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\Networking.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\OsInformation.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\PerformanceData.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\Power.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\RemoteControl.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\ResourceManager.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\PerformanceData.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\TaskManager.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\WiFiManagement.cs" />
    <Compile Include="$(MSBuildThisFileDirectory)Core\WindowsErrorReporting.cs" />