/// <summary> /// Handles MessageReceived event. /// All messages received from all controllers comes to this method. /// </summary> /// <param name="sender">Sender object</param> /// <param name="e">Event arguments</param> private void NGRIDController_MessageReceived(object sender, MessageReceivedFromRemoteApplicationEventArgs e) { try { //Response to Ping messages if ((e.Message.MessageTypeId == NGRIDMessageFactory.MessageTypeIdNGRIDPingMessage) && string.IsNullOrEmpty(e.Message.RepliedMessageId)) { //Reply ping message SendMessage(new NGRIDPingMessage { RepliedMessageId = e.Message.MessageId }, e.Communicator); return; } //Do not process messages other than NGRIDControllerMessage if (e.Message.MessageTypeId != NGRIDMessageFactory.MessageTypeIdNGRIDControllerMessage) { return; } //Cast message to NGRIDControllerMessage var controllerMessage = e.Message as NGRIDControllerMessage; if (controllerMessage == null) { return; } //Create (deserialize) ControlMessage from MessageData of controllerMessage object var controlMessage = NGRIDSerializationHelper.DeserializeFromByteArray( () => ControlMessageFactory.CreateMessageByTypeId(controllerMessage.ControllerMessageTypeId), controllerMessage.MessageData); //Process message ProcessControllerMessage(e.Communicator, controllerMessage, controlMessage); } catch (Exception ex) { Logger.Warn(ex.Message, ex); } }
/// <summary> /// This method is handles all adjacent server's and client application's MessageReceived events. /// </summary> /// <param name="sender">Creator of event</param> /// <param name="e">Event arguments</param> private void RemoteApplication_MessageReceived(object sender, MessageReceivedFromRemoteApplicationEventArgs e) { try { //Incoming data transfer message request if (e.Message.MessageTypeId == NGRIDMessageFactory.MessageTypeIdNGRIDDataTransferMessage) { //Get, check and process message var dataTransferMessage = e.Message as NGRIDDataTransferMessage; ProcessDataTransferMessage(e.Application as NGRIDPersistentRemoteApplicationBase, e.Communicator, dataTransferMessage); } //Incoming delivery result (ACK/Reject message) else if ((e.Message.MessageTypeId == NGRIDMessageFactory.MessageTypeIdNGRIDOperationResultMessage) && (!string.IsNullOrEmpty(e.Message.RepliedMessageId))) { //Find and send signal/pulse to waiting thread for this message WaitingMessage waitingMessage = null; lock (_waitingMessages) { if (_waitingMessages.ContainsKey(e.Message.RepliedMessageId)) { waitingMessage = _waitingMessages[e.Message.RepliedMessageId]; } } if(waitingMessage != null) { waitingMessage.ResponseMessage = e.Message as NGRIDOperationResultMessage; waitingMessage.WaitEvent.Set(); } } //Incoming Ping message else if ((e.Message.MessageTypeId == NGRIDMessageFactory.MessageTypeIdNGRIDPingMessage) && string.IsNullOrEmpty(e.Message.RepliedMessageId)) { //Reply ping message e.Application.SendMessage(new NGRIDPingMessage {RepliedMessageId = e.Message.MessageId}, e.Communicator); } } catch (Exception ex) { Logger.Warn(ex.Message, ex); } }