Don't display the same message multiple times.
This commit is contained in:
parent
6e3cf1da63
commit
84e00a23d2
2 changed files with 24 additions and 7 deletions
|
@ -69,7 +69,7 @@ final class ConnectionHandler(settings: SettingsInterface, database: DatabaseInt
|
||||||
|
|
||||||
val msg = new Message(header, body)
|
val msg = new Message(header, body)
|
||||||
val encrypted = crypto.encryptAndSign(msg)
|
val encrypted = crypto.encryptAndSign(msg)
|
||||||
router.onReceive(encrypted)
|
router.forwardMessage(encrypted)
|
||||||
onNewMessage(msg)
|
onNewMessage(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,13 +81,15 @@ final class ConnectionHandler(settings: SettingsInterface, database: DatabaseInt
|
||||||
* Decrypts and verifies incoming messages, forwards valid ones to [[onNewMessage()]].
|
* Decrypts and verifies incoming messages, forwards valid ones to [[onNewMessage()]].
|
||||||
*/
|
*/
|
||||||
def onMessageReceived(msg: Message): Unit = {
|
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 {
|
crypto.verifyAndDecrypt(msg) match {
|
||||||
case Some(m) => onNewMessage(m)
|
case Some(m) => onNewMessage(m)
|
||||||
case None => Log.i(Tag, "Ignoring message with invalid signature from " + msg.header.origin)
|
case None => Log.i(Tag, "Ignoring message with invalid signature from " + msg.header.origin)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
router.onReceive(msg)
|
router.forwardMessage(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,17 +9,32 @@ final private[core] class Router(activeConnections: () => Set[Address], send: (A
|
||||||
|
|
||||||
private var messageSeen = Set[(Address, Int)]()
|
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)
|
val info = (msg.header.origin, msg.header.seqNum)
|
||||||
if (messageSeen.contains(info))
|
val seen = messageSeen.contains(info)
|
||||||
return
|
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)
|
val updated = incHopCount(msg)
|
||||||
if (updated.header.hopCount >= updated.header.hopLimit)
|
if (updated.header.hopCount >= updated.header.hopLimit)
|
||||||
return
|
return
|
||||||
|
|
||||||
activeConnections().foreach(a => send(a, updated))
|
activeConnections().foreach(a => send(a, updated))
|
||||||
|
|
||||||
|
markMessageSeen(info)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def markMessageSeen(info: (Address, Int)): Unit = {
|
||||||
trimMessageSeen(info._1, info._2)
|
trimMessageSeen(info._1, info._2)
|
||||||
messageSeen += info
|
messageSeen += info
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue