static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Title = $"{NamesRegistry.Router}:{Ports.Router}"; Config config = AkkaDistributedHelper.GetAkkaSettings(); string routerAddress = $"{AkkaDistributedHelper.GetFullyQualifiedDomainName()}:{Ports.Router}"; config = config .WithFallback(string.Format("akka.remote.dot-netty.tcp.hostname = \"{0}\"", AkkaDistributedHelper.GetFullyQualifiedDomainName())) .WithFallback(string.Format("akka.remote.dot-netty.tcp.port = {0}", Ports.Router)) .WithFallback(string.Format("akka.cluster.roles = [\"{0}\"]", NamesRegistry.Router)) .WithFallback(string.Format("akka.cluster.seed-nodes = [\"{1}://App-Cluster@{0}\"]", routerAddress, AkkaDistributedHelper.TcpProtocol)); MyActorSystem = ActorSystem.Create(NamesRegistry.Cluster, config); Props props = Props.Create <RouterActor>(MyActorSystem); var routerActor = MyActorSystem.ActorOf(props, NamesRegistry.Router); ClusterClientReceptionist.Get(MyActorSystem).RegisterService(routerActor); Console.WriteLine("Router running..."); // This blocks the current thread from exiting until MyActorSystem is shut down // The ConsoleReaderActor will shut down the ActorSystem once it receives an // "exit" command from the user MyActorSystem.WhenTerminated.Wait(); Console.WriteLine("Press any key..."); Console.ReadKey(); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.Title = $"{NamesRegistry.Server}:{Ports.Server}"; Config config = AkkaDistributedHelper.GetAkkaSettings(); string routerAddress = $"{AkkaDistributedHelper.GetFullyQualifiedDomainName()}:{Ports.Router}"; config = config .WithFallback(string.Format("akka.remote.dot-netty.tcp.hostname = \"{0}\"", AkkaDistributedHelper.GetFullyQualifiedDomainName())) .WithFallback(string.Format("akka.remote.dot-netty.tcp.port = {0}", Ports.Server)) .WithFallback(string.Format("akka.cluster.roles = [\"{0}\"]", NamesRegistry.Server)) .WithFallback(string.Format("akka.cluster.seed-nodes = [\"{1}://App-Cluster@{0}\"]", routerAddress, AkkaDistributedHelper.TcpProtocol)); MyActorSystem = ActorSystem.Create(NamesRegistry.Cluster, config); CreateProgressPublisher(); CreateEvaluatorActor(); // This blocks the current thread from exiting until MyActorSystem is shut down // The ConsoleReaderActor will shut down the ActorSystem once it receives an // "exit" command from the user MyActorSystem.WhenTerminated.Wait(); }
private static void InitializeProgressSubscription() { string receptionistActorPath = string.Format("{3}://App-Cluster@{0}:{1}/system/{2}", AkkaDistributedHelper.GetFullyQualifiedDomainName(), Ports.Router, "receptionist", TcpProtocol); ImmutableHashSet <ActorPath> initialContacts = ImmutableHashSet.Create(ActorPath.Parse(receptionistActorPath)); var settings = ClusterClientSettings.Create(MyActorSystem).WithInitialContacts(initialContacts); clusterClient = MyActorSystem.ActorOf(ClusterClient.Props(settings), "Client"); subscriber = MyActorSystem.ActorOf(Props.Create <ProgressSubscriberActor>(), NamesRegistry.ProgressSubscriber); Timer progressTimer = new Timer { Interval = 500 }; progressTimer.Enabled = true; progressTimer.Elapsed += (sender, args) => { object response = clusterClient.Ask(new ClusterClient.Publish(NamesRegistry.ProgressTopic, new ProgressPublisherActor.ProgressUpdateRequest())).Result; subscriber.Tell(response); }; Console.WriteLine($"Created Subscriber for progress updates: {clusterClient.Path} on {MyActorSystem.Name}"); }
static void Main(string[] args) { Console.Title = $"Client:{Ports.Client}"; Console.ForegroundColor = ConsoleColor.DarkMagenta; Config config = AkkaDistributedHelper.GetAkkaSettings(); string routerAddress = $"{AkkaDistributedHelper.GetFullyQualifiedDomainName()}:{Ports.Client}"; config = config .WithFallback(string.Format("akka.remote.dot-netty.tcp.hostname = \"{0}\"", AkkaDistributedHelper.GetFullyQualifiedDomainName())) .WithFallback(string.Format("akka.remote.dot-netty.tcp.port = {0}", Ports.Client)) .WithFallback(string.Format("akka.cluster.roles = [\"{0}\"]", NamesRegistry.Client)) .WithFallback(string.Format("akka.cluster.seed-nodes = [\"{1}://App-Client@{0}\"]", routerAddress, AkkaDistributedHelper.TcpProtocol)); MyActorSystem = ActorSystem.Create("App-Client"); bool isClusterClientInitialized = false; string routerActorPath = string.Format("{3}://App-Cluster@{0}:{1}/user/{2}", AkkaDistributedHelper.GetFullyQualifiedDomainName(), Ports.Router, NamesRegistry.Router, TcpProtocol); while (true) { Console.WriteLine("Press any key to trigger evaluation, q to exit"); if (Console.ReadKey().KeyChar != 'q') { if (!isClusterClientInitialized) { InitializeProgressSubscription(); isClusterClientInitialized = true; } MyActorSystem.ActorSelection(routerActorPath).ResolveOne(TimeSpan.FromSeconds(30)).Result.Tell(new RouterActor.EvaluationRequested()); } else { break; } } IActorRef router = MyActorSystem.ActorSelection(routerActorPath).ResolveOne(TimeSpan.FromSeconds(30)).Result; router.Tell(new RouterActor.ShutdownRequested()); Console.WriteLine(); Console.WriteLine("Exiting, sent shutdown..."); }
public RouterActor(ActorSystem actorSystem) { _actorSystem = actorSystem; Receive <EvaluationRequested>(s => { IActorRef server; string serverActorPath = string.Format("{3}://App-Cluster@{0}:{1}/user/{2}", AkkaDistributedHelper.GetFullyQualifiedDomainName(), Ports.Server, NamesRegistry.Server, AkkaDistributedHelper.TcpProtocol); if (AkkaDistributedHelper.ActorExists(_actorSystem, serverActorPath, out server)) { Console.WriteLine("Sending evaluation to server..."); server.Tell(new ServerActor.JobRequested(), Self); } else { Console.WriteLine("No server found, retry..."); } }); Receive <ShutdownRequested>(s => { Console.WriteLine("Received shutdown, exiting!"); // shut down the entire actor system via the ActorContext // causes MyActorSystem.AwaitTermination(); to stop blocking the current thread // and allows the application to exit. Context.System.Terminate(); }); Receive <ProgressPublisherActor.ProgressUpdate>(s => { Console.WriteLine($"Received progress update, {s.Progress}"); // shut down the entire actor system via the ActorContext // causes MyActorSystem.AwaitTermination(); to stop blocking the current thread // and allows the application to exit. Context.System.Terminate(); }); }
static void Main(string[] args) { Console.Title = "Client - No Port"; Console.ForegroundColor = ConsoleColor.DarkMagenta; MyActorSystem = ActorSystem.Create("App-Client"); bool isClusterClientInitialized = false; string routerActorPath = string.Format("{3}://App-Cluster@{0}:{1}/user/{2}", AkkaDistributedHelper.GetFullyQualifiedDomainName(), Ports.Router, NamesRegistry.Router, TcpProtocol); string progressActorPath = string.Format("{3}://App-Cluster@{0}:{1}/user/{2}", AkkaDistributedHelper.GetFullyQualifiedDomainName(), Ports.Server, NamesRegistry.ProgressPublisher, TcpProtocol); while (true) { Console.WriteLine("Press any key to trigger evaluation, q to exit"); if (Console.ReadKey().KeyChar != 'q') { if (!isClusterClientInitialized) { IActorRef progressPublisher = MyActorSystem.ActorSelection(progressActorPath).ResolveOne(TimeSpan.FromSeconds(30)).Result; int delay = 100; var cancellationTokenSource = new CancellationTokenSource(); var token = cancellationTokenSource.Token; var listener = Task.Factory.StartNew(async() => { while (true) { try { var progressUpdate = (ProgressPublisherActor.ProgressUpdate) await progressPublisher.Ask( new ProgressPublisherActor.ProgressUpdateRequest()); Console.WriteLine($"Current Status: {progressUpdate.Progress}"); Thread.Sleep(delay); if (token.IsCancellationRequested) { break; } } catch (Exception e) { Console.WriteLine(e); } } // cleanup, e.g. close connection }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default); isClusterClientInitialized = true; } MyActorSystem.ActorSelection(routerActorPath).ResolveOne(TimeSpan.FromSeconds(30)).Result.Tell(new RouterActor.EvaluationRequested()); } else { break; } } Console.WriteLine(); Console.WriteLine("Exiting, sent shutdown..."); }