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 void SendEvents() { ReturnCode rc; try { eventPush = new WspEventPublish(); } catch (Exception e) { Console.WriteLine(e.Message); return; } WebpageEvent localEvent = new WebpageEvent(); localEvent.ActiveXControls = true; localEvent.AnonymousId = Guid.NewGuid(); localEvent.Aol = true; localEvent.BackgroundSounds = true; localEvent.Beta = false; localEvent.Browser = @"IE 6.0"; localEvent.Cdf = false; localEvent.ClrVersion = new Version(@"1.2.3.4"); localEvent.Cookies = true; localEvent.Crawler = false; localEvent.EcmaScriptVersion = new Version(@"1.2.3.4"); localEvent.Ext = string.Empty; localEvent.Frames = true; localEvent.HostDomain = @"microsoft"; localEvent.JavaApplets = true; localEvent.LogicalUri = new Uri(@"http://www.microsoft.com"); localEvent.MajorVersion = 1; localEvent.MinorVersion = 2.3; localEvent.MSDomVersion = new Version(@"1.2.3.4"); localEvent.Platform = @"Windows XP"; localEvent.RequestType = @"Page Request"; localEvent.Source = @"source"; localEvent.SourceServer = @"sourceserver"; localEvent.StatusCode = 0; localEvent.SubDirectory = @"eventcollection"; localEvent.Tables = true; localEvent.Type = @"type"; localEvent.UriHash = new System.Security.Cryptography.MD5CryptoServiceProvider(); localEvent.UriHash.Initialize(); localEvent.UriQuery = new Uri(@"http://www.microsoft.com/test"); localEvent.UriStem = new Uri(@"http://www.microsoft.com"); localEvent.UrlReferrer = new Uri(@"http://www.microsoft.com"); localEvent.UrlReferrerDomain = @"microsoft.com"; localEvent.UserAgent = @"useragent kasgfig;lkartpoiwhtlnvoaing;oakng;aih;akng;lna;kn"; localEvent.UserHostAddress = new IPAddress(0x2414188f); localEvent.VBScript = true; localEvent.Version = @"1.2.3.4"; localEvent.VirtualRoot = @"http://www.microsoft.com/test"; localEvent.W3CDomVersion = new Version(@"1.2.3.4"); localEvent.Win16 = false; localEvent.Win32 = true; Dictionary <byte, string> pb = new Dictionary <byte, string>(); MD5 md5Hasher = MD5.Create(); Random rdm = new Random(10000); try { for (int i = 0; i < numIterations; i++) { byte[] body = localEvent.Serialize(); byte[] bodyHash = md5Hasher.ComputeHash(body); pb[100] = "test" + rdm.Next(10000).ToString(); pb[101] = Convert.ToBase64String(bodyHash); WspEvent wspEvent = new WspEvent(localEvent.EventType, pb, body); eventPush.OnNext(wspEvent, out rc); // Verifying that serialization/deserialization work byte[] serializedEvent = wspEvent.SerializedEvent; WspEvent dupEvent = new WspEvent(serializedEvent); WebpageEvent dupWebPageEvent = new WebpageEvent(dupEvent.Body); if (rc != ReturnCode.Success) { Thread.Sleep(0); Console.WriteLine("Item: " + i.ToString() + @" Return Code: " + rc.ToString()); i--; } } } catch (Exception e) { Console.WriteLine(@"Exception: " + e.Message); } DateTime stopTime = DateTime.Now; TimeSpan totalTime = new TimeSpan(stopTime.Ticks - startTime.Ticks); Console.WriteLine("Total time was: " + totalTime.TotalSeconds.ToString() + " seconds"); Console.WriteLine((numIterations / totalTime.TotalSeconds).ToString() + " events per second"); localEvent.UriHash.Clear(); localEvent.UriHash = null; }
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; } }
/// <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; } } } } } }
private void SendPersistEvent(PersistFileState fileState, PersistEventInfo eventInfo, string outFileName) { PersistFileEvent persistFileEvent; PubSubManager.ReturnCode rc; try { persistFileEvent = persistFileEvents.Pop(); } catch { persistFileEvent = new PersistFileEvent(); } persistFileEvent.PersistEventType = eventInfo.PersistEventType; persistFileEvent.FileState = fileState; persistFileEvent.FileName = outFileName; persistFileEvent.SettingLocalOnly = eventInfo.LocalOnly; persistFileEvent.SettingMaxCopyInterval = (int)(eventInfo.CopyIntervalTicks / 10000000); persistFileEvent.SettingMaxFileSize = eventInfo.MaxFileSize; persistFileEvent.SettingFieldTerminator = eventInfo.FieldTerminator; persistFileEvent.SettingRowTerminator = eventInfo.RowTerminator; persistFileEvent.SettingBeginObjectSeparator = eventInfo.BeginObjectSeparator; persistFileEvent.SettingEndObjectSeparator = eventInfo.EndObjectSeparator; persistFileEvent.SettingBeginArraySeparator = eventInfo.BeginArraySeparator; persistFileEvent.SettingEndArraySeparator = eventInfo.EndArraySeparator; persistFileEvent.SettingKeyValueSeparator = eventInfo.KeyValueSeparator; persistFileEvent.SettingStringDelimiter = eventInfo.StringDelimiter; persistFileEvent.SettingEscapeCharacter = eventInfo.EscapeCharacter; persistFileEvent.FileNameBase = fileNameBase; if (fileState == PersistFileState.Open || outFileName == null) { persistFileEvent.FileSize = 0; } else { try { persistFileEvent.FileSize = (new FileInfo(outFileName)).Length; } catch { persistFileEvent.FileSize = 0; } } for (int i = 0; i < 20; i++) { try { WspEvent wspEvent = new WspEvent(PersistFileEvent.EventType, null, persistFileEvent.Serialize()); eventPush.OnNext(wspEvent, out rc); if (rc == PubSubManager.ReturnCode.Success) { break; } } catch { } } persistFileEvents.Push(persistFileEvent); }