Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.Composition;
- using System.Linq;
- using System.Runtime.Serialization;
- using XSockets.Core.Globals;
- using XSockets.Core.XSocket;
- using XSockets.Core.XSocket.Event.Attributes;
- using XSockets.Core.XSocket.Helpers;
- using XSockets.Core.XSocket.Interface;
- namespace XSocketHandler
- {
- /// <summary>
- /// Provides method for knowing which client is listening to what event(s).
- /// Will be improved in the next version when the JSAPI provides callbacks for the bind method.
- /// </summary>
- [Export(typeof(IXBaseSocket))]
- [XBaseSocketMetadata("EventController", Constants.GenericTextBufferSize)]
- public class EventController : XBaseSocket
- {
- //Static list holding all current subscriptions
- public static IDictionary<string, IDictionary<string, IList<Guid>>> EventSubscribers;
- //Methods for Create, Read, Delete
- public static void AddSubscriber(string xsocketAlias, string @event, Guid node)
- {
- //Add the handler if it does not exists
- if (!EventSubscribers.ContainsKey(xsocketAlias))
- {
- EventSubscribers.Add(xsocketAlias, new Dictionary<string, IList<Guid>>());
- }
- //Add the event to the handler if it does not exits
- if (!EventSubscribers[xsocketAlias].ContainsKey(@event))
- {
- EventSubscribers[xsocketAlias].Add(new KeyValuePair<string, IList<Guid>>(@event, new List<Guid>()));
- }
- //Add the guid to the handler and event
- EventSubscribers[xsocketAlias][@event].Add(node);
- }
- public static void RemoveSubscriber(string xsocketAlias, string @event, Guid node)
- {
- //If there is a handler and event registered, remove the node...
- if (EventSubscribers.ContainsKey(xsocketAlias))
- if (EventSubscribers[xsocketAlias].ContainsKey(@event))
- EventSubscribers[xsocketAlias][@event].Remove(node);
- }
- public static IList<Guid> GetSubscribers(string xsocketAlias, string @event)
- {
- //Return 0 if there is no registered handler and event... Else return number of guid registered
- if (!EventSubscribers.ContainsKey(xsocketAlias))
- return null;
- return EventSubscribers[xsocketAlias].ContainsKey(@event) ? EventSubscribers[xsocketAlias][@event] : null;
- }
- public static IEnumerable<PubSub> GetAllSubscribers()
- {
- return from eventSubscriber in EventSubscribers from @event in eventSubscriber.Value select new PubSub() { Alias = eventSubscriber.Key, Event = @event.Key, Nodes = @event.Value };
- }
- public static void RemoveSubscriptions(Guid node)
- {
- foreach (var n in
- from @event in EventSubscribers.Values from n in @event.Values where n.Contains(node) select n)
- {
- n.Remove(node);
- }
- }
- public EventController()
- {
- //Setup disconnect event
- this.OnClientDisConnect += EventController_OnClientDisConnect;
- }
- static EventController()
- {
- //Create a new instance (once)
- EventSubscribers = new Dictionary<string, IDictionary<string, IList<Guid>>>();
- }
- //Remove a clients subscriptions on disconnect
- static void EventController_OnClientDisConnect(object sender, XSockets.Core.XSocket.Event.Arguments.OnClientDisConnectArgs e)
- {
- RemoveSubscriptions(e.XNode.Guid);
- }
- //Add a subscription
- [HandlerEvent("Subscribe")]
- public void Subscribe(PubSub e)
- {
- AddSubscriber(e.Alias, e.Event, this.XNode.Guid);
- }
- //remove a subscription
- [HandlerEvent("Unsubscribe")]
- public void Unsubscribe(PubSub e)
- {
- RemoveSubscriber(e.Alias, e.Event, this.XNode.Guid);
- }
- //Get all to a Alias and a Event
- [HandlerEvent("GetSubscriptions")]
- public void GetSubscriptions(PubSub e)
- {
- var subscribers = GetSubscribers(e.Alias, e.Event);
- this.Send(new { Subscribers = subscribers }.AsXSocketMessage("OnGetSubscriptions"));
- }
- //Get all subscriptions a a list of PubSub objects.
- [HandlerEvent("GetAllSubscriptions")]
- public void GetAllSubscriptions(PubSub e)
- {
- var subscriptions = GetAllSubscribers();
- this.Send(new { Subscriptions = subscriptions }.AsXSocketMessage("OnGetAllSubscriptions"));
- }
- public override IXBaseSocket NewInstance()
- {
- return new EventController();
- }
- }
- [Serializable]
- [DataContract]
- public class PubSub
- {
- public PubSub()
- {
- }
- [DataMember]
- public string Event { get; set; }
- [DataMember]
- public string Alias { get; set; }
- [DataMember]
- public IList<Guid> Nodes { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement