/// <summary>
 /// Initializes a new instance of the <see cref="ReceivedMessageEventArgs" /> class.
 /// </summary>
 /// <param name="message">The message that was received.</param>
 public ReceivedMessageEventArgs(Message message)
 {
     _message = message;
 }
 /// <summary>
 /// Updates the response and response result.
 /// </summary>
 /// <param name="result">The result of waiting for the response.</param>
 /// <param name="response">The response message.</param>
 internal void ResponseReceived(ResponseResults result, Message response)
 {
     lock (_lockObject)
     {
         _responseResult = result;
         _responseMessage = response;
         _progress = MessageResponseProgress.Completed;
     }
 }
Ejemplo n.º 3
0
 /// <inheritdoc></inheritdoc>
 protected override void ReceivedSystemMessage(Message message)
 {
     // Do nothing, there are no system messages used in a mesh network.
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Called when a neighbor message is received.
        /// </summary>
        /// <param name="message">The message received.</param>
        private void ReceivedNeighborsMessage(Message message)
        {
            if (message.AwaitingResponse)
            {
                // request for information
                var builder = new StringBuilder();
                foreach (var item in GetNeighbors())
                {
                    builder.Append(item.IpAddress);
                    builder.Append(":");
                    builder.Append(item.Port);
                    builder.Append(";");
                }

                if (builder.Length == 0)
                {
                    builder.Append(";");
                }

                SendResponseInternal(message, MessageType.Neighbors, builder.ToString());
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Called when a ping message is received.
 /// </summary>
 /// <param name="message">The message received.</param>
 private void ReceivedPing(Message message)
 {
     NetworkConnection connection = GetApprovedNetworkConnection(message.Sender);
     if (connection != null)
     {
         connection.LastPingReceived = DateTime.UtcNow;
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Called when an approval message is received.
        /// </summary>
        /// <param name="message">The message that was received.</param>
        private void ReceivedApprovalMessage(Message message)
        {
            if (message.AwaitingResponse)
            {
                if (message.Data == GetNetworkType())
                {
                    // Setup a connection to the sender so that we can approve it.
                    Task.WaitAll(GetUnapprovedNetworkConnection(message.Sender));
                    lock (_sendingLockObject)
                    {
                        if (_sendingConnections.ContainsKey(message.Sender) && _sendingConnections[message.Sender] != null)
                        {
                            _sendingConnections[message.Sender].Approved = true;
                        }
                    }

                    SendResponseInternal(message, MessageType.Approval, "approved");

                    lock (_recentlyApprovedNodesLockObject)
                    {
                        _recentlyApprovedNodes.Enqueue(new ApprovedNodeDetails(message.Sender, true));
                    }
                }
                else
                {
                    SendResponseInternal(message, MessageType.Approval, "failure");
                }
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Processes all received messages.
        /// </summary>
        private void ProcessMessages()
        {
            while (_receivedMessages.Count > 0)
            {
                var messageObject = _receivedMessages.Dequeue();
                var message = new Message(
                                        messageObject.Item1.Sender,
                                        messageObject.Item1.Data,
                                        messageObject.Item1.MessageId,
                                        messageObject.Item1.WaitingForResponse,
                                        messageObject.Item1.InResponseToMessage);
                _logger.Write("Message received, " + messageObject.Item1, LogLevels.Debug);

                if (message.InResponseToMessage)
                {
                    lock (_responsesLockObject)
                    {
                        _responses[message.MessageId] = message;
                    }
                }

                switch (messageObject.Item1.Type)
                {
                    case MessageType.Approval:
                        ReceivedApprovalMessage(message);
                        break;

                    case MessageType.Neighbors:
                        ReceivedNeighborsMessage(message);
                        break;

                    case MessageType.Ping:
                        ReceivedPing(message);
                        break;

                    case MessageType.System:
                        ReceivedSystemMessage(message);
                        break;

                    case MessageType.User:
                        if (ReceivedMessage != null)
                        {
                            ReceivedMessage(this, new ReceivedMessageEventArgs(message));
                        }

                        break;

                    case MessageType.Unknown:
                        break;
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Sends a response message to a node.
        /// </summary>
        /// <param name="responseTo">The message that this message is in response to.</param>
        /// <param name="type">The type of message to send.</param>
        /// <param name="message">The message to send.</param>
        /// <returns>A value indicating whether the message was successfully sent.</returns>
        protected MessageSendResult SendResponseInternal(Message responseTo, MessageType type, string message)
        {
            var result = new MessageSendResult();
            var composedMessage = InternalMessage.CreateResponseMessage(
                responseTo.Sender,
                new NodeProperties("localhost", _port),
                type,
                message,
                responseTo.MessageId,
                result);

            lock (_messagesToSendLockObject)
            {
                _messagesToSend.Enqueue(composedMessage);
            }

            return result;
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Called when a system message is received.
 /// </summary>
 /// <param name="message">The message that was received.</param>
 protected abstract void ReceivedSystemMessage(Message message);
Ejemplo n.º 10
0
 /// <summary>
 /// Sends a message response.
 /// </summary>
 /// <param name="responseTo">The message that is being responded to.</param>
 /// <param name="message">The message to send.</param>
 /// <returns>A value indicating whether the message was successfully sent.</returns>
 public MessageSendResult SendResponse(Message responseTo, string message)
 {
     return SendResponseInternal(responseTo, MessageType.User, message);
 }