-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated Presence with PubNub stage1
- Loading branch information
1 parent
170a300
commit dd8786e
Showing
23 changed files
with
473 additions
and
165 deletions.
There are no files selected for viewing
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
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,6 @@ | ||
package com.pubnub.api.eventengine | ||
|
||
interface EventEngine { | ||
fun start() | ||
fun stop() | ||
} |
12 changes: 0 additions & 12 deletions
12
src/main/kotlin/com/pubnub/api/eventengine/EventEngineConf.kt
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/pubnub/api/eventengine/EventEngineManager.kt
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,7 @@ | ||
package com.pubnub.api.eventengine | ||
|
||
interface EventEngineManager { | ||
fun addEventToQueue(event: Event) | ||
fun start() | ||
fun stop() | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/pubnub/api/eventengine/EventEnginesConf.kt
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,18 @@ | ||
package com.pubnub.api.eventengine | ||
|
||
import com.pubnub.api.presence.eventengine.effect.PresenceEffectInvocation | ||
import com.pubnub.api.presence.eventengine.event.PresenceEvent | ||
import com.pubnub.api.subscribe.eventengine.effect.SubscribeEffectInvocation | ||
import com.pubnub.api.subscribe.eventengine.event.SubscribeEvent | ||
|
||
interface EventEnginesConf { | ||
val subscribeEventSink: Sink<SubscribeEvent> | ||
val subscribeEventSource: Source<SubscribeEvent> | ||
val subscribeEffectSink: Sink<SubscribeEffectInvocation> | ||
val subscribeEffectSource: Source<SubscribeEffectInvocation> | ||
|
||
val presenceEventSink: Sink<PresenceEvent> | ||
val presenceEventSource: Source<PresenceEvent> | ||
val presenceEffectSink: Sink<PresenceEffectInvocation> | ||
val presenceEffectSource: Source<PresenceEffectInvocation> | ||
} |
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
package com.pubnub.api.eventengine | ||
|
||
interface State<T : EffectInvocation, U : Event, V : State<T, U, V>> { | ||
open fun onEntry(): Set<T> = setOf() | ||
open fun onExit(): Set<T> = setOf() | ||
abstract fun transition(event: U): Pair<V, Set<T>> | ||
interface State<Ei : EffectInvocation, Ev : Event, S : State<Ei, Ev, S>> { | ||
open fun onEntry(): Set<Ei> = setOf() | ||
open fun onExit(): Set<Ei> = setOf() | ||
abstract fun transition(event: Ev): Pair<S, Set<Ei>> | ||
} | ||
|
||
fun <T : EffectInvocation, U : Event, V : State<T, U, V>> V.noTransition(): Pair<V, Set<T>> = Pair(this, emptySet()) | ||
fun <T : EffectInvocation, U : Event, V : State<T, U, V>> V.transitionTo( | ||
state: V, | ||
vararg invocations: T | ||
): Pair<V, Set<T>> { | ||
fun <Ei : EffectInvocation, Ev : Event, S : State<Ei, Ev, S>> S.noTransition(): Pair<S, Set<Ei>> = Pair(this, emptySet()) | ||
fun <Ei : EffectInvocation, Ev : Event, S : State<Ei, Ev, S>> S.transitionTo( | ||
state: S, | ||
vararg invocations: Ei | ||
): Pair<S, Set<Ei>> { | ||
val effectInvocations = this.onExit() + invocations + state.onEntry() | ||
return Pair(state, effectInvocations) | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/com/pubnub/api/managers/PresenceEventEngineManager.kt
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,30 @@ | ||
package com.pubnub.api.managers | ||
|
||
import com.pubnub.api.eventengine.EffectDispatcher | ||
import com.pubnub.api.eventengine.Event | ||
import com.pubnub.api.eventengine.EventEngineManager | ||
import com.pubnub.api.eventengine.Sink | ||
import com.pubnub.api.presence.eventengine.PresenceEventEngine | ||
import com.pubnub.api.presence.eventengine.effect.PresenceEffectInvocation | ||
import com.pubnub.api.presence.eventengine.event.PresenceEvent | ||
|
||
class PresenceEventEngineManager( | ||
private val presenceEventEngine: PresenceEventEngine, | ||
private val effectDispatcher: EffectDispatcher<PresenceEffectInvocation>, | ||
private val eventSink: Sink<PresenceEvent> | ||
) : EventEngineManager { | ||
|
||
override fun addEventToQueue(event: Event) { | ||
eventSink.add(event as PresenceEvent) | ||
} | ||
|
||
override fun start() { | ||
presenceEventEngine.start() | ||
effectDispatcher.start() | ||
} | ||
|
||
override fun stop() { | ||
presenceEventEngine.stop() | ||
effectDispatcher.stop() | ||
} | ||
} |
14 changes: 8 additions & 6 deletions
14
...pubnub/api/managers/EventEngineManager.kt → ...i/managers/SubscribeEventEngineManager.kt
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
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/com/pubnub/api/presence/eventengine/PresenceEventEngine.kt
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 @@ | ||
package com.pubnub.api.presence.eventengine | ||
|
||
import com.pubnub.api.eventengine.EventEngine | ||
import com.pubnub.api.eventengine.Sink | ||
import com.pubnub.api.eventengine.Source | ||
import com.pubnub.api.eventengine.transition | ||
import com.pubnub.api.presence.eventengine.effect.PresenceEffectInvocation | ||
import com.pubnub.api.presence.eventengine.event.PresenceEvent | ||
import com.pubnub.api.presence.eventengine.state.PresenceState | ||
import java.util.concurrent.ExecutorService | ||
import java.util.concurrent.Executors | ||
|
||
class PresenceEventEngine( | ||
private val effectSink: Sink<PresenceEffectInvocation>, | ||
private val eventSource: Source<PresenceEvent>, | ||
private var currentState: PresenceState = PresenceState.HearbeatInactive, | ||
private val executorService: ExecutorService = Executors.newSingleThreadExecutor() | ||
) : EventEngine { | ||
|
||
override fun start() { | ||
executorService.submit { | ||
try { | ||
while (true) { | ||
val event = eventSource.take() | ||
performTransitionAndEmitEffects(event) | ||
} | ||
} catch (e: InterruptedException) { | ||
Thread.currentThread().interrupt() | ||
} | ||
} | ||
} | ||
|
||
override fun stop() { | ||
executorService.shutdownNow() | ||
} | ||
|
||
internal fun performTransitionAndEmitEffects(presenceEvent: PresenceEvent) { // todo add unit tests | ||
val (newState, invocations) = transition(currentState, presenceEvent) | ||
currentState = newState | ||
invocations.forEach { invocation -> effectSink.add(invocation) } | ||
} | ||
} |
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
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
Oops, something went wrong.