private void Join(RoleName from, RoleName to) { RunOn(() => { Cluster.Join(Node(to).Address); _nodeConfig = new AkkaNodeConfig(from.Name) { GossipTimeFrameInSeconds = 5 }; _messageSystem = AkkaXmlMessageSystem.Create(this.Sys); }, from); EnterBarrier(from.Name + "-joined"); }
static void StartRestServer( AkkaMessageSystem <XmlMessage, XmlMessagePattern> system, HttpServer restServer, int port, TimeSpan requestTimeout, RestRequestConvertersRegistry <XmlMessage> registry) { restServer.RequestReceived += ConverterToHttpRequestEventHandler(registry, system, requestTimeout); if (port != 0) { restServer.EndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, port); } restServer.Start(); Console.WriteLine(@"Rest server started and ready on endpoint : {0}", restServer.EndPoint); }
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; } };