Beispiel #1
0
        /// <summary>
        /// Pings other node
        /// </summary>
        /// <param name="node">Node to ping</param>
        /// <returns></returns>
        public NodeMessage Ping(KadContactNode node)
        {
            NodeMessage msg = new NodeMessage.Builder()
                              .SetType(MessageType.Ping)
                              .SetOriginator(node.Id)
                              .Build();

            msg.AddCallback(UpdateLastSeen);
            return(_server.SendMessageSync(node.EndPoint, msg, TimeoutInMSec).Response);
        }
Beispiel #2
0
        /// <summary>
        /// Checks if other node can store value
        /// </summary>
        /// <param name="node">Node to check</param>
        /// <returns>True if node can store value</returns>
        public bool CanStoreValue(KadContactNode node)
        {
            NodeMessage canStoreValueMsg = new NodeMessage.Builder()
                                           .SetType(MessageType.CanStoreValue)
                                           .SetOriginator(Id)
                                           .SetIsRequest(true)
                                           .Build();

            canStoreValueMsg.AddCallback(UpdateLastSeen);
            NodeMessage response = _server.SendMessageSync(node.EndPoint, canStoreValueMsg, TimeoutInMSec).Response;

            return(response != null && response.Payload[0] > 0);
        }
Beispiel #3
0
        /// <summary>
        /// Retrives value from other node
        /// </summary>
        /// <param name="node">Node to query</param>
        /// <param name="valId">Value id</param>
        /// <returns>Value if queried node has value, otherwise closest nodes to id</returns>
        public NodeMessage FindValue(KadContactNode node, KadId valId)
        {
            NodeMessage findValMsg = new NodeMessage.Builder()
                                     .SetType(MessageType.FindValue)
                                     .SetOriginator(Id)
                                     .SetIsRequest(true)
                                     .SetPayload(valId.Value)
                                     .Build();

            findValMsg.AddCallback(UpdateLastSeen);

            return(_server.SendMessageSync(node.EndPoint, findValMsg, TimeoutInMSec).Response);
        }
Beispiel #4
0
        /// <summary>
        /// Store value in contact
        /// </summary>
        /// <param name="node">Contact node</param>
        /// <param name="value">Value to store</param>
        /// <returns>true, if node stored value</returns>
        public bool StoreValue(KadContactNode node, KadValue value)     //TODO: support chunking
        {
            NodeMessage storeValueMsg = new NodeMessage.Builder()
                                        .SetType(MessageType.StoreValue)
                                        .SetOriginator(Id)
                                        .SetIsRequest(true)
                                        .SetKadValueAsPayload(value)
                                        .Build();

            storeValueMsg.AddCallback(UpdateLastSeen);
            var response = _server.SendMessageSync(node.EndPoint, storeValueMsg, TimeoutInMSec).Response;

            return(response != null && response.Type == MessageType.CanStoreValue && response.Payload[0] > 0);
        }
Beispiel #5
0
        /// <summary>
        /// Find contact's closest nodes to id
        /// </summary>
        /// <param name="node">Node to query</param>
        /// <param name="id">Id to compare</param>
        /// <returns>Closest nodes to id</returns>
        public IEnumerable <KadContactNode> FindNode(KadContactNode node, KadId id)
        {
            NodeMessage msg = new NodeMessage.Builder()
                              .SetType(MessageType.FindNode)
                              .SetOriginator(Id)
                              .SetIsRequest(true)
                              .SetPayload(id.Value)
                              .Build();

            NodeMessage response = _server.SendMessageSync(node.EndPoint, msg, TimeoutInMSec).Response;

            msg.AddCallback(UpdateLastSeen);
            if (response != null)
            {
                return(new List <KadContactNode>(response.Contacts));
            }

            return(new List <KadContactNode>());
        }