private static bool IsSpecificConnectionId(GetConnectionsRequest request, out UniqueId connectionId)
        {
            connectionId = request.ConnectionId.ToUniqueId();
            var isSpecificConnectionId = connectionId != UniqueId.Empty;

            return(isSpecificConnectionId);
        }
Exemplo n.º 2
0
        public InteropContext(Plexus.UniqueId trustedLauncherId, string metadataDir, IRegistryProvider registryProvider)
        {
            RegistryProvider = registryProvider;
            var appRegistryProvider = new JsonFileAppRegistryProvider(Path.Combine(metadataDir, "apps.json"));

            _appLifecycleManagerClientClientRepository = new AppLifecycleManagerClientClientRepository();
            var appLaunchedEventProvider = new AppLaunchedEventProvider();

            _appLifecycleManager = new AppLifecycleManager(appRegistryProvider, appLaunchedEventProvider, _appLifecycleManagerClientClientRepository);
            _appLifecycleManager.RegisterAppInstance(trustedLauncherId);
            _appLaunchedEventSubscriber = new AppLaunchedEventSubscriber(_appLifecycleManager, registryProvider, appLaunchedEventProvider, _appLifecycleManagerClientClientRepository);

            var nativeLauncherInstanceId = Plexus.UniqueId.Generate();

            _appLifecycleManager.RegisterAppInstanceConnection(Generated.NativeAppLauncherClient.Id, nativeLauncherInstanceId);
            _nativeAppLauncherClient = new NativeAppLauncherClient(metadataDir, nativeLauncherInstanceId);

            _appMetadataService     = new AppMetadataServiceImpl(appRegistryProvider, registryProvider);
            _appLifecycleService    = new AppLifecycleServiceImpl(_appLifecycleManager);
            _contextLinkageService  = new ContextLinkageServiceImpl(registryProvider, _appLifecycleManager, appLaunchedEventProvider);
            _appRegistrationService = new AppRegistrationServiceImpl(_appLifecycleManager);

            OnStop(_nativeAppLauncherClient.Stop);
            OnStop(_appLifecycleManagerClientClientRepository.Stop);
        }
Exemplo n.º 3
0
 public bool TryGetOnlineConnection(UniqueId id, out IAppConnection connection)
 {
     lock (_connections)
     {
         return(_connections.TryGetValue(id, out connection));
     }
 }
Exemplo n.º 4
0
        private async Task <IClient> CreateClientAsync(string appId, UniqueId suggestedAppInstanceId, AppLaunchReferrer requestReferrer)
        {
            if (!_clientFactories.TryGetValue(appId, out var clientFactory))
            {
                throw new InvalidOperationException($"Unknown application launch requested: {appId}");
            }

            _appLaunchedEvents.OnNext(new AppLaunchedEvent {
                AppInstanceId = ConvertUniqueId(suggestedAppInstanceId), Referrer = requestReferrer, AppIds = { appId }
            });

            var client = await clientFactory.CreateClientAsync(_broker, suggestedAppInstanceId).ConfigureAwait(false);

            lock (_sync)
            {
                if (!_createdClients.TryGetValue(appId, out var clientList))
                {
                    clientList             = new List <IClient>();
                    _createdClients[appId] = clientList;
                }

                clientList.Add(client);

                client.Completion.IgnoreExceptions().ContinueWithInBackground(_ =>
                {
                    lock (_sync)
                    {
                        return(clientList.Remove(client));
                    }
                }).IgnoreAwait();
            }

            return(client);
        }
Exemplo n.º 5
0
        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
                }
            });
        }
Exemplo n.º 6
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
                }
            });
        }
Exemplo n.º 7
0
 public NativeAppLauncher(string brokerWorkingDir, string cmdBasePath, SubProcessLauncher subProcessLauncher, JsonSerializer jsonSerializer)
 {
     Id = Plexus.UniqueId.Generate();
     _brokerWorkingDir   = brokerWorkingDir;
     _cmdBasePath        = cmdBasePath;
     _subProcessLauncher = subProcessLauncher;
     _jsonSerializer     = jsonSerializer;
 }
Exemplo n.º 8
0
 public static UniqueId ToUniqueId(this Generated.UniqueId uniqueId)
 {
     if (uniqueId is null)
     {
         return(UniqueId.Empty);
     }
     return(UniqueId.FromHiLo(uniqueId.Hi, uniqueId.Lo));
 }
Exemplo n.º 9
0
 public static Generated.UniqueId ToProto(this UniqueId id)
 {
     return(new Generated.UniqueId
     {
         Hi = id.Hi,
         Lo = id.Lo
     });
 }
Exemplo n.º 10
0
 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));
 }
Exemplo n.º 11
0
 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));
 }
Exemplo n.º 12
0
 private static Generated.UniqueId ConvertUniqueId(UniqueId suggestedAppInstanceId)
 {
     return(new Generated.UniqueId
     {
         Hi = suggestedAppInstanceId.Hi,
         Lo = suggestedAppInstanceId.Lo,
     });
 }
        private void SubscribeToApplicationLaunchedEventStream(string applicationId, UniqueId connectionId)
        {
            var appLauncherServiceId           = AppLauncherService.Id;
            var appLaunchedEventStreamMethodId = AppLauncherService.AppLaunchedEventStreamMethodId;
            var methodCallDescriptor           = ProvidedMethodReference.CreateWithConnectionId(appLauncherServiceId, appLaunchedEventStreamMethodId, applicationId, connectionId);

            _lifecycleManagerClientRepo.GetClientObservable()
            .Subscribe(client => SubscribeToLaunchedEventStream(client, connectionId, applicationId, methodCallDescriptor));
        }
 public NativeAppLauncherClient(
     string cmdBasePath,
     JsonSerializer jsonSerializer)
 {
     Id                  = Plexus.UniqueId.Generate();
     _cmdBasePath        = cmdBasePath;
     _subProcessLauncher = new SubProcessLauncher();
     _jsonSerializer     = jsonSerializer;
 }
