/// <summary> /// Raises the <see cref="ReceivedServiceResponse"/> event. /// </summary> /// <param name="response"><see cref="ServiceResponse"/> received.</param> protected virtual void OnReceivedServiceResponse(ServiceResponse response) { if (ReceivedServiceResponse != null) ReceivedServiceResponse(this, new EventArgs<ServiceResponse>(response)); }
/// <summary> /// Attempts to parse an actionable reponse sent from the service. /// </summary> /// <param name="serviceResponse"><see cref="ServiceResponse"/> to test for actionable response.</param> /// <param name="sourceCommand">Command that invoked <paramref name="serviceResponse"/>.</param> /// <param name="responseSuccess">Boolean success state of <paramref name="serviceResponse"/>.</param> /// <returns><c>true</c> if actionable response was able to be parsed successfully; otherwise <c>false</c>.</returns> public static bool TryParseActionableResponse(ServiceResponse serviceResponse, out string sourceCommand, out bool responseSuccess) { bool parseSucceeded = false; sourceCommand = null; responseSuccess = false; try { string response = serviceResponse.Type; // Attempt to parse response message if (!string.IsNullOrWhiteSpace(response)) { // Reponse types are formatted as "Command:Success" or "Command:Failure" string[] parts = response.Split(':'); if (parts.Length > 1) { sourceCommand = parts[0].Trim().ToTitleCase(); responseSuccess = (string.Compare(parts[1].Trim(), "Success", true) == 0); parseSucceeded = true; } } } catch { parseSucceeded = false; } return parseSucceeded; }
private void SendServiceStateChangedResponse(ServiceState currentState) { ServiceResponse serviceResponse = new ServiceResponse("SERVICESTATECHANGED"); serviceResponse.Attachments.Add(new ObjectState<ServiceState>(Name, currentState)); SendResponse(serviceResponse); }
private void SendProcessStateChangedResponse(string processName, ServiceProcessState currentState) { ServiceResponse serviceResponse = new ServiceResponse("PROCESSSTATECHANGED"); serviceResponse.Attachments.Add(new ObjectState<ServiceProcessState>(processName, currentState)); SendResponse(serviceResponse); }
private void SendUpdateClientStatusResponse(Guid clientID, UpdateType type, string response) { ServiceResponse serviceResponse = new ServiceResponse(); serviceResponse.Type = "UPDATECLIENTSTATUS-" + type.ToString().ToUpper(); serviceResponse.Message = response; SendResponse(clientID, serviceResponse); }
/// <summary> /// Sends an actionable response to client along with an optional formatted message and attachment. /// </summary> /// <param name="requestInfo"><see cref="ClientRequestInfo"/> instance containing the client request.</param> /// <param name="success">Flag that determines if this response to client request was a success.</param> /// <param name="attachment">Attachment to send with response.</param> /// <param name="status">Formatted status message to send with response.</param> /// <param name="args">Arguments of the formatted status message.</param> /// <remarks> /// This method is used to send an actionable client response that can be used for responding to an event after a command has been issued. /// </remarks> public void SendActionableResponse(ClientRequestInfo requestInfo, bool success, object attachment = null, string status = null, params object[] args) { try { string responseType = requestInfo.Request.Command + (success ? ":Success" : ":Failure"); string message = ""; if (!string.IsNullOrWhiteSpace(status)) { if (args.Length == 0) message = status + "\r\n\r\n"; else message = string.Format(status, args) + "\r\n\r\n"; } ServiceResponse response = new ServiceResponse(responseType, message); // Add any specified attachment to the service response if (attachment != null) response.Attachments.Add(attachment); // Add original command arguments as an attachment response.Attachments.Add(requestInfo.Request.Arguments); // Send response to service SendResponse(requestInfo.Sender.ClientID, response); } catch (Exception ex) { ErrorLogger.Log(ex); UpdateStatus(UpdateType.Alarm, "Failed to send actionable client response with attachment due to an exception: " + ex.Message + "\r\n\r\n"); } }
/// <summary> /// Sends the specified <paramref name="response"/> to the specified <paramref name="client"/> only. /// </summary> /// <param name="client">ID of the client to whom the <paramref name="response"/> is to be sent.</param> /// <param name="response">The <see cref="ServiceResponse"/> to be sent to the <paramref name="client"/>.</param> /// <param name="async">Flag to determine whether to wait for the send operations to complete.</param> public void SendResponse(Guid client, ServiceResponse response, bool async) { try { WaitHandle[] handles = new WaitHandle[0]; if (client != Guid.Empty) { // Send message directly to specified client. handles = new WaitHandle[] { m_remotingServer.SendToAsync(client, response) }; } else { // Send message to all of the connected clients. if (m_remoteCommandClientID == Guid.Empty) { lock (m_remoteClients) { handles = m_remoteClients.Select(clientInfo => m_remotingServer.SendToAsync(clientInfo.ClientID, response)).ToArray(); } } } if (!async) WaitHandle.WaitAll(handles); } catch (Exception ex) { // Log the exception. m_errorLogger.Log(ex); } }
/// <summary> /// Sends the specified <paramref name="response"/> to the specified <paramref name="client"/> only. /// </summary> /// <param name="client">ID of the client to whom the <paramref name="response"/> is to be sent.</param> /// <param name="response">The <see cref="ServiceResponse"/> to be sent to the <paramref name="client"/>.</param> public void SendResponse(Guid client, ServiceResponse response) { SendResponse(client, response, true); }
/// <summary> /// Sends the specified <paramref name="response"/> to all <see cref="RemoteClients"/>. /// </summary> /// <param name="response">The <see cref="ServiceResponse"/> to be sent to all <see cref="RemoteClients"/>.</param> public void SendResponse(ServiceResponse response) { SendResponse(Guid.Empty, response); }