public void MatchesDynamicUrlWithTrailingValuesAheadOfMultiValue()
 {
     var target = new RoutingTable();
     var expected = typeof(RoutingTableTests);
     target.Add("/tests/{Year}/{Month}", typeof(int));
     target.Add("/tests/{Id}/bar", expected);
     IDictionary<string, string[]> variables;
     var actual = target.Get("/tests/1/bar", out variables);
     Assert.Equal(expected, actual);
 }
Example #2
0
        public void MatchesDynamicUrlWithTrailingValuesAheadOfMultiValue()
        {
            var target   = new RoutingTable();
            var expected = typeof(RoutingTableTests);

            target.Add("/tests/{Year}/{Month}", typeof(int));
            target.Add("/tests/{Id}/bar", expected);
            IDictionary <string, string[]> variables;
            var actual = target.Get("/tests/1/bar", out variables);

            Assert.Equal(expected, actual);
        }
        public void returns_k_nearest_ordered()
        {
            RoutingTable <int> table = Mother.CreateIntRoutingTable();

            table.Add(new NodeIdentifier <int>("", 1));
            table.Add(new NodeIdentifier <int>("", 2));
            table.Add(new NodeIdentifier <int>("", 4));
            table.Add(new NodeIdentifier <int>("", 5));

            CollectionAssert.IsOrdered(table.NearTo(1).Select(n => n.NodeId),
                                       new LamdaComparer <int>((a, b) => a.CompareTo(b)));
        }
Example #4
0
        public void MatchesUrlWhenTwoRegexesHaveSameNumberOfGroups()
        {
            var target      = new RoutingTable();
            var expectedFoo = typeof(int);
            var expectedBar = typeof(string);

            target.Add("/tests/{Id}/foo", expectedFoo);
            target.Add("/tests/{Id}/bar", expectedBar);
            IDictionary <string, string[]> variables;

            Assert.Equal(expectedFoo, target.Get("/tests/1/foo", out variables));
            Assert.Equal(expectedBar, target.Get("/tests/1/bar", out variables));
        }
Example #5
0
        public void AddSame()
        {
            _table.Clear();
            for (var i = 0; i < Bucket.MaxCapacity; i++)
            {
                var id = (byte[])_id.Clone();
                _table.Add(new Node(new NodeId(id), new IPEndPoint(IPAddress.Any, 0)));
            }

            Assert.AreEqual(1, _addedCount, "#a");
            Assert.AreEqual(1, _table.Buckets.Count, "#1");
            Assert.AreEqual(1, _table.Buckets[0].Nodes.Count, "#2");

            CheckBuckets();
        }
Example #6
0
        public void TestTable()
        {
            var nodekey     = Key.ComputeKey("node0");
            var table       = new RoutingTable(nodekey, REDUNDANCY);
            var keys        = Enumerable.Range(0, KEY_COUNT).Select(x => Key.ComputeKey($"item{x}")).ToList();
            var orderedKeys = keys.OrderBy(x => new KeyDistance(nodekey, x)).ToList();
            var tmp         = new List <Key>();

            foreach (var k in keys)
            {
                if (table.Add(new PeerInfo(k, new System.Net.IPEndPoint(1, 1))))
                {
                    tmp.Add(k);
                }
            }

            var expected = tmp.OrderBy(x => new KeyDistance(nodekey, x)).Take(SAMPLE).ToList();
            var found    = table.Nearest(nodekey, SAMPLE);

            Assert.Equal(SAMPLE, found.Count);

            var sort = found.Select(x => x.Key).OrderBy(x => new KeyDistance(nodekey, x)).ToList();

            for (var i = 0; i < SAMPLE; i++)
            {
                if (expected[i] != sort[i])
                {
                    throw new Exception($"Wrong key at position {i}");
                }
            }
        }
        public void returns_right_2_nearest()
        {
            RoutingTable <int> table = Mother.CreateIntRoutingTable();

            table.Add(new NodeIdentifier <int>("", 1));
            table.Add(new NodeIdentifier <int>("", 2));
            table.Add(new NodeIdentifier <int>("", 4));
            table.Add(new NodeIdentifier <int>("", 5));

            List <int> items = table.NearTo(2).Select(n => n.NodeId).ToList();

            Assert.AreEqual(items[0], 2);
            Assert.AreEqual(items[1], 1);
            Assert.AreEqual(items[2], 4);
            Assert.AreEqual(items[3], 5);
        }
