mirror of https://github.com/zlatinb/muwire
start and stop poller thread on events
parent
6240b22e66
commit
5b41106476
|
@ -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()) {
|
||||||
|
|
Loading…
Reference in New Issue