Advertisement
codeplanner

XSockets EventController

Apr 6th, 2012
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.Composition;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using XSockets.Core.Globals;
  7. using XSockets.Core.XSocket;
  8. using XSockets.Core.XSocket.Event.Attributes;
  9. using XSockets.Core.XSocket.Helpers;
  10. using XSockets.Core.XSocket.Interface;
  11.  
  12. namespace XSocketHandler
  13. {
  14.     /// <summary>
  15.     /// Provides method for knowing which client is listening to what event(s).
  16.     /// Will be improved in the next version when the JSAPI provides callbacks for the bind method.
  17.     /// </summary>
  18.     [Export(typeof(IXBaseSocket))]
  19.     [XBaseSocketMetadata("EventController", Constants.GenericTextBufferSize)]
  20.     public class EventController : XBaseSocket
  21.     {
  22.         //Static list holding all current subscriptions
  23.         public static IDictionary<string, IDictionary<string, IList<Guid>>> EventSubscribers;
  24.  
  25.         //Methods for Create, Read, Delete
  26.         public static void AddSubscriber(string xsocketAlias, string @event, Guid node)
  27.         {
  28.             //Add the handler if it does not exists
  29.             if (!EventSubscribers.ContainsKey(xsocketAlias))
  30.             {
  31.                 EventSubscribers.Add(xsocketAlias, new Dictionary<string, IList<Guid>>());
  32.             }
  33.             //Add the event to the handler if it does not exits
  34.             if (!EventSubscribers[xsocketAlias].ContainsKey(@event))
  35.             {
  36.                 EventSubscribers[xsocketAlias].Add(new KeyValuePair<string, IList<Guid>>(@event, new List<Guid>()));
  37.             }
  38.             //Add the guid to the handler and event
  39.             EventSubscribers[xsocketAlias][@event].Add(node);
  40.         }
  41.         public static void RemoveSubscriber(string xsocketAlias, string @event, Guid node)
  42.         {
  43.             //If there is a handler and event registered, remove the node...
  44.             if (EventSubscribers.ContainsKey(xsocketAlias))
  45.                 if (EventSubscribers[xsocketAlias].ContainsKey(@event))
  46.                     EventSubscribers[xsocketAlias][@event].Remove(node);
  47.         }
  48.         public static IList<Guid> GetSubscribers(string xsocketAlias, string @event)
  49.         {
  50.             //Return 0 if there is no registered handler and event... Else return number of guid registered
  51.             if (!EventSubscribers.ContainsKey(xsocketAlias))
  52.                 return null;
  53.             return EventSubscribers[xsocketAlias].ContainsKey(@event) ? EventSubscribers[xsocketAlias][@event] : null;
  54.         }
  55.         public static IEnumerable<PubSub> GetAllSubscribers()
  56.         {
  57.             return from eventSubscriber in EventSubscribers from @event in eventSubscriber.Value select new PubSub() { Alias = eventSubscriber.Key, Event = @event.Key, Nodes = @event.Value };
  58.         }
  59.  
  60.         public static void RemoveSubscriptions(Guid node)
  61.         {
  62.             foreach (var n in
  63.                 from @event in EventSubscribers.Values from n in @event.Values where n.Contains(node) select n)
  64.             {
  65.                 n.Remove(node);
  66.             }
  67.         }
  68.  
  69.         public EventController()
  70.         {
  71.             //Setup disconnect event
  72.             this.OnClientDisConnect += EventController_OnClientDisConnect;
  73.         }
  74.  
  75.         static EventController()
  76.         {
  77.             //Create a new instance (once)
  78.             EventSubscribers = new Dictionary<string, IDictionary<string, IList<Guid>>>();
  79.         }
  80.  
  81.         //Remove a clients subscriptions on disconnect
  82.         static void EventController_OnClientDisConnect(object sender, XSockets.Core.XSocket.Event.Arguments.OnClientDisConnectArgs e)
  83.         {
  84.             RemoveSubscriptions(e.XNode.Guid);
  85.         }
  86.  
  87.         //Add a subscription
  88.         [HandlerEvent("Subscribe")]
  89.         public void Subscribe(PubSub e)
  90.         {
  91.             AddSubscriber(e.Alias, e.Event, this.XNode.Guid);
  92.         }
  93.  
  94.         //remove a subscription
  95.         [HandlerEvent("Unsubscribe")]
  96.         public void Unsubscribe(PubSub e)
  97.         {
  98.             RemoveSubscriber(e.Alias, e.Event, this.XNode.Guid);
  99.         }
  100.  
  101.         //Get all to a Alias and a Event
  102.         [HandlerEvent("GetSubscriptions")]
  103.         public void GetSubscriptions(PubSub e)
  104.         {
  105.             var subscribers = GetSubscribers(e.Alias, e.Event);
  106.             this.Send(new { Subscribers = subscribers }.AsXSocketMessage("OnGetSubscriptions"));
  107.         }
  108.  
  109.         //Get all subscriptions a a list of PubSub objects.
  110.         [HandlerEvent("GetAllSubscriptions")]
  111.         public void GetAllSubscriptions(PubSub e)
  112.         {
  113.             var subscriptions = GetAllSubscribers();
  114.             this.Send(new { Subscriptions = subscriptions }.AsXSocketMessage("OnGetAllSubscriptions"));
  115.         }
  116.  
  117.         public override IXBaseSocket NewInstance()
  118.         {
  119.             return new EventController();
  120.         }
  121.     }
  122.  
  123.     [Serializable]
  124.     [DataContract]
  125.     public class PubSub
  126.     {
  127.         public PubSub()
  128.         {
  129.         }
  130.         [DataMember]
  131.         public string Event { get; set; }
  132.         [DataMember]
  133.         public string Alias { get; set; }
  134.         [DataMember]
  135.         public IList<Guid> Nodes { get; set; }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement