Ejemplo n.º 1
0
        /// <summary>
        /// Sends a mesh Handshake through the specified channel.
        /// </summary>
        /// <param name="channel">The channel to send the packet to.</param>
        internal static void SendMeshHandshake(this Connection channel)
        {
            // Acquire and set the handhshake packet
            var packet = MeshHandshake.Acquire();

            packet.Key      = Murmur32.GetHash(Service.Mesh.Cluster);
            packet.Identity = new GossipMember(Service.Mesh.BroadcastEndpoint);

            // Send the handshake packet
            channel.Send(packet);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="channel">The channel sending the command.</param>
        /// <param name="command">The command received.</param>
        private static void OnHandshake(Connection channel, MeshHandshake handshake)
        {
            // Deserialize the handshake
            var endpoint = handshake.Identity.EndPoint;
            var id       = endpoint.ToIdentifier();

            // Set the identifier of the channel
            channel.MeshIdentifier = id;

            // Validate the credentials
            if (!(Murmur32.GetHash(Service.Mesh.Cluster) == handshake.Key))
            {
                throw new UnauthorizedAccessException("Cluster access was not authorized.");
            }

            // Check if we're trying to connect to ourselves
            if (Service.Mesh.Identifier == id)
            {
                // It's ourselves, remove from tracking
                //var ep = channel.RemoteEndPoint as IPEndPoint;
                var port = ((IPEndPoint)Service.Mesh.Binding.EndPoint).Port;
                var ep   = new IPEndPoint(channel.RemoteEndPoint.Address, port);
                if (ep != null)
                {
                    Service.Mesh.Members.ForgetPeer(ep);
                }

                channel.Close();
                return;
            }

            // Attempt to register
            MeshMember node;

            if (Service.Mesh.Members.TryRegister(endpoint, channel, out node))
            {
                // Send ACK first
                channel.SendMeshHandshakeAck();

                // Send an event since we're connected to the node
                Service.InvokeNodeConnect(new ClusterEventArgs(node));
            }
            else
            {
                channel.Close();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the digest for this version.
        /// </summary>
        /// <returns></returns>
        public int Digest()
        {
            // Construct the string list
            var entities = new SortedSet <string>();

            lock (this.Vector)
            {
                entities.Add(this.NodeId + ":" + this.Version);
                foreach (var kvp in this.Vector)
                {
                    entities.Add(kvp.Key + ":" + kvp.Value);
                }
            }

            // Compute the hash
            int hash = 0;

            foreach (var kvp in entities)
            {
                hash ^= Murmur32.GetHash(kvp);
            }
            return(hash);
        }