From 84e00a23d2b0ef8768c8366f1f5120f82b8b3c76 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 1 Feb 2016 14:54:58 +0100 Subject: [PATCH] Don't display the same message multiple times. --- .../ensichat/core/ConnectionHandler.scala | 8 ++++--- .../com/nutomic/ensichat/core/Router.scala | 23 +++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala b/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala index fccc306..59eb9a1 100644 --- a/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala +++ b/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala @@ -69,7 +69,7 @@ final class ConnectionHandler(settings: SettingsInterface, database: DatabaseInt val msg = new Message(header, body) val encrypted = crypto.encryptAndSign(msg) - router.onReceive(encrypted) + router.forwardMessage(encrypted) onNewMessage(msg) } } @@ -81,13 +81,15 @@ final class ConnectionHandler(settings: SettingsInterface, database: DatabaseInt * Decrypts and verifies incoming messages, forwards valid ones to [[onNewMessage()]]. */ def onMessageReceived(msg: Message): Unit = { - if (msg.header.target == crypto.localAddress) { + if (router.isMessageSeen(msg)) { + Log.v(Tag, "Ignoring message from " + msg.header.origin + " that we already received") + } else if (msg.header.target == crypto.localAddress) { crypto.verifyAndDecrypt(msg) match { case Some(m) => onNewMessage(m) case None => Log.i(Tag, "Ignoring message with invalid signature from " + msg.header.origin) } } else { - router.onReceive(msg) + router.forwardMessage(msg) } } diff --git a/core/src/main/scala/com/nutomic/ensichat/core/Router.scala b/core/src/main/scala/com/nutomic/ensichat/core/Router.scala index 9f6415d..a487917 100644 --- a/core/src/main/scala/com/nutomic/ensichat/core/Router.scala +++ b/core/src/main/scala/com/nutomic/ensichat/core/Router.scala @@ -9,17 +9,32 @@ final private[core] class Router(activeConnections: () => Set[Address], send: (A private var messageSeen = Set[(Address, Int)]() - def onReceive(msg: Message): Unit = { + /** + * Returns true if we have received the same message before. + */ + def isMessageSeen(msg: Message): Boolean = { val info = (msg.header.origin, msg.header.seqNum) - if (messageSeen.contains(info)) - return + val seen = messageSeen.contains(info) + markMessageSeen(info) + seen + } + /** + * Sends message to all connected devices. Should only be called if [[isMessageSeen()]] returns + * true. + */ + def forwardMessage(msg: Message): Unit = { + val info = (msg.header.origin, msg.header.seqNum) val updated = incHopCount(msg) if (updated.header.hopCount >= updated.header.hopLimit) return activeConnections().foreach(a => send(a, updated)) - + + markMessageSeen(info) + } + + private def markMessageSeen(info: (Address, Int)): Unit = { trimMessageSeen(info._1, info._2) messageSeen += info }