private void SendServiceStateChangedResponse(ServiceState currentState) { ServiceResponse response = new ServiceResponse("SERVICESTATECHANGED"); response.Attachments.Add(new ObjectState<ServiceState>(Name, currentState)); SendResponse(response); }
private void SendProcessStateChangedResponse(string processName, ServiceProcessState currentState) { ServiceResponse response = new ServiceResponse("PROCESSSTATECHANGED"); response.Attachments.Add(new ObjectState<ServiceProcessState>(processName, currentState)); SendResponse(response); }
/// <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, CurtailMessageLength(message)); // Add any specified attachment to the service response if ((object)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); OnUpdatedStatus(requestInfo.Sender.ClientID, response.Message, success ? UpdateType.Information : UpdateType.Alarm); } 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"); } }
private void SendUpdateClientStatusResponse(Guid clientID, UpdateType type, string responseMessage, bool telnetMessage = false) { if (string.IsNullOrEmpty(responseMessage) || (clientID == m_remoteCommandClientID && !telnetMessage)) return; ServiceResponse response = new ServiceResponse(); response.Type = "UPDATECLIENTSTATUS-" + type.ToString().ToUpper(); response.Message = CurtailMessageLength(responseMessage); SendResponse(clientID, response); OnUpdatedStatus(clientID, response.Message, type); }
/// <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. if (m_remotingServer.IsClientConnected(client)) handles = new[] { 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. LogException(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); }
/// <summary> /// Attempts to parse an actionable response 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)) { // Response 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", StringComparison.OrdinalIgnoreCase) == 0); parseSucceeded = true; } } } catch { parseSucceeded = false; } return parseSucceeded; }
/// <summary> /// Raises the <see cref="ReceivedServiceResponse"/> event. /// </summary> /// <param name="response"><see cref="ServiceResponse"/> received.</param> protected virtual void OnReceivedServiceResponse(ServiceResponse response) { if ((object)ReceivedServiceResponse != null) ReceivedServiceResponse(this, new EventArgs<ServiceResponse>(response)); }