예제 #1
0
        public TestService(Communicator serviceManagerCommunicator)
        {
            var facet = new TestFacet();

            // Install a custom admin facet.
            serviceManagerCommunicator.AddAdminFacet("TestFacet", facet);

            // The TestFacetI servant also implements PropertiesAdminUpdateCallback. Set the callback on the admin facet.
            IObject?propFacet = serviceManagerCommunicator.FindAdminFacet("IceBox.Service.TestService.Properties");

            if (propFacet is IPropertiesAdmin admin)
            {
                admin.Updated += (_, updates) => facet.Updated(updates);
            }
        }
예제 #2
0
파일: TestI.cs 프로젝트: tchernobog/ice
        public IRemoteCommunicatorPrx createCommunicator(Dictionary <string, string> props, Current current)
        {
            //
            // Prepare the property set using the given properties.
            //
            ILogger?logger = null;
            string? value;
            int     nullLogger;

            if (props.TryGetValue("NullLogger", out value) && int.TryParse(value, out nullLogger) && nullLogger > 0)
            {
                logger = new NullLogger();
            }

            //
            // Initialize a new communicator.
            //
            var communicator = new Communicator(props, logger: logger);

            //
            // Install a custom admin facet.
            //
            try
            {
                var testFacet = new TestFacet();
                communicator.AddAdminFacet(testFacet, "TestFacet");
            }
            catch (System.ArgumentException)
            {
            }

            //
            // The RemoteCommunicator servant also implements PropertiesAdminUpdateCallback.
            // Set the callback on the admin facet.
            //
            var servant   = new RemoteCommunicator(communicator);
            var propFacet = communicator.FindAdminFacet("Properties");

            if (propFacet != null)
            {
                var admin = (INativePropertiesAdmin)propFacet;
                Debug.Assert(admin != null);
                admin.AddUpdateCallback(servant.updated);
            }

            return(current.Adapter.Add(servant, IRemoteCommunicatorPrx.Factory));
        }
예제 #3
0
        public IRemoteCommunicatorPrx createCommunicator(Dictionary <string, string> props, Current current)
        {
            //
            // Prepare the property set using the given properties.
            //
            ILogger?logger = null;

            if (props.TryGetValue("NullLogger", out string?value) &&
                int.TryParse(value, out int nullLogger) && nullLogger > 0)
            {
                logger = new NullLogger();
            }

            //
            // Initialize a new communicator.
            //
            var communicator = new Communicator(props, logger: logger);

            //
            // Install a custom admin facet.
            //
            try
            {
                var testFacet = new TestFacet();
                communicator.AddAdminFacet("TestFacet", testFacet);
            }
            catch (System.ArgumentException)
            {
            }

            // The RemoteCommunicator servant also implements PropertiesAdminUpdateCallback.
            var servant   = new RemoteCommunicator(communicator);
            var propFacet = communicator.FindAdminFacet("Properties");

            if (propFacet is IPropertiesAdmin admin)
            {
                admin.Updated += (_, updates) => servant.Updated(updates);
            }

            return(current.Adapter.AddWithUUID(servant, IRemoteCommunicatorPrx.Factory));
        }
예제 #4
0
        public static void TestFacets(Communicator com, bool builtInFacets, bool filtered)
        {
            if (builtInFacets && !filtered)
            {
                TestHelper.Assert(com.FindAdminFacet("Properties") != null);
                TestHelper.Assert(com.FindAdminFacet("Process") != null);
                TestHelper.Assert(com.FindAdminFacet("Logger") != null);
                TestHelper.Assert(com.FindAdminFacet("Metrics") != null);
            }

            var f1 = new TestFacet();
            var f2 = new TestFacet();
            var f3 = new TestFacet();

            if (!filtered)
            {
                com.AddAdminFacet("Facet1", f1);
                com.AddAdminFacet("Facet2", f2);
                com.AddAdminFacet("Facet3", f3);
            }
            else
            {
                try
                {
                    com.AddAdminFacet("Facet1", f1);
                    TestHelper.Assert(false);
                }
                catch (ArgumentException)
                {
                }

                try
                {
                    com.AddAdminFacet("Facet2", f2);
                    TestHelper.Assert(false);
                }
                catch (ArgumentException)
                {
                }

                try
                {
                    com.AddAdminFacet("Facet3", f3);
                    TestHelper.Assert(false);
                }
                catch (ArgumentException)
                {
                }
            }

            if (!filtered)
            {
                TestHelper.Assert(com.FindAdminFacet("Facet1") == f1);
                TestHelper.Assert(com.FindAdminFacet("Facet2") == f2);
                TestHelper.Assert(com.FindAdminFacet("Facet3") == f3);
            }
            else
            {
                TestHelper.Assert(com.FindAdminFacet("Facet1") == null);
                TestHelper.Assert(com.FindAdminFacet("Facet2") == null);
                TestHelper.Assert(com.FindAdminFacet("Facet3") == null);
            }
            TestHelper.Assert(com.FindAdminFacet("Bogus") == null);

            IReadOnlyDictionary <string, IObject> facetMap = com.FindAllAdminFacets();

            if (builtInFacets)
            {
                TestHelper.Assert(facetMap.Count == 7);
                TestHelper.Assert(facetMap.ContainsKey("Properties"));
                TestHelper.Assert(facetMap.ContainsKey("Process"));
                TestHelper.Assert(facetMap.ContainsKey("Logger"));
                TestHelper.Assert(facetMap.ContainsKey("Metrics"));
            }
            else if (filtered)
            {
                TestHelper.Assert(facetMap.Count >= 1);
                TestHelper.Assert(facetMap.ContainsKey("Properties"));
            }

            if (!filtered)
            {
                TestHelper.Assert(facetMap.ContainsKey("Facet1"));
                TestHelper.Assert(facetMap.ContainsKey("Facet2"));
                TestHelper.Assert(facetMap.ContainsKey("Facet3"));

                try
                {
                    com.AddAdminFacet("Facet1", f1);
                    TestHelper.Assert(false);
                }
                catch (ArgumentException)
                {
                    // Expected
                }
            }

            IObject?facet = com.RemoveAdminFacet("Bogus");

            TestHelper.Assert(facet == null);

            if (!filtered)
            {
                facet = com.RemoveAdminFacet("Facet1");
                TestHelper.Assert(facet == f1);
                com.RemoveAdminFacet("Facet2");
                com.RemoveAdminFacet("Facet3");
                facet = com.RemoveAdminFacet("Facet1");
                TestHelper.Assert(facet == null);
            }
        }