Example #8
0
        internal static void ManyNodes(out RoutingTable routingTable, out List <NodeId> nodes)
        {
            // Generate our local id
            byte[] id = new byte[20];
            id[19] = 7;

            nodes = new List <NodeId>();
            RoutingTable table = new RoutingTable(new Node(new NodeId(id), new IPEndPoint(IPAddress.Any, 0)));

            for (int i = 0; i <= 30; i++)
            {
                if (i == 7)
                {
                    continue;
                }

                id     = new byte[20];
                id[19] = (byte)i;
                nodes.Add(new NodeId(id));
                table.Add(new Node(new NodeId(id), new IPEndPoint(IPAddress.Any, 0)));
            }

            nodes.Sort(delegate(NodeId left, NodeId right)
            {
                NodeId dLeft  = left.Xor(table.LocalNode.Id);
                NodeId dRight = right.Xor(table.LocalNode.Id);
                return(dLeft.CompareTo(dRight));
            });

            nodes.RemoveAll(delegate(NodeId n) { return(table.FindNode(n) == null); });
            routingTable = table;
        }
 public void MatchesStaticUrl()
 {
     var target = new RoutingTable();
     var expected = typeof(RoutingTableTests);
     target.Add("/", expected);
     IDictionary<string, string> _;
     var actual = target.Get("/", out _);
     Assert.Equal(expected, actual);
 }
 public void MatchesDynamicUrlWithOneVariable()
 {
     var target = new RoutingTable();
     var expected = typeof(RoutingTableTests);
     target.Add("/tests/{Id}", expected);
     IDictionary<string, string> variables;
     var actual = target.Get("/tests/1", out variables);
     Assert.Equal(expected, actual);
     Assert.Equal("1", variables["Id"]);
 }
Example #11
0
 public void Setup()
 {
     _id               = new byte[20];
     _id[1]            = 128;
     _node             = new Node(new NodeId(_id), new IPEndPoint(IPAddress.Any, 0));
     _table            = new RoutingTable(_node);
     _table.NodeAdded += delegate { _addedCount++; };
     _table.Add(_node); //the local node is no more in routing table so add it to show test is still ok
     _addedCount = 0;
 }
Example #12
0
 private void AddToTable(IProtocolNode node, int count)
 {
     node?.Connections.ForEach(c =>
     {
         if (!RoutingTable.Any(t => t.Item1.Contains(c)))
         {
             RoutingTable.Add(new Tuple <string, string, int>(c, node.Id, count));
         }
     });
 }
 public void MatchesVanityUrlWithTrailingSlash()
 {
     var target = new RoutingTable();
     var expected = typeof(RoutingTableTests);
     target.Add("/{Name}", expected);
     IDictionary<string, string> variables;
     var actual = target.Get("/test/", out variables);
     Assert.Equal(expected, actual);
     Assert.Equal("test", variables["Name"]);
 }
Example #14
0
        public void MatchesStaticUrl()
        {
            var target   = new RoutingTable();
            var expected = typeof(RoutingTableTests);

            target.Add("/", expected);
            IDictionary <string, string[]> _;
            var actual = target.Get("/", out _);

            Assert.Equal(expected, actual);
        }
 public void MatchesDynamicUrlWithTwoVariables()
 {
     var target = new RoutingTable();
     var expected = typeof(RoutingTableTests);
     target.Add("/tests/{Year}/{Month}", expected);
     IDictionary<string, string> variables;
     var actual = target.Get("/tests/2012/2", out variables);
     Assert.Equal(expected, actual);
     Assert.Equal("2012", variables["Year"]);
     Assert.Equal("2", variables["Month"]);
 }
Example #16
0
        public void MatchesDynamicUrlWithOneVariable()
        {
            var target   = new RoutingTable();
            var expected = typeof(RoutingTableTests);

            target.Add("/tests/{Id}", expected);
            IDictionary <string, string[]> variables;
            var actual = target.Get("/tests/1", out variables);

            Assert.Equal(expected, actual);
            Assert.Equal("1", variables["Id"][0]);
        }
Example #17
0
 /// <summary>
 /// Adds a route to the host's routing table.
 /// </summary>
 /// <param name="route">The route to add to the host's routing table.</param>
 /// <param name="index">The row number to insert the route add or null to add the route
 /// to the end of the routing table.</param>
 /// <exception cref="ArgumentNullException">The route parameter is null.</exception>
 public void AddRoute(Route route, int?index = null)
 {
     route.ThrowIfNull(nameof(route));
     if (!index.HasValue)
     {
         routingTable.Add(route);
     }
     else
     {
         routingTable.Insert(index.Value, route);
     }
 }
Example #18
0
        public void MatchesVanityUrlWithTrailingSlash()
        {
            var target   = new RoutingTable();
            var expected = typeof(RoutingTableTests);

            target.Add("/{Name}", expected);
            IDictionary <string, string[]> variables;
            var actual = target.Get("/test/", out variables);

            Assert.Equal(expected, actual);
            Assert.Equal("test", variables["Name"][0]);
        }
Example #19
0
        public void MatchesDynamicUrlWithTwoVariables()
        {
            var target   = new RoutingTable();
            var expected = typeof(RoutingTableTests);

            target.Add("/tests/{Year}/{Month}", expected);
            IDictionary <string, string[]> variables;
            var actual = target.Get("/tests/2012/2", out variables);

            Assert.Equal(expected, actual);
            Assert.Equal("2012", variables["Year"][0]);
            Assert.Equal("2", variables["Month"][0]);
        }
 public void UpdateRouteTable(Router sender, RoutingTable recievedTable)
 {
     Links[sender] = routerTimeout;
     foreach (Router rTo in recievedTable.Keys.Where(x => x != this))
     {
         if (!table.ContainsKey(rTo))
         {
             table.Add(rTo, new Dictionary <Router, int>());
         }
         if (!table[rTo].ContainsKey(sender))
         {
             table[rTo].Add(sender, recievedTable[rTo].First().Value + 1);
         }
         table[rTo][sender] = Math.Min(16, recievedTable[rTo].Values.Min() + 1);
     }
 }
        public void AddLink(Router otherRouter)//cost nesvarbu RIP algoritme bet o jeigu prireiks
        {
            if (Links.ContainsKey(otherRouter))
            {
                return;
            }
            Links.Add(otherRouter, routerTimeout);
            //tikrai buvo galima graziau parasyt, gal perrasyk
            RoutingTable temp = new RoutingTable();

            temp.Add(otherRouter, new Dictionary <Router, int>()
            {
                { otherRouter, 0 }
            });                                                                         //i kur, per kur ir keliones ilgis(hop count).
            UpdateRouteTable(otherRouter, temp);
            //as esu f*****g durna ir nezinau naudot this ar otherRouters
        }
 private void LoadRoutingTable(GXXmlReader reader)
 {
     RoutingTable.Clear();
     if (reader.IsStartElement("RoutingTable", true))
     {
         while (reader.IsStartElement("Item", false))
         {
             GXDLMSRoutingTable it = new GXDLMSRoutingTable();
             RoutingTable.Add(it);
             it.DestinationAddress = (UInt16)reader.ReadElementContentAsInt("DestinationAddress");
             it.NextHopAddress     = (UInt16)reader.ReadElementContentAsInt("NextHopAddress");
             it.RouteCost          = (UInt16)reader.ReadElementContentAsInt("RouteCost");
             it.HopCount           = (byte)reader.ReadElementContentAsInt("HopCount");
             it.WeakLinkCount      = (byte)reader.ReadElementContentAsInt("WeakLinkCount");
             it.ValidTime          = (UInt16)reader.ReadElementContentAsInt("ValidTime");
         }
         reader.ReadEndElement("RoutingTable");
     }
 }
