Ejemplo n.º 1
0
        /// <summary>
        /// Registers an event handler of a specific type of personal events.
        /// </summary>
        /// <param name="PersonalEventType">Type of personal event.</param>
        /// <param name="Handler">Event handler.</param>
        public void RegisterHandler(Type PersonalEventType, PersonalEventNotificationEventHandler Handler)
        {
            if (!typeof(IPersonalEvent).GetTypeInfo().IsAssignableFrom(PersonalEventType.GetTypeInfo()))
            {
                throw new ArgumentException("Not a personal event type.", nameof(PersonalEventType));
            }

            IPersonalEvent PersonalEvent = (IPersonalEvent)Activator.CreateInstance(PersonalEventType);

            lock (this.handlers)
            {
                if (!this.handlers.TryGetValue(PersonalEventType, out PersonalEventNotificationEventHandler[] Handlers))
Ejemplo n.º 2
0
        /// <summary>
        /// Publishes an item on a node.
        /// </summary>
        /// <param name="PersonalEvent">Personal event.</param>
        /// <param name="Callback">Method to call when operation completes.</param>
        /// <param name="State">State object to pass on to callback method.</param>
        public void Publish(IPersonalEvent PersonalEvent, ItemResultEventHandler Callback, object State)
        {
            string ItemId = PersonalEvent.ItemId;

            if (ItemId is null)
            {
                this.pubSubClient?.Publish(string.Empty, PersonalEvent.Node, string.Empty, PersonalEvent.PayloadXml, Callback, State);
            }
            else
            {
                this.pubSubClient?.Publish(string.Empty, PersonalEvent.Node, ItemId, PersonalEvent.PayloadXml, Callback, State);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Publishes an item on a node.
        /// </summary>
        /// <param name="PersonalEvent">Personal event.</param>
        /// <returns>ID of published item.</returns>
        public Task <string> PublishAsync(IPersonalEvent PersonalEvent)
        {
            TaskCompletionSource <string> Result = new TaskCompletionSource <string>();
            string ItemId = PersonalEvent.ItemId;

            if (ItemId is null)
            {
                this.pubSubClient?.Publish(string.Empty, PersonalEvent.Node, string.Empty, PersonalEvent.PayloadXml, this.AsyncCallback, Result);
            }
            else
            {
                this.pubSubClient?.Publish(string.Empty, PersonalEvent.Node, ItemId, PersonalEvent.PayloadXml, this.AsyncCallback, Result);
            }

            return(Result.Task);
        }
Ejemplo n.º 4
0
        private static Dictionary <string, IPersonalEvent> GetPersonalEventTypes2()
        {
            Dictionary <string, IPersonalEvent> Result = new Dictionary <string, IPersonalEvent>();

            foreach (Type T in Types.GetTypesImplementingInterface(typeof(IPersonalEvent)))
            {
                if (T.GetTypeInfo().IsAbstract)
                {
                    continue;
                }

                try
                {
                    IPersonalEvent PersonalEvent = (IPersonalEvent)Activator.CreateInstance(T);
                    Result[PersonalEvent.LocalName + " " + PersonalEvent.Namespace] = PersonalEvent;
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }

            return(Result);
        }
Ejemplo n.º 5
0
        private void PubSubClient_ItemNotification(object Sender, ItemNotificationEventArgs e)
        {
            if (this.hasPubSubComponent && e.From.IndexOf('@') < 0)
            {
                try
                {
                    this.NonPepItemNotification?.Invoke(this, e);
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }
            else
            {
                if (string.Compare(e.FromBareJID, this.client.BareJID, true) != 0)
                {
                    RosterItem Item = this.client[e.FromBareJID];
                    if (Item is null || (Item.State != SubscriptionState.Both && Item.State != SubscriptionState.To))
                    {
                        return;
                    }
                }

                IPersonalEvent PersonalEvent = null;

                foreach (XmlNode N in e.Item.ChildNodes)
                {
                    if (N is XmlElement E && personalEventTypes.TryGetValue(E.LocalName + " " + E.NamespaceURI, out IPersonalEvent PersonalEvent2))
                    {
                        PersonalEvent = PersonalEvent2.Parse(E);
                        break;
                    }
                }

                if (PersonalEvent != null)
                {
                    PersonalEventNotificationEventHandler[] Handlers;

                    lock (this.handlers)
                    {
                        if (!this.handlers.TryGetValue(PersonalEvent.GetType(), out Handlers))
                        {
                            return;
                        }
                    }

                    PersonalEventNotificationEventArgs e2 = new PersonalEventNotificationEventArgs(PersonalEvent, this, e);

                    foreach (PersonalEventNotificationEventHandler Handler in Handlers)
                    {
                        try
                        {
                            Handler.Invoke(this, e2);
                        }
                        catch (Exception ex)
                        {
                            Log.Critical(ex);
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Event argument for personal event notification events.
 /// </summary>
 /// <param name="e">Message event arguments</param>
 public PersonalEventNotificationEventArgs(PersonalEventNotificationEventArgs e)
     : base(e)
 {
     this.personalEvent = e.personalEvent;
     this.pepClient     = e.pepClient;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Event argument for personal event notification events.
 /// </summary>
 /// <param name="PersonalEvent">Personal event</param>
 /// <param name="PepClient">Personal Eventing Protocol (PEP) Client.</param>
 /// <param name="e">Message event arguments</param>
 public PersonalEventNotificationEventArgs(IPersonalEvent PersonalEvent, PepClient PepClient, ItemNotificationEventArgs e)
     : base(e)
 {
     this.personalEvent = PersonalEvent;
     this.pepClient     = PepClient;
 }