/// <summary> /// Serializes a message to send to NGRID server from Web Service. /// </summary> /// <param name="responseMessage">Message to serialize</param> /// <returns>Serialized message</returns> public static byte[] SerializeMessage(IWebServiceResponseMessage responseMessage) { CheckResponseMessage(responseMessage); var response = ((ResponseMessage)responseMessage).CreateDataTransferResponseMessage(); return(NGRIDSerializationHelper.SerializeToByteArray(response)); }
/// <summary> /// Sends a ControlMessage to NGRID server. /// </summary> /// <param name="message">Message to send</param> public void SendMessage(ControlMessage message) { SendMessageInternal(new NGRIDControllerMessage { MessageData = NGRIDSerializationHelper.SerializeToByteArray(message), ControllerMessageTypeId = message.MessageTypeId }); }
/// <summary> /// Sends a message to a spesific communicator as a reply to an incoming message. /// </summary> /// <param name="communicator">Communicator to send message</param> /// <param name="message">Message to send</param> /// <param name="incomingMessage">Incoming message which is being replied</param> private void ReplyMessageToCommunicator(ICommunicator communicator, ControlMessage message, NGRIDControllerMessage incomingMessage) { //Create NGRIDControllerMessage that includes serialized GetApplicationListResponseMessage message var outgoingMessage = new NGRIDControllerMessage { ControllerMessageTypeId = message.MessageTypeId, MessageData = NGRIDSerializationHelper.SerializeToByteArray(message), RepliedMessageId = incomingMessage.MessageId }; //Send message to communicator that sent to message SendMessage(outgoingMessage, communicator); }
/// <summary> /// Saves a NGRIDMessageRecord. /// </summary> /// <param name="messageRecord">NGRIDMessageRecord object to save</param> /// <returns>Auto Increment Id of saved message</returns> public int StoreMessage(NGRIDMessageRecord messageRecord) { var bytesOfMessage = NGRIDSerializationHelper.SerializeToByteArray(messageRecord.Message); var id = InsertAndGetLastId( "INSERT INTO messages(MessageId, DestServer, NextServer, DestApplication, MessageData, MessageDataLength, RecordDate) VALUES(?,?,?,?,?,?,now());", new OdbcParameter("?MessageId", messageRecord.MessageId), new OdbcParameter("?DestServer", messageRecord.DestServer), new OdbcParameter("?NextServer", messageRecord.NextServer), new OdbcParameter("?DestApplication", messageRecord.DestApplication), new OdbcParameter("?MessageData", bytesOfMessage), new OdbcParameter("?MessageDataLength", bytesOfMessage.Length) ); messageRecord.Id = id; return(id); }
/// <summary> /// Saves a NGRIDMessageRecord. /// </summary> /// <param name="messageRecord">NGRIDMessageRecord object to save</param> /// <returns>Auto Increment Id of saved message</returns> public int StoreMessage(NGRIDMessageRecord messageRecord) { lock (_syncObj) { var bytesOfMessage = NGRIDSerializationHelper.SerializeToByteArray(messageRecord.Message); var id = InsertAndGetLastId( "INSERT INTO messages(MessageId, DestServer, NextServer, DestApplication, MessageData, MessageDataLength, RecordDate) VALUES(@MessageId,@DestServer,@NextServer,@DestApplication,@MessageData,@MessageDataLength,@RecordDate);", new SQLiteParameter("@MessageId", messageRecord.MessageId), new SQLiteParameter("@DestServer", messageRecord.DestServer), new SQLiteParameter("@NextServer", messageRecord.NextServer), new SQLiteParameter("@DestApplication", messageRecord.DestApplication), new SQLiteParameter("@MessageData", bytesOfMessage), new SQLiteParameter("@MessageDataLength", bytesOfMessage.Length), new SQLiteParameter("@RecordDate", DateTime.Now) ); messageRecord.Id = id; return(id); } }
/// <summary> /// Sends a ControlMessage to NGRID server and gets it's response message. /// </summary> /// <param name="message">Message to send</param> /// <returns>Response message from server</returns> public ControlMessage SendMessageAndGetResponse(ControlMessage message) { //Create a WaitingMessage to wait and get response message and add it to waiting messages var outgoingMessage = new NGRIDControllerMessage { MessageData = NGRIDSerializationHelper.SerializeToByteArray(message), ControllerMessageTypeId = message.MessageTypeId }; var waitingMessage = new WaitingMessage(); lock (_waitingMessages) { _waitingMessages[outgoingMessage.MessageId] = waitingMessage; } try { //Send message to the server SendMessageInternal(outgoingMessage); //Wait until thread is signalled by another thread to get response (Signalled by CommunicationChannel_MessageReceived method) waitingMessage.WaitEvent.WaitOne(TimeSpan.FromSeconds(90)); //Check if response received or timeout occured if (waitingMessage.ResponseMessage == null) { throw new NGRIDException("Timeout occured. Response message did not received."); } return(DeserializeControlMessage(waitingMessage.ResponseMessage)); } finally { //Remove message from waiting messages lock (_waitingMessages) { if (_waitingMessages.ContainsKey(outgoingMessage.MessageId)) { _waitingMessages.Remove(outgoingMessage.MessageId); } } } }
/// <summary> /// Sends a ControlMessage to all connected NGRIDController instances. /// </summary> /// <param name="message">Message to send</param> private void SendMessageToAllReceivers(ControlMessage message) { var outgoingMessage = new NGRIDControllerMessage { ControllerMessageTypeId = message.MessageTypeId, MessageData = NGRIDSerializationHelper.SerializeToByteArray(message) }; var receivers = GetAllReceiverCommunicators(); foreach (var receiver in receivers) { try { SendMessage(outgoingMessage, receiver); } catch (Exception ex) { Logger.Warn(ex.Message, ex); } } }