예제 #1
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;
        }
예제 #2
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 nodeConfig3 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30002), "tag3");

            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))
                    using (var node3 = new GossipNode(nodeConfig3, clusterConfig))
                    {
                        node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                        //node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig3, node3.Authenticator));

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

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

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

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

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

                        node3.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes("Node 3 Info"))).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();
        }
예제 #3
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 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)));
     }
 }
예제 #4
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 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)));
            }
        }
예제 #5
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();
        }
예제 #6
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");
                //}
            }
        }
예제 #7
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;
            }
        }
예제 #8
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));
        }