start and stop poller thread on events

pull/34/head
Zlatin Balevsky 2019-11-14 03:45:21 +00:00
parent 6240b22e66
commit 5b41106476
1 changed files with 26 additions and 15 deletions

View File

@ -32,29 +32,33 @@ class ChatServerModel {
void mvcGroupInit(Map<String, String> params) { void mvcGroupInit(Map<String, String> params) {
disconnectActionEnabled = host != core.me // can't disconnect from myself disconnectActionEnabled = host != core.me // can't disconnect from myself
core.eventBus.register(ChatConnectionEvent.class, this)
connect() connect()
} }
void connect() { void connect() {
core.eventBus.publish(new UIConnectChatEvent(host : host))
}
void mvcGroupDestroy() {
stopPoller()
core.eventBus.unregister(ChatConnectionEvent.class, this)
}
private void startPoller() {
if (running) if (running)
return return
running = true running = true
core.eventBus.with {
register(ChatConnectionEvent.class, this)
publish(new UIConnectChatEvent(host : host))
}
poller = new Thread({eventLoop()} as Runnable) poller = new Thread({eventLoop()} as Runnable)
poller.setDaemon(true) poller.setDaemon(true)
poller.start() poller.start()
} }
void mvcGroupDestroy() { private void stopPoller() {
core.eventBus.unregister(ChatConnectionEvent.class, this)
running = false running = false
poller?.interrupt() poller?.interrupt()
link = null
} }
void onChatConnectionEvent(ChatConnectionEvent e) { void onChatConnectionEvent(ChatConnectionEvent e) {
@ -65,17 +69,24 @@ class ChatServerModel {
status = e.status status = e.status
} }
ChatLink link = e.connection if (e.status == ChatConnectionAttemptStatus.SUCCESSFUL) {
if (link == null) ChatLink link = e.connection
return if (link == null)
this.link = e.connection return
this.link = e.connection
mvcGroup.childrenGroups.each {k,v ->
v.controller.rejoinRoom() startPoller()
mvcGroup.childrenGroups.each {k,v ->
v.controller.rejoinRoom()
}
} else {
stopPoller()
} }
} }
private void eventLoop() { private void eventLoop() {
Thread.sleep(1000)
while(running) { while(running) {
ChatLink link = this.link ChatLink link = this.link
if (link == null || !link.isUp()) { if (link == null || !link.isUp()) {