-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
State transitions for presence event engine #203
Merged
Merged
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b4bcc30
add events
Xavrax c405b51
proper formatting
Xavrax 135c876
all states declared
Xavrax 0ac1aba
fields declared
Xavrax 2ffd098
joined event
Xavrax f61a79a
Left event
Xavrax 3798d8b
left all
Xavrax 13b22a8
fix function param
Xavrax edc602a
hb fail + transition fix
Xavrax 3f4ce72
give up
Xavrax 3881560
reconnect
Xavrax d0a12d3
disconnect
Xavrax d4c7a1e
timeup
Xavrax 384ef5d
missing enumerable
Xavrax e371d59
building should work
Xavrax c6bcba0
enumerable instead of list
Xavrax 9cb95a5
add UT
Xavrax 3427cbc
fix/build: missing reference links for effect handlers
mohitpubnub 2581fa9
attempt to fix UWP project build errors
mohitpubnub 56ebedd
fix CSC : error CS1617: error for language version
mohitpubnub 19eb3f3
updating language version to 9 suppoerted by VS2019
mohitpubnub 9b26375
updated LangVersion to `preview`
mohitpubnub dc61a26
take-4/fix language version. `default`
mohitpubnub abd4327
take-5: reverting LangVersion to `latest`
mohitpubnub 491d5c9
removed `<LangVersion>` overrides
mohitpubnub 3fff0d1
reverted 491d5c9
mohitpubnub c6b03c6
Update run-tests.yml
mohitpubnub 39d45f1
take-2
mohitpubnub eae3301
take-3
mohitpubnub e03a01f
fix state switches
Xavrax b223349
wip for ci
Xavrax 8db5621
leave this branch as is - new branch for testing
Xavrax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/Api/PubnubApi/EventEngine/Presence/Common/PresenceInput.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace PubnubApi.EventEngine.Presence.Common | ||
{ | ||
public class PresenceInput | ||
{ | ||
public IEnumerable<string> Channels { get; set; } | ||
public IEnumerable<string> ChannelGroups { get; set; } | ||
|
||
public static PresenceInput operator +(PresenceInput a, PresenceInput b) | ||
{ | ||
return new PresenceInput | ||
{ | ||
Channels = a.Channels?.Union(b.Channels ?? new string[0]), | ||
ChannelGroups = a.ChannelGroups?.Union(b.ChannelGroups ?? new string[0]), | ||
}; | ||
} | ||
|
||
public static PresenceInput operator -(PresenceInput a, PresenceInput b) | ||
{ | ||
return new PresenceInput | ||
{ | ||
Channels = a.Channels?.Except(b.Channels ?? new string[0]), | ||
ChannelGroups = a.ChannelGroups?.Except(b.ChannelGroups ?? new string[0]), | ||
}; | ||
} | ||
|
||
public bool IsEmpty() | ||
{ | ||
return (Channels == null && ChannelGroups == null) | ||
|| ((Channels != null && Channels.Count() == 0) | ||
&& (ChannelGroups != null && ChannelGroups.Count() == 0)); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Api/PubnubApi/EventEngine/Presence/Events/PresenceEvents.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Collections.Generic; | ||
using PubnubApi; | ||
using PubnubApi.EventEngine.Presence.Common; | ||
|
||
namespace PubnubApi.EventEngine.Presence.Events { | ||
public class JoinedEvent : Core.IEvent | ||
{ | ||
public PresenceInput Input { get; set; } | ||
} | ||
|
||
public class LeftEvent : Core.IEvent | ||
{ | ||
public PresenceInput Input { get; set; } | ||
} | ||
|
||
public class LeftAllEvent : Core.IEvent {} | ||
|
||
public class HeartbeatSuccessEvent : Core.IEvent {} | ||
|
||
public class HeartbeatFailureEvent : Core.IEvent | ||
{ | ||
public PNStatus Status { get; set; } | ||
} | ||
|
||
public class HeartbeatGiveUpEvent : Core.IEvent | ||
{ | ||
public PNStatus Status { get; set; } | ||
} | ||
|
||
public class ReconnectEvent : Core.IEvent {} | ||
|
||
public class DisconnectEvent : Core.IEvent {} | ||
|
||
public class TimesUpEvent : Core.IEvent {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// TODO: Dummy Invocation until we have real ones | ||
namespace PubnubApi.EventEngine.Presence.Invocations { | ||
public class DummyInvocation : Core.IEffectInvocation {} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Api/PubnubApi/EventEngine/Presence/States/APresenceState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using PubnubApi.EventEngine.Core; | ||
using PubnubApi.EventEngine.Presence.Common; | ||
|
||
namespace PubnubApi.EventEngine.Presence.States | ||
{ | ||
public abstract class APresenceState : Core.State | ||
{ | ||
public PresenceInput Input { get; set; } | ||
|
||
public bool IsEmpty() | ||
{ | ||
return Input.IsEmpty(); | ||
} | ||
|
||
protected TransitionResult HandleLeftEvent(Events.LeftEvent e) | ||
{ | ||
var newInput = this.Input - e.Input; | ||
|
||
return newInput.IsEmpty() | ||
? (TransitionResult)new InactiveState() | ||
: (TransitionResult)new HeartbeatingState() | ||
{ | ||
Input = newInput, | ||
}; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Api/PubnubApi/EventEngine/Presence/States/CooldownState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using PubnubApi; | ||
using PubnubApi.EventEngine.Presence.Invocations; | ||
using PubnubApi.EventEngine.Core; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
namespace PubnubApi.EventEngine.Presence.States | ||
{ | ||
public class CooldownState : APresenceState | ||
{ | ||
// TODO: Dummy Invocation until we have real ones | ||
public override IEnumerable<IEffectInvocation> OnEntry => new DummyInvocation().AsArray(); | ||
public override IEnumerable<IEffectInvocation> OnExit => new DummyInvocation().AsArray(); | ||
|
||
// TODO: transitions | ||
public override TransitionResult Transition(IEvent ev) | ||
{ | ||
return ev switch | ||
{ | ||
Events.JoinedEvent e => new HeartbeatingState() | ||
{ | ||
Input = e.Input != this.Input ? this.Input + e.Input : this.Input, | ||
}, | ||
Events.LeftEvent e => HandleLeftEvent(e), | ||
Events.LeftAllEvent e => new InactiveState(), | ||
Events.HeartbeatSuccessEvent e => new CooldownState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
Events.DisconnectEvent e => new StoppedState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
Events.TimesUpEvent e => new HeartbeatingState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
_ => null, | ||
}; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Api/PubnubApi/EventEngine/Presence/States/FailedState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using PubnubApi.EventEngine.Presence.Invocations; | ||
using PubnubApi.EventEngine.Core; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
namespace PubnubApi.EventEngine.Presence.States | ||
{ | ||
public class FailedState : APresenceState | ||
{ | ||
public PNStatus Reason { get; set; } | ||
|
||
// TODO: Dummy Invocation until we have real ones | ||
public override IEnumerable<IEffectInvocation> OnEntry => new DummyInvocation().AsArray(); | ||
public override IEnumerable<IEffectInvocation> OnExit => new DummyInvocation().AsArray(); | ||
|
||
// TODO: transitions | ||
public override TransitionResult Transition(IEvent ev) | ||
{ | ||
return ev switch | ||
{ | ||
Events.JoinedEvent e => new HeartbeatingState() | ||
{ | ||
Input = e.Input != this.Input ? this.Input + e.Input : this.Input, | ||
}, | ||
Events.LeftEvent e => HandleLeftEvent(e), | ||
Events.LeftAllEvent e => new InactiveState(), | ||
Events.ReconnectEvent e => new HeartbeatingState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
Events.DisconnectEvent e => new StoppedState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
_ => null, | ||
}; | ||
} | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/Api/PubnubApi/EventEngine/Presence/States/HeartBeatingState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using PubnubApi.EventEngine.Presence.Invocations; | ||
using PubnubApi.EventEngine.Core; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
namespace PubnubApi.EventEngine.Presence.States | ||
{ | ||
public class HeartbeatingState : APresenceState | ||
{ | ||
// TODO: Dummy Invocation until we have real ones | ||
public override IEnumerable<IEffectInvocation> OnEntry => new DummyInvocation().AsArray(); | ||
public override IEnumerable<IEffectInvocation> OnExit => new DummyInvocation().AsArray(); | ||
|
||
// TODO: transitions | ||
public override TransitionResult Transition(IEvent ev) | ||
{ | ||
return ev switch | ||
{ | ||
Events.JoinedEvent e => new HeartbeatingState() | ||
{ | ||
Input = e.Input != this.Input ? this.Input + e.Input : this.Input, | ||
}, | ||
Events.LeftEvent e => HandleLeftEvent(e), | ||
Events.LeftAllEvent e => new InactiveState(), | ||
Events.HeartbeatFailureEvent e => HandleHeartbeatFailureEvent(e), | ||
Events.DisconnectEvent e => new StoppedState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
_ => null, | ||
}; | ||
} | ||
|
||
private TransitionResult HandleHeartbeatFailureEvent(Events.HeartbeatFailureEvent e) | ||
{ | ||
return e.Status.Category == PNStatusCategory.PNCancelledCategory | ||
? (TransitionResult)null | ||
: (TransitionResult)new ReconnectingState() | ||
{ | ||
Input = this.Input, | ||
RetryCount = 1, | ||
Reason = e.Status, | ||
}; | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/Api/PubnubApi/EventEngine/Presence/States/InactiveState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using PubnubApi.EventEngine.Presence.Invocations; | ||
using PubnubApi.EventEngine.Core; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
namespace PubnubApi.EventEngine.Presence.States | ||
{ | ||
public class InactiveState : APresenceState | ||
{ | ||
public InactiveState() | ||
{ | ||
Input = null; | ||
} | ||
|
||
// TODO: Dummy Invocation until we have real ones | ||
public override IEnumerable<IEffectInvocation> OnEntry => new DummyInvocation().AsArray(); | ||
public override IEnumerable<IEffectInvocation> OnExit => new DummyInvocation().AsArray(); | ||
|
||
// TODO: transitions | ||
public override TransitionResult Transition(IEvent ev) | ||
{ | ||
return ev switch | ||
{ | ||
Events.JoinedEvent e => new HeartbeatingState() | ||
{ | ||
Input = e.Input, | ||
}, | ||
_ => null, | ||
}; | ||
} | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/Api/PubnubApi/EventEngine/Presence/States/ReconnectingState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using PubnubApi.EventEngine.Presence.Invocations; | ||
using PubnubApi.EventEngine.Core; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
namespace PubnubApi.EventEngine.Presence.States | ||
{ | ||
public class ReconnectingState : APresenceState | ||
{ | ||
public int RetryCount { get; set; } | ||
public PNStatus Reason { get; set; } | ||
|
||
// TODO: Dummy Invocation until we have real ones | ||
public override IEnumerable<IEffectInvocation> OnEntry => new DummyInvocation().AsArray(); | ||
public override IEnumerable<IEffectInvocation> OnExit => new DummyInvocation().AsArray(); | ||
|
||
// TODO: transitions | ||
public override TransitionResult Transition(IEvent ev) | ||
{ | ||
return ev switch | ||
{ | ||
Events.JoinedEvent e => new HeartbeatingState() | ||
{ | ||
Input = e.Input != this.Input ? this.Input + e.Input : this.Input, | ||
}, | ||
Events.LeftEvent e => HandleLeftEvent(e), | ||
Events.LeftAllEvent e => new InactiveState(), | ||
Events.HeartbeatSuccessEvent e => new CooldownState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
Events.HeartbeatFailureEvent e => HandleHeartbeatFailureEvent(e), | ||
Events.HeartbeatGiveUpEvent e => new FailedState() | ||
{ | ||
Input = this.Input, | ||
Reason = this.Reason, | ||
}, | ||
Events.DisconnectEvent e => new StoppedState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
_ => null, | ||
}; | ||
} | ||
|
||
private TransitionResult HandleHeartbeatFailureEvent(Events.HeartbeatFailureEvent e) | ||
{ | ||
return e.Status.Category == PNStatusCategory.PNCancelledCategory | ||
? (TransitionResult)null | ||
: (TransitionResult)new ReconnectingState() | ||
{ | ||
Input = this.Input, | ||
RetryCount = this.RetryCount + 1, | ||
Reason = e.Status, | ||
}; | ||
} | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
src/Api/PubnubApi/EventEngine/Presence/States/StoppedState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using PubnubApi.EventEngine.Presence.Invocations; | ||
using PubnubApi.EventEngine.Core; | ||
using System.Collections.Generic; | ||
using System; | ||
|
||
namespace PubnubApi.EventEngine.Presence.States | ||
{ | ||
public class StoppedState : APresenceState | ||
{ | ||
// TODO: Dummy Invocation until we have real ones | ||
public override IEnumerable<IEffectInvocation> OnEntry => new DummyInvocation().AsArray(); | ||
public override IEnumerable<IEffectInvocation> OnExit => new DummyInvocation().AsArray(); | ||
|
||
// TODO: transitions | ||
public override TransitionResult Transition(IEvent ev) | ||
{ | ||
return ev switch | ||
{ | ||
Events.JoinedEvent e => new StoppedState() | ||
{ | ||
Input = e.Input != this.Input ? this.Input + e.Input : this.Input, | ||
}, | ||
Events.LeftEvent e => HandleLeftEvent(e), | ||
Events.LeftAllEvent e => new InactiveState(), | ||
Events.ReconnectEvent e => new HeartbeatingState() | ||
{ | ||
Input = this.Input, | ||
}, | ||
_ => null, | ||
}; | ||
} | ||
|
||
protected TransitionResult HandleLeftEvent(Events.LeftEvent e) | ||
{ | ||
var newInput = this.Input - e.Input; | ||
|
||
return newInput.IsEmpty() | ||
? (TransitionResult)new InactiveState() | ||
: (TransitionResult)new StoppedState() | ||
{ | ||
Input = newInput, | ||
}; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to handle
HeratbeatSuccess
when we are onCooldown
state ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Probably I've added that in favour of Heartbeating and Reconnecting! I will change that after the compiling fix.