private static void Main(string[] args)
{
var commonConfig = ConfigurationFactory.ParseString(@"
akka {
actor {
provider = ""Akka.Cluster.ClusterActorRefProvider, Akka.Cluster""
serializers {
wire = ""Akka.Serialization.WireSerializer, Akka.Serialization.Wire""
}
serialization-bindings {
""System.Object"" = wire
}
}
remote {
helios.tcp {
hostname = ""127.0.0.1""
}
}
cluster {
seed-nodes = [""akka.tcp://[email protected]:3001""]
auto-down-unreachable-after = 30s
}
}");
var tablePort = 3000;
var containerPort = 4000;
var standAlone = args.Length > 0 && args[0] == "standalone";
if (standAlone)
{
CreateClusterNode(commonConfig, ++tablePort, "table", "container");
}
else
{
CreateClusterNode(commonConfig, ++tablePort, "table");
CreateClusterNode(commonConfig, ++containerPort, "container");
CreateClusterNode(commonConfig, ++containerPort, "container");
}
// wait for stop signal
Action printUsage = () =>
{
Console.WriteLine("Press command:");
Console.WriteLine("[usage]");
Console.WriteLine(" c [data] create actor");
Console.WriteLine(" g [id] get actor with id");
Console.WriteLine(" d [id] delete actor with id");
Console.WriteLine(" i get all actors' id");
Console.WriteLine(" cc [role] create node (t: table, c: table-container)");
Console.WriteLine(" ck [role] kill node (t: table, c: table-container)");
Console.WriteLine(" q quit");
};
printUsage();
while (true)
{
var line = Console.ReadLine().ToLower();
if (line == "q")
{
break;
}
else if (line == "c" || line.StartsWith("c "))
{
var table = GetTable();
if (table == null)
{
Console.WriteLine("No Table");
return;
}
long id = 0;
if (line.Length > 2)
long.TryParse(line.Substring(2), out id);
var reply = table.Ask<Message.CreateReply>(
new Message.Create(id, new object[] { DateTime.Now.ToString() })).Result;
Console.WriteLine($"Result ID: {reply.Id} {reply.Actor?.Path}");
}
else if (line.StartsWith("g "))
{
var table = GetTable();
if (table == null)
{
Console.WriteLine("No Table");
return;
}
long id;
long.TryParse(line.Substring(2), out id);
var reply = table.Ask<Message.GetReply>(new Message.Get(id)).Result;
if (reply.Actor != null)
Console.WriteLine($"Result Actor: {reply.Actor.Path}");
else
Console.WriteLine($"No actor");
}
else if (line.StartsWith("d "))
{
var table = GetTable();
if (table == null)
{
Console.WriteLine("No Table");
return;
}
long id;
long.TryParse(line.Substring(2), out id);
var reply = table.Ask<Message.GetReply>(new Message.Get(id)).Result;
if (reply.Actor != null)
{
Console.WriteLine($"Try to delete actor: {reply.Actor.Path}");
reply.Actor.Tell(PoisonPill.Instance);
}
else
{
Console.WriteLine($"No actor");
}
}
else if (line == "i")
{
var table = GetTable();
if (table == null)
{
Console.WriteLine("No Table");
return;
}
var reply = table.Ask<Message.GetIdsReply>(new Message.GetIds()).Result;
Console.WriteLine($"Actors: {string.Join(" ", reply.Ids.Select(x => x.ToString()))}");
}
else if (line.StartsWith("cc "))
{
var name = line.Substring(2).Trim();
switch (name)
{
case "t":
CreateClusterNode(commonConfig, ++tablePort, "table");
break;
case "c":
CreateClusterNode(commonConfig, ++containerPort, "container");
break;
default:
Console.WriteLine("Unknown: " + name);
break;
}
}
else if (line.StartsWith("ck "))
{
var name = line.Substring(2).Trim();
switch (name)
{
case "t":
KillOneClusterNode("table");
break;
case "c":
KillOneClusterNode("container");
break;
default:
Console.WriteLine("Unknown: " + name);
break;
}
}
else
{
printUsage();
}
}
// stop
Console.WriteLine("Stopping table");
foreach (var i in _clusterNodes)
{
if (i.Roles.Any(r => r == "table"))
{
i.RootActors[0].GracefulStop(TimeSpan.FromMinutes(1),
new Message.GracefulStop(null)).Wait();
}
}
Console.WriteLine("Table stoped");
// shutdown
_clusterNodes.Reverse();
foreach (var cluster in _clusterNodes)
{
cluster.Context.System.Terminate();
}
}