|
| 1 | +// Copyright 2014-2015 Aaron Stannard, Petabridge LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 4 | +// this file except in compliance with the License. You may obtain a copy of the |
| 5 | +// License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software distributed |
| 10 | +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 11 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 12 | +// specific language governing permissions and limitations under the License. |
| 13 | + |
| 14 | +using System.Linq; |
| 15 | +using Akka.Actor; |
| 16 | +using Akka.Configuration; |
| 17 | +using ConfigurationException = Akka.Configuration.ConfigurationException; |
| 18 | + |
| 19 | +namespace Lighthouse.NetCoreApp |
| 20 | +{ |
| 21 | + /// <summary> |
| 22 | + /// Launcher for the Lighthouse <see cref="ActorSystem"/> |
| 23 | + /// </summary> |
| 24 | + public static class LighthouseHostFactory |
| 25 | + { |
| 26 | + public static ActorSystem LaunchLighthouse(string ipAddress = null, int? specifiedPort = null) |
| 27 | + { |
| 28 | + var systemName = "lighthouse"; |
| 29 | + var clusterConfig = GetConfig(); |
| 30 | + |
| 31 | + var lighthouseConfig = clusterConfig.GetConfig("lighthouse"); |
| 32 | + if (lighthouseConfig != null) |
| 33 | + { |
| 34 | + systemName = lighthouseConfig.GetString("actorsystem", systemName); |
| 35 | + } |
| 36 | + |
| 37 | + var remoteConfig = clusterConfig.GetConfig("akka.remote"); |
| 38 | + ipAddress = ipAddress ?? |
| 39 | + remoteConfig.GetString("dot-netty.tcp.public-hostname") ?? |
| 40 | + "127.0.0.1"; //localhost as a final default |
| 41 | + int port = specifiedPort ?? remoteConfig.GetInt("dot-netty.tcp.port"); |
| 42 | + |
| 43 | + if (port == 0) throw new ConfigurationException("Need to specify an explicit port for Lighthouse. Found an undefined port or a port value of 0 in App.config."); |
| 44 | + |
| 45 | + var selfAddress = string.Format("akka.tcp://{0}@{1}:{2}", systemName, ipAddress, port); |
| 46 | + var seeds = clusterConfig.GetStringList("akka.cluster.seed-nodes"); |
| 47 | + if (!seeds.Contains(selfAddress)) |
| 48 | + { |
| 49 | + seeds.Add(selfAddress); |
| 50 | + } |
| 51 | + |
| 52 | + var injectedClusterConfigString = seeds.Aggregate("akka.cluster.seed-nodes = [", (current, seed) => current + (@"""" + seed + @""", ")); |
| 53 | + injectedClusterConfigString += "]"; |
| 54 | + |
| 55 | + var finalConfig = ConfigurationFactory.ParseString( |
| 56 | + string.Format(@"akka.remote.dot-netty.tcp.public-hostname = {0} |
| 57 | +akka.remote.dot-netty.tcp.port = {1}", ipAddress, port)) |
| 58 | + .WithFallback(ConfigurationFactory.ParseString(injectedClusterConfigString)) |
| 59 | + .WithFallback(clusterConfig); |
| 60 | + |
| 61 | + return ActorSystem.Create(systemName, finalConfig); |
| 62 | + } |
| 63 | + |
| 64 | + private static Config GetConfig() |
| 65 | + { |
| 66 | + var configString = @" |
| 67 | + lighthouse{ |
| 68 | + actorsystem: ""webcrawler"" #POPULATE NAME OF YOUR ACTOR SYSTEM HERE |
| 69 | + } |
| 70 | + |
| 71 | + akka { |
| 72 | + actor { |
| 73 | + provider = ""Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"" |
| 74 | + } |
| 75 | + |
| 76 | + remote { |
| 77 | + log-remote-lifecycle-events = DEBUG |
| 78 | + dot-netty.tcp { |
| 79 | + transport-class = ""Akka.Remote.Transport.DotNetty.TcpTransport, Akka.Remote"" |
| 80 | + applied-adapters = [] |
| 81 | + transport-protocol = tcp |
| 82 | + #will be populated with a dynamic host-name at runtime if left uncommented |
| 83 | + #public-hostname = ""POPULATE STATIC IP HERE"" |
| 84 | + hostname = ""0.0.0.0"" |
| 85 | + port = 4053 |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + cluster { |
| 90 | + #will inject this node as a self-seed node at run-time |
| 91 | + seed-nodes = [] #manually populate other seed nodes here, i.e. ""akka.tcp://[email protected]:4053"", ""akka.tcp://[email protected]:4044"" |
| 92 | + roles = [lighthouse] |
| 93 | + } |
| 94 | + } |
| 95 | + "; |
| 96 | + return ConfigurationFactory.ParseString(configString); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments