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_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_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);
            }
        }
Example #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));
        }
Example #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;
        }
Example #8
0
        private static void ProcessSimpleTextMessage(GossipNode node, GossipConnection connection, GossipMessage message)
        {
            var rawMessage = message as RawGossipMessage;

            if (rawMessage == null)
            {
                return;
            }
            if (rawMessage.MessageType == 1)
            {
                Debug.WriteLine(String.Format("Node Info: {0}", node.NodeConfig.Tags.First()));
                Debug.WriteLine(String.Format("Node received: {0}", node.NodeConfig.NodeId.ToHexStringLower()));
                Debug.WriteLine("Message Size: {0} bytes", rawMessage.Size);
                Debug.WriteLine(String.Format("Message String: {0}", Encoding.UTF8.GetString(rawMessage.Buffer)));
            }
        }
        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);
                }
        }
        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);
                }
            }
        }
Example #12
0
        public static void Main(string[] args)
        {
            try
            {
                // Create service collection
                ServiceCollection serviceCollection = new ServiceCollection();
                ConfigureServices(serviceCollection);

                // Create service provider
                _serviceProvider = serviceCollection.BuildServiceProvider();

                GossipSettings settings = new GossipSettings();
                ConfigurationBinder.Bind(_configuration.GetSection("gossip"), settings);

                GossipNode gossipNode = new GossipNode(settings);
                gossipNode.NewPeerFound += GossipNode_NewPeerFound;
                gossipNode.PeerDown     += GossipNode_PeerDown;
                gossipNode.PeerWakeUp   += GossipNode_PeerWakeUp;

                gossipNode.Init();

                Console.ReadKey();
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("The given file is not found!");
            }
            catch (FormatException)
            {
                Console.WriteLine("The given file is not in the correct JSON format!");
            }
            catch (IOException e)
            {
                Console.WriteLine("Could not read the configuration file: " + e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error while starting the gossip service: " + e.Message);
            }
        }
Example #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);
            }
        }
Example #15
0
        private void ProcessInfo(GossipNode node, GossipConnection connection, GossipMessage message)
        {
            var    rawMessage = message as RawGossipMessage;
            string msg        = Encoding.UTF8.GetString(rawMessage.Buffer);

            if (msg == "")
            {
                Console.WriteLine("First Handshake");

                JoinCluster(node.NodeConfig.BindToEndPoint);
                string senMsg = JsonConvert.SerializeObject(RingInfos);

                Gossip.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes(senMsg))).Wait();
            }
            else
            {
                Console.WriteLine("Second Handshake");

                var ringInfos = JsonConvert.DeserializeObject <List <RingInfo> >(msg);

                RingInfos = ringInfos;
            }
        }
Example #16
0
 public void StateMachine_WalkThroghtStates_Success()
 {
     var           fakeNode = new GossipNode(new GossipSettings());
     GossipManager manager  = new GossipManager(fakeNode);
 }
        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);
            }
        }