public void Fire() { config = MessageProcessor.GetConfig; if (config == null) { return; } if (HasPersistenceFiles()) { Logger.info("Stored files are present, attempting to restablish communications"); System.Console.WriteLine("Stored files are present, attempting to restablish communications"); //grab the first one AddDataRequestMsg rr = MessageProcessor.ReadItemsFromDisk(); if (rr != null) { bool online = MessageProcessor.ProcessSingleMessage(rr); if (!online) { Logger.warn("Still offline, we'll try again later"); System.Console.WriteLine("Still offline, we'll try again later"); MessageProcessor.WriteToDisk(rr); } else { Logger.info("We're online, add all items to the cache."); System.Console.WriteLine("We're online, add all items to the cache."); while (HasPersistenceFiles()) { rr = MessageProcessor.ReadItemsFromDisk(); if (rr != null) { //use standard built in functions to enqueue the data MessageProcessor.ProcessMessage(rr); } //throttling if (MessageProcessor.GetQueueSize() > 100) { Thread.Sleep(5000); } } } } } }
/// <summary> /// Attempts to send a single message sychronously /// this assumes that the prepedMessage object is populated as required. /// minimum settings, url, classification and a unique message id /// </summary> /// <param name="mc"></param> public static bool ProcessSingleMessage(AddDataRequestMsg prepedMessage) { MessageProcessor mp = MessageProcessor.Instance; if (prepedMessage == null) throw new ArgumentNullException(); if (dcsservice != null) try { ((IClientChannel)dcsservice).Dispose(); ((IClientChannel)dcsservice).Close(); } catch { } dcsservice = config.GetDCSProxy(config.dcsurl[0]); AddDataRequestMsg[] req = new AddDataRequestMsg[1]; ; req[0] = prepedMessage; bool success = false; for (int b = 0; b < config.dcsurl.Count; b++) { if (!success) for (int a = 0; a < config.DCSretrycount; a++) { try { AddDataResponseMsg response = dcsservice.AddMoreData(req); if ((response.Status == DataResponseStatus.Failure)) { //this case usually only happens when the same transaction was attempted to be written more than once or there was a problemn on the other end success = true; } else { success = true; break; } } catch (Exception ex) { _lasterror = ex.GetType().FullName + " " + ex.Message; Logger.error(ex, "FGSMS.MessageProcessor" + " ProcessSingleMessage - Data acquistion successful, but there was an error sending the information to the data collector. (incorrect password or URL?) "); } } if (b + 1 < config.dcsurl.Count) { if (dcsservice != null) { try { ((IClientChannel)dcsservice).Close(); ((IClientChannel)dcsservice).Dispose(); } catch (Exception ex) { } } dcsservice = config.GetDCSProxy(config.dcsurl[b + 1]); } } try { ((IClientChannel)dcsservice).Close(); ((IClientChannel)dcsservice).Dispose(); } catch { } return success; }
private static AddDataRequestMsg PrepMessage(MessageCorrelator message) { Logger.debug(DateTime.Now.ToString("o") + " Prepmessage enter " + message.MessageID); MessageProcessor i = MessageProcessor.Instance; Logger.info("SendPerformanceData, Data acquistion successful, sending data to the data collector." + message.URL + " at " + message.RecievedAt + " recording agent " + message.agenttype); //if (FGSMSConstants.log) log.TraceEvent(TraceEventType.Information, 0, ()); AddDataRequestMsg request = new AddDataRequestMsg(); request = new AddDataRequestMsg(); request.Action = message.soapAction; if (String.IsNullOrEmpty(message.URL)) { Logger.warn("FGSMS.MessageProcessor Send Peformance Data, the url on this message is null, skipping"); message = null; return null; } if (message.agenttype.ToLower().Contains(".client")) message.URL = ModifyURL(message.URL, true); else message.URL = ModifyURL(message.URL, false); Logger.debug( DateTime.Now.ToString("o") + " Prepmessage after url mod " + message.MessageID); //This double check is required due to adjusted urls do not remove if (IsOnIgnoreList(message.URL)) { message = null; try { //if (FGSMSConstants.log) EventLog.WriteEntry("FGSMS.MessageProcessor", "Message ignored due to ignore list " + message.originalUrl + " translated " + message.URL, EventLogEntryType.Information); } catch { } return null; } PolicyHelper myPolicy = FetchPolicy(message.URL); Logger.debug( DateTime.Now.ToString("o") + " Prepmessage after pol fetch" + message.MessageID); if (myPolicy == null) return null; //record the request if (!String.IsNullOrEmpty(message.RequestMessage)) if (myPolicy.policy.RecordRequestMessage) { request.XmlRequest = message.RequestMessage; //truncate the request if (myPolicy.policy.RecordedMessageCap < request.XmlRequest.Length) request.XmlRequest = request.XmlRequest.Substring(0, myPolicy.policy.RecordedMessageCap); } //record response if (!String.IsNullOrEmpty(message.ResponseMessage)) if (myPolicy.policy.RecordResponseMessage) { request.XmlResponse = message.ResponseMessage; //truncate the response if (myPolicy.policy.RecordedMessageCap < request.XmlResponse.Length) request.XmlResponse = request.XmlResponse.Substring(0, myPolicy.policy.RecordedMessageCap); } request.requestSize = (int)message.requestsize; request.responseSize = (int)message.responsesize; if (myPolicy.policy.RecordFaultsOnly && message.IsFault) { request.XmlRequest = message.RequestMessage; request.XmlResponse = message.ResponseMessage; //truncate the request if (myPolicy.policy.RecordedMessageCap < request.XmlRequest.Length) request.XmlRequest = request.XmlRequest.Substring(0, myPolicy.policy.RecordedMessageCap); //truncate the response if (myPolicy.policy.RecordedMessageCap < request.XmlResponse.Length) request.XmlResponse = request.XmlResponse.Substring(0, myPolicy.policy.RecordedMessageCap); } if (myPolicy.policy.RecordHeaders) { if (message.RequestHeaders != null && message.RequestHeaders.Count > 0) { List<header> l = new List<header>(); for (int b = 0; b < message.RequestHeaders.AllKeys.Length; b++) { header h = new header(); h.name = message.RequestHeaders.AllKeys[b]; h.value = new string[] { message.RequestHeaders[message.RequestHeaders.AllKeys[b]] }; l.Add(h); } request.headersRequest = l.ToArray(); l = null; } if (message.ResponseHeaders != null && message.ResponseHeaders.Count > 0) { List<header> l = new List<header>(); for (int b = 0; b < message.ResponseHeaders.Count; b++) { header h = new header(); h.name = message.ResponseHeaders.AllKeys[b]; h.value = new string[] { message.ResponseHeaders[message.ResponseHeaders.AllKeys[b]] }; l.Add(h); } request.headersResponse = l.ToArray(); l = null; } } request.Identity = GetUserIdentity(myPolicy.policy.UserIdentification, ref message); long ticks = message.CompletedAt.Ticks - message.RecievedAt.Ticks; request.recordedat = message.RecievedAt; TimeSpan ts = new TimeSpan(ticks); request.responseTime = (ts.Hours * 60 * 60 * 1000) + (ts.Minutes * 60 * 1000) + (ts.Seconds * 1000) + (ts.Milliseconds); request.ServiceHost = System.Environment.MachineName.ToLower(); request.Success = !message.IsFault; request.URI = message.URL; request.TransactionID = message.MessageID; request.TransactionThreadID = message.threadid; request.RelatedTransactionID = message.relatedtransactionid; request.agentType = message.agenttype; request.RequestURI = message.originalUrl; request.Action = message.soapAction; request.Action = request.Action.Replace("\"", string.Empty); request.Action = request.Action.Replace("'", string.Empty); long memused = System.GC.GetTotalMemory(false); ulong installedMemory = 0; ulong freememroy = 0; MEMORYSTATUSEX memStatus = new MEMORYSTATUSEX(); if (GlobalMemoryStatusEx(memStatus)) { installedMemory = memStatus.ullTotalPhys; freememroy = memStatus.ullAvailPhys; } request.Message += "Agent Outbound queue: " + the_queue.Count + " Outbound Threads " + ThreadPoolSize() + " Thread " + Thread.CurrentThread.Name; request.Message += " mem stat " + freememroy + "/" + installedMemory; /*if (String.IsNullOrEmpty(message.RequestMessage)) request.requestSize = 0; else request.requestSize = message.RequestMessage.Length; if (String.IsNullOrEmpty(message.ResponseMessage)) request.responseSize = 0; else request.responseSize = message.ResponseMessage.Length;*/ request.classification = currentlevel; Logger.debug( DateTime.Now.ToString("o") + " Prepmessage exit " + message.MessageID); message = null; return request; }
/// <summary> /// Enqueues a previously prepped message, typically used by the persistent storage agent /// this assumes that the message corrleator object is populated as required. /// minimum settings, url, classification and a unique message id /// </summary> /// <param name="mc"></param> public static void ProcessMessage(AddDataRequestMsg req) { MessageProcessor mp = MessageProcessor.Instance; if (req == null) return; the_queue.Enqueue(req); lock (publishingThread) { if (publishingThread == null || publishingThread.Count == 0) { Thread t = new Thread(new ThreadStart(SendBulkPerformanceData)); t.Name = "FGSMS DataPusher from thread " + Thread.CurrentThread.ManagedThreadId; t.Start(); publishingThread.Add(t); } if (the_queue.Count > 1000)// && !Event.agenttype.EndsWith("client", StringComparison.CurrentCultureIgnoreCase)) { lock (publishingThread) { if (publishingThread.Count < 2) { //Shutdown = false; // ThreadStart ts = new ThreadStart(SendPerformanceData); Thread t = new Thread(new ThreadStart(SendBulkPerformanceData)); t.Name = "FGSMS Data Pusher from thread " + Thread.CurrentThread.Name; t.IsBackground = true; t.Start(); publishingThread.Add(t); } else { for (int k = 0; k < publishingThread.Count; k++) { if (publishingThread[k] == null || !publishingThread[k].IsAlive) { publishingThread[k] = new Thread(new ThreadStart(SendBulkPerformanceData)); publishingThread[k].Name = "FGSMS Data Pusher from thread " + Thread.CurrentThread.Name; publishingThread[k].IsBackground = true; publishingThread[k].Start(); } } } } } } }
/// <summary> /// uses configuration object to store data to disk, if it's not possible, it's thrown out /// </summary> /// <param name="addDataRequestMsg"></param> public static void WriteToDisk(AddDataRequestMsg addDataRequestMsg) { MessageProcessor m = MessageProcessor.Instance; if (config.ServiceUnavailableBehavior == ConfigLoader.UnavailableBehavior.HOLDPERSIST) { if (!String.IsNullOrEmpty(config.PersistLocation)) { if (!Directory.Exists(config.PersistLocation)) { try { Directory.CreateDirectory(config.PersistLocation); } catch { } } if (Directory.Exists(config.PersistLocation)) { try { StringWriter sw = new StringWriter(); XmlSerializer xs = new XmlSerializer(typeof(AddDataRequestMsg)); xs.Serialize(sw, addDataRequestMsg); File.WriteAllText(config.PersistLocation + "\\" + addDataRequestMsg.TransactionID + ".msg", new Util().EN(sw.ToString()), System.Text.Encoding.UTF8); } catch (Exception ex) { Logger.error(ex, "FGSMS.MessageProcessor" + " WriteItemsFromDisk (HOLDANDPERSIST) Error caught writing to disk." + config.PersistLocation); } } } } }