Пример #1
0
        void XmlMessageSystem_must_work()
        {
            Within(TimeSpan.FromSeconds(30), () =>
            {
                RunOn(() =>
                {
                    _registry.AddAsyncAction(
                        new XmlMessagePattern($@"/request[@routeto = ""{_first.Name}""]"),
                        async(message, sender, self, resource, messageSystem, logger) =>
                    {
                        XmlMessage answer = await messageSystem.SendMessageAndAwaitResponseAsync(XmlMessage.FromString(@"<question>Why?</question>"), self);
                        answer.Should().Match <XmlMessage>(mess => mess.Match(@"/answer[. = ""Because.""]"));
                        TestActor.Tell("OK");
                    },
                        _first.Name);
                }, _first);

                RunOn(() =>
                {
                    _registry.AddAction(
                        new XmlMessagePattern(true, @"/request"),
                        (message, sender, self, resource, messageSystem, logger) =>
                    {
                        TestActor.Tell("OK");
                    },
                        _second.Name);
                    _registry.AddPowershellScriptBody(
                        new XmlMessagePattern(@"/question[. = ""Why?""]"),
                        @"'<answer>Because.</answer>'",
                        _second.Name
                        );
                }, _second);

                _messageSystem.Start(_nodeConfig, _registry);
                EnterBarrier("2-started");

                RunOn(() =>
                {
                    string xml = @"<request routeto=""first"">GO</request>";
                    _messageSystem.SendMessage(XmlMessage.FromString(xml), null);
                }, _third);
                RunOn(() => ExpectMsg <string>("OK"), _first);
                RunOn(() => ExpectMsg <string>("OK"), _second);

                EnterBarrier("3-done");
            });
        }
Пример #2
0
 static HttpRequestEventHandler ConverterToHttpRequestEventHandler(
     RestRequestConvertersRegistry <XmlMessage> registry,
     AkkaMessageSystem <XmlMessage, XmlMessagePattern> system,
     TimeSpan requestTimeout)
 => (s, e) =>
 {
     try
     {
         RestRequest restRequest     = HttpToRestRequest(e.Request);
         var         converter       = registry.GetMatchingConverter(restRequest);
         var         matchingMessage = converter?.ConvertFromRequest(restRequest);
         if (matchingMessage.HasValue)
         {
             if (matchingMessage.Value.expectResponse)
             {
                 var response = system.SendMessageAndAwaitResponse(matchingMessage.Value.message, null, requestTimeout);
                 using (var writer = new StreamWriter(e.Response.OutputStream))
                 {
                     if (response != null)
                     {
                         var responseJson = converter.ConvertToResponse(response);
                         writer.Write(responseJson.ToString());
                         e.Response.ContentType = "application/json";
                     }
                     else
                     {
                         writer.Write(@"Cannot route message {0}", matchingMessage.Value.message);
                         e.Response.ContentType = "text/plain";
                         e.Response.StatusCode  = 412;
                     }
                 }
             }
             else
             {
                 system.SendMessage(matchingMessage.Value.message, null);
             }
         }
         else
         {
             using (var writer = new StreamWriter(e.Response.OutputStream))
             {
                 writer.Write(@"The request does not match any registered handler");
             }
             e.Response.ContentType = "text/plain";
             e.Response.StatusCode  = 404;
         }
     }
     catch (JsonReaderException ex)
     {
         using (var writer = new StreamWriter(e.Response.OutputStream))
         {
             writer.Write(@"JSON is not valid : {0}", ex.Message);
         }
         e.Response.ContentType = "text/plain";
         e.Response.StatusCode  = 400;
     }
     catch (Exception ex)
     {
         using (var writer = new StreamWriter(e.Response.OutputStream))
         {
             writer.Write(@"Error : {0}", ex.Message);
         }
         e.Response.ContentType = "text/plain";
         e.Response.StatusCode  = 500;
     }
 };