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

            return(this.CopyTo(clone));
        }
예제 #2
0
        /// <summary>
        /// Handles the response.
        /// </summary>
        /// <param name="message">The message.</param>
        public void HandleResponse(DsrRouteReplyMessage message)
        {
            // search for a route for this destination
            var entry = this.GetRouteFor(message.Sender);

            // if no route found or the metric of the found route is bigger
            if (entry == null || entry.Metric > message.Route.Count - 1)
            {
                this.Entries.Add(
                    new DsrTableEntry()
                {
                    Destination = message.Sender,
                    Metric      = message.Route.Count - 1,
                    Route       = new List <string>(message.Route)
                });
            }
        }
예제 #3
0
        /// <summary>
        /// Handles the request route caching.
        /// </summary>
        /// <param name="repMessage">The rep message.</param>
        /// <param name="clientId">The client identifier.</param>
        public void HandleReplyRouteCaching(DsrRouteReplyMessage repMessage, string clientId)
        {
            // search for already cached route
            var route = (DsrTableEntry)this.GetRouteFor(repMessage.Sender);

            // skip the route until the own id is found in it - the rest is the path to the endpointFrom
            var cachedRoute = repMessage.Route.SkipWhile(e => !e.Equals(clientId)).ToList();

            // if route is empty drop
            if (cachedRoute.Count == 0)
            {
                return;
            }

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

                    // add new route
                    this.Entries.Add(
                        new DsrTableEntry()
                    {
                        Destination = repMessage.Sender,
                        Route       = cachedRoute,
                        Metric      = cachedRoute.Count - 1
                    });
                }
            }
        }
예제 #4
0
        private void IncomingDsrRouteReplyMessageHandler(NetSimMessage message)
        {
            DsrTable             dsrTable   = this.Table as DsrTable;
            DsrRouteReplyMessage repMessage = (DsrRouteReplyMessage)message;

            // handle route caching
            dsrTable?.HandleReplyRouteCaching(repMessage, this.Client.Id);

            // check if the respone is for this node
            if (repMessage.Receiver.Equals(this.Client.Id))
            {
                // save found route to table
                dsrTable?.HandleResponse(repMessage);
            }
            else
            {
                // forward message
                this.SendMessage(repMessage);
            }
        }
예제 #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);
        }