Exemplo n.º 1
0
        /// <summary>
        /// This method is used to add a message to outgoing messages queue.
        /// It is called by CommunicatorBase.
        /// </summary>
        /// <param name="message">Message to send</param>
        protected override void SendMessageInternal(NGRIDMessage message)
        {
            if (message.MessageTypeId != NGRIDMessageFactory.MessageTypeIdNGRIDDataTransferMessage)
            {
                return;
            }

            _outgoingMessageQueue.Add(message as NGRIDDataTransferMessage);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Serializes and writes a NGRIDMessage according to the protocol rules.
 /// </summary>
 /// <param name="serializer">Serializer to serialize message</param>
 /// <param name="message">Message to be serialized</param>
 public void WriteMessage(INGRIDSerializer serializer, NGRIDMessage message)
 {
     //Write protocol type
     serializer.WriteUInt32(NGRIDDefaultProtocolType);
     
     //Write the message type
     serializer.WriteInt32(message.MessageTypeId);
     
     //Write message
     serializer.WriteObject(message);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Sends a NGRIDMessage object to NGRID server.
 /// </summary>
 /// <param name="message"></param>
 private void SendMessageInternal(NGRIDMessage message)
 {
     try
     {
         _communicationChannel.SendMessage(message);
         LastOutgoingMessageTime = DateTime.Now;
     }
     catch (Exception)
     {
         CloseCommunicationChannel();
         throw;
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Sends a mssage to NGRID server and waits a response for timeoutMilliseconds milliseconds.
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <param name="waitingResponseType">What type of response is being waited</param>
        /// <param name="timeoutMilliseconds">Maximum waiting time for response</param>
        /// <returns>Received message from server</returns>
        private NGRIDMessage SendAndWaitForReply(NGRIDMessage message, int waitingResponseType, int timeoutMilliseconds)
        {
            //Create a WaitingMessage to wait and get response message and add it to waiting messages
            var waitingMessage = new WaitingMessage(waitingResponseType);
            lock (_waitingMessages)
            {
                _waitingMessages[message.MessageId] = waitingMessage;
            }

            try
            {
                //Create a WaitingMessage to wait and get response message and add it to waiting messages
                SendMessageInternal(message);

                //Send message to the server
                waitingMessage.WaitEvent.WaitOne(timeoutMilliseconds);

                //Check for errors
                switch (waitingMessage.State)
                {
                    case WaitingMessageStates.WaitingForResponse:
                        throw new NGRIDTimeoutException("Timeout occured. Can not received response to message.");
                    case WaitingMessageStates.ClientDisconnected:
                        throw new NGRIDException("Client disconnected from NGRID server before response received to message.");
                    case WaitingMessageStates.MessageRejected:
                        throw new NGRIDException("Message is rejected by NGRID server or destination application.");
                }

                if (waitingMessage.ResponseMessage == null)
                {
                    throw new NGRIDException("An unexpected error occured, message can not send.");
                }

                return waitingMessage.ResponseMessage;
            }
            finally
            {
                //Remove message from waiting messages
                lock (_waitingMessages)
                {
                    if (_waitingMessages.ContainsKey(message.MessageId))
                    {
                        _waitingMessages.Remove(message.MessageId);
                    }
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Derived class must override this method to send a message.
 /// </summary>
 protected abstract void SendMessageInternal(NGRIDMessage message);
Exemplo n.º 6
0
 /// <summary>
 /// When a NGRIDMessage received, this method is called from derived class.
 /// </summary>
 /// <param name="message">incoming message from communicator</param>
 protected void OnMessageReceived(NGRIDMessage message)
 {
     if (MessageReceived != null)
     {
         MessageReceived(this, new MessageReceivedFromCommunicatorEventArgs {Communicator = this, Message = message});
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Sends a message to the communicator.
 /// </summary>
 /// <param name="message"></param>
 public void SendMessage(NGRIDMessage message)
 {
     lock (_sendLock)
     {
         SendMessageInternal(message);                
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Sends NGRIDMessage object to the socket.
        /// </summary>
        /// <param name="message">Message to be sent</param>
        private void SendMessageToSocket(NGRIDMessage message)
        {
            Logger.Debug("Message is being sent to communicator " + ComminicatorId + ": " + message.GetType().Name);

            //Create MemoryStream to write message to a byte array
            var memoryStream = new MemoryStream();

            //Write message
            _wireProtocol.WriteMessage(new NGRIDDefaultSerializer(memoryStream), message);

            //Check the length of message data
            if (memoryStream.Length > CommunicationConsts.MaxMessageSize)
            {
                throw new Exception("Message is too big to send.");
            }

            //SendMessage message (contents of created memory stream)
            var sendBuffer = memoryStream.ToArray();
            var length = sendBuffer.Length;
            var totalSent = 0;
            while (totalSent < length)
            {
                var sent = _socket.Send(sendBuffer, totalSent, length - totalSent, SocketFlags.None);
                if (sent <= 0)
                {
                    throw new Exception("Message can not be sent via TCP socket. Only " + totalSent + " bytes of " + length + " bytes are sent.");
                }

                totalSent += sent;
            }

            Logger.Debug("Message is sent to communicator " + ComminicatorId + ": " + message.GetType().Name);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Sends a message to the TCP communicator according Communication type
        /// </summary>
        /// <param name="message">Message to send</param>
        protected override void SendMessageInternal(NGRIDMessage message)
        {
            if(State != CommunicationStates.Connected)
            {
                throw new NGRIDException("Communicator's state is not connected. It can not send message.");
            }

            SendMessageToSocket(message);
        }
            /// <summary>
            /// Sends a NGRIDMessage to a spesific communicator of this application.
            /// This method does not block calling thread to wait an ACK for sending message.
            /// If communicator is null, then it sends message first communicator of receiver communicators.
            /// </summary>
            /// <param name="message">outgoing message</param>
            /// <param name="communicator">Communicator to send message (may be null)</param>
            public void SendMessage(NGRIDMessage message, ICommunicator communicator)
            {
                if(communicator == null)
                {
                    lock (_remoteApplication._communicators)
                    {
                        var receiverCommunicator = GetAnyReceiverCommunicator();
                        //If no receiver is connected to server, throw exception
                        if (receiverCommunicator == null)
                        {
                            throw new NGRIDNoCommunicatorException("There is no communicator for remote application '" + _remoteApplication.Name + "' to send message.");
                        }

                        communicator = receiverCommunicator.Communicator;
                    }
                }

                communicator.SendMessage(message);
                _remoteApplication.LastOutgoingMessageTime = DateTime.Now;
            }
Exemplo n.º 11
0
        /// <summary>
        /// When a NGRIDMessage received from NGRID server, this method is called to raise MessageReceived event.
        /// </summary>
        /// <param name="message">Incoming message from server</param>
        private void OnMessageReceived(NGRIDMessage message)
        {
            if (MessageReceived == null)
            {
                return;
            }

            try
            {
                MessageReceived(this,
                                new MessageReceivedEventArgs
                                {
                                    Message = message
                                });
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Sends a NGRIDMessage to the NGRID server
        /// </summary>
        /// <param name="message">Message to send</param>
        public void SendMessage(NGRIDMessage message)
        {
            lock (_sendLock)
            {
                if (State != CommunicationStates.Connected || !_socket.Connected)
                {
                    throw new NGRIDException("Client's state is not connected. It can not send message.");
                }

                SendMessageToSocket(message);
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Sends a NGRIDMessage to a spesific communicator of this application.
 /// This method does not block calling thread to wait an ACK for sending message.
 /// If communicator is null, then it sends message first communicator of receiver communicators.
 /// </summary>
 /// <param name="message">outgoing message</param>
 /// <param name="communicator">Communicator to send message (may be null)</param>
 public void SendMessage(NGRIDMessage message, ICommunicator communicator)
 {
     _messageDeliverer.SendMessage(message, communicator);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Sends a NGRIDMessage to this application.
 /// This method does not block calling thread to wait an ACK for sending message.
 /// This method is just an overload for SendMessage(NGRIDMessage, ICommunicator) method as communicator is null.
 /// </summary>
 /// <param name="message">outgoing message</param>
 public void SendMessage(NGRIDMessage message)
 {
     SendMessage(message, null);
 }