public AgentActor() { Become(new Behavior <IPeerActor <TKey, TValue> >( a => { var keys = a.AskKeys(); var peers = a.AskPeers(); // peek key out of centroid var key = CenterKey <TKey> .Calc(keys.Result()); // calc nearest peer var orderedPeers = peers.Result().OrderBy(n => n.GetPeerHashKey().ToString()); var hashKey = HashKey.ComputeHash(key.ToString()); foreach (var peer in orderedPeers) { if (hashKey.CompareTo(peer.GetPeerHashKey()) > 0) { // deposit // get current K V var result = a.GetNode(key).Result(); if (result != null) { // set current K V (peer as IPeerActor <TKey, TValue>).StoreNode(key, result); } break; } } })); }
public PeerFindPeer() : base() { this.Pattern = (s, k, i) => s == PeerOrder.PeerFindPeer; this.Apply = (s, k, i) => { var key = HashKey.ComputeHash(k.ToString()); var current = (LinkedTo as PeerBehaviors <TKey, TValue>).CurrentPeer; Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Search Key {0} in {1}", key.ToString(), current.ToString()), "PeerBehavior"); if (key.CompareTo(current) <= 0) { // Store here i.SendMessage(LinkedActor as IPeerActor <TKey, TValue>); } else { // find best peer var nextPeer = (LinkedTo as PeerBehaviors <TKey, TValue>).Peers .Where(n => n.Key.CompareTo(current) > 0).OrderBy(n => n.Key).FirstOrDefault(); if (nextPeer.Key != null) { nextPeer.Value.SendMessage(s, k, i); } else { i.SendMessage(LinkedActor as IPeerActor <TKey, TValue>); } } }; }
public static TKey Calc(IEnumerable <TKey> keys) { CheckArg.IEnumerable(keys); var dic = new Dictionary <HashKey, TKey>(); foreach (var k in keys) { dic[HashKey.ComputeHash(k.ToString())] = k; } var elected = dic.Keys.OrderBy(h => h.ToString()).FirstOrDefault(); return(dic[elected]); }