Exemplo n.º 15
0
 public bool Equals(Plexus.UniqueId other)
 {
     if (Lo != other.Lo)
     {
         return(false);
     }
     if (Hi != other.Hi)
     {
         return(false);
     }
     return(true);
 }
Exemplo n.º 16
0
        private void SubscribeToApplicationLaunchedEventStream(string applicationId, UniqueId connectionId)
        {
            var appLauncherServiceId           = AppLauncherService.Id;
            var appLaunchedEventStreamMethodId = AppLauncherService.AppLaunchedEventStreamMethodId;
            var methodCallDescriptor           = ProvidedMethodReference.Create(appLauncherServiceId, appLaunchedEventStreamMethodId, applicationId, connectionId);

            Task.Factory.StartNew(async() =>
            {
                Log.Info($"Subscribing to ApplicationLaunchedEventStream of {connectionId} application ({applicationId})");
                await _client.Value.CallInvoker
                .CallServerStreaming <Empty, AppLaunchedEvent>(methodCallDescriptor.CallDescriptor, new Empty())
                .ResponseStream.PipeAsync(_appLaunchedSubject).ConfigureAwait(false);
                Log.Info($"Subscription to ApplicationLaunchedEventStream of {connectionId} application ({applicationId}) have finished");
            }, TaskCreationOptions.LongRunning);
        }
Exemplo n.º 17
0
        private async Task <AppLaunchResponse> LaunchAppAsync(AppLaunchRequest request, MethodCallContext context)
        {
            if (!_clients.TryGetValue(request.AppId, out var clientOptions))
            {
                throw new InvalidOperationException($"Unknown application launch requested: {request.AppId}");
            }
            var instanceId = UniqueId.Generate();
            var client     = ClientFactory.Instance.Create(clientOptions.WithAppInstanceId(instanceId).Build());
            await client.ConnectAsync().ConfigureAwait(false);

            return(new AppLaunchResponse
            {
                AppInstanceId = new Generated.UniqueId
                {
                    Hi = instanceId.Hi,
                    Lo = instanceId.Lo
                }
            });
        }
Exemplo n.º 18
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);
            }
        }
Exemplo n.º 19
0
        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);
        }
Exemplo n.º 20
0
 private static bool Equals(UniqueId uniqueId1, Plexus.UniqueId uniqueId2)
 {
     return(uniqueId1.Hi.Equals(uniqueId2.Hi) && uniqueId1.Lo.Equals(uniqueId2.Lo));
 }
Exemplo n.º 21
0
 public UniqueId(Plexus.UniqueId uniqueId)
 {
     Lo = uniqueId.Lo;
     Hi = uniqueId.Hi;
 }
Exemplo n.º 22
0
 public static UniqueId ToUniqueId(this Generated.UniqueId uniqueId)
 {
     return(UniqueId.FromHiLo(uniqueId.Hi, uniqueId.Lo));
 }
Exemplo n.º 23
0
 private static ContextDto ConvertContextToProto(Context context, UniqueId appInstanceId) => new ContextDto
 {
     Id   = context.Id,
     Own  = context.OwnerAppInstanceId == appInstanceId,
     Kind = context.Kind
 };
        private static bool IsSpecificAppIdWithInstanceId(GetConnectionsRequest request, out string appId, out UniqueId appInstanceId)
        {
            appId         = request.ApplicationId;
            appInstanceId = request.AppInstanceId.ToUniqueId();
            var isSpecificAppIdWithInstanceId = !string.IsNullOrEmpty(appId) && appInstanceId != UniqueId.Empty;

            return(isSpecificAppIdWithInstanceId);
        }
        private void SubscribeToLaunchedEventStream(AppLifecycleManagerClient client, UniqueId connectionId, string applicationId, ProvidedMethodReference methodCallDescriptor)
        {
            Task.Factory.StartNew(async() =>
            {
                Log.Info($"Subscribing client '{client.ApplicationInstanceId}' to ApplicationLaunchedEventStream of {connectionId} application ({applicationId})");

                await client.CallInvoker
                .CallServerStreaming <Empty, AppLaunchedEvent>(methodCallDescriptor.CallDescriptor, new Empty())
                .ResponseStream.PipeAsync(_appLaunchedEventConsumer.AppLaunchedEventObserver).ConfigureAwait(false);
                Log.Info($"Subscription to ApplicationLaunchedEventStream of {connectionId} application ({applicationId}) have finished");
            }, TaskCreationOptions.LongRunning);
        }
 private void RegisterInstanceId(Plexus.UniqueId appInstanceId)
 => _appLifecycleManager.RegisterAppInstance(appInstanceId);
 public NativeAppLauncherClient(string cmdBasePath)
 {
     Id                  = Plexus.UniqueId.Generate();
     _cmdBasePath        = cmdBasePath;
     _subProcessLauncher = new SubProcessLauncher();
 }