Created
March 1, 2015 15:10
-
-
Save ralfw/db45e634e699e1f936e5 to your computer and use it in GitHub Desktop.
ToDictionary mit Akka.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Threading; | |
using Akka; | |
using Akka.Actor; | |
namespace todict | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
using (var akka = ActorSystem.Create ("sys")) { | |
var result = Inbox.Create (akka); | |
var todict = akka.ActorOf (Props.Create (() => new ToDictionary (result.Receiver))); | |
while (true) { | |
Console.Write ("config: "); | |
var config = Console.ReadLine (); | |
if (config == "") break; | |
todict.Tell (config); | |
var dict = result.Receive () as Dictionary<string,string>; | |
foreach (var k in dict.Keys) { | |
Console.WriteLine (" {0}:{1}", k, dict[k]); | |
} | |
} | |
} | |
} | |
} | |
class ToDictionary : ReceiveActor { | |
public ToDictionary(ActorRef onResult) { | |
var builddict = Context.ActorOf (Props.Create (() => new BuildDict(onResult))); | |
var splitassign = Context.ActorOf (Props.Create(() => new SplitAssignments(builddict))); | |
var splitcfg = Context.ActorOf (Props.Create (() => new SplitConfig (splitassign))); | |
Receive<string> (config => splitcfg.Tell(config)); | |
} | |
} | |
class SplitConfig : TypedActor, IHandle<string> { | |
#region IHandle implementation | |
public void Handle (string message) | |
{ | |
var parts = message.Split (';'); | |
onAssignments.Tell (parts); | |
} | |
#endregion | |
ActorRef onAssignments; | |
public SplitConfig(ActorRef onAssignments) { | |
this.onAssignments = onAssignments; | |
} | |
} | |
class SplitAssignments : TypedActor, IHandle<string[]> { | |
#region IHandle implementation | |
public void Handle (string[] assignments) | |
{ | |
var keyValues = assignments.Select (a => a.Split ('=')); | |
onKeyValues.Tell (keyValues); | |
} | |
#endregion | |
ActorRef onKeyValues; | |
public SplitAssignments(ActorRef onKeyValues) { | |
this.onKeyValues = onKeyValues; | |
} | |
} | |
class BuildDict : TypedActor, IHandle<IEnumerable<string[]>> { | |
#region IHandle implementation | |
public void Handle (IEnumerable<string[]> keyValues) | |
{ | |
var dict = keyValues.Aggregate(new Dictionary<string,string>(), | |
(result, kv) => { | |
result[kv[0]] = kv[1]; | |
return result; | |
}); | |
onDict.Tell (dict); | |
} | |
#endregion | |
ActorRef onDict; | |
public BuildDict(ActorRef onDict) { | |
this.onDict = onDict; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment