public void describe_connecting_to_node_that_is_down()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message     = new RawGossipMessage();

            var connectionFailed = new ManualResetEventSlim(false);

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    node1.StartListening();
                    node2.StartListening();

                    var wrongConfig = nodeConfig2.Clone();
                    wrongConfig.BindToEndPoint = new IPEndPoint(wrongConfig.BindToEndPoint.Address, 1);
                    var member = new GossipClusterMember(nodeConfig1.NodeId, wrongConfig, node2.Authenticator);
                    member.ConnectionTimeout   = TimeSpan.FromMilliseconds(300);
                    member.OnConnectionFailed += (m, e) => connectionFailed.Set();

                    node1.Cluster.Join(member);
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                    node1.Cluster.BroadcastMessage(message);
                    if (!connectionFailed.Wait(member.ConnectionTimeout.Add(TimeSpan.FromMilliseconds(100))))
                    {
                        Assert.Fail();
                    }
                }
        }
        public void describe_removal_of_expired_connections()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message     = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    node1.StartListening();
                    node2.StartListening();

                    node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                    node1.Cluster.BroadcastMessage(message);

                    var clusterMember = node1.Cluster[nodeConfig2.NodeId];
                    GossipClusterMember.MinConnectionInactivity = TimeSpan.Zero;
                    clusterMember.MaxConnectionInactivity       = TimeSpan.Zero;
                    Thread.Sleep(1);
                    clusterMember.RemoveInactiveConnections(null);
                    Assert.AreEqual(0, clusterMember.NumberOfOpenConnections);
                }
        }
        public void describe_connecting_to_node_that_is_down()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message = new RawGossipMessage();

            var connectionFailed = new ManualResetEventSlim(false);

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                node1.StartListening();
                node2.StartListening();

                var wrongConfig = nodeConfig2.Clone();
                wrongConfig.BindToEndPoint = new IPEndPoint(wrongConfig.BindToEndPoint.Address, 1);
                var member = new GossipClusterMember(nodeConfig1.NodeId, wrongConfig, node2.Authenticator);
                member.ConnectionTimeout = TimeSpan.FromMilliseconds(300);
                member.OnConnectionFailed += (m, e) => connectionFailed.Set();

                node1.Cluster.Join(member);
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                node1.Cluster.BroadcastMessage(message);
                if (!connectionFailed.Wait(member.ConnectionTimeout.Add(TimeSpan.FromMilliseconds(100))))
                    Assert.Fail();
            }
        }
        public void describe_creating_connection_for_broadcasting_message_to_cluster()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message     = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    node1.StartListening();
                    node2.StartListening();

                    node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));
                    Assert.IsNotNull(node1.Cluster[nodeConfig2.NodeId]);
                    Assert.IsNotNull(node2.Cluster[nodeConfig1.NodeId]);

                    node1.Cluster.BroadcastMessage(message);
                    Assert.AreEqual(1, node1.Cluster[nodeConfig2.NodeId].NumberOfOpenConnections);
                    Assert.AreEqual(1, node2.Cluster[nodeConfig1.NodeId].NumberOfOpenConnections);
                }
        }
        public void describe_custom_gossip_message_factory_creating_different_message_types()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message1 = new GossipMessage1();
            var message2 = new GossipMessage2();
            GossipMessageFactory.CreateMessage += CreateMessage;

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                var received = new AutoResetEvent(false);
                GossipMessage receivedMsg = null;
                node1.OnMessageReceived += (node, conn, msg) =>
                                               {
                                                   receivedMsg = msg;
                                                   received.Set();
                                               };

                node1.StartListening();
                node2.StartListening();

                node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                node2.Cluster.BroadcastMessage(message1);
                if (!received.WaitOne(Timeout)) Assert.Fail();
                Assert.IsInstanceOf<GossipMessage1>(receivedMsg);

                node2.Cluster.BroadcastMessage(message2);
                if (!received.WaitOne(Timeout)) Assert.Fail();
                Assert.IsInstanceOf<GossipMessage2>(receivedMsg);
            }
        }
Exemple #6
0
        private void JoinCluster(IPEndPoint ipEndPoint)
        {
            var contactNodeConfig = new GossipNodeConfig(ipEndPoint);
            var contactNode       = new GossipNode(contactNodeConfig, clusterConfig);

            Gossip = new GossipNode(gossipConfig, clusterConfig);
            Gossip.Cluster.Join(new GossipClusterMember(gossipConfig.NodeId, contactNodeConfig, contactNode.Authenticator));
        }
Exemple #7
0
        public Node(IPEndPoint ipEndpoint, string clusterKey)
        {
            clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes(clusterKey) };
            gossipConfig = new GossipNodeConfig(ipEndpoint);
            Gossip = new GossipNode(gossipConfig, clusterConfig);

            Gossip.OnMessageReceived += ProcessInfo;
        }
Exemple #8
0
        public Node(IPEndPoint ipEndpoint, string clusterKey)
        {
            clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes(clusterKey)
            };
            gossipConfig = new GossipNodeConfig(ipEndpoint);
            Gossip       = new GossipNode(gossipConfig, clusterConfig);

            Gossip.OnMessageReceived += ProcessInfo;
        }
        public void describe_custom_gossip_message_factory_creating_different_message_types()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message1    = new GossipMessage1();
            var message2    = new GossipMessage2();

            GossipMessageFactory.CreateMessage += CreateMessage;

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    var           received    = new AutoResetEvent(false);
                    GossipMessage receivedMsg = null;
                    node1.OnMessageReceived += (node, conn, msg) =>
                    {
                        receivedMsg = msg;
                        received.Set();
                    };

                    node1.StartListening();
                    node2.StartListening();

                    node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                    node2.Cluster.BroadcastMessage(message1);
                    if (!received.WaitOne(Timeout))
                    {
                        Assert.Fail();
                    }
                    Assert.IsInstanceOf <GossipMessage1>(receivedMsg);

                    node2.Cluster.BroadcastMessage(message2);
                    if (!received.WaitOne(Timeout))
                    {
                        Assert.Fail();
                    }
                    Assert.IsInstanceOf <GossipMessage2>(receivedMsg);
                }
        }
Exemple #10
0
        static void Main()
        {
            Debug.Listeners.Add(new ConsoleTraceListener());

            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Any, 30000), "tag1");
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30001), "tag2");

            var buffer = new byte[1024 * 1024];
            var bulkMessage = new RawGossipMessage(0, buffer);

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                node1.OnMessageReceived += ProcessSimpleTextMessage;
                node2.OnMessageReceived += ProcessSimpleTextMessage;

                node1.StartListening();
                node2.StartListening();

                node1.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes("Hello World!")), m => m.HasTag("tag2")).Wait();

                const int iterations = 1000;
                var sw = Stopwatch.StartNew();
                var tasks = new List<Task>();
                for (var i = 0; i < iterations; i++)
                {
                    tasks.Add(node1.Cluster.BroadcastMessageAsync(bulkMessage));
                }
                Task.WaitAll(tasks.ToArray());
               Console.WriteLine("{0} iterations of {1} KB transferred in {2}ms ({3:F1} ops/s, {4:F1} MB/s)",
                    iterations,
                    buffer.Length / 1024,
                    sw.ElapsedMilliseconds,
                    iterations / (sw.ElapsedMilliseconds / 1000m),
                    iterations * buffer.Length / (sw.ElapsedMilliseconds / 1000m) / 1024m / 1024m);
            }

            Console.ReadLine();
        }
        public void describe_connection_with_successful_authentication()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort));

            using (var node = new GossipNode(nodeConfig, clusterConfig))
            {
                var signal = new ManualResetEventSlim(false);
                bool? authenticated = null;
                node.OnClientConnectionAuthenticationFailed += (n, c) => { authenticated = false; signal.Set(); };
                node.OnClientConnectionAuthenticationSucceeded += (n, c) => { authenticated = true; signal.Set(); };
                node.StartListening();
                using (GossipConnection.ConnectAsync(nodeConfig.NodeId, nodeConfig.BindToEndPoint, node.Authenticator, c => signal.Set()))
                {
                    signal.Wait();
                    Assert.That(authenticated == true);
                }
            }
        }
        public void describe_connection_with_successful_authentication()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort));

            using (var node = new GossipNode(nodeConfig, clusterConfig))
            {
                var  signal        = new ManualResetEventSlim(false);
                bool?authenticated = null;
                node.OnClientConnectionAuthenticationFailed    += (n, c) => { authenticated = false; signal.Set(); };
                node.OnClientConnectionAuthenticationSucceeded += (n, c) => { authenticated = true; signal.Set(); };
                node.StartListening();
                using (GossipConnection.ConnectAsync(nodeConfig.NodeId, nodeConfig.BindToEndPoint, node.Authenticator, c => signal.Set()))
                {
                    signal.Wait();
                    Assert.That(authenticated == true);
                }
            }
        }
Exemple #13
0
        static void Main(string[] args)
        {
            while (true)
            {
                var clusterConfig = new GossipClusterConfig {
                    ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
                };
                var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30001), "tag2");
                var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30002), "tag2");
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                    using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                    {
                        node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));

                        node1.OnMessageReceived += ProcessSimpleTextMessage;

                        node1.StartListening();

                        node1.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes("Node 1 Info"))).Wait();
                    }

                Console.ReadLine();
                //var port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                //string startPort = ConfigurationManager.AppSettings["StartPort"];

                //if (startPort != "")
                //{
                //    Console.WriteLine("Join Cluster");
                //    var startEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32(startPort));
                //    Node node = new Node(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port), "ClusterKey", startEndPoint);
                //}
                //else
                //{
                //    Console.WriteLine("First Seed");
                //    Node node = new Node(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port), "ClusterKey");
                //}
            }
        }
        public void describe_creating_connection_for_broadcasting_message_to_cluster()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                node1.StartListening();
                node2.StartListening();

                node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));
                Assert.IsNotNull(node1.Cluster[nodeConfig2.NodeId]);
                Assert.IsNotNull(node2.Cluster[nodeConfig1.NodeId]);

                node1.Cluster.BroadcastMessage(message);
                Assert.AreEqual(1, node1.Cluster[nodeConfig2.NodeId].NumberOfOpenConnections);
                Assert.AreEqual(1, node2.Cluster[nodeConfig1.NodeId].NumberOfOpenConnections);
            }
        }
        public void describe_keeping_of_nonexpired_connections()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                node1.StartListening();
                node2.StartListening();

                node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                node1.Cluster.BroadcastMessage(message);

                var clusterMember = node1.Cluster[nodeConfig2.NodeId];
                Thread.Sleep(1);
                clusterMember.RemoveInactiveConnections(null);
                Assert.AreEqual(1, clusterMember.NumberOfOpenConnections);
            }
        }
Exemple #16
0
        static void Main(string[] args)
        {
            while (true)
            {
                var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
                var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30001), "tag2");
                var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30002), "tag2");
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                {
                    node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));

                    node1.OnMessageReceived += ProcessSimpleTextMessage;

                    node1.StartListening();

                    node1.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes("Node 1 Info"))).Wait();
                }

                Console.ReadLine();
                //var port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                //string startPort = ConfigurationManager.AppSettings["StartPort"];

                //if (startPort != "")
                //{
                //    Console.WriteLine("Join Cluster");
                //    var startEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32(startPort));
                //    Node node = new Node(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port), "ClusterKey", startEndPoint);
                //}
                //else
                //{
                //    Console.WriteLine("First Seed");
                //    Node node = new Node(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port), "ClusterKey");
                //}
            }
        }
Exemple #17
0
        private void JoinCluster(IPEndPoint ipEndPoint)
        {
            var contactNodeConfig = new GossipNodeConfig(ipEndPoint);
            var contactNode = new GossipNode(contactNodeConfig, clusterConfig);

            Gossip = new GossipNode(gossipConfig, clusterConfig);
            Gossip.Cluster.Join(new GossipClusterMember(gossipConfig.NodeId, contactNodeConfig, contactNode.Authenticator));
        }