server-side handling of disconnects and trust events

This commit is contained in:
Zlatin Balevsky
2019-11-11 14:54:10 +00:00
parent 29b5c55328
commit aa221cd6dc
4 changed files with 57 additions and 2 deletions

View File

@@ -311,8 +311,12 @@ public class Core {
connectionEstablisher = new ConnectionEstablisher(eventBus, i2pConnector, props, connectionManager, hostCache)
log.info("initializing chat server")
chatServer = new ChatServer(eventBus, props, trustService, me)
eventBus.register(ChatMessageEvent.class, chatServer)
chatServer = new ChatServer(eventBus, props, trustService, me)
eventBus.with {
register(ChatMessageEvent.class, chatServer)
register(ChatDisconnectionEvent.class, chatServer)
register(TrustEvent.class, chatServer)
}
log.info("initializing chat manager")
chatManager = new ChatManager(eventBus, me, i2pConnector, trustService, props)

View File

@@ -125,6 +125,7 @@ class ChatConnection implements Closeable {
switch(json.type) {
case "Ping" : break // just ignore
case "Chat" : handleChat(json); break
case "Leave": handleLeave(json); break
default :
throw new Exception("unknown json type ${json.type}")
}
@@ -193,6 +194,11 @@ class ChatConnection implements Closeable {
eventBus.publish(event)
}
private void handleLeave(def json) {
Persona leaver = fromString(json.persona)
eventBus.publish(new UserDisconnectedEvent(user : leaver, host : persona))
}
private static Persona fromString(String base64) {
new Persona(new ByteArrayInputStream(Base64.decode(base64)))
}
@@ -216,6 +222,7 @@ class ChatConnection implements Closeable {
void sendChat(ChatMessageEvent e) {
def chat = [:]
chat.type = "Chat"
chat.uuid = e.uuid.toString()
chat.host = e.host.toBase64()
chat.sender = e.sender.toBase64()
@@ -225,4 +232,11 @@ class ChatConnection implements Closeable {
chat.sig = Base64.encode(e.sig)
messages.put(chat)
}
void sendLeave(Persona p) {
def leave = [:]
leave.type = "Leave"
leave.persona = p.toBase64()
messages.put(leave)
}
}

View File

@@ -9,6 +9,8 @@ import com.muwire.core.EventBus
import com.muwire.core.MuWireSettings
import com.muwire.core.Persona
import com.muwire.core.connection.Endpoint
import com.muwire.core.trust.TrustEvent
import com.muwire.core.trust.TrustLevel
import com.muwire.core.trust.TrustService
import com.muwire.core.util.DataUtil
@@ -84,6 +86,32 @@ class ChatServer {
eventBus.publish(new ChatConnectionEvent(connection : connection, status : ChatConnectionAttemptStatus.SUCCESSFUL, persona : client))
}
void onChatDisconnectionEvent(ChatDisconnectionEvent e) {
ChatConnection con = connections.remove(e.persona.destination)
if (con == null)
return
Set<String> rooms = memberships.get(e.persona)
if (rooms != null) {
rooms.each {
leaveRoom(e.persona, it)
}
}
connections.each { k, v ->
v.sendLeave(e.persona)
}
}
void onTrustEvent(TrustEvent e) {
if (e.level == TrustLevel.TRUSTED)
return
if (settings.allowUntrusted && e.level == TrustLevel.NEUTRAL)
return
ChatConnection connection = connections.remove(e.persona.destination)
connection?.close()
}
private void joinRoom(Persona p, String room) {
Set<Persona> existing = rooms.get(room)
if (existing == null) {

View File

@@ -0,0 +1,9 @@
package com.muwire.core.chat
import com.muwire.core.Event
import com.muwire.core.Persona
class UserDisconnectedEvent extends Event {
Persona user
Persona host
}