Ejemplo n.º 1
0
        static void Register(PatternActionsRegistry <XmlMessage, XmlMessagePattern> registry)
        {
            registry.AddAction(
                new XmlMessagePattern(@"/ping"),
                (message, sender, self, resource, ms, logger) =>
            {
                // Pretend to use the lazy resource
                var res = resource.Value;
                logger.Info(@"Received {0}", message);
                dynamic xml = message.AsDynamic();
                if (xml.ping == "crash")
                {
                    throw new Exception("Crash requested");
                }
                sender.Tell(XmlMessage.FromString($@"
                        <pong>
                            <ping>{xml.ping}</ping>
                            <node>{self.Path}</node>
                        </pong>"), self);
            },
                "actor1");

            registry.SetProcessingUnitResourceFactory(
                "actor1",
                logger => new Lazy <IDisposable>(() => new FakeResource(logger))
                );
        }
Ejemplo n.º 2
0
        protected override void EndProcessing()
        {
            PatternActionsRegistry <XmlMessage, XmlMessagePattern> registry = new PatternActionsRegistry <XmlMessage, XmlMessagePattern>();

            if (MessagePatterns != null && MessagePatterns.Any())
            {
                foreach (var messagePattern in MessagePatterns)
                {
                    string processingUnitId = (string)messagePattern.Properties["Id"].Value;
                    var    pattern          = ((object[])messagePattern.Properties["Pattern"].Value).Select(o => (string)o).ToArray();
                    var    script           = (ScriptBlock)messagePattern.Properties["Script"].Value;
                    var    sec               = messagePattern.Properties["Secondary"];
                    bool   secondary         = sec != null ? (bool)sec.Value : false;
                    var    xmlMessagePattern = new XmlMessagePattern(secondary, pattern);
                    registry.AddPowershellScript(xmlMessagePattern, script, processingUnitId);
                }
            }
            if (Resources != null && Resources.Any())
            {
                foreach (var resource in Resources)
                {
                    string      processingUnitId    = (string)resource.Properties["Id"].Value;
                    ScriptBlock resourceConstructor = (ScriptBlock)resource.Properties["Constructor"].Value;
                    ScriptBlock resourceDestructor  = (ScriptBlock)resource.Properties["Destructor"]?.Value;
                    registry.SetProcessingUnitResourceFactory(
                        processingUnitId,
                        logger => new Lazy <IDisposable>(() =>
                    {
                        var outputs = new PowershellScriptExecutor(logger).Execute(resourceConstructor.ToString());
                        if (outputs == null || !outputs.Any())
                        {
                            return(null);
                        }
                        System.Collections.Hashtable resources = outputs.First() as System.Collections.Hashtable;
                        if (resources == null)
                        {
                            return(null);
                        }
                        return(new PSResources(resources, logger, resourceDestructor));
                    })
                        );
                }
            }
            var config = Hexagon.NodeConfig.FromFile <AkkaNodeConfig>(NodeConfig);

            using (MessageSystem <XmlMessage, XmlMessagePattern> xmlMessageSystem = CreateSystem(config))
            {
                xmlMessageSystem.Start(config, registry);
                Console.WriteLine("Press Control-C to exit, Enter to clear screen.");
                Console.CancelKeyPress += (sender, e) =>
                {
                    _quitEvent.Set();
                    e.Cancel = true;
                };
                bool exit = false;
                do
                {
                    if (Console.KeyAvailable)
                    {
                        var key = Console.ReadKey(true);
                        if (key.Key == ConsoleKey.Enter)
                        {
                            Console.Clear();
                        }
                    }
                    exit = _quitEvent.WaitOne(100);
                } while (!exit);
            }
        }