예제 #5
0
        internal async Task <int> RunAsync()
        {
            try
            {
                // Create an object adapter. Services probably should NOT share this object adapter, as the endpoint(s)
                // for this object adapter will most likely need to be behind a firewall for security reasons.
                ObjectAdapter?adapter = null;
                if (_communicator.GetProperty("IceBox.ServiceManager.Endpoints") != null)
                {
                    adapter = _communicator.CreateObjectAdapter("IceBox.ServiceManager");
                    string instanceName = _communicator.GetProperty("IceBox.InstanceName") ?? "IceBox";
                    adapter.Add(new Identity("ServiceManager", instanceName), this);
                }

                // Parse the property set with the prefix "IceBox.Service.". These properties should have the following
                // format:
                //
                // IceBox.Service.Foo=<assembly>:Package.Foo [args]
                //
                // We parse the service properties specified in IceBox.LoadOrder first, then the ones from remaining
                // services.
                string prefix = "IceBox.Service.";
                Dictionary <string, string> services = _communicator.GetProperties(forPrefix: prefix);

                if (services.Count == 0)
                {
                    throw new InvalidConfigurationException(
                              "IceBox.ServiceManager: configuration must include at least one IceBox service");
                }

                string[] loadOrder = (_communicator.GetPropertyAsList("IceBox.LoadOrder") ?? Array.Empty <string>()).Where(
                    s => s.Length > 0).ToArray();
                var servicesInfo = new List <StartServiceInfo>();
                foreach (string name in loadOrder)
                {
                    string key = prefix + name;
                    if (!services.TryGetValue(key, out string?value))
                    {
                        throw new InvalidConfigurationException(
                                  $"IceBox.ServiceManager: no service definition for `{name}'");
                    }
                    servicesInfo.Add(new StartServiceInfo(name, value, _argv));
                    services.Remove(key);
                }

                foreach (KeyValuePair <string, string> entry in services)
                {
                    servicesInfo.Add(new StartServiceInfo(entry.Key.Substring(prefix.Length), entry.Value, _argv));
                }

                // Check if some services are using the shared communicator in which case we create the shared
                // communicator now with a property set that is the union of all the service properties (from services
                // that use the shared communicator).
                if (_communicator.GetProperties(forPrefix: "IceBox.UseSharedCommunicator.").Count > 0)
                {
                    Dictionary <string, string> properties = CreateServiceProperties("SharedCommunicator");
                    foreach (StartServiceInfo service in servicesInfo)
                    {
                        if (!(_communicator.GetPropertyAsBool($"IceBox.UseSharedCommunicator.{service.Name}") ?? false))
                        {
                            continue;
                        }

                        // Load the service properties using the shared communicator properties as the default properties.
                        properties.ParseIceArgs(ref service.Args);

                        // Parse <service>.* command line options (the Ice command line options were parsed by the call
                        // to createProperties above).
                        properties.ParseArgs(ref service.Args, service.Name);
                    }

                    string facetNamePrefix = "IceBox.SharedCommunicator.";
                    bool   addFacets       = ConfigureAdmin(properties, facetNamePrefix);

                    _sharedCommunicator = new Communicator(properties);

                    if (addFacets)
                    {
                        // Add all facets created on shared communicator to the IceBox communicator
                        // but renamed <prefix>.<facet-name>, except for the Process facet which is
                        // never added.
                        foreach (KeyValuePair <string, IObject> p in _sharedCommunicator.FindAllAdminFacets())
                        {
                            if (!p.Key.Equals("Process"))
                            {
                                _communicator.AddAdminFacet(facetNamePrefix + p.Key, p.Value);
                            }
                        }
                    }
                }

                foreach (StartServiceInfo s in servicesInfo)
                {
                    StartService(s.Name, s.EntryPoint, s.Args);
                }

                // Start Admin (if enabled) and/or deprecated IceBox.ServiceManager OA
                _communicator.AddAdminFacet("IceBox.ServiceManager", this);
                _communicator.GetAdmin();
                if (adapter != null)
                {
                    await adapter.ActivateAsync().ConfigureAwait(false);
                }

                // We may want to notify external scripts that the services have started and that IceBox is "ready".
                // This is done by defining the property IceBox.PrintServicesReady=bundleName, bundleName is whatever
                // you choose to call this set of services. It will be echoed back as "bundleName ready".
                //
                // This must be done after start() has been invoked on the services.
                if (_communicator.GetProperty("IceBox.PrintServicesReady") is string bundleName)
                {
                    Console.Out.WriteLine($"{bundleName} ready");
                }

                await _communicator.WaitForShutdownAsync().ConfigureAwait(false);
            }
            catch (ObjectDisposedException)
            {
                // Expected if the communicator or ObjectAdater are disposed
            }
            catch (Exception ex)
            {
                _logger.Error($"IceBox.ServiceManager: caught exception:\n{ex}");
                return(1);
            }
            finally
            {
                StopAll();
            }

            return(0);
        }
