Notifier.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using InABox.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. namespace InABox.Core
  7. {
  8. public interface IPollHandler
  9. {
  10. Type Type { get; }
  11. /// <summary>
  12. /// Polls for notifications, called on client connection.
  13. /// </summary>
  14. /// <returns>New notifications</returns>
  15. IEnumerable<object> Poll(Guid session);
  16. }
  17. public class PollHandler<TNotification> : IPollHandler
  18. {
  19. public delegate IEnumerable<TNotification> PollEvent(Guid session);
  20. public event PollEvent? OnPoll;
  21. public Type Type => typeof(TNotification);
  22. public PollHandler() { }
  23. public PollHandler(PollEvent poll)
  24. {
  25. OnPoll += poll;
  26. }
  27. public IEnumerable<object> Poll(Guid session) => (OnPoll?.Invoke(session) ?? Array.Empty<TNotification>()).Cast<object>();
  28. }
  29. public abstract class Notifier
  30. {
  31. private List<IPollHandler> Handlers = new List<IPollHandler>();
  32. protected abstract void NotifyAll<TNotification>(TNotification notification) where TNotification : BaseObject;
  33. protected abstract void NotifySession<TNotification>(Guid session, TNotification notification) where TNotification : BaseObject;
  34. protected abstract void NotifySession(Guid session, Type TNotification, object? notification);
  35. protected abstract IEnumerable<Guid> GetUserSessions(Guid user);
  36. protected abstract IEnumerable<Guid> GetSessions(Platform platform);
  37. public void Push<TNotification>(TNotification notification)
  38. where TNotification : BaseObject
  39. {
  40. NotifyAll(notification);
  41. }
  42. public void Push<TNotification>(Guid session, TNotification notification)
  43. where TNotification : BaseObject
  44. {
  45. NotifySession(session, notification);
  46. }
  47. public void PushUser<TNotification>(Guid user, TNotification notification)
  48. where TNotification : BaseObject
  49. {
  50. foreach (var session in GetUserSessions(user))
  51. {
  52. NotifySession(session, notification);
  53. }
  54. }
  55. public void Push<TNotification>(Platform platform, TNotification notification)
  56. where TNotification : BaseObject
  57. {
  58. foreach (var session in GetSessions(platform))
  59. {
  60. NotifySession(session, notification);
  61. }
  62. }
  63. public void Poll(Guid session)
  64. {
  65. foreach (var handler in Handlers)
  66. {
  67. foreach (var notification in handler.Poll(session))
  68. {
  69. NotifySession(session, handler.Type, notification);
  70. }
  71. }
  72. }
  73. public void AddPollHandler<TNotification>(PollHandler<TNotification> handler)
  74. where TNotification : BaseObject
  75. {
  76. Handlers.Add(handler);
  77. }
  78. public void AddPollHandler<TNotification>(PollHandler<TNotification>.PollEvent poll)
  79. where TNotification : BaseObject
  80. {
  81. Handlers.Add(new PollHandler<TNotification>(poll));
  82. }
  83. }
  84. }