Exemplo n.º 1
0
        internal void Store(KademliaId id, KyruObjectMetadata[] newMetadata)
        {
            lock (storage)
            {
                if (!storage.ContainsKey(id))
                {
                    storage[id] = new List<KyruObjectMetadata>();
                }
                var metadata = storage[id];

                foreach (var newItem in newMetadata)
                {
                    // make sure the timestamp isn't in the future
                    newItem.Timestamp = Math.Min(DateTime.Now.UnixTimestamp(), newItem.Timestamp);

                    var item = metadata.FirstOrDefault(m => m.IpAddress == newItem.IpAddress && m.NodeId == newItem.NodeId);

                    if (item == null)
                    {
                        VerifyOwnership(id, newItem);
                        metadata.Add(newItem);
                    }
                    else
                    {
                        item.Timestamp = Math.Max(item.Timestamp, newItem.Timestamp);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void VerifyOwnership(KademliaId objectId, KyruObjectMetadata newItem)
        {
            var message = new UdpMessage();
            message.KeepObjectRequest = new KeepObjectRequest();
            message.KeepObjectRequest.ObjectId = objectId;
            message.ResponseCallback = delegate(UdpMessage udpMessage)
                                       {
                                           if (!udpMessage.KeepObjectResponse.HasObject) return;

                                           lock (storage)
                                           {
                                               if (!storage.ContainsKey(objectId))
                                                   storage[objectId] = new List<KyruObjectMetadata>();

                                               newItem.Timestamp = DateTime.Now.UnixTimestamp();
                                               storage[objectId].Add(newItem);
                                           }
                                       };
            node.SendUdpMessage(message, new IPEndPoint(newItem.IpAddress, newItem.Port), newItem.NodeId);
        }