예제 #1
0
        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns>The cloned instance.</returns>
        public override object Clone()
        {
            var clone = new DsrRouteRequestMessage()
            {
                RequestId = this.RequestId,
                Nodes     = new List <string>(this.Nodes)
            };

            return(this.CopyTo(clone));
        }
예제 #2
0
        /// <summary>
        /// Adds the cached request.
        /// </summary>
        /// <param name="reqMessage">The request message.</param>
        private void AddCachedRequest(DsrRouteRequestMessage reqMessage)
        {
            var nodeCache = this.RequestCache.FirstOrDefault(r => r.Id.Equals(reqMessage.Sender));

            if (nodeCache == null)
            {
                nodeCache = new NetSimRequestCacheEntry()
                {
                    Id = reqMessage.Sender
                };

                this.RequestCache.Add(nodeCache);
            }

            nodeCache.CachedRequests.Add(reqMessage.RequestId);
        }
예제 #3
0
        /// <summary>
        /// Handles the request route caching.
        /// </summary>
        /// <param name="reqMessage">The request message.</param>
        public void HandleRequestRouteCaching(DsrRouteRequestMessage reqMessage)
        {
            // search for already cached route
            var route = (DsrTableEntry)this.GetRouteFor(reqMessage.Sender);

            // reverse the request route
            var cachedRoute = reqMessage.Nodes.Reverse <string>().ToList();

            if (route == null)
            {
                // add route to table
                this.Entries.Add(
                    new DsrTableEntry()
                {
                    Destination = reqMessage.Sender,
                    Route       = cachedRoute,
                    Metric      = cachedRoute.Count - 1
                });
            }
            else
            {
                // check if new route is shorter
                if (cachedRoute.Count < route.Metric)
                {
                    // remove route and add new one
                    this.Entries.Remove(route);

                    // add new route
                    this.Entries.Add(
                        new DsrTableEntry()
                    {
                        Destination = reqMessage.Sender,
                        Route       = cachedRoute,
                        Metric      = cachedRoute.Count - 1
                    });
                }
            }
        }
예제 #4
0
 /// <summary>
 /// Determines whether the given dsr route request message is a own request.
 /// </summary>
 /// <param name="reqMessage">The request message.</param>
 /// <returns>
 ///   <c>true</c> if is own request; otherwise, <c>false</c>.
 /// </returns>
 private bool IsOwnRequest(DsrRouteRequestMessage reqMessage)
 {
     return(reqMessage.Nodes.Count > 0 && reqMessage.Nodes[0].Equals(this.Client.Id));
 }
예제 #5
0
        private void IncomingDsrRouteRequestMessageHandler(NetSimMessage message)
        {
            DsrTable dsrTable = (DsrTable)this.Table;
            DsrRouteRequestMessage reqMessage = (DsrRouteRequestMessage)message;

            // if duplicate
            if (this.HasCachedRequest(reqMessage))
            {
                // ignore message and proceed
                return;
            }

            // add request to cache
            this.AddCachedRequest(reqMessage);

            // add this node id to message Route
            reqMessage.Nodes.Add(this.Client.Id);

            // if this node was sender of request - ignore
            if (this.IsOwnRequest(reqMessage))
            {
                return;
            }

            // cache route
            dsrTable.HandleRequestRouteCaching(reqMessage);

            // check if message destination is current node (me)
            if (reqMessage.Receiver.Equals(this.Client.Id))
            {
                // send back rrep mesage the reverse way with found route
                var response = new DsrRouteReplyMessage()
                {
                    Receiver = reqMessage.Sender,
                    Sender   = this.Client.Id,
                    Route    = new List <string>(reqMessage.Nodes)
                };

                // enqueue message for sending
                this.SendMessage(response);

                return;
            }
            else
            {
                // Check if route to the end destination for request is cached
                var route = this.Table.GetRouteFor(reqMessage.Receiver);

                if (route != null)
                {
                    var dsrRoute = (DsrTableEntry)route;

                    var newRoute = new List <string>(reqMessage.Nodes);

                    // remove last entry
                    newRoute.RemoveAt(newRoute.Count - 1);

                    // add cached route entries
                    newRoute.AddRange(dsrRoute.Route);

                    // send back rrep mesage the reverse way with found route
                    // note: sender is the orig. receiver of the req
                    var response = new DsrRouteReplyMessage()
                    {
                        Receiver = reqMessage.Sender,
                        Sender   = reqMessage.Receiver,
                        Route    = newRoute
                    };

                    // enqueue message for sending
                    this.SendMessage(response);

                    return;
                }
            }

            // forward message to outgoing messages
            this.SendMessage(reqMessage);
        }
예제 #6
0
 /// <summary>
 /// Determines whether this protocol instance has cached request the specified request id.
 /// </summary>
 /// <param name="reqMessaged">The request messaged.</param>
 /// <returns>
 ///   <c>true</c> if has cached request with the specified request message; otherwise, <c>false</c>.
 /// </returns>
 private bool HasCachedRequest(DsrRouteRequestMessage reqMessaged)
 {
     return
         (this.RequestCache
          .FirstOrDefault(r => r.Id.Equals(reqMessaged.Sender))?.HasCachedRequest(reqMessaged.RequestId) ?? false);
 }