private async Task RegisterApplicationInstance(GrpcConnection availableConnection, CancellationToken token)
        {
            if (DictionaryUtil.IsNull(RemoteDownstreamConfig.Agent.ApplicationInstanceId))
            {
                var instanceDiscoveryService =
                    new InstanceDiscoveryService.InstanceDiscoveryServiceClient(availableConnection.GrpcChannel);

                var agentUUID    = Guid.NewGuid().ToString("N");
                var registerTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

                var hostName = Dns.GetHostName();

                var osInfo = new OSInfo
                {
                    Hostname  = hostName,
                    OsName    = Environment.OSVersion.ToString(),
                    ProcessNo = Process.GetCurrentProcess().Id
                };

                // todo fix Device not configured
                //var ipv4s = Dns.GetHostAddresses(hostName);
                //foreach (var ipAddress in ipv4s.Where(x => x.AddressFamily == AddressFamily.InterNetwork))
                //   osInfo.Ipv4S.Add(ipAddress.ToString());

                var applicationInstance = new ApplicationInstance
                {
                    ApplicationId = RemoteDownstreamConfig.Agent.ApplicationId,
                    AgentUUID     = agentUUID,
                    RegisterTime  = registerTime,
                    Osinfo        = osInfo
                };

                var retry = 0;
                var applicationInstanceId = 0;
                while (retry++ < 5 && DictionaryUtil.IsNull(applicationInstanceId))
                {
                    var applicationInstanceMapping = await instanceDiscoveryService.registerInstanceAsync(applicationInstance);

                    applicationInstanceId = applicationInstanceMapping.ApplicationInstanceId;
                    if (!DictionaryUtil.IsNull(applicationInstanceId))
                    {
                        break;
                    }
                    await Task.Delay(500, token);
                }

                if (!DictionaryUtil.IsNull(applicationInstanceId))
                {
                    RemoteDownstreamConfig.Agent.ApplicationInstanceId = applicationInstanceId;
                    _logger.Info(
                        $"Register application instance success. [applicationInstanceId] = {applicationInstanceId}");
                }
                else
                {
                    _logger.Warning(
                        "Register application instance fail. Server response null.");
                }
            }
        }
Esempio n. 2
0
        private async Task RegisterApplicationInstance(GrpcConnection availableConnection, CancellationToken token)
        {
            if (!DictionaryUtil.IsNull(RemoteDownstreamConfig.Agent.ApplicationId) && DictionaryUtil.IsNull(RemoteDownstreamConfig.Agent.ApplicationInstanceId))
            {
                var instanceDiscoveryService =
                    new InstanceDiscoveryService.InstanceDiscoveryServiceClient(availableConnection.GrpcChannel);

                var agentUUID    = Guid.NewGuid().ToString("N");
                var registerTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

                var hostName = Dns.GetHostName();

                var osInfo = new OSInfo
                {
                    Hostname  = hostName,
                    OsName    = PlatformInformation.GetOSName(),
                    ProcessNo = Process.GetCurrentProcess().Id
                };

                osInfo.Ipv4S.AddRange(GetIpV4S(hostName));

                var applicationInstance = new ApplicationInstance
                {
                    ApplicationId = RemoteDownstreamConfig.Agent.ApplicationId,
                    AgentUUID     = agentUUID,
                    RegisterTime  = registerTime,
                    Osinfo        = osInfo
                };

                var retry = 0;
                var applicationInstanceId = 0;
                while (retry++ < 5 && DictionaryUtil.IsNull(applicationInstanceId))
                {
                    var applicationInstanceMapping = await instanceDiscoveryService.registerInstanceAsync(applicationInstance);

                    applicationInstanceId = applicationInstanceMapping.ApplicationInstanceId;
                    if (!DictionaryUtil.IsNull(applicationInstanceId))
                    {
                        break;
                    }

                    await Task.Delay(500, token);
                }

                if (!DictionaryUtil.IsNull(applicationInstanceId))
                {
                    RemoteDownstreamConfig.Agent.ApplicationInstanceId = applicationInstanceId;
                    _logger.Info(
                        $"Register application instance success. [applicationInstanceId] = {applicationInstanceId}");
                }
                else
                {
                    _logger.Warning(
                        "Register application instance fail. Server response null.");
                }
            }
        }
Esempio n. 3
0
        public async Task ConnectAsync()
        {
            // using async lock
            using (await _lock.LockAsync())
            {
                if (_connection != null && _connection.CheckState())
                {
                    return;
                }

                _connection = new GrpcConnection(GetServer(_connection?.Server));
                await _connection.ConnectAsync();
            }
        }
Esempio n. 4
0
        public async Task ConnectAsync(TimeSpan timeout)
        {
            // using async lock
            using (await _lock.LockAsync())
            {
                if (_connection != null && _connection.CheckState())
                {
                    return;
                }

                if (_connection != null && !_connection.CheckState())
                {
                    await _connection.ShutdowmAsync();
                }

                _connection = new GrpcConnection(GetServer(_connection?.Server));
                await _connection.ConnectAsync(timeout);
            }
        }
        public async Task ConnectAsync(TimeSpan timeout)
        {
            // using async lock
            using (await _lock.LockAsync())
            {
                if (_connection != null && _connection.CheckState())
                {
                    return;
                }

                if (_connection != null && !_connection.CheckState())
                {
                    await _connection.ShutdowmAsync();
                }

                var metadata = GetServerMetadata(_connection?.Server);
                _connection = new GrpcConnection(metadata.Address, metadata.CertificatePath, metadata.Token);
                await _connection.ConnectAsync(timeout);
            }
        }
        private async Task RegisterApplication(GrpcConnection availableConnection, CancellationToken token)
        {
            if (DictionaryUtil.IsNull(RemoteDownstreamConfig.Agent.ApplicationId))
            {
                var application = new Application {
                    ApplicationCode = AgentConfig.ApplicationCode
                };
                var applicationRegisterService =
                    new ApplicationRegisterService.ApplicationRegisterServiceClient(availableConnection.GrpcChannel);

                var retry         = 0;
                var applicationId = 0;
                while (retry++ < 3 && DictionaryUtil.IsNull(applicationId))
                {
                    var applicationMapping = await applicationRegisterService.applicationCodeRegisterAsync(application);

                    applicationId = applicationMapping?.Application?.Value ?? 0;
                    if (!DictionaryUtil.IsNull(applicationId))
                    {
                        break;
                    }
                    await Task.Delay(500, token);
                }

                if (DictionaryUtil.IsNull(applicationId))
                {
                    _logger.Warning(
                        "Register application fail. Server response null.");
                    return;
                }

                _logger.Info(
                    $"Register application success. [applicationCode] = {application.ApplicationCode}. [applicationId] = {applicationId}");
                RemoteDownstreamConfig.Agent.ApplicationId = applicationId;
            }
        }