Exemplo n.º 1
0
        public static void AddCommandServer(ICommandServer commandServer)
        {
            lock (serviceLock)
            {
                var exposedTypes = Discovery.GetTypesFromAttribute(typeof(ServiceExposedAttribute));
                foreach (var type in exposedTypes)
                {
                    if (type.IsClass)
                    {
                        if (TypeAnalyzer.GetTypeDetail(type).Interfaces.Any(x => x == typeof(ICommand)))
                        {
                            var interfaceStack = ProviderLayers.GetProviderInterfaceStack();
                            var hasHandler     = Discovery.HasImplementationType(TypeAnalyzer.GetGenericType(typeof(ICommandHandler <>), type), interfaceStack, interfaceStack.Length - 1);
                            if (hasHandler)
                            {
                                if (commandClients.Keys.Contains(type))
                                {
                                    throw new InvalidOperationException($"Cannot create loopback. Command Client already registered for type {type.GetNiceName()}");
                                }
                                if (!commandServerTypes.Contains(type))
                                {
                                    commandServerTypes.Add(type);
                                }
                                commandServer.RegisterCommandType(type);
                            }
                        }
                    }
                }

                commandServer.SetHandler(HandleRemoteCommandDispatchAsync, HandleRemoteCommandDispatchAwaitAsync);
                commandServers.Add(commandServer);
                commandServer.Open();
            }
        }
