示例#1
0
        private static string GetAbbreviationForWellKnownModules(IDocumentAnalysis analysis, string fullyQualifiedName)
        {
            if (WellKnownAbbreviationMap.TryGetValue(fullyQualifiedName, out var abbreviation))
            {
                // for now, use module wide unique name for abbreviation. even though technically we could use
                // context based unique name since variable declared in lower scope will hide it and there is no conflict
                return(UniqueNameGenerator.Generate(analysis, abbreviation));
            }

            return(null);
        }
示例#2
0
        public static async Task StartAsync(Brain brain, string name, IDeviceBuilder[] devices, IPAddress ipAddress, int port, CancellationToken cancellationToken)
        {
            if (Server._host != null)
            {
                throw new InvalidOperationException("Host is already running - it must be stopped to start a new host.");
            }
            if (brain == null)
            {
                throw new ArgumentNullException(nameof(brain));
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Non-blank name is required.", nameof(name));
            }
            if (devices == null || devices.Length == 0 || Array.IndexOf(devices, default) != -1)
            {
                throw new ArgumentException("Devices collection can not be null/empty or contain null.", nameof(devices));
            }
            if (port < 0 || port > ushort.MaxValue)
            {
                throw new ArgumentException("Invalid port.", nameof(port));
            }
            string adapterName = $"src-{UniqueNameGenerator.Generate(name)}";
            IHost  host        = Server.CreateHostBuilder(brain, adapterName, devices, ipAddress, port).Build();
            await host.StartAsync(cancellationToken).ConfigureAwait(false);

            Server._host = host;
            ILogger <Brain> logger  = host.Services.GetRequiredService <ILogger <Brain> >();
            string          baseUrl = $"http://{ipAddress}:{port}";

            for (int i = 0; i < Constants.MaxConnectionRetries; i++)
            {
                try
                {
                    await brain.PostAsync("registerSdkDeviceAdapter", new { Name = adapterName, BaseUrl = baseUrl }, cancellationToken).ConfigureAwait(false);

                    logger.LogInformation("SDK Adapter registered on brain @ http://{host}:{port}", brain.HostName, brain.Port);
                    return;
                }
                catch
                {
                    logger.LogWarning("Failed to register with brain {times} time(s).", i + 1);
                }
            }
            throw new ApplicationException("Failed to connect to brain.");
        }
        private static void Test(IDocumentAnalysis analysis, int position, string name, string expected)
        {
            var actual = UniqueNameGenerator.Generate(analysis, position, name);

            actual.Should().Be(expected);
        }