Skip to content
This repository has been archived by the owner on Dec 8, 2019. It is now read-only.

Commit

Permalink
Don't display the same message multiple times.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Feb 1, 2016
1 parent 6e3cf1d commit 84e00a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}

Expand Down
23 changes: 19 additions & 4 deletions core/src/main/scala/com/nutomic/ensichat/core/Router.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 84e00a2

Please sign in to comment.