Don't display the same message multiple times.

This commit is contained in:
Felix Ableitner 2016-02-01 14:54:58 +01:00
parent 6e3cf1da63
commit 84e00a23d2
2 changed files with 24 additions and 7 deletions

View file

@ -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)
}
}

View file

@ -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
}