/// <summary>
        /// Uses DNS-SD to find all Factory Orchestrator services on your local network.
        /// </summary>
        /// <param name="secondsToWait">Number of seconds to wait for services to respond</param>
        /// <param name="serverIdentity">The service certificate identity to use</param>
        /// <param name="certhash">The service certificate hash to use</param>
        /// <returns>List of FactoryOrchestratorClient representing all discovered clients</returns>
        public static List <FactoryOrchestratorClient> DiscoverFactoryOrchestratorDevices(int secondsToWait = 5, string serverIdentity = "FactoryServer", string certhash = "E8BF0011168803E6F4AF15C9AFE8C9C12F368C8F")
        {
            List <FactoryOrchestratorClient> clients = new List <FactoryOrchestratorClient>();

            using (var sd = new ServiceDiscovery())
            {
                sd.ServiceInstanceDiscovered += ((s, e) =>
                {
                    foreach (var srv in e.Message.AdditionalRecords.Union(e.Message.Answers).OfType <SRVRecord>().Where(x => x.CanonicalName.EndsWith("_factorch._tcp.local", StringComparison.InvariantCultureIgnoreCase)).Distinct())
                    {
                        var port = srv.Port;
                        foreach (var ip in e.Message.AdditionalRecords.Union(e.Message.Answers).OfType <ARecord>())
                        {
                            var client = new FactoryOrchestratorClient(ip.Address, port, serverIdentity, certhash);
                            client.HostName = ip.CanonicalName.Replace(".factorch.local", "");
                            var osVer = e.Message.AdditionalRecords.Union(e.Message.Answers).OfType <TXTRecord>().SelectMany(x => x.Strings).Where(x => x.StartsWith("OSVersion=", StringComparison.InvariantCultureIgnoreCase)).DefaultIfEmpty(string.Empty).FirstOrDefault().Replace("OSVersion=", "");
                            client.OSVersion = osVer;
                            clients.Add(client);
                        }
                    }
                });

                sd.QueryServiceInstances("_factorch._tcp");

                Thread.Sleep(secondsToWait * 1000);
            }
            return(clients);
        }
Example #2
0
        /// <summary>
        /// Creates a PowerShell object.
        /// </summary>
        protected override void ProcessRecord()
        {
            var asyncClients = FactoryOrchestratorClient.DiscoverFactoryOrchestratorDevices(SecondsToWait);

            FactoryOrchestratorClientSync[] syncClients = new FactoryOrchestratorClientSync[asyncClients.Count];
            int i = 0;

            foreach (var ac in asyncClients)
            {
                syncClients[i++] = new FactoryOrchestratorClientSync(ac);
            }

            this.WriteObject(syncClients);
        }
