using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Net; namespace InABox.Core { public interface IPollHandler { Type Type { get; } /// /// Polls for notifications, called on client connection. /// /// New notifications IEnumerable Poll(Guid session); } public class PollHandler : IPollHandler where TNotification : BaseObject { public delegate IEnumerable PollEvent(Guid session); public event PollEvent? OnPoll; public Type Type => typeof(TNotification); public PollHandler() { } public PollHandler(PollEvent poll) { OnPoll += poll; } public IEnumerable Poll(Guid session) => OnPoll?.Invoke(session) ?? Array.Empty(); } public abstract class Notifier { protected abstract void NotifyAll(TNotification notification) where TNotification : BaseObject; protected abstract void NotifySession(Guid session, TNotification notification) where TNotification : BaseObject; protected abstract void NotifySession(Guid session, Type TNotification, BaseObject notification); protected abstract IEnumerable GetUserSessions(Guid user); protected abstract IEnumerable GetSessions(Platform platform); public void Push(TNotification notification) where TNotification : BaseObject { NotifyAll(notification); } public void Push(Guid session, Type TNotification, BaseObject notification) { NotifySession(session, TNotification, notification); } public void Push(Guid session, TNotification notification) where TNotification : BaseObject { NotifySession(session, notification); } public void PushUser(Guid user, TNotification notification) where TNotification : BaseObject { foreach (var session in GetUserSessions(user)) { NotifySession(session, notification); } } public void Push(Platform platform, TNotification notification) where TNotification : BaseObject { foreach (var session in GetSessions(platform)) { NotifySession(session, notification); } } } }