Esempio n. 1
0
        public void RegisterAction(ControllerAction action, Action <HttpEntityManager, UriTemplateMatch> handler)
        {
            Ensure.NotNull(action, "action");
            Ensure.NotNull(handler, "handler");

            var        segments = new Uri("http://fake" + action.UriTemplate, UriKind.Absolute).Segments;
            RouterNode node     = _root;

            foreach (var segm in segments)
            {
                var    segment = Uri.UnescapeDataString(segm);
                string path    = segment.StartsWith("{*") ? GreedyPlaceholder
                            : segment.StartsWith("{") ? Placeholder
                            : segment;

                RouterNode child;
                if (!node.Children.TryGetValue(path, out child))
                {
                    child = new RouterNode();
                    node.Children.Add(path, child);
                }
                node = child;
            }
            if (node.LeafRoutes.Contains(x => x.Action.Equals(action)))
            {
                throw new ArgumentException("Duplicate route.");
            }
            node.LeafRoutes.Add(new HttpRoute(action, handler));
        }
Esempio n. 2
0
        private void GetAllUriMatches(RouterNode node, Uri baseAddress, Uri uri, string[] segments, int index, List <UriToActionMatch> matches)
        {
            RouterNode child;

            if (index == segments.Length)
            {
                // /stats/ should match /stats/{*greedyStatsPath}
                if (uri.OriginalString.EndsWith("/") && node.Children.TryGetValue(GreedyPlaceholder, out child))
                {
                    AddMatchingRoutes(child.LeafRoutes, baseAddress, uri, matches);
                }

                AddMatchingRoutes(node.LeafRoutes, baseAddress, uri, matches);
                return;
            }

            if (node.Children.TryGetValue(GreedyPlaceholder, out child))
            {
                GetAllUriMatches(child, baseAddress, uri, segments, segments.Length, matches);
            }
            if (node.Children.TryGetValue(Placeholder, out child))
            {
                GetAllUriMatches(child, baseAddress, uri, segments, index + 1, matches);
            }
            if (node.Children.TryGetValue(segments[index], out child))
            {
                GetAllUriMatches(child, baseAddress, uri, segments, index + 1, matches);
            }
        }
Esempio n. 3
0
        public void RegisterAction(ControllerAction action, Func<HttpEntityManager, UriTemplateMatch, RequestParams> handler)
        {
            Ensure.NotNull(action, "action");
            Ensure.NotNull(handler, "handler");

            var segments = new Uri("http://fake" + action.UriTemplate, UriKind.Absolute).Segments;
            RouterNode node = _root;
            foreach (var segm in segments)
            {
                var segment = Uri.UnescapeDataString(segm);
                string path = segment.StartsWith("{*") ? GreedyPlaceholder
                            : segment.StartsWith("{") ? Placeholder
                            : segment;

                RouterNode child;
                if (!node.Children.TryGetValue(path, out child))
                {
                    child = new RouterNode();
                    node.Children.Add(path, child);
                }
                node = child;
            }
            if (node.LeafRoutes.Contains(x => x.Action.Equals(action)))
                throw new ArgumentException("Duplicate route.");
            node.LeafRoutes.Add(new HttpRoute(action, handler));
        }
Esempio n. 4
0
    public static Network getLevel1()
    {
        Network newNetwork = new Network();

        RouterNode  router = new RouterNode("home-router", "192.168.0.2");
        DesktopNode home   = new DesktopNode("home-desktop", "192.168.0.3");
        DesktopNode mobile = new DesktopNode("george-phone", "192.168.0.4");

        FileSystem hfs = home.fileSystem;

        hfs.createDirectory("home");
        hfs.createDirectory("/home/desktop");
        hfs.createFile("/home/desktop/pass.txt")
        .setContents("Don't tell anyone, but my password...\nis...\nhelloworld123");
        hfs.createDirectory("tmp");
        hfs.createFile("/tmp/flies.src")
        .setContents("Nothing here but us flies.");
        hfs.createDirectory("bin");

        router.addConnection(home);
        router.addConnection(mobile);
        newNetwork.setStart(home);

        return(newNetwork);
    }
Esempio n. 5
0
    public void CanPingHandleDownNodes()
    {
        RouterNode node1 = new RouterNode("test.computer", "1");
        RouterNode node2 = new RouterNode("test.computer", "2");
        RouterNode node3 = new RouterNode("test.computer", "3");
        RouterNode node4 = new RouterNode("test.computer", "4");
        RouterNode node5 = new RouterNode("test.computer", "5");

        // 1 - 2 - 5
        // | / | /
        // 3 - 4
        node1.addConnection(node2);
        node1.addConnection(node3);
        node2.addConnection(node3);
        node2.addConnection(node4);
        node2.addConnection(node5);
        node3.addConnection(node4);
        node4.addConnection(node5);

        Assert.AreEqual(0, node1.ping(node1));
        Assert.AreEqual(1, node1.ping(node2));
        Assert.AreEqual(1, node1.ping(node3));
        Assert.AreEqual(2, node1.ping(node4));
        Assert.AreEqual(2, node1.ping(node5));

        // 1   2   5
        // |     /
        // 3 - 4
        node2.active = false;

        Assert.AreEqual(0, node1.ping(node1));
        Assert.AreEqual(-1, node1.ping(node2));
        Assert.AreEqual(1, node1.ping(node3));
        Assert.AreEqual(2, node1.ping(node4));
        Assert.AreEqual(3, node1.ping(node5));

        // Dead nodes can't ping (or do anything)
        Assert.Throws(typeof(InvalidOperationException), delegate {
            node2.ping(node1);
        });
    }
Esempio n. 6
0
        private void GetAllUriMatches(RouterNode node, Uri baseAddress, Uri uri, string[] segments, int index, List<UriToActionMatch> matches)
        {
            RouterNode child;

            if (index == segments.Length)
            {
                // /stats/ should match /stats/{*greedyStatsPath}
                if (uri.OriginalString.EndsWith("/") && node.Children.TryGetValue(GreedyPlaceholder, out child))
                    AddMatchingRoutes(child.LeafRoutes, baseAddress, uri, matches);

                AddMatchingRoutes(node.LeafRoutes, baseAddress, uri, matches);
                return;
            }

            if (node.Children.TryGetValue(GreedyPlaceholder, out child))
                GetAllUriMatches(child, baseAddress, uri, segments, segments.Length, matches);
            if (node.Children.TryGetValue(Placeholder, out child))
                GetAllUriMatches(child, baseAddress, uri, segments, index + 1, matches);
            if (node.Children.TryGetValue(segments[index], out child))
                GetAllUriMatches(child, baseAddress, uri, segments, index + 1, matches);
        }
Esempio n. 7
0
        public static bool CheckOuterCount(this RouterNode self, long timeNow)
        {
            if (self.LastCheckTime == 0)
            {
                self.LastCheckTime = timeNow;
            }

            if (timeNow - self.LastCheckTime > 1000)
            {
                //Log.Debug($"router recv packet per second: {self.LimitCountPerSecond}");
                self.LimitCountPerSecond = 0;
                self.LastCheckTime       = timeNow;
            }

            if (++self.LimitCountPerSecond > 1000)
            {
                return(false);
            }

            return(true);
        }