public bool EnqueueItem(RESTMessageToSend item) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START"); bool success = false; try { workerThreadPool.AddWorkItem(new outgoingDelegate(this.ThreadDoMethod), item); } catch (Exception ex) { logger.Error(MethodBase.GetCurrentMethod().Name + "() - Enqueue exception: ", ex); success = false; } logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END"); return success; }
/// <summary> /// The sendMessage method /// </summary> //public bool SendMessageFromFriendToFriend2(RESTMessageToSend rMessageToSend, out System.Net.HttpStatusCode responseCode) //public bool SendMessageFromUserAToUserB(String RequestToken, String MxitUserID_From, String MxitUserID_To, String messageBody, out System.Net.HttpStatusCode responseCode) public bool SendMessageFromUserAToUserB(String RequestToken, RESTMessageToSend rMessageToSend, out System.Net.HttpStatusCode responseCode) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START"); bool success = false; responseCode = System.Net.HttpStatusCode.Unauthorized;//Need to improve this try { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestClient..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestClient..."); var client = new RestClient(); client.BaseUrl = "http://api.mxit.com"; client.Authenticator = new RESTMxitOAuth2Authenticator(RequestToken); logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestRequest..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestRequest..."); var RESTRequest = new RestRequest(); RESTRequest.Method = Method.POST; RESTRequest.RequestFormat = DataFormat.Json; RESTRequest.AddHeader("Content-Type", "application/json"); RESTRequest.AddHeader("Accept", "application/json"); RESTRequest.Resource = "/message/send/"; //Resource points to the method of the API we want to access RESTRequest.AddBody(rMessageToSend); logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Executing RESTRequest (SendMessage)"); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Executing RESTRequest (SendMessage)"); RestResponse RESTResponse = (RestResponse)client.Execute(RESTRequest); //Set the out parameter, so that the calling method can redo auth if needed and retry: System.Net.HttpStatusCode RESTResponseHTTPStatusCode = RESTResponse.StatusCode; bool sentMessageOK = (RESTResponseHTTPStatusCode == System.Net.HttpStatusCode.OK); if (sentMessageOK) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Sent message OK."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Sent message to OK."); success = true; } else // Something went wrong, we'll handle the error code in the calling wrapper method { logger.Error(MethodBase.GetCurrentMethod().Name + "() - RestSendMessage Failed: (user:"******") (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ")"); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " RestSendMessage FAILED. (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ") Detail:" + RESTResponse.Content); responseCode = RESTResponse.StatusCode; success = false; } } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString() + " Exception sending REST message:" + ex.ToString()); logger.Error(MethodBase.GetCurrentMethod().Name + "() - Exception sending REST message: " + ex.GetType() + " " + ex.ToString()); success = false; } logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END"); return success; }
public bool SendMessageFromUserAToUserB(String token, RESTMessageToSend restMessageToSend) { System.Net.HttpStatusCode responseCode; bool sentMessageOK = false; sentMessageOK = SendMessageFromUserAToUserB(token, restMessageToSend, out responseCode); return sentMessageOK; }
public bool SendMessageFromUserAToUserB(String token, MessageToSend messageToSend) { MXitConnectionModule.RESTMessageToSend RESTMessageToSend = new RESTMessageToSend(messageToSend); return this.SendMessageFromUserAToUserB(token, RESTMessageToSend); }
public bool SendMessage(RESTMessageToSend restMessageToSend) { bool success = false; System.Net.HttpStatusCode responseCode; bool sentMessageOK = false; sentMessageOK = SendMessage(restMessageToSend, out responseCode); if (sentMessageOK) // If all went well sending the message { success = true; } else // If something went wrong sending the message (sentMessagOK == false) { bool isRetryableError = true;//((responseCodeReceived == ) || (responseCodeReceived == System.Net.HttpStatusCode.)); bool isAuthenticationExpired = (responseCode == System.Net.HttpStatusCode.Unauthorized); if (isRetryableError) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - SendMessage Failed, retry sending..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " SendMessage Failed, retry sending..."); bool reauthenticationSuccess = false; if (isAuthenticationExpired) { // was an expired error, so lets re-authenticate: reauthenticationSuccess = this.doRESTAuthentication(); if (reauthenticationSuccess) { if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Redoing authentication..."); sentMessageOK = SendMessage(restMessageToSend, out responseCode); } } else // Was an internal error, so lets just retry sending the message: { sentMessageOK = SendMessage(restMessageToSend, out responseCode); } if (sentMessageOK) { if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Managed to send message after retry."); success = true; } else { logger.Error(MethodBase.GetCurrentMethod().Name + "() - Could not send message even after retry."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Could not send message even after retry."); } } else //Not a retryable error { logger.Error(MethodBase.GetCurrentMethod().Name + "() - Could not send message due to non retryable error: " + responseCode); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Could not send message due to non retryable error: " + responseCode); } } return success; }
public bool SendMessage(MessageToSend messageToSend) { MXitConnectionModule.RESTMessageToSend RESTMessageToSend = new RESTMessageToSend(messageToSend); return this.SendMessage(RESTMessageToSend); }
private bool ThreadDoMethod(RESTMessageToSend rMessageToSend) { bool sendMessageSuccess = false; try { //Print out where we are in the queue: int queueItemCount = workerThreadPool.QueueLength; logger.Info(MethodBase.GetCurrentMethod().Name + "() - queue size: " + queueItemCount); Console.WriteLine(DateTime.Now.ToString() + " queue size: " + queueItemCount); //Do the work: sendMessageSuccess = RESTConnectionHelper.Instance.SendMessage(rMessageToSend); if (!sendMessageSuccess) { logger.Error(MethodBase.GetCurrentMethod().Name + "() - Problem: Couldn't send REST broadcast message..."); Console.WriteLine(DateTime.Now.ToString() + " ERROR: Couldn't send REST broadcast message...: "); } } catch (Exception e) { logger.Error(MethodBase.GetCurrentMethod().Name + "() - Exception: " + e.ToString()); sendMessageSuccess = false; } return sendMessageSuccess; }
/// <summary> /// The sendMessage method /// </summary> public bool SendMessage(RESTMessageToSend rMessageToSend, out System.Net.HttpStatusCode responseCode) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START"); bool success = false; responseCode = System.Net.HttpStatusCode.Unauthorized;//Need to improve this try { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestClient..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestClient..."); var client = new RestClient(); client.BaseUrl = "http://api.mxit.com"; client.Authenticator = new RESTMxitOAuth2Authenticator(this.REST_AccessToken); logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Creating RestRequest..."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Creating RestRequest..."); var REST_SendMessageRequest = new RestRequest(); REST_SendMessageRequest.Method = Method.POST; REST_SendMessageRequest.RequestFormat = DataFormat.Json; REST_SendMessageRequest.AddHeader("Content-Type", "application/json"); REST_SendMessageRequest.AddHeader("Accept", "application/json"); REST_SendMessageRequest.Resource = "/message/send/"; //Resource points to the method of the API we want to access REST_SendMessageRequest.AddBody(rMessageToSend); //Start - Temporary Code //TODO: REMOVE ONCE MXIT MESSAGE PARSING IS FIXED //The below is a Hack to allow the Links Array to be appended if it contains 1 or more values, or remove it otherwise //(if an empty Links array is passed, the message won't send correctly) Parameter messageBody = REST_SendMessageRequest.Parameters.Find( delegate(Parameter p) { return p.Name == "application/json"; }); if (messageBody != null) { //If an empty Link Array exists, remove it from the JSON message messageBody.Value = messageBody.Value.ToString().Replace("\"Links\":[],", ""); REST_SendMessageRequest.Parameters.Find( delegate(Parameter p) { return p.Name == "application/json"; }).Value = messageBody.Value; } //End - Temporary Code logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Executing RESTRequest (SendMessage)"); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Executing RESTRequest (SendMessage)"); RestResponse RESTResponse = (RestResponse)client.Execute(REST_SendMessageRequest); //Set the out parameter, so that the calling method can redo auth if needed and retry: System.Net.HttpStatusCode RESTResponseHTTPStatusCode = RESTResponse.StatusCode; bool sentMessageOK = (RESTResponseHTTPStatusCode == System.Net.HttpStatusCode.OK); //Persist the message sent to DB: rMessageToSend.persistRESTMessageSent_toDB(RESTResponse.Content, RESTResponse.StatusCode.ToString()); if (sentMessageOK) { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - Sent message OK."); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " Sent message to OK."); success = true; } else // Something went wrong, we'll handle the error code in the calling wrapper method { logger.Error(MethodBase.GetCurrentMethod().Name + "() - RestSendMessage Failed: (user:"******") (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ")"); if (logger.IsDebugEnabled) Console.WriteLine(DateTime.Now.ToString() + " RestSendMessage FAILED. (responseCode: " + (Int16)RESTResponseHTTPStatusCode + ")"); responseCode = RESTResponse.StatusCode; success = false; } } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString() + " Exception sending REST message:" + ex.ToString()); logger.Error(MethodBase.GetCurrentMethod().Name + "() - Exception sending REST message: " + ex.GetType() + " " + ex.ToString()); success = false; } logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END"); return success; }
public void EnqueueMessage(RESTMessageToSend rMessageToSend) { ThreadPool_SendRESTMessage.Instance.EnqueueItem(rMessageToSend); }