Example #23
0
        private void AddNodeRt(IProtocolNode node, int count)
        {
            if (node == null)
            {
                return;
            }

            List <Tuple <string, string, int> > tempTable = new List <Tuple <string, string, int> >(node.GetRoutingTable());

            if (tempTable.Count < 1)
            {
                return;
            }
            tempTable.ForEach(c =>
            {
                if (!RoutingTable.Any(t => t.Item1.Contains(c.Item1)))
                {
                    RoutingTable.Add(new Tuple <string, string, int>(c.Item1, node.Id, count));
                }
            });
        }
Example #24
0
 private void UpdateRoute(TopologyEntry newEntry)
 {
     lock (tablesLock)
     {
         var oldEntry = RoutingTable.FirstOrDefault(r => r.Destination.Equals(newEntry.Destination));
         if (oldEntry == null && newEntry.Feasible != -1)
         {
             RoutingTable.Add(new RouteEntry(newEntry.Destination, newEntry.Neighbor, DateTime.Now));
         }
         else
         {
             var fastest = TopologyTable.Where(t => t.Destination.Equals(newEntry.Destination)).OrderBy(t => t.Feasible).FirstOrDefault();
             if (fastest == null)
             {
                 RoutingTable.Remove(oldEntry);
             }
             else if (!oldEntry.Neighbor.Equals(fastest.Neighbor))
             {
                 RoutingTable.Remove(oldEntry);
                 RoutingTable.Add(new RouteEntry(fastest.Destination, fastest.Neighbor, DateTime.Now));
             }
         }
     }
 }
