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 pushes, called on client connection.
///
/// New pushes
IEnumerable Poll(Guid session);
}
public class PollHandler : IPollHandler
where TPush : BaseObject
{
public delegate IEnumerable PollEvent(Guid session);
public event PollEvent? OnPoll;
public Type Type => typeof(TPush);
public PollHandler() { }
public PollHandler(PollEvent poll)
{
OnPoll += poll;
}
public IEnumerable Poll(Guid session) => OnPoll?.Invoke(session) ?? Array.Empty();
}
///
/// Classes that implement this interface implement the manner by which the server actually gets messages pushed to the client.
/// The implementation will manage the transport layer.
///
///
/// If one wishes to implement , it is likely one should also register it with ;
/// this way, when the server wishes to push a message, it will go through this pusher (as well as any other registered ones).
///
public interface IPusher
{
void PushToAll(TPush push) where TPush : BaseObject;
void PushToSession(Guid session, TPush push) where TPush : BaseObject;
void PushToSession(Guid session, Type TPush, BaseObject push);
IEnumerable GetUserSessions(Guid user);
IEnumerable GetSessions(Platform platform);
public void Push(TPush push)
where TPush : BaseObject
{
PushToAll(push);
}
public void Push(Guid session, Type TPush, BaseObject push)
{
PushToSession(session, TPush, push);
}
public void Push(Guid session, TPush push)
where TPush : BaseObject
{
PushToSession(session, push);
}
public void PushUser(Guid user, TPush push)
where TPush : BaseObject
{
foreach (var session in GetUserSessions(user))
{
PushToSession(session, push);
}
}
public void Push(Platform platform, TPush push)
where TPush : BaseObject
{
foreach (var session in GetSessions(platform))
{
PushToSession(session, push);
}
}
}
}