예제 #6
0
파일: AllTests.cs 프로젝트: xingx001/ice
            testFacets(Communicator com, bool builtInFacets, bool filtered)
            {
                if (builtInFacets && !filtered)
                {
                    test(com.FindAdminFacet("Properties").servant != null);
                    test(com.FindAdminFacet("Process").servant != null);
                    test(com.FindAdminFacet("Logger").servant != null);
                    test(com.FindAdminFacet("Metrics").servant != null);
                }

                var f1 = new TestFacetI();
                var f2 = new TestFacetI();
                var f3 = new TestFacetI();

                if (!filtered)
                {
                    com.AddAdminFacet <TestFacet, TestFacetTraits>(f1, "Facet1");
                    com.AddAdminFacet <TestFacet, TestFacetTraits>(f2, "Facet2");
                    com.AddAdminFacet <TestFacet, TestFacetTraits>(f3, "Facet3");
                }
                else
                {
                    try
                    {
                        com.AddAdminFacet <TestFacet, TestFacetTraits>(f1, "Facet1");
                        test(false);
                    }
                    catch (ArgumentException)
                    {
                    }

                    try
                    {
                        com.AddAdminFacet <TestFacet, TestFacetTraits>(f2, "Facet2");
                        test(false);
                    }
                    catch (ArgumentException)
                    {
                    }

                    try
                    {
                        com.AddAdminFacet <TestFacet, TestFacetTraits>(f3, "Facet3");
                        test(false);
                    }
                    catch (ArgumentException)
                    {
                    }
                }

                if (!filtered)
                {
                    test(com.FindAdminFacet("Facet1").servant == f1);
                    test(com.FindAdminFacet("Facet2").servant == f2);
                    test(com.FindAdminFacet("Facet3").servant == f3);
                }
                else
                {
                    test(com.FindAdminFacet("Facet1").servant == null);
                    test(com.FindAdminFacet("Facet2").servant == null);
                    test(com.FindAdminFacet("Facet3").servant == null);
                }
                test(com.FindAdminFacet("Bogus").servant == null);

                Dictionary <string, (object servant, Disp disp)> facetMap = com.FindAllAdminFacets();

                if (builtInFacets)
                {
                    test(facetMap.Count == 7);
                    test(facetMap.ContainsKey("Properties"));
                    test(facetMap.ContainsKey("Process"));
                    test(facetMap.ContainsKey("Logger"));
                    test(facetMap.ContainsKey("Metrics"));
                }
                else if (filtered)
                {
                    test(facetMap.Count >= 1);
                    test(facetMap.ContainsKey("Properties"));
                }

                if (!filtered)
                {
                    test(facetMap.ContainsKey("Facet1"));
                    test(facetMap.ContainsKey("Facet2"));
                    test(facetMap.ContainsKey("Facet3"));

                    try
                    {
                        com.AddAdminFacet <TestFacet, TestFacetTraits>(f1, "Facet1");
                        test(false);
                    }
                    catch (AlreadyRegisteredException)
                    {
                        // Expected
                    }
                }

                try
                {
                    com.RemoveAdminFacet("Bogus");
                    test(false);
                }
                catch (NotRegisteredException)
                {
                    // Expected
                }

                if (!filtered)
                {
                    com.RemoveAdminFacet("Facet1");
                    com.RemoveAdminFacet("Facet2");
                    com.RemoveAdminFacet("Facet3");
                    try
                    {
                        com.RemoveAdminFacet("Facet1");
                        test(false);
                    }
                    catch (NotRegisteredException)
                    {
                        // Expected
                    }
                }
            }