public async Task <AppLaunchResponse> Launch(AppLaunchRequest request, MethodCallContext context)
        {
            if (!_clientFactories.TryGetValue(request.AppId, out var clientFactory))
            {
                throw new InvalidOperationException($"Unknown application launch requested: {request.AppId}");
            }

            var client = await clientFactory.CreateClientAsync(
                _broker,
                UniqueId.FromHiLo(
                    request.SuggestedAppInstanceId.Hi,
                    request.SuggestedAppInstanceId.Lo));

            OnStop(client.Disconnect);
            await client.ConnectAsync().ConfigureAwait(false);

            return(new AppLaunchResponse
            {
                AppInstanceId = new Generated.UniqueId
                {
                    Hi = client.ApplicationInstanceId.Hi,
                    Lo = client.ApplicationInstanceId.Lo
                }
            });
        }
Example #2
0
        public async Task <AppLaunchResponse> Launch(AppLaunchRequest request, MethodCallContext context)
        {
            var appId = request.AppId;
            var suggestedAppInstanceId = UniqueId.FromHiLo(
                request.SuggestedAppInstanceId.Hi,
                request.SuggestedAppInstanceId.Lo);
            Task <IClient> clientTask = null;

            lock (_sync)
            {
                if (request.LaunchMode == AppLaunchMode.SingleInstance &&
                    _createdClients.TryGetValue(appId, out var clientList) &&
                    clientList.Any())
                {
                    var existingClient = clientList.First();
                    clientTask = Task.FromResult(existingClient);
                }

                if (request.LaunchMode != AppLaunchMode.MultiInstance && clientTask == null)
                {
                    if (_createClientTasks.TryGetValue(appId, out var list) && list.Any())
                    {
                        clientTask = list.First();
                    }
                }

                clientTask = clientTask ?? CreateClientAsync(appId, suggestedAppInstanceId);
            }

            var client = await clientTask.ConfigureAwait(false);

            OnStop(client.Disconnect);

            await client.ConnectAsync().ConfigureAwait(false);

            if (client.ApplicationInstanceId == suggestedAppInstanceId)
            {
                lock (_sync)
                {
                    if (_createClientTasks.TryGetValue(appId, out var list))
                    {
                        list.Remove(clientTask);
                        if (!list.Any())
                        {
                            _createClientTasks.Remove(appId);
                        }
                    }
                }
            }

            return(new AppLaunchResponse
            {
                AppInstanceId = new Generated.UniqueId
                {
                    Hi = client.ApplicationInstanceId.Hi,
                    Lo = client.ApplicationInstanceId.Lo
                }
            });
        }
 public static Maybe <PlexusUniqueId> ConvertFromProto(this UniqueId guid)
 {
     if (guid == null || (long)guid.Lo == 0L && (long)guid.Hi == 0L)
     {
         return(Maybe <PlexusUniqueId> .Nothing);
     }
     return(PlexusUniqueId.FromHiLo(guid.Hi, guid.Lo));
 }
 public static PlexusUniqueId ConvertFromProtoStrict(this UniqueId guid)
 {
     if (guid == null || (long)guid.Lo == 0L && (long)guid.Hi == 0L)
     {
         throw new InvalidOperationException($"Cannot convert from proto as UniqueId: {guid}");
     }
     return(PlexusUniqueId.FromHiLo(guid.Hi, guid.Lo));
 }
Example #5
0
 public static UniqueId ToUniqueId(this Generated.UniqueId uniqueId)
 {
     if (uniqueId is null)
     {
         return(UniqueId.Empty);
     }
     return(UniqueId.FromHiLo(uniqueId.Hi, uniqueId.Lo));
 }
Example #6
0
        private async Task LaunchAppAsync(string appId)
        {
            try
            {
                Log.Info("Launching app {0}", appId);
                var request = new ResolveAppRequest
                {
                    AppId          = appId,
                    AppResolveMode = AppLaunchMode.MultiInstance
                };
                var response = await _client.AppLifecycleService.ResolveApp(request).ConfigureAwait(false);

                var connectionId  = UniqueId.FromHiLo(response.AppConnectionId.Hi, response.AppConnectionId.Lo);
                var appInstanceId = UniqueId.FromHiLo(response.AppInstanceId.Hi, response.AppInstanceId.Lo);
                Log.Info("Launched app {0}: connectionId={1}, appInstanceId={2}", appId, connectionId, appInstanceId);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Failed to launch app {0}", appId);
            }
        }
        public async ValueTask <UniqueId> LaunchAsync(string appId)
        {
            Log.Info("Launching {0}", appId);

            if (string.Equals(appId, "interop.NativeAppLauncher"))
            {
                return((await _startNativeAppLauncherTask.Value.ConfigureAwait(false)).Id);
            }

            var appDto = _appsDto.Apps.FirstOrDefault(x => string.Equals(x.Id, appId));

            if (appDto == null)
            {
                throw new InvalidOperationException($"The requested application {appId} is not defined in application registry");
            }

            if (string.IsNullOrEmpty(appDto.LauncherId))
            {
                throw new InvalidOperationException($"Launcher is not defined for application {appId}");
            }

            var launchMethodReference =
                ProvidedMethodReference.Create("interop.AppLauncherService", "Launch", appDto.LauncherId);

            var response = await _client
                           .CallUnary <AppLaunchRequest, AppLaunchResponse>(
                launchMethodReference.CallDescriptor,
                new AppLaunchRequest
            {
                AppId            = appId,
                LaunchParamsJson = appDto.LauncherParams.ToString()
            })
                           .ConfigureAwait(false);

            var appInstanceId = UniqueId.FromHiLo(response.AppInstanceId.Hi, response.AppInstanceId.Lo);

            Log.Info("Launched app {0} instance {1}", appId, appInstanceId);

            return(appInstanceId);
        }
Example #8
0
 public static UniqueId ToUniqueId(this Generated.UniqueId uniqueId)
 {
     return(UniqueId.FromHiLo(uniqueId.Hi, uniqueId.Lo));
 }