Example #25
0
 void IGXDLMSBase.SetValue(GXDLMSSettings settings, ValueEventArgs e)
 {
     if (e.Index == 1)
     {
         if (e.Value is string)
         {
             LogicalName = e.Value.ToString();
         }
         else
         {
             LogicalName = GXDLMSClient.ChangeType((byte[])e.Value, DataType.OctetString).ToString();
         }
     }
     else if (e.Index == 2)
     {
         MaxHops = Convert.ToByte(e.Value);
     }
     else if (e.Index == 3)
     {
         WeakLqiValue = Convert.ToByte(e.Value);
     }
     else if (e.Index == 4)
     {
         SecurityLevel = Convert.ToByte(e.Value);
     }
     else if (e.Index == 5)
     {
         List <object> list = new List <object>();
         if (e.Value != null)
         {
             list.AddRange((object[])e.Value);
         }
         PrefixTable = list.ToArray();
     }
     else if (e.Index == 6)
     {
         RoutingConfiguration.Clear();
         if (e.Value != null)
         {
             foreach (object v in (object[])e.Value)
             {
                 object[] tmp = (object[])v;
                 GXDLMSRoutingConfiguration it = new GXDLMSRoutingConfiguration();
                 it.NetTraversalTime     = Convert.ToByte(tmp[0]);
                 it.RoutingTableEntryTtl = Convert.ToUInt16(tmp[1]);
                 it.Kr                     = Convert.ToByte(tmp[2]);
                 it.Km                     = Convert.ToByte(tmp[3]);
                 it.Kc                     = Convert.ToByte(tmp[4]);
                 it.Kq                     = Convert.ToByte(tmp[5]);
                 it.Kh                     = Convert.ToByte(tmp[6]);
                 it.Krt                    = Convert.ToByte(tmp[7]);
                 it.RreqRetries            = Convert.ToByte(tmp[8]);
                 it.RreqRerrWait           = Convert.ToByte(tmp[9]);
                 it.BlacklistTableEntryTtl = Convert.ToUInt16(tmp[10]);
                 it.UnicastRreqGenEnable   = Convert.ToBoolean(tmp[11]);
                 it.RlcEnabled             = Convert.ToBoolean(tmp[12]);
                 it.AddRevLinkCost         = Convert.ToByte(tmp[13]);
                 RoutingConfiguration.Add(it);
             }
         }
     }
     else if (e.Index == 7)
     {
         BroadcastLogTableEntryTtl = Convert.ToUInt16(e.Value);
     }
     else if (e.Index == 8)
     {
         RoutingTable.Clear();
         if (e.Value != null)
         {
             foreach (object v in (object[])e.Value)
             {
                 object[]           tmp = (object[])v;
                 GXDLMSRoutingTable it  = new GXDLMSRoutingTable();
                 it.DestinationAddress = Convert.ToUInt16(tmp[10]);
                 it.NextHopAddress     = Convert.ToUInt16(tmp[10]);
                 it.RouteCost          = Convert.ToUInt16(tmp[10]);
                 it.HopCount           = Convert.ToByte(tmp[10]);
                 it.WeakLinkCount      = Convert.ToByte(tmp[10]);
                 it.ValidTime          = Convert.ToUInt16(tmp[10]);
                 RoutingTable.Add(it);
             }
         }
     }
     else if (e.Index == 9)
     {
         ContextInformationTable.Clear();
         if (e.Value != null)
         {
             foreach (object v in (object[])e.Value)
             {
                 object[] tmp = (object[])v;
                 GXDLMSContextInformationTable it = new GXDLMSContextInformationTable();
                 it.CID           = (string)tmp[0];
                 it.Context       = (byte[])tmp[2];
                 it.Compression   = Convert.ToBoolean(tmp[3]);
                 it.ValidLifetime = Convert.ToUInt16(tmp[4]);
                 ContextInformationTable.Add(it);
             }
         }
     }
     else if (e.Index == 10)
     {
         List <GXKeyValuePair <UInt16, UInt16> > list = new List <GXKeyValuePair <UInt16, UInt16> >();
         if (e.Value != null)
         {
             foreach (object v in (object[])e.Value)
             {
                 object[] tmp = (object[])v;
                 list.Add(new GXKeyValuePair <UInt16, UInt16>(Convert.ToUInt16(tmp[0]), Convert.ToUInt16(tmp[1])));
             }
         }
         BlacklistTable = list;
     }
     else if (e.Index == 11)
     {
         BroadcastLogTable.Clear();
         if (e.Value != null)
         {
             foreach (object v in (object[])e.Value)
             {
                 object[] tmp = (object[])v;
                 GXDLMSBroadcastLogTable it = new GXDLMSBroadcastLogTable();
                 it.SourceAddress  = Convert.ToUInt16(tmp[0]);
                 it.SequenceNumber = Convert.ToByte(tmp[1]);
                 it.ValidTime      = Convert.ToUInt16(tmp[2]);
                 BroadcastLogTable.Add(it);
             }
         }
     }
     else if (e.Index == 12)
     {
         List <UInt16> list = new List <UInt16>();
         if (e.Value != null)
         {
             foreach (object it in (object[])e.Value)
             {
                 list.Add(Convert.ToUInt16(it));
             }
         }
         GroupTable = list.ToArray();
     }
     else if (e.Index == 13)
     {
         MaxJoinWaitTime = Convert.ToUInt16(e.Value);
     }
     else if (e.Index == 14)
     {
         PathDiscoveryTime = Convert.ToByte(e.Value);
     }
     else if (e.Index == 15)
     {
         ActiveKeyIndex = Convert.ToByte(e.Value);
     }
     else if (e.Index == 16)
     {
         MetricType = Convert.ToByte(e.Value);
     }
     else if (e.Index == 17)
     {
         CoordShortAddress = Convert.ToUInt16(e.Value);
     }
     else if (e.Index == 18)
     {
         DisableDefaultRouting = (bool)e.Value;
     }
     else if (e.Index == 19)
     {
         DeviceType = (DeviceType)Convert.ToInt32(e.Value);
     }
     else
     {
         e.Error = ErrorCode.ReadWriteDenied;
     }
 }
 public void MatchesUrlWhenTwoRegexesHaveSameNumberOfGroups()
 {
     var target = new RoutingTable();
     var expectedFoo = typeof(int);
     var expectedBar = typeof(string);
     target.Add("/tests/{Id}/foo", expectedFoo);
     target.Add("/tests/{Id}/bar", expectedBar);
     IDictionary<string, string> variables;
     Assert.Equal(expectedFoo, target.Get("/tests/1/foo", out variables));
     Assert.Equal(expectedBar, target.Get("/tests/1/bar", out variables));
 }