public UUID Register(string appName, string host)
        {
            if (string.IsNullOrWhiteSpace(appName))
            {
                throw new RpcException(new Grpc.Core.Status(StatusCode.InvalidArgument, $"Application name { appName } cannot be null or empty"));
            }

            if (string.IsNullOrWhiteSpace(host))
            {
                throw new RpcException(new Grpc.Core.Status(StatusCode.InvalidArgument, $"Host { host } cannot be null or empty"));
            }

            if (!Uri.TryCreate(host, UriKind.Absolute, out var _))
            {
                throw new RpcException(new Grpc.Core.Status(StatusCode.InvalidArgument, $"Host { host } is not a valid host"));
            }

            var existingRegistration = _registrations.Values.FirstOrDefault(x => x.AppName.Equals(appName, StringComparison.OrdinalIgnoreCase) && x.Host.Equals(host, StringComparison.OrdinalIgnoreCase));

            if (existingRegistration != null)
            {
                return(existingRegistration.UniqueIdentifier);
                //throw new RpcException(new Grpc.Core.Status(StatusCode.AlreadyExists, $"Application name { appName } with host { host } already registered"));
            }

            var uniqueIdGuid = Guid.NewGuid();
            var uniqueId     = new UUID()
            {
                Value = uniqueIdGuid.ToString()
            };

            _registrations.AddOrUpdate(uniqueIdGuid, (key) => new Registration(uniqueId, appName, host), (key, value) => new Registration(uniqueId, appName, host));

            var registration = new RegistrationCreatedEventArgs()
            {
                Registration = _registrations[uniqueIdGuid]
            };

            RegistrationCreated?.Invoke(this, registration);

            _logger.LogInformation($"Registered application with appName {appName} and host {host}");

            return(uniqueId);
        }
Esempio n. 2
0
        private void RegistrationCreated(object sender, RegistrationCreatedEventArgs e)
        {
            if (_hostDistributor.TryGetValue(e.Registration.AppName, out var hostDistributor) && hostDistributor.HasHost(e.Registration.Host))
            {
                throw new RpcException(new Grpc.Core.Status(StatusCode.AlreadyExists, $"Application name {e.Registration.AppName} with host {e.Registration.Host} already exists"));
            }

            _hostDistributor.AddOrUpdate(e.Registration.AppName,
                                         (x) =>
            {
                hostDistributor = new HostDistributor();
                hostDistributor.AddHost(e.Registration.Host);
                return(hostDistributor);
            },
                                         (x, h) =>
            {
                h.AddHost(e.Registration.Host);
                return(h);
            });

            _logger.LogInformation($"Registration received for host for application with appName {e.Registration.AppName} and host appName {e.Registration.Host}");
        }