/// <summary> /// Processes messages received from the server. /// </summary> /// <param name="message">Message received from the server.</param> private void ProcessMessage(string message) { XmlDocument document = new XmlDocument(); document.LoadXml(message); IProcEventData ed = new IProcEventData(document); switch (ed.Type) { case "NodeEventArgs": { lock ( nodeEventMessageQueue ) { nodeEventMessageQueue.Enqueue(ed); } break; } case "CollectionSyncEventArgs": case "FileSyncEventArgs": case "NotifyEventArgs": { lock ( syncEventMessageQueue ) { syncEventMessageQueue.Enqueue(ed); } break; } } // Indicate that a message is ready to process. messageReadyEvent.Set(); }
/// <summary> /// Thread that indicates node and sync events via registered delegates. /// </summary> private void EventThread() { // Stay in the loop until the client has been shutdown. while (state != ClientState.Shutdown) { IProcEventData eventData = null; try { // See if there are any sync events to process. lock ( syncEventMessageQueue ) { if (syncEventMessageQueue.Count > 0) { eventData = syncEventMessageQueue.Dequeue() as IProcEventData; } } if (eventData != null) { ProcessEventData(eventData); } else { // See if there are any node events to process. lock ( nodeEventMessageQueue ) { if (nodeEventMessageQueue.Count > 0) { eventData = nodeEventMessageQueue.Dequeue() as IProcEventData; } } if (eventData != null) { ProcessEventData(eventData); } } } catch (Exception e) { // Don't let the thread terminate because of an exception. ReportError(new ApplicationException("Error in event thread.", e)); } // See if there are any message left to process. if (eventData == null) { messageReadyEvent.WaitOne(); messageReadyEvent.Reset(); } } }
/// <summary> /// Processes queued event data messages by calling the delegates registered for the respective events. /// </summary> /// <param name="eventData">Event message received from the server.</param> private void ProcessEventData(IProcEventData eventData) { switch (eventData.Type) { case "NodeEventArgs": { // Get the node arguments from the document. NodeEventArgs nodeArgs = eventData.ToNodeEventArgs(); // Determine the type of event that occurred. switch (( EventType )Enum.Parse(typeof(EventType), nodeArgs.EventData)) { case EventType.NodeChanged: { if (onChangedNodeEvent != null) { Delegate[] cbList = onChangedNodeEvent.GetInvocationList(); foreach (IProcEventHandler cb in cbList) { try { cb(nodeArgs); } catch (Exception ex) { ReportError(new ApplicationException("Removing subscriber because of exception", ex)); onChangedNodeEvent -= cb; } } } break; } case EventType.NodeCreated: { if (onCreatedNodeEvent != null) { Delegate[] cbList = onCreatedNodeEvent.GetInvocationList(); foreach (IProcEventHandler cb in cbList) { try { cb(nodeArgs); } catch (Exception ex) { ReportError(new ApplicationException("Removing subscriber because of exception", ex)); onCreatedNodeEvent -= cb; } } } break; } case EventType.NodeDeleted: { if (onDeletedNodeEvent != null) { Delegate[] cbList = onDeletedNodeEvent.GetInvocationList(); foreach (IProcEventHandler cb in cbList) { try { cb(nodeArgs); } catch (Exception ex) { ReportError(new ApplicationException("Removing subscriber because of exception", ex)); onDeletedNodeEvent -= cb; } } } break; } } break; } case "CollectionSyncEventArgs": { if (onCollectionSyncEvent != null) { // Get the collection sync arguments from the document. CollectionSyncEventArgs collectionArgs = eventData.ToCollectionSyncEventArgs(); Delegate[] cbList = onCollectionSyncEvent.GetInvocationList(); foreach (IProcEventHandler cb in cbList) { try { cb(collectionArgs); } catch (Exception ex) { ReportError(new ApplicationException("Removing subscriber because of exception", ex)); onCollectionSyncEvent -= cb; } } } break; } case "FileSyncEventArgs": { if (onFileSyncEvent != null) { // Get the file sync arguments from the document. FileSyncEventArgs fileArgs = eventData.ToFileSyncEventArgs(); Delegate[] cbList = onFileSyncEvent.GetInvocationList(); foreach (IProcEventHandler cb in cbList) { try { cb(fileArgs); } catch (Exception ex) { ReportError(new ApplicationException("Removing subscriber because of exception", ex)); onFileSyncEvent -= cb; } } } break; } case "NotifyEventArgs": { if (onNotifyEvent != null) { // Get the notify arguments from the document. NotifyEventArgs notifyArgs = eventData.ToNotifyEventArgs(); Delegate[] cbList = onNotifyEvent.GetInvocationList(); foreach (IProcEventHandler cb in cbList) { try { cb(notifyArgs); } catch (Exception ex) { ReportError(new ApplicationException("Removing subscriber because of exception", ex)); onNotifyEvent -= cb; } } } break; } } }