Ejemplo n.º 1
0
 /// <summary>
 /// Saves a Manta Event.
 /// </summary>
 /// <param name="evt">Event to save.</param>
 /// <returns>The Events ID</returns>
 internal int Save(MantaEvent evt)
 {
     return(SaveAsync(evt).Result);
 }
Ejemplo n.º 2
0
 internal async Task <int> SaveAsync(MantaEvent evt)
 {
     return(await EventDB.SaveAsync(evt));
 }
Ejemplo n.º 3
0
        private async Task ForwardEventAsync(MantaEvent evt)
        {
            try
            {
                if (_IsStopping)
                {
                    return;
                }

                // Create the HTTP POST request to the remove endpoint.
                var httpRequest = (HttpWebRequest)WebRequest.Create(MtaParameters.EventForwardingHttpPostUrl);
                httpRequest.Method      = "POST";
                httpRequest.ContentType = "text/json";

                // Convert the Event to JSON.
                string eventJson = string.Empty;
                switch (evt.EventType)
                {
                case MantaEventType.Abuse:
                    eventJson = new JavaScriptSerializer().Serialize((MantaAbuseEvent)evt);
                    break;

                case MantaEventType.Bounce:
                    eventJson = new JavaScriptSerializer().Serialize((MantaBounceEvent)evt);
                    break;

                case MantaEventType.TimedOutInQueue:
                    eventJson = new JavaScriptSerializer().Serialize((MantaTimedOutInQueueEvent)evt);
                    break;

                default:
                    eventJson = new JavaScriptSerializer().Serialize(evt);
                    break;
                }

                // Remove the forwarded property as it is internal only.
                eventJson = Regex.Replace(eventJson, ",\"Forwarded\":(false|true)", string.Empty);

                // Write the event json to the POST body.
                using (StreamWriter writer = new StreamWriter(await httpRequest.GetRequestStreamAsync()))
                {
                    await writer.WriteAsync(eventJson);
                }

                // Send the POST and get the response.
                HttpWebResponse httpResponse = (HttpWebResponse)await httpRequest.GetResponseAsync();

                // Get the response body.
                string responseBody = string.Empty;
                using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    responseBody = await reader.ReadToEndAsync();
                }

                // If response body is just a "." then event was received successfully.
                if (responseBody.Trim().StartsWith("."))
                {
                    // Log that the event forwared.
                    evt.Forwarded = true;
                    await EventsManager.Instance.SaveAsync(evt);
                }
            }
            catch (Exception ex)
            {
                // We failed to forward the event. Most likly because the remote server didn't respond.
                Logging.Error("Failed to forward event " + evt.ID, ex);
            }
        }