PushManager.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace InABox.Core
  5. {
  6. /// <summary>
  7. /// Static class through which the server registers <see cref="IPusher"/>s and <see cref="IPollHandler"/>s, as well as actually pushing messages.
  8. /// </summary>
  9. public class PushManager
  10. {
  11. private static List<IPollHandler> Handlers = new List<IPollHandler>();
  12. private static List<IPusher> Pushers { get; set; } = new List<IPusher>();
  13. private PushManager() { }
  14. public static void AddPusher(IPusher pusher) =>
  15. Pushers.Add(pusher);
  16. public static void Push<TPush>(TPush push) where TPush : BaseObject =>
  17. Pushers.ForEach(x => x.Push(push));
  18. public static void Push<TPush>(Guid session, TPush push) where TPush : BaseObject =>
  19. Pushers.ForEach(x => x.Push(session, push));
  20. public static void PushUser<TPush>(Guid userID, TPush push) where TPush : BaseObject =>
  21. Pushers.ForEach(x => x.PushUser(userID, push));
  22. public static void Push<TPush>(Platform platform, TPush push) where TPush : BaseObject =>
  23. Pushers.ForEach(x => x.Push(platform, push));
  24. public static void Poll(Guid session)
  25. {
  26. foreach (var handler in Handlers)
  27. {
  28. foreach (var push in handler.Poll(session))
  29. {
  30. Pushers.ForEach(x => x.Push(session, handler.Type, push));
  31. }
  32. }
  33. }
  34. public static void AddPollHandler<TPush>(PollHandler<TPush> handler)
  35. where TPush : BaseObject
  36. {
  37. Handlers.Add(handler);
  38. }
  39. public static void AddPollHandler<TPush>(PollHandler<TPush>.PollEvent poll)
  40. where TPush : BaseObject
  41. {
  42. Handlers.Add(new PollHandler<TPush>(poll));
  43. }
  44. }
  45. }