public static void ListenToEvents() { EventSource eventSource; SocketInfo socketInfo; WspEvent wspEvent; try { byte[] buffer; int i; try { Manager.ThreadInitialize.Release(); } catch { // If the thread is restarted, this could throw an exception but just ignore } while (true) { Thread.Sleep(0); if (hubRole == true) { buffer = eventQueue.Dequeue((UInt32)configSettings.HubRoleSettings.ThisRouter.Timeout); } else { buffer = eventQueue.Dequeue((UInt32)configSettings.NodeRoleSettings.ParentRouter.Timeout); } if (buffer == null) { continue; } try { wspEvent = new WspEvent(buffer); } catch (Exception e) { EventLog.WriteEntry("WspEventRouter", "Event has invalid format: " + e.ToString(), EventLogEntryType.Error); continue; } eventsProcessed.Increment(); eventsProcessedBytes.IncrementBy((long)buffer.Length); if (string.Compare(wspEvent.InRouterName, LocalRouterName, true) == 0) { eventSource = EventSource.FromLocal; } else { if (Communicator.commSockets.TryGetValue(wspEvent.InRouterName, out socketInfo) == true) { if (socketInfo.Hub == true) { if (string.Compare(socketInfo.Group, configSettings.EventRouterSettings.Group, true) == 0) { eventSource = EventSource.FromHub; } else { eventSource = EventSource.FromPeer; } } else { eventSource = EventSource.FromNode; } } else { eventSource = EventSource.FromLocal; } } if (wspEvent.EventType == Subscription.SubscriptionEvent) { QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = eventSource; try { element.BodyEvent = new Subscription(wspEvent.Body); } catch (EventTypeNotSupportedException) { EventLog.WriteEntry("WspEventRouter", "Invalid subscription event is being published on server: " + wspEvent.OriginatingRouterName, EventLogEntryType.Warning); continue; } for (i = 0; i < 10; i++) { try { subscriptionMgrQueue.Enqueue(element); break; } catch (System.TimeoutException) { continue; } } continue; } try { if (SubscriptionMgr.generalSubscriptions.ContainsKey(wspEvent.EventType) == true || SubscriptionMgr.filteredSubscriptions.ContainsKey(wspEvent.EventType) == true) { QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = eventSource; for (i = 0; i < 10; i++) { try { forwarderQueue.Enqueue(element); break; } catch (System.TimeoutException) { continue; } } } } catch { } if (wspEvent.EventType == configSettings.EventRouterSettings.MgmtGuid) { QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = eventSource; for (i = 0; i < 10; i++) { try { mgmtQueue.Enqueue(element); break; } catch (System.TimeoutException) { continue; } } } if (wspEvent.EventType == configSettings.EventRouterSettings.CmdGuid) { QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = eventSource; for (i = 0; i < 10; i++) { try { cmdQueue.Enqueue(element); break; } catch (System.TimeoutException) { continue; } } } if (Persister.persistEvents.ContainsKey(wspEvent.EventType) == true) { if (Persister.persistEvents[wspEvent.EventType].InUse == true) { QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = eventSource; for (i = 0; i < 10; i++) { try { persisterQueue.Enqueue(element); break; } catch (System.TimeoutException) { continue; } } } } } } catch (ThreadAbortException) { // Another thread has signalled that this worker // thread must terminate. Typically, this occurs when // the main service thread receives a service stop // command. } }
public override void Start() { WspEventPublish eventPush = null; QueueElement element; QueueElement newElement = new QueueElement(); bool elementRetrieved; PubSubManager.ReturnCode rc; try { try { Manager.ThreadInitialize.Release(); } catch { // If the thread is restarted, this could throw an exception but just ignore } while (true) { try { if (hubRole == true) { eventPush = new WspEventPublish((uint)configSettings.HubRoleSettings.ThisRouter.Timeout); } else { eventPush = new WspEventPublish((uint)configSettings.NodeRoleSettings.ParentRouter.Timeout); } break; } catch (SharedQueueException) { Thread.Sleep(10000); } } while (true) { try { element = rePublisherQueue.Dequeue(); if (element == default(QueueElement)) { element = newElement; elementRetrieved = false; } else { elementRetrieved = true; } } catch (InvalidOperationException) { element = newElement; elementRetrieved = false; } while (elementRetrieved == true) { try { eventPush.OnNext(element.WspEvent, out rc); if (rc == PubSubManager.ReturnCode.Success) { break; } else { Thread.Sleep(1); } } catch { rePublisherQueue.Enqueue(element); } } } } catch (ThreadAbortException) { // Another thread has signalled that this worker // thread must terminate. Typically, this occurs when // the main service thread receives a service stop // command. } catch (Exception e) { EventLog.WriteEntry("WspEventRouter", e.ToString(), EventLogEntryType.Warning); } }
/// <summary> /// Resend all subscriptions. This is intended to be used after a connection is made. /// </summary> internal static void ResendSubscriptions(string outRouterName) { Dictionary <byte, string> extendedHeaders = new Dictionary <byte, string>(); lock (subscriptionsLock) { foreach (Guid subscriptionEventType in generalSubscriptions.Keys) { foreach (string inRouterName in generalSubscriptions[subscriptionEventType].Routes.Keys) { if (string.Compare(inRouterName, outRouterName, true) != 0) { extendedHeaders[(byte)HeaderType.OriginatingRouter] = inRouterName; Subscription subscription = new Subscription(); subscription.LocalOnly = false; subscription.Subscribe = true; subscription.SubscriptionEventType = subscriptionEventType; WspEvent wspEvent = new WspEvent(Subscription.SubscriptionEvent, extendedHeaders, subscription.Serialize()); QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = EventSource.FromLocal; element.BodyEvent = subscription; Communicator.socketQueues[outRouterName].Enqueue(element); break; } } } foreach (Guid subscriptionEventType in filteredSubscriptions.Keys) { foreach (string filter in filteredSubscriptions[subscriptionEventType].Filters.Keys) { foreach (string inRouterName in filteredSubscriptions[subscriptionEventType].Filters[filter].GlobalRoutes.Routes.Keys) { if (string.Compare(inRouterName, outRouterName, true) != 0) { extendedHeaders[(byte)HeaderType.OriginatingRouter] = inRouterName; Subscription subscription = new Subscription(); subscription.LocalOnly = false; subscription.Subscribe = true; subscription.SubscriptionEventType = subscriptionEventType; subscription.MethodBody = filteredSubscriptions[subscriptionEventType].Filters[filter].subscription.MethodBody; subscription.UsingLibraries = filteredSubscriptions[subscriptionEventType].Filters[filter].subscription.UsingLibraries; subscription.ReferencedAssemblies = filteredSubscriptions[subscriptionEventType].Filters[filter].subscription.ReferencedAssemblies; WspEvent wspEvent = new WspEvent(Subscription.SubscriptionEvent, extendedHeaders, subscription.Serialize()); QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = EventSource.FromLocal; element.BodyEvent = subscription; Communicator.socketQueues[outRouterName].Enqueue(element); break; } } foreach (string inRouterName in filteredSubscriptions[subscriptionEventType].Filters[filter].UniqueRoutes.Routes.Keys) { if (string.Compare(inRouterName, outRouterName, true) != 0) { extendedHeaders[(byte)HeaderType.OriginatingRouter] = inRouterName; Subscription subscription = new Subscription(); subscription.LocalOnly = false; subscription.Subscribe = true; subscription.SubscriptionEventType = subscriptionEventType; subscription.MethodBody = filteredSubscriptions[subscriptionEventType].Filters[filter].subscription.MethodBody; subscription.UsingLibraries = filteredSubscriptions[subscriptionEventType].Filters[filter].subscription.UsingLibraries; subscription.ReferencedAssemblies = filteredSubscriptions[subscriptionEventType].Filters[filter].subscription.ReferencedAssemblies; WspEvent wspEvent = new WspEvent(Subscription.SubscriptionEvent, extendedHeaders, subscription.Serialize()); QueueElement element = new QueueElement(); element.WspEvent = wspEvent; element.Source = EventSource.FromLocal; element.BodyEvent = subscription; Communicator.socketQueues[outRouterName].Enqueue(element); break; } } } } } }
public override void Start() { QueueElement element; QueueElement newElement = new QueueElement(); Subscription subscriptionEvent; bool elementRetrieved; try { Manager.ThreadInitialize.Release(); } catch { // If the thread is restarted, this could throw an exception but just ignore } try { while (true) { try { element = subscriptionMgrQueue.Dequeue(); if (element == default(QueueElement)) { element = newElement; elementRetrieved = false; } else { elementRetrieved = true; } } catch (InvalidOperationException) { element = newElement; elementRetrieved = false; } if (elementRetrieved == true) { if (element.BodyEvent == null) { element.BodyEvent = new Subscription(element.WspEvent.Body); } subscriptionEvent = (Subscription)element.BodyEvent; if (subscriptionEvent.LocalOnly == false) { if (subscriptionEvent.Subscribe == true) { lock (subscriptionsLock) { if (string.IsNullOrEmpty(subscriptionEvent.MethodBody) == true) { RouteDetail routeDetail; if (generalSubscriptions.TryGetValue(subscriptionEvent.SubscriptionEventType, out routeDetail) == false) { routeDetail = new RouteDetail(); routeDetail.Routes[element.WspEvent.InRouterName] = DateTime.UtcNow.AddMinutes(subscriptionManagement.ExpirationIncrement); generalSubscriptions[subscriptionEvent.SubscriptionEventType] = routeDetail; subscriptionEntries.Increment(); FilterSummary filterSummary; if (filteredSubscriptions.TryGetValue(subscriptionEvent.SubscriptionEventType, out filterSummary) == true) { foreach (string filter in filterSummary.Filters.Keys) { if (filterSummary.Filters[filter].UniqueRoutes.Routes.ContainsKey(element.WspEvent.InRouterName) == true) { filterSummary.Filters[filter].GlobalRoutes.Routes[element.WspEvent.InRouterName] = filterSummary.Filters[filter].UniqueRoutes.Routes[element.WspEvent.InRouterName]; filterSummary.Filters[filter].UniqueRoutes.Routes.Remove(element.WspEvent.InRouterName); } } } } else { routeDetail.Routes[element.WspEvent.InRouterName] = DateTime.UtcNow.AddMinutes(subscriptionManagement.ExpirationIncrement); } } else { FilterSummary filterSummary; FilterDetail filterDetail; if (filteredSubscriptions.TryGetValue(subscriptionEvent.SubscriptionEventType, out filterSummary) == false) { filterSummary = new FilterSummary(); filterDetail = new FilterDetail(); filterSummary.Filters[subscriptionEvent.MethodBody] = filterDetail; CompilerResults results; if (WspEventObservable.CompileFilterMethod(subscriptionEvent.MethodBody, subscriptionEvent.UsingLibraries, subscriptionEvent.ReferencedAssemblies, out filterDetail.filterMethod, out results) == false) { continue; } filterDetail.subscription = subscriptionEvent; subscriptionEntries.Increment(); filteredSubscriptions[subscriptionEvent.SubscriptionEventType] = filterSummary; } else { if (filterSummary.Filters.TryGetValue(subscriptionEvent.MethodBody, out filterDetail) == false) { filterDetail = new FilterDetail(); CompilerResults results; if (WspEventObservable.CompileFilterMethod(subscriptionEvent.MethodBody, subscriptionEvent.UsingLibraries, subscriptionEvent.ReferencedAssemblies, out filterDetail.filterMethod, out results) == false) { continue; } filterDetail.subscription = subscriptionEvent; subscriptionEntries.Increment(); filterSummary.Filters[subscriptionEvent.MethodBody] = filterDetail; } } if (generalSubscriptions.ContainsKey(subscriptionEvent.SubscriptionEventType) == true) { filterDetail.GlobalRoutes.Routes[element.WspEvent.InRouterName] = DateTime.UtcNow.AddMinutes(subscriptionManagement.ExpirationIncrement); } else { filterDetail.UniqueRoutes.Routes[element.WspEvent.InRouterName] = DateTime.UtcNow.AddMinutes(subscriptionManagement.ExpirationIncrement); } } } forwarderQueue.Enqueue(element); } } } if (subscriptionEntries.RawValue > 0 && DateTime.UtcNow > nextTimeout) { lock (subscriptionsLock) { RemoveExpiredEntries(); } nextTimeout = DateTime.UtcNow.AddMinutes(subscriptionManagement.ExpirationIncrement); } } } catch { // intentionally left blank } }
public override void Start() { bool elementRetrieved; WspBuffer serializedEvent; long currentTick; long configFileTick; long localConfigFileTick; PersistEventInfo eventInfo; string eventFieldTerminator = @","; StreamWriter eventStream; PubSubManager.ReturnCode rc; string propName; byte propType; object propValue; try { Manager.ThreadInitialize.Release(); } catch { // If the thread is restarted, this could throw an exception but just ignore } try { eventPush = new WspEventPublish(); while (true) { currentTick = DateTime.UtcNow.Ticks; if (currentTick > nextConfigFileCheckTick) { nextConfigFileCheckTick = currentTick + 300000000; configFileTick = Configurator.GetConfigFileTick(); localConfigFileTick = Configurator.GetLocalConfigFileTick(); if (configFileTick != lastConfigFileTick || localConfigFileTick != lastLocalConfigFileTick) { nextCopyTick = 0; foreach (PersistEventInfo eInfo in persistEvents.Values) { eInfo.Loaded = false; } lock (configFileLock) { Configurator.LoadLogSettings(); Configurator.LoadLocalLogSettings(); } foreach (PersistEventInfo eInfo in persistEvents.Values) { if (eInfo.Loaded == false) { eInfo.InUse = false; eInfo.NextCopyTick = currentTick - 1; eInfo.subscription.Subscribe = false; WspEvent wspEvent = new WspEvent(Subscription.SubscriptionEvent, null, eInfo.subscription.Serialize()); eventPush.OnNext(wspEvent, out rc); } } lastConfigFileTick = configFileTick; lastLocalConfigFileTick = localConfigFileTick; } foreach (PersistEventInfo eInfo in persistEvents.Values) { if (eInfo.InUse == true) { eInfo.subscription.Subscribe = true; WspEvent wspEvent = new WspEvent(Subscription.SubscriptionEvent, null, eInfo.subscription.Serialize()); eventPush.OnNext(wspEvent, out rc); } } } try { element = persisterQueue.Dequeue(); if (element == default(QueueElement)) { elementRetrieved = false; } else { elementRetrieved = true; } } catch (InvalidOperationException) { elementRetrieved = false; } currentTick = DateTime.UtcNow.Ticks; if (currentTick > nextCopyTick) { nextCopyTick = long.MaxValue; foreach (PersistEventInfo persistEventInfo in persistEvents.Values) { if (currentTick > persistEventInfo.NextCopyTick) { persistEventInfo.NextCopyTick = currentTick + persistEventInfo.CopyIntervalTicks; if (persistEventInfo.OutStream != null) { persistEventInfo.OutStream.Close(); SendPersistEvent(PersistFileState.Close, persistEventInfo, persistEventInfo.OutFileName); persistEventInfo.OutStream = null; } if (persistEventInfo.InUse == true) { persistEventInfo.OutFileName = persistEventInfo.TempFileDirectory + fileNameBase + DateTime.UtcNow.ToString("u").Replace(":", "-") + fileNameSuffix; if (File.Exists(persistEventInfo.OutFileName) == true) { persistEventInfo.OutStream = new StreamWriter(File.Open(persistEventInfo.OutFileName, FileMode.Append, FileAccess.Write, FileShare.None), Encoding.Unicode); } else { persistEventInfo.OutStream = new StreamWriter(File.Open(persistEventInfo.OutFileName, FileMode.Create, FileAccess.Write, FileShare.None), Encoding.Unicode); } SendPersistEvent(PersistFileState.Open, persistEventInfo, persistEventInfo.OutFileName); } } if (persistEventInfo.NextCopyTick < nextCopyTick) { nextCopyTick = persistEventInfo.NextCopyTick; } } if (copyInProcess == false) { Thread copyThread = new Thread(new ThreadStart(CopyFile)); copyInProcess = true; copyThread.Start(); } } if (elementRetrieved == true) { eventInfo = persistEvents[element.WspEvent.EventType]; if (eventInfo.InUse == true) { eventFieldTerminator = eventInfo.FieldTerminator.ToString(); eventStream = eventInfo.OutStream; if (eventStream == null) { eventInfo.NextCopyTick = DateTime.UtcNow.Ticks + eventInfo.CopyIntervalTicks; eventInfo.OutFileName = eventInfo.TempFileDirectory + fileNameBase + DateTime.UtcNow.ToString("u").Replace(":", "-") + fileNameSuffix; if (File.Exists(eventInfo.OutFileName) == true) { eventInfo.OutStream = new StreamWriter(File.Open(eventInfo.OutFileName, FileMode.Append, FileAccess.Write, FileShare.None), Encoding.Unicode); } else { eventInfo.OutStream = new StreamWriter(File.Open(eventInfo.OutFileName, FileMode.Create, FileAccess.Write, FileShare.None), Encoding.Unicode); } eventStream = eventInfo.OutStream; if (eventInfo.NextCopyTick < nextCopyTick) { nextCopyTick = eventInfo.NextCopyTick; } SendPersistEvent(PersistFileState.Open, eventInfo, eventInfo.OutFileName); } eventStream.Write(eventInfo.BeginObjectSeparator); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write("OriginatingRouterName"); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(eventInfo.KeyValueSeparator.ToString()); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(CleanseString(element.WspEvent.OriginatingRouterName, eventInfo)); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(eventInfo.FieldTerminator.ToString()); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write("InRouterName"); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(eventInfo.KeyValueSeparator.ToString()); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(CleanseString(element.WspEvent.InRouterName, eventInfo)); eventStream.Write(eventInfo.StringDelimiter.ToString()); serializedEvent = new WspBuffer(element.WspEvent.Body); while (serializedEvent.Position < serializedEvent.Size) { if (serializedEvent.Read(out propName) == false) { throw new EventDeserializationException("Error reading PropertyName from buffer"); } eventStream.Write(eventInfo.FieldTerminator.ToString()); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(CleanseString(propName, eventInfo)); eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(eventInfo.KeyValueSeparator.ToString()); if (serializedEvent.Read(out propType) == false) { throw new EventDeserializationException("Error reading PropertyType from buffer"); } switch (propType) { case (byte)PropertyType.StringDictionary: WriteStringDictionary(eventStream, serializedEvent, eventInfo); break; case (byte)PropertyType.ObjectDictionary: WriteObjectDictionary(eventStream, serializedEvent, eventInfo); break; case (byte)PropertyType.StringList: WriteStringList(eventStream, serializedEvent, eventInfo); break; case (byte)PropertyType.ObjectList: WriteObjectList(eventStream, serializedEvent, eventInfo); break; case (byte)PropertyType.ByteArray: WriteByteArray(eventStream, serializedEvent, eventInfo); break; case (byte)PropertyType.CharArray: WriteCharArray(eventStream, serializedEvent, eventInfo); break; case (byte)PropertyType.DateTime: WriteDateTime(eventStream, serializedEvent, eventInfo); break; case (byte)PropertyType.Int64: if (string.Compare(propName, "EventTime", true) == 0) { WriteDateTime(eventStream, serializedEvent, eventInfo); } else { if (serializedEvent.Read((PropertyType)propType, out propValue) == false) { throw new EventDeserializationException("Error reading PropertyValue from buffer"); } eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(CleanseString(propValue.ToString(), eventInfo)); eventStream.Write(eventInfo.StringDelimiter.ToString()); } break; default: if (serializedEvent.Read((PropertyType)propType, out propValue) == false) { throw new EventDeserializationException("Error reading PropertyValue from buffer"); } eventStream.Write(eventInfo.StringDelimiter.ToString()); eventStream.Write(CleanseString(propValue.ToString(), eventInfo)); eventStream.Write(eventInfo.StringDelimiter.ToString()); break; } } eventStream.Write(eventInfo.EndObjectSeparator); eventStream.Write(eventInfo.RowTerminator); if (eventStream.BaseStream.Length >= eventInfo.MaxFileSize) { eventInfo.NextCopyTick = currentTick - 1; nextCopyTick = eventInfo.NextCopyTick; } } } } } catch (IOException e) { EventLog.WriteEntry("WspEventRouter", e.ToString(), EventLogEntryType.Error); Thread.Sleep(60000); } catch (ThreadAbortException) { // Another thread has signalled that this worker // thread must terminate. Typically, this occurs when // the main service thread receives a service stop // command. } catch (Exception e) { EventLog.WriteEntry("WspEventRouter", e.ToString(), EventLogEntryType.Error); throw e; } }