public bool StartupNetworkingClient(out Networking.IClient networkingClient, ref string error) { networkingClient = null; lock (this) { Thread.MemoryBarrier(); if (this.CurrentClient != null) { networkingClient = this.CurrentClient; return(true); } else { try { this.CurrentClient = new Client(this.RemoteServerAddress, this.RemoteServerPort, this); } catch { return(false); } Thread.MemoryBarrier(); networkingClient = this.CurrentClient; return(true); } } }
/// <summary> /// Prepares the component to be handled by the <see cref="ComponentController"/>. /// </summary> /// <param name="logMarshal">The <see cref="LogMarshal"/> provided by the <see cref="LogController"/>.</param> /// <param name="client">The <see cref="Networking.IClient"/> made for this component.</param> /// <param name="workingClient">A <see cref="Networking.IClient"/> made for temporary, unrestricted, communications. This client is not opened by default and should be closed when not being used.</param> /// <param name="logSourceId">The value used as the source id when creating log entries.</param> /// <param name="networkLogSourceId">The value used as the source id when creating network specific log entries.</param> /// <param name="packageConfiguration">The <see cref="Packaging.IPackageConfiguration"/> for the package this component was located in.</param> /// <param name="customConfiguration">The custom configuration, if found; otherwise, null.</param> /// <param name="installRootPath">The root path where the <see cref="Installation.IInstaller"/> installs packages.</param> /// <param name="installPath">The install directory path where this component's package has been installed.</param> /// <param name="temporaryPath">A temporary folder for the <see cref="IComponent"/> to use.</param> /// <param name="longTermStoragePath">A permanent folder for the <see cref="IComponent"/> to use.</param> /// <param name="folderAccessItems">A collection of folders.</param> public void Initialize(LogMarshal logMarshal, Networking.IClient client, Networking.IClient workingClient, string logSourceId, string networkLogSourceId, Packaging.IPackageConfiguration packageConfiguration, Configuration.IConfigurationGroup customConfiguration, string installRootPath, string installPath, string temporaryPath, string longTermStoragePath, IFolderAccessItems folderAccessItems) { _LogMarshal = logMarshal ?? throw new ArgumentNullException(nameof(logMarshal)); Client = client ?? throw new ArgumentNullException(nameof(client)); WorkingClient = workingClient ?? throw new ArgumentNullException(nameof(workingClient)); if (Client.ClientConfiguration.ReceivableTypesFilter.Count() > 0) { // add required message types to receivable message types filter var list = new List <Networking.IPayloadTypeInfo>(Client.ClientConfiguration.ReceivableTypesFilter); list.Add(new Networking.PayloadTypeInfo(typeof(SupportsComponentDesignationRequest))); Client.ClientConfiguration.ReplaceReceivableTypesFilter(list); } // if (string.IsNullOrWhiteSpace(logSourceId)) { throw new ArgumentNullException(nameof(logSourceId)); } LogSourceId = logSourceId; // if (string.IsNullOrWhiteSpace(networkLogSourceId)) { throw new ArgumentNullException(nameof(networkLogSourceId)); } _NetworkLogSourceId = networkLogSourceId; // PackageConfiguration = packageConfiguration ?? throw new ArgumentNullException(nameof(packageConfiguration)); CustomConfiguration = customConfiguration; // if (string.IsNullOrWhiteSpace(installRootPath)) { throw new ArgumentNullException(nameof(installRootPath)); } InstallRootPath = installRootPath; // if (string.IsNullOrWhiteSpace(installPath)) { throw new ArgumentNullException(nameof(installPath)); } InstallPath = installPath; // if (string.IsNullOrWhiteSpace(temporaryPath)) { throw new ArgumentNullException(nameof(temporaryPath)); } TemporaryPath = temporaryPath; // if (string.IsNullOrWhiteSpace(longTermStoragePath)) { throw new ArgumentNullException(nameof(longTermStoragePath)); } LongTermStoragePath = longTermStoragePath; // FolderAccessItems = folderAccessItems ?? throw new ArgumentNullException(nameof(folderAccessItems)); }
/// <summary> /// Will broadcast a <see cref="SupportsComponentDesignationRequest"/> message and wait up to <paramref name="timeout"/> for <see cref="SupportsComponentDesignationResponse"/> messages. /// </summary> /// <param name="client">The <see cref="Networking.IClient"/> used to communicate with the network.</param> /// <param name="componentDesignationSearch">The <see cref="IComponent.ComponentDesignation"/> to search for.</param> /// <param name="timeout">The total amount of time to wait for <see cref="SupportsComponentDesignationResponse"/> messages.</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the search.</param> /// <param name="wasCancelled">Indicates whether the search was canceled. <b>True</b> indicates that the search was canceled; otherwise, <b>false</b> indicates that the search timed out.</param> /// <param name="resultsDescription">Describes the results of the search.</param> /// <returns>Returns a list of <see cref="SupportsComponentDesignationResponse"/>s for all of the components that responded.</returns> public static List <SupportsComponentDesignationResponse> DiscoverComponents(Networking.IClient client, string componentDesignationSearch, TimeSpan timeout, CancellationToken cancellationToken, out bool wasCancelled, out string resultsDescription) { // ######## check parameters if ((client == null) || (client.State != Networking.ChannelStates.Open)) { throw new ArgumentException($"Communication client is either null or not open.", nameof(client)); } if (string.IsNullOrWhiteSpace(componentDesignationSearch)) { throw new ArgumentNullException(nameof(componentDesignationSearch)); } if ((timeout == null) || (timeout < TimeSpan.Zero)) { throw new ArgumentNullException(nameof(timeout), $"The {nameof(timeout)} parameter cannot be null or less than {TimeSpan.Zero}; value={timeout}"); } if (cancellationToken == null) { throw new ArgumentNullException(nameof(cancellationToken)); } // DateTimeOffset startTimeUtc = DateTimeOffset.UtcNow; var processId = Guid.NewGuid().ToString("N"); List <SupportsComponentDesignationResponse> results = new List <SupportsComponentDesignationResponse>(); // try { // ######## connect to the message bus, temporarily client.MessageBus += LocalMessageBus; // ######## send a SupportsComponentDesignationRequest message to all clients client.SendMessage("", new SupportsComponentDesignationRequest(componentDesignationSearch, processId)); // ######## wait for either the timeout or cancellation request while (true) { // ######## check for cancellation if (cancellationToken.IsCancellationRequested) { wasCancelled = true; resultsDescription = $"Canceled by Client, Elapsed Time={DateTimeOffset.UtcNow - startTimeUtc}"; return(results); } // ######## check for timeout if ((DateTimeOffset.UtcNow - startTimeUtc) > timeout) { wasCancelled = false; resultsDescription = $"Operation Timed Out, Elapsed Time={DateTimeOffset.UtcNow - startTimeUtc}"; return(results); } // ######## wait a bit... (will keep this method from hammering the CPU). Threading.ThreadingHelper.Sleep(50); } } finally { // #### disconnect from the message bus client.MessageBus -= LocalMessageBus; } // ######################## // internal methods // ######################## void LocalMessageBus(object sender, Networking.MessageEventArgs e) { var response = e.Message.PayloadMessage <SupportsComponentDesignationResponse>(); if ((response != null) && (response.ComponentDesignation.Equals(componentDesignationSearch, StringComparison.InvariantCultureIgnoreCase)) && (response.ProcessId.Equals(processId, StringComparison.InvariantCultureIgnoreCase))) { response.ClientId = e.Message.ClientId; results.Add(response); } } }