Example #3
0
        /// <summary>
        /// Starts polling the object.
        /// </summary>
        /// <param name="client">The FactoryOrchestratorClient object to use for polling.</param>
        public void StartPolling(FactoryOrchestratorClient client)
        {
            _client = client ?? throw new ArgumentNullException(nameof(client));

            if (!_client.IsConnected)
            {
                throw new FactoryOrchestratorConnectionException(Resources.ClientNotConnected);
            }

            if (_stopped != false)
            {
                _stopped         = false;
                LatestObject     = null;
                _lastEventObject = null;
                _timer           = new Timer(GetUpdatedObjectAsync, null, 0, _pollingInterval);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FactoryOrchestratorVersionMismatchException"/> class.
 /// </summary>
 /// <param name="ip">The ip address of the Service.</param>
 /// <param name="serviceVersion">The service version.</param>
 public FactoryOrchestratorVersionMismatchException(IPAddress ip, string serviceVersion) : base(string.Format(CultureInfo.CurrentCulture, Resources.FactoryOrchestratorVersionMismatchException, ip?.ToString() ?? "", serviceVersion, FactoryOrchestratorClient.GetClientVersionString()))
 {
     ServiceVersion = serviceVersion;
     ClientVersion  = FactoryOrchestratorClient.GetClientVersionString();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FactoryOrchestratorVersionMismatchException"/> class.
 /// </summary>
 /// <param name="message">The error message that explains the reason for the exception.</param>
 /// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
 public FactoryOrchestratorVersionMismatchException(string message, Exception innerException) : base(message, innerException)
 {
     ClientVersion  = FactoryOrchestratorClient.GetClientVersionString();
     ServiceVersion = Resources.Unknown;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FactoryOrchestratorVersionMismatchException"/> class.
 /// </summary>
 public FactoryOrchestratorVersionMismatchException() : base()
 {
     ClientVersion  = FactoryOrchestratorClient.GetClientVersionString();
     ServiceVersion = Resources.Unknown;
 }
        /// <summary>
        /// Establishes a connection to the Factory Orchestrator Service.
        /// Throws an exception if it cannot connect.
        /// </summary>
        /// <param name="ignoreVersionMismatch">If true, ignore a Client-Service version mismatch.</param>
        public async Task Connect(bool ignoreVersionMismatch = false)
        {
            ServiceProvider serviceProvider = new ServiceCollection()
                                              .AddTcpIpcClient <IFactoryOrchestratorService>("Client", (_, options) =>
            {
                options.ConnectionTimeout     = 10000;
                options.ServerIp              = IpAddress;
                options.ServerPort            = Port;
                options.SslServerIdentity     = ServerIdentity;
                options.EnableSsl             = true;
                options.SslValidationCallback = ServerCertificateValidationCallback;
            })
                                              .BuildServiceProvider();

            // resolve IPC client factory
            var clientFactory = serviceProvider
                                .GetRequiredService <IIpcClientFactory <IFactoryOrchestratorService> >();

            // create client
            _IpcClient = clientFactory.CreateClient("Client");

            string serviceVersion;

            // Test a command to make sure connection works
            try
            {
                serviceVersion = await _IpcClient.InvokeAsync <string>(CreateIpcRequest("GetServiceVersionString"));
            }
            catch (Exception ex)
            {
                throw CreateIpcException(ex);
            }

            if (!ignoreVersionMismatch)
            {
                // Verify client and service are compatible
                var clientVersion = GetClientVersionString();
                var clientMajor   = clientVersion.Split('.').First();
                var serviceMajor  = serviceVersion.Split('.').First();

                if (clientMajor != serviceMajor)
                {
                    throw new FactoryOrchestratorVersionMismatchException(IpAddress, serviceVersion);
                }
            }

            try
            {
                HostName = await _IpcClient.InvokeAsync <string>(CreateIpcRequest("GetDnsHostName"));

                OSVersion = await _IpcClient.InvokeAsync <string>(CreateIpcRequest("GetOSVersionString"));

                await _IpcClient.InvokeAsync(CreateIpcRequest("Connect", new object[] { Dns.GetHostName(), $"OS: {Environment.OSVersion.VersionString}. Client version: {FactoryOrchestratorClient.GetClientVersionString()}." }));
            }
            catch (Exception)
            {
                HostName  = Resources.Unknown;
                OSVersion = Resources.Unknown;
            }

            IsConnected = true;
            OnConnected?.Invoke();
        }
Example #8
0
 internal FactoryOrchestratorClientSync(FactoryOrchestratorClient asyncClient)
 {
     OnConnected = null;
     AsyncClient = asyncClient;
 }
Example #9
0
 /// <summary>
 /// Creates a new FactoryOrchestratorSyncClient instance. Most .NET clients are recommended to use FactoryOrchestratorClient instead which is fully asynchronous.
 /// </summary>
 /// <param name="host">IP address of the device running Factory Orchestrator Service. Use IPAddress.Loopback for local device.</param>
 /// <param name="certificateValidationCallback">A System.Net.Security.RemoteCertificateValidationCallback delegate responsible for validating the server certificate.</param>
 /// <param name="port">Port to use. Factory Orchestrator Service defaults to 45684.</param>
 /// <param name="serverIdentity">Distinguished name for the server defaults to FactoryServer.</param>
 public FactoryOrchestratorClientSync(IPAddress host, RemoteCertificateValidationCallback certificateValidationCallback, int port = 45684, string serverIdentity = "FactoryServer")
 {
     OnConnected = null;
     AsyncClient = new FactoryOrchestratorClient(host, certificateValidationCallback, port, serverIdentity);
 }
Example #10
0
 /// <summary>
 /// Creates a new FactoryOrchestratorSyncClient instance. Most .NET clients are recommended to use FactoryOrchestratorClient instead which is fully asynchronous.
 /// </summary>
 /// <param name="host">IP address of the device running Factory Orchestrator Service. Use IPAddress.Loopback for local device.</param>
 /// <param name="port">Port to use. Factory Orchestrator Service defaults to 45684.</param>
 /// <param name="serverIdentity">Distinguished name for the server defaults to FactoryServer.</param>
 /// <param name="certhash">Hash value for the server certificate defaults to E8BF0011168803E6F4AF15C9AFE8C9C12F368C8F.</param>
 public FactoryOrchestratorClientSync(IPAddress host, int port = 45684, string serverIdentity = "FactoryServer", string certhash = "E8BF0011168803E6F4AF15C9AFE8C9C12F368C8F")
 {
     OnConnected = null;
     AsyncClient = new FactoryOrchestratorClient(host, port, serverIdentity, certhash);
 }
Example #11
0
 /// <summary>
 /// Gets the build number of FactoryOrchestratorClient.
 /// </summary>
 /// <returns></returns>
 public static string GetClientVersionString()
 {
     return(FactoryOrchestratorClient.GetClientVersionString());
 }