Exemplo n.º 2
0
        private static void SelectBrowser(string browser)
        {
            switch (browser)
            {
            case "Firefox":
                _driver = new FirefoxDriver();
                break;

            case "Chrome":
                var chromeDriverService = ChromeDriverService.CreateDefaultService(DriversPath, "chromedriver.exe");
                chromeDriverService.Start();
                _driver        = new RemoteWebDriver(chromeDriverService.ServiceUrl, DesiredCapabilities.Chrome());
                _driverService = chromeDriverService;
                break;

            case "IE":
                _driver = new InternetExplorerDriver(DriversPath);
                break;

            case "PhantomJS":
                var phantomJsPath          = SmokeTestPaths.GetPhantomJsPath();
                var phantomJsDriverService = PhantomJSDriverService.CreateDefaultService(phantomJsPath);
                _driver        = new PhantomJSDriver(phantomJsDriverService);
                _driverService = phantomJsDriverService;
                break;

            default:
                throw new ArgumentException("Unknown browser");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Connect the command server to the Hyperion Device specific functions.
        /// </summary>
        /// <param name="commandServer">Command server processing Hyperion Device remote commands.</param>
        private void ConnectCommandServer( ICommandServer commandServer )
        {
            commandServer.AddCommand(
                                      "#ReadRegister",
                                      "Read the specified device register address.",
                                      false,
                                      false,
                                      new ServerCommandDelegate( ReadRegister ) );

            commandServer.AddCommand(
                                      "#WriteRegister",
                                      "Write value to the specified device register address.",
                                      false,
                                      false,
                                      new ServerCommandDelegate( WriteRegister ) );

            commandServer.AddCommand(
                                      "#GetRawPeaks",
                                      "Get the raw (as transferred from the FPGA) peak data.",
                                      false,
                                      false,
                                      new ServerCommandDelegate( GetRawPeaks ) );

            commandServer.AddCommand(
                                      "#GetRawSpectra",
                                      "Get the raw (as transferred from the FPGA) reflected optical spectrum data.",
                                      false,
                                      false,
                                      new ServerCommandDelegate( GetRawSpectra ) );
        }
        public string SendCommand(string remoteName, string commandName, object[] parameters)
        {
            ICommandServer clientProxy = channelFactory.CreateChannel();
            string         str         = clientProxy.TriggerCommand(remoteName, commandName, parameters);

            channelFactory.Close();
            return(str);
        }
        public string RequestDeviceStateVariable(string deviceName, string variableName)
        {
            ICommandServer clientProxy   = channelFactory.CreateChannel();
            string         variableValue = clientProxy.GetDeviceStateVariable(deviceName, variableName);

            channelFactory.Close();
            return(variableValue);
        }
        public string RequestDeviceState(string deviceName)
        {
            ICommandServer clientProxy = channelFactory.CreateChannel();
            string         state       = clientProxy.GetDeviceState(deviceName);

            channelFactory.Close();
            return(state);
        }
Exemplo n.º 7
0
        public async Task <IActionResult> ChangeEnrollment([FromServices] ICommandServer commands, string pages, Id dealerId)
        {
            if (!TryParsePages(pages, out var parsedPages))
            {
                return(new NotFoundResult());
            }

            return(await commands.Execute(
                       new ChangeEnrollment(dealerId, parsedPages),
                       When <DealerNotEnrolled> .ThenNotFound,
                       When <EnrollmentChanged> .ThenOk,
                       When <EnrollmentUnchanged> .ThenOk));
        }
Exemplo n.º 8
0
        public static void StartServices(string serviceName, ServiceSettings serviceSettings, IServiceCreator serviceCreator, IRelayRegister relayRegister = null)
        {
            _ = Log.InfoAsync($"Starting {serviceName}");
            lock (serviceLock)
            {
                var serverSetting = serviceSettings.Services.FirstOrDefault(x => x.Name == serviceName);
                if (serverSetting == null)
                {
                    throw new Exception($"Service {serviceName} not found in {CQRSSettings.SettingsFileName}");
                }

                var serverUrl = Config.GetSetting("urls");
                if (String.IsNullOrWhiteSpace(serverUrl))
                {
                    serverUrl = Config.GetSetting("ASPNETCORE_URLS");
                }
                if (String.IsNullOrWhiteSpace(serverUrl))
                {
                    serverUrl = Config.GetSetting("DOTNET_URLS");
                }
                if (String.IsNullOrWhiteSpace(serverUrl))
                {
                    serverUrl = serverSetting.ExternalUrl;
                }

                ICommandServer commandServer = null;
                IEventServer   eventServer   = null;
                IQueryServer   queryServer   = null;

                var serverTypes = new HashSet <Type>();
                foreach (var clientSetting in serviceSettings.Services)
                {
                    if (clientSetting.Types == null || clientSetting.Types.Length == 0)
                    {
                        continue;
                    }
                    if (clientSetting != serverSetting)
                    {
                        continue;
                    }
                    foreach (var typeName in clientSetting.Types)
                    {
                        var type = Discovery.GetTypeFromName(typeName);
                        if (!type.IsInterface)
                        {
                            throw new Exception($"{type.GetNiceName()} is not an interface");
                        }
                        var typeDetails = TypeAnalyzer.GetTypeDetail(type);
                        if (!typeDetails.Interfaces.Contains(typeof(IBaseProvider)))
                        {
                            throw new Exception($"{type.GetNiceName()} does not inherit {nameof(IBaseProvider)}");
                        }

                        var commandTypes = GetCommandTypesFromInterface(type);
                        foreach (var commandType in commandTypes)
                        {
                            serverTypes.Add(commandType);
                        }

                        var eventTypes = GetEventTypesFromInterface(type);
                        foreach (var eventType in eventTypes)
                        {
                            serverTypes.Add(eventType);
                        }

                        if (typeDetails.Attributes.Any(x => x is ServiceExposedAttribute))
                        {
                            serverTypes.Add(type);
                        }
                    }
                }

                foreach (var serviceSetting in serviceSettings.Services)
                {
                    if (serviceSetting.Types == null || serviceSetting.Types.Length == 0)
                    {
                        continue;
                    }

                    ICommandClient commandClient = null;
                    IEventClient   eventClient   = null;
                    IQueryClient   queryClient   = null;

                    foreach (var typeName in serviceSetting.Types)
                    {
                        var type = Discovery.GetTypeFromName(typeName);
                        if (!type.IsInterface)
                        {
                            throw new Exception($"{type.GetNiceName()} is not an interface");
                        }
                        var typeDetails = TypeAnalyzer.GetTypeDetail(type);
                        if (!typeDetails.Interfaces.Contains(typeof(IBaseProvider)))
                        {
                            throw new Exception($"{type.GetNiceName()} does not inherit {nameof(IBaseProvider)}");
                        }

                        var commandTypes = GetCommandTypesFromInterface(type);
                        if (commandTypes.Count > 0)
                        {
                            if (serviceSetting == serverSetting)
                            {
                                if (commandServer == null)
                                {
                                    var encryptionKey = String.IsNullOrWhiteSpace(serviceSetting.EncryptionKey) ? null : SymmetricEncryptor.GetKey(serviceSetting.EncryptionKey);
                                    commandServer = serviceCreator.CreateCommandServer(serverUrl, encryptionKey);
                                    commandServer.SetHandler(HandleRemoteCommandDispatchAsync, HandleRemoteCommandDispatchAwaitAsync);
                                    if (!commandServers.Contains(commandServer))
                                    {
                                        commandServers.Add(commandServer);
                                    }
                                }
                                foreach (var commandType in commandTypes)
                                {
                                    if (commandClients.Keys.Contains(commandType))
                                    {
                                        throw new InvalidOperationException($"Command Client already registered for type {commandType.GetNiceName()}");
                                    }
                                    if (commandServerTypes.Contains(commandType))
                                    {
                                        throw new InvalidOperationException($"Command Server already registered for type {commandType.GetNiceName()}");
                                    }
                                    commandServerTypes.Add(commandType);
                                    commandServer.RegisterCommandType(commandType);
                                }
                            }
                            else
                            {
                                if (commandClient == null)
                                {
                                    var encryptionKey = String.IsNullOrWhiteSpace(serviceSetting.EncryptionKey) ? null : SymmetricEncryptor.GetKey(serviceSetting.EncryptionKey);
                                    commandClient = serviceCreator.CreateCommandClient(relayRegister?.RelayUrl ?? serviceSetting.ExternalUrl, encryptionKey);
                                }
                                var clientCommandTypes = commandTypes.Where(x => !serverTypes.Contains(x)).ToArray();
                                if (clientCommandTypes.Length > 0)
                                {
                                    foreach (var commandType in clientCommandTypes)
                                    {
                                        if (commandServerTypes.Contains(commandType))
                                        {
                                            throw new InvalidOperationException($"Command Server already registered for type {commandType.GetNiceName()}");
                                        }
                                        if (commandClients.Keys.Contains(commandType))
                                        {
                                            throw new InvalidOperationException($"Command Client already registered for type {commandType.GetNiceName()}");
                                        }
                                        commandClients.TryAdd(commandType, commandClient);
                                    }
                                }
                                else
                                {
                                    if (commandClient is IDisposable disposable)
                                    {
                                        disposable.Dispose();
                                    }
                                }
                            }
                        }

                        var eventTypes = GetEventTypesFromInterface(type);
                        if (eventTypes.Count > 0)
                        {
                            if (serviceSetting == serverSetting)
                            {
                                if (eventServer == null)
                                {
                                    var encryptionKey = String.IsNullOrWhiteSpace(serviceSetting.EncryptionKey) ? null : SymmetricEncryptor.GetKey(serviceSetting.EncryptionKey);
                                    eventServer = serviceCreator.CreateEventServer(serverUrl, encryptionKey);
                                    eventServer.SetHandler(HandleRemoteEventDispatchAsync);
                                    if (!eventServers.Contains(eventServer))
                                    {
                                        eventServers.Add(eventServer);
                                    }
                                }
                                foreach (var eventType in eventTypes)
                                {
                                    if (eventClients.Keys.Contains(eventType))
                                    {
                                        throw new InvalidOperationException($"Event Client already registered for type {eventType.GetNiceName()}");
                                    }
                                    if (eventServerTypes.Contains(eventType))
                                    {
                                        throw new InvalidOperationException($"Event Server already registered for type {eventType.GetNiceName()}");
                                    }
                                    eventServerTypes.Add(eventType);
                                    eventServer.RegisterEventType(eventType);
                                }
                            }
                            else
                            {
                                if (eventClient == null)
                                {
                                    var encryptionKey = String.IsNullOrWhiteSpace(serviceSetting.EncryptionKey) ? null : SymmetricEncryptor.GetKey(serviceSetting.EncryptionKey);
                                    eventClient = serviceCreator.CreateEventClient(relayRegister?.RelayUrl ?? serviceSetting.ExternalUrl, encryptionKey);
                                }
                                var clientEventTypes = eventTypes.Where(x => !serverTypes.Contains(x)).ToArray();
                                if (clientEventTypes.Length > 0)
                                {
                                    foreach (var eventType in eventTypes)
                                    {
                                        if (eventServerTypes.Contains(eventType))
                                        {
                                            throw new InvalidOperationException($"Event Server already registered for type {eventType.GetNiceName()}");
                                        }
                                        if (!eventClients.Keys.Contains(eventType))
                                        {
                                            eventClients.TryAdd(eventType, eventClient);
                                        }
                                    }
                                }
                                else
                                {
                                    if (eventClient is IDisposable disposable)
                                    {
                                        disposable.Dispose();
                                    }
                                }
                            }
                        }

                        if (typeDetails.Attributes.Any(x => x is ServiceExposedAttribute))
                        {
                            if (serviceSetting == serverSetting)
                            {
                                if (queryServer == null)
                                {
                                    var encryptionKey = String.IsNullOrWhiteSpace(serviceSetting.EncryptionKey) ? null : SymmetricEncryptor.GetKey(serviceSetting.EncryptionKey);
                                    queryServer = serviceCreator.CreateQueryServer(serverUrl, encryptionKey);
                                    queryServer.SetHandler(HandleRemoteQueryCallAsync);
                                    if (!queryServers.Contains(queryServer))
                                    {
                                        queryServers.Add(queryServer);
                                    }
                                }
                                if (queryClients.Keys.Contains(type))
                                {
                                    throw new InvalidOperationException($"Query Client already registered for type {type.GetNiceName()}");
                                }
                                if (queryServerTypes.Contains(type))
                                {
                                    throw new InvalidOperationException($"Query Server already registered for type {type.GetNiceName()}");
                                }
                                queryServerTypes.Add(type);
                                queryServer.RegisterInterfaceType(type);
                            }
                            else
                            {
                                if (queryClient == null)
                                {
                                    var encryptionKey = String.IsNullOrWhiteSpace(serviceSetting.EncryptionKey) ? null : SymmetricEncryptor.GetKey(serviceSetting.EncryptionKey);
                                    queryClient = serviceCreator.CreateQueryClient(relayRegister?.RelayUrl ?? serviceSetting.ExternalUrl, encryptionKey);
                                }
                                if (!serverTypes.Contains(type))
                                {
                                    if (queryServerTypes.Contains(type))
                                    {
                                        throw new InvalidOperationException($"Query Server already registered for type {type.GetNiceName()}");
                                    }
                                    if (commandClients.Keys.Contains(type))
                                    {
                                        throw new InvalidOperationException($"Query Client already registered for type {type.GetNiceName()}");
                                    }
                                    queryClients.TryAdd(type, queryClient);
                                }
                                else
                                {
                                    if (queryClient is IDisposable disposable)
                                    {
                                        disposable.Dispose();
                                    }
                                }
                            }
                        }
                    }
                }

                Dictionary <string, HashSet <string> > relayRegisterTypes = null;
                if (relayRegister != null)
                {
                    relayRegisterTypes = new Dictionary <string, HashSet <string> >();
                }

                if (commandServer != null)
                {
                    commandServer.Open();
                    if (relayRegister != null)
                    {
                        var commandTypes = commandServer.GetCommandTypes().Select(x => x.GetNiceName()).ToArray();
                        if (!relayRegisterTypes.TryGetValue(commandServer.ConnectionString, out HashSet <string> relayTypes))
                        {
                            relayTypes = new HashSet <string>();
                            relayRegisterTypes.Add(commandServer.ConnectionString, relayTypes);
                        }
                        foreach (var type in commandTypes)
                        {
                            relayTypes.Add(type);
                        }
                    }
                }
                if (eventServer != null)
                {
                    eventServer.Open();
                    if (relayRegister != null)
                    {
                        var eventTypes = eventServer.GetEventTypes().Select(x => x.GetNiceName()).ToArray();
                        if (!relayRegisterTypes.TryGetValue(eventServer.ConnectionString, out HashSet <string> relayTypes))
                        {
                            relayTypes = new HashSet <string>();
                            relayRegisterTypes.Add(eventServer.ConnectionString, relayTypes);
                        }
                        foreach (var type in eventTypes)
                        {
                            relayTypes.Add(type);
                        }
                    }
                }
                if (queryServer != null)
                {
                    queryServer.Open();
                    if (relayRegister != null)
                    {
                        var queryTypes = queryServer.GetInterfaceTypes().Select(x => x.GetNiceName()).ToArray();
                        if (!relayRegisterTypes.TryGetValue(queryServer.ConnectionString, out HashSet <string> relayTypes))
                        {
                            relayTypes = new HashSet <string>();
                            relayRegisterTypes.Add(queryServer.ConnectionString, relayTypes);
                        }
                        foreach (var type in queryTypes)
                        {
                            relayTypes.Add(type);
                        }
                    }
                }

                if (relayRegister != null)
                {
                    foreach (var group in relayRegisterTypes)
                    {
                        relayRegister.Register(group.Key, group.Value.ToArray());
                    }
                }

                if (serverSetting.InstantiateTypes != null && serverSetting.InstantiateTypes.Length > 0)
                {
                    foreach (var instantiation in serverSetting.InstantiateTypes)
                    {
                        try
                        {
                            var type     = Discovery.GetTypeFromName(instantiation);
                            var instance = Activator.CreateInstance(type);
                            instanciations.Add(instance);
                        }
                        catch (Exception ex)
                        {
                            _ = Log.ErrorAsync($"Failed to instantiate {instantiation}", ex);
                        }
                    }
                }
            }
        }
Exemplo n.º 9
0
 public ChatController(IQueryServer queries, ICommandServer commands)
 {
     _queries  = queries;
     _commands = commands;
 }
Exemplo n.º 10
0
 public Task <IActionResult> StartImport([FromServices] ICommandServer commands) =>
 commands.Execute(
     new StartImport(),
     When <ImportStarted> .ThenOk,
     When <ImportAlreadyStarted> .ThenConflict);
 private static void SelectBrowser(string browser)
 {
     switch (browser)
     {
         case "Firefox":
             _driver = new FirefoxDriver();
             break;
         case "Chrome":
             var chromeDriverService = ChromeDriverService.CreateDefaultService(DriversPath, "chromedriver.exe");
             chromeDriverService.Start();
             _driver = new RemoteWebDriver(chromeDriverService.ServiceUrl, DesiredCapabilities.Chrome());
             _driverService = chromeDriverService;
             break;
         case "IE":
             _driver = new InternetExplorerDriver(DriversPath);
             break;
         case "PhantomJS":
             var phantomJsPath = SmokeTestPaths.GetPhantomJsPath();
             var phantomJsDriverService = PhantomJSDriverService.CreateDefaultService(phantomJsPath);
             _driver = new PhantomJSDriver(phantomJsDriverService);
             _driverService = phantomJsDriverService;
             break;
         default:
             throw new ArgumentException("Unknown browser");
     }
 }
 public AutoPilotModel()
 {
     Server             = null;
     commandsCollection = new BlockingCollection <string>();
 }
Exemplo n.º 13
0
 public ManualControlModel()
 {
     Server  = null;
     aileron = elevator = throttle = rudder = 0;
 }
Exemplo n.º 14
0
 public CardController(ICommandServer commands, IQueryServer queries)
 {
     _commands = commands;
     _queries  = queries;
 }
Exemplo n.º 15
0
 public LegacyEventsController(IQueryServer queries, ICommandServer commands)
 {
     _queries  = queries;
     _commands = commands;
 }