Skip to content

Commit

Permalink
Presence States
Browse files Browse the repository at this point in the history
  • Loading branch information
budgetpreneur committed Sep 1, 2023
1 parent 1617f6b commit 0b8c0d1
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/Api/PubnubApi/EventEngine/Presence/Events/PresenceEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,19 @@ public class HeartbeatFailureEvent : Core.IEvent {
public PNStatus Status { get; set; }
public virtual string Name { get; set; } = "HEARTBEAT_FAILURE";
}
public class HeartbeatGiveUpEvent : Core.IEvent {
public PNStatus Status { get; set; }
public virtual string Name { get; set; }
}
public class DisconnectEvent : Core.IEvent {
public IEnumerable<string> Channels{ get; set; }
public IEnumerable<string> ChannelGroups{ get; set; }
public string Name { get; set; } = "DISCONNECT";
}
public class TimesUpEvent : Core.IEvent
{
public IEnumerable<string> Channels{ get; set; }
public IEnumerable<string> ChannelGroups{ get; set; }
public string Name { get; set; } = "TIMES_UP";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,33 @@ public class CancelHeartbeatInvocation : HeartbeatInvocation, Core.IEffectCancel
public override string Name { get; set; } = "CANCEL_HANDSHAKE";
}

public class WaitInvocation : Core.IEffectInvocation
{
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public virtual string Name { get; set; } = "WAIT";
}
public class CancelWaitInvocation : WaitInvocation, Core.IEffectCancelInvocation
{
public override string Name { get; set; } = "CANCEL_WAIT";
}

public class LeaveInvocation : Core.IEffectInvocation
{
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public virtual string Name { get; set; } = "LEAVE";
}

public class DelayedHeartbeatInvocation : Core.IEffectInvocation
{
public IEnumerable<string> Channels;
public IEnumerable<string> ChannelGroups;
public virtual string Name { get; set; } = "DELAYED_HEARTBEAT";
}
public class CancelDelayedHeartbeatInvocation : DelayedHeartbeatInvocation, Core.IEffectCancelInvocation
{
public override string Name { get; set; } = "CANCEL_DELAYED_HEARTBEAT";
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using PubnubApi.EventEngine.Core;
using PubnubApi.EventEngine.Presence.Common;
using PubnubApi.EventEngine.Presence.Invocations;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,9 +10,53 @@ namespace PubnubApi.EventEngine.Presence.States
{
public class HeartbeatCooldownState : PresenceState
{
public override IEnumerable<IEffectInvocation> OnEntry => new WaitInvocation()
{ Channels = this.Channels, ChannelGroups = this.ChannelGroups }.AsArray();
public override IEnumerable<IEffectInvocation> OnExit { get; } = new CancelWaitInvocation().AsArray();
public override TransitionResult Transition(IEvent e)
{
throw new NotImplementedException();
return e switch
{
Events.LeftAllEvent leftAll => new HeartbeatInactiveState()
{
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.JoinedEvent joined => new States.HeartbeatingState()
{
Channels = (Channels ?? Enumerable.Empty<string>()).Union(joined.Channels),
ChannelGroups = (ChannelGroups ?? Enumerable.Empty<string>()).Union(joined.ChannelGroups),
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.LeftEvent left => new States.HeartbeatingState()
{
Channels = (Channels ?? Enumerable.Empty<string>()).Union(left.Channels),
ChannelGroups = (ChannelGroups ?? Enumerable.Empty<string>()).Union(left.ChannelGroups),
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.StateSetEvent stateSet => new States.HeartbeatingState()
{
Channels = (Channels ?? Enumerable.Empty<string>()).Union(stateSet.Channels),
ChannelGroups = (ChannelGroups ?? Enumerable.Empty<string>()).Union(stateSet.ChannelGroups),
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.TimesUpEvent timesUp => new States.HeartbeatingState()
{
Channels = (Channels ?? Enumerable.Empty<string>()).Union(timesUp.Channels),
ChannelGroups = (ChannelGroups ?? Enumerable.Empty<string>()).Union(timesUp.ChannelGroups),
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.DisconnectEvent disconnect => new States.HeartbeatStoppedState()
{
ReconnectionConfiguration = this.ReconnectionConfiguration,
},

_ => null
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using PubnubApi.EventEngine.Core;
using PubnubApi.EventEngine.Presence.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PubnubApi.EventEngine.Presence.States
{
public class HeartbeatFailedState : PresenceState
{
public override TransitionResult Transition(IEvent e)
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using PubnubApi.EventEngine.Core;
using PubnubApi.EventEngine.Presence.Common;
using PubnubApi.EventEngine.Presence.Invocations;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,9 +10,61 @@ namespace PubnubApi.EventEngine.Presence.States
{
public class HeartbeatReconnectingState : PresenceState
{
public override IEnumerable<IEffectInvocation> OnEntry => new DelayedHeartbeatInvocation()
{ Channels = this.Channels, ChannelGroups = this.ChannelGroups }.AsArray();
public override IEnumerable<IEffectInvocation> OnExit { get; } = new CancelDelayedHeartbeatInvocation().AsArray();
public override TransitionResult Transition(IEvent e)
{
throw new NotImplementedException();
return e switch
{
Events.LeftAllEvent leftAll => new HeartbeatInactiveState()
{
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.HeartbeatFailureEvent heartbeatFailure => new States.HeartbeatReconnectingState()
{
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.JoinedEvent joined => new States.HeartbeatingState()
{
Channels = (Channels ?? Enumerable.Empty<string>()).Union(joined.Channels),
ChannelGroups = (ChannelGroups ?? Enumerable.Empty<string>()).Union(joined.ChannelGroups),
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.LeftEvent left => new States.HeartbeatingState()
{
Channels = (Channels ?? Enumerable.Empty<string>()).Union(left.Channels),
ChannelGroups = (ChannelGroups ?? Enumerable.Empty<string>()).Union(left.ChannelGroups),
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.StateSetEvent stateSet => new States.HeartbeatingState()
{
Channels = (Channels ?? Enumerable.Empty<string>()).Union(stateSet.Channels),
ChannelGroups = (ChannelGroups ?? Enumerable.Empty<string>()).Union(stateSet.ChannelGroups),
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.HeartbeatSuccessEvent heartbeatSuccess => new States.HeartbeatCooldownState()
{
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.HeartbeatGiveUpEvent heartbeatGiveup => new States.HeartbeatFailedState()
{
ReconnectionConfiguration = this.ReconnectionConfiguration
},

Events.DisconnectEvent disconnect => new States.HeartbeatStoppedState()
{
ReconnectionConfiguration = this.ReconnectionConfiguration,
},

_ => null
};
}
}
}

0 comments on commit 0b8c0d1

Please sign in to comment.