Add option fields for ContentHeader to AbstractHeader.
This will hopefully save some LoC and complexity (and not increase them).
This commit is contained in:
parent
0aeb82553a
commit
02f7d87929
10 changed files with 34 additions and 30 deletions
|
@ -91,14 +91,14 @@ class RouterTest extends AndroidTestCase {
|
|||
|
||||
def testHopLimit(): Unit = Range(19, 22).foreach { i =>
|
||||
val msg = new Message(
|
||||
new ContentHeader(AddressTest.a1, AddressTest.a2, 1, 1, 1, new Date(), i), new Text(""))
|
||||
new ContentHeader(AddressTest.a1, AddressTest.a2, 1, 1, Some(1), Some(new Date()), i), new Text(""))
|
||||
val router: Router = new Router(neighbors, (a, m) => fail())
|
||||
router.onReceive(msg)
|
||||
}
|
||||
|
||||
private def generateMessage(sender: Address, receiver: Address, seqNum: Int): Message = {
|
||||
val header = new ContentHeader(sender, receiver, seqNum, UserInfo.Type, 5,
|
||||
new GregorianCalendar(2014, 6, 10).getTime)
|
||||
val header = new ContentHeader(sender, receiver, seqNum, UserInfo.Type, Some(5),
|
||||
Some(new GregorianCalendar(2014, 6, 10).getTime))
|
||||
new Message(header, new UserInfo("", ""))
|
||||
}
|
||||
|
||||
|
|
|
@ -10,19 +10,19 @@ import junit.framework.Assert._
|
|||
object ContentHeaderTest {
|
||||
|
||||
val h1 = new ContentHeader(AddressTest.a1, AddressTest.a2, 1234,
|
||||
Text.Type, 123, new GregorianCalendar(1970, 1, 1).getTime, 5)
|
||||
Text.Type, Some(123), Some(new GregorianCalendar(1970, 1, 1).getTime), 5)
|
||||
|
||||
val h2 = new ContentHeader(AddressTest.a1, AddressTest.a3,
|
||||
30000, Text.Type, 8765, new GregorianCalendar(2014, 6, 10).getTime, 20)
|
||||
30000, Text.Type, Some(8765), Some(new GregorianCalendar(2014, 6, 10).getTime), 20)
|
||||
|
||||
val h3 = new ContentHeader(AddressTest.a4, AddressTest.a2,
|
||||
250, Text.Type, 77, new GregorianCalendar(2020, 11, 11).getTime, 123)
|
||||
250, Text.Type, Some(77), Some(new GregorianCalendar(2020, 11, 11).getTime), 123)
|
||||
|
||||
val h4 = new ContentHeader(Address.Null, Address.Broadcast,
|
||||
ContentHeader.SeqNumRange.last, 0, 0xffff, new Date(0L), 0xff)
|
||||
ContentHeader.SeqNumRange.last, 0, Some(0xffff), Some(new Date(0L)), 0xff)
|
||||
|
||||
val h5 = new ContentHeader(Address.Broadcast, Address.Null,
|
||||
0, 0xff, 0, new Date(0xffffffffL), 0)
|
||||
0, 0xff, Some(0), Some(new Date(0xffffffffL)), 0)
|
||||
|
||||
val headers = Set(h1, h2, h3, h4, h5)
|
||||
|
||||
|
|
|
@ -27,11 +27,11 @@ class AddContactsHandlerTest extends AndroidTestCase {
|
|||
private lazy val crypto = new Crypto(getContext)
|
||||
|
||||
private lazy val header1 = new ContentHeader(UserTest.u1.address, crypto.localAddress, 0,
|
||||
RequestAddContact.Type, 0, new GregorianCalendar(1970, 1, 1).getTime)
|
||||
RequestAddContact.Type, Some(0), Some(new GregorianCalendar(1970, 1, 1).getTime))
|
||||
private lazy val header2 = new ContentHeader(UserTest.u1.address, crypto.localAddress, 0,
|
||||
ResultAddContact.Type, 0, new GregorianCalendar(2014, 6, 10).getTime)
|
||||
ResultAddContact.Type, Some(0), Some(new GregorianCalendar(2014, 6, 10).getTime))
|
||||
private lazy val header3 = new ContentHeader(crypto.localAddress, UserTest.u1.address, 0,
|
||||
ResultAddContact.Type, 0, new GregorianCalendar(2020, 11, 11).getTime)
|
||||
ResultAddContact.Type, Some(0), Some(new GregorianCalendar(2020, 11, 11).getTime))
|
||||
|
||||
override def tearDown(): Unit = {
|
||||
super.tearDown()
|
||||
|
|
|
@ -125,7 +125,7 @@ class ChatService extends Service {
|
|||
|
||||
val messageId = preferences.getLong("message_id", 0)
|
||||
val header = new ContentHeader(crypto.localAddress, target, seqNumGenerator.next(),
|
||||
body.contentType, messageId, new Date())
|
||||
body.contentType, Some(messageId), Some(new Date()))
|
||||
preferences.edit().putLong("message_id", messageId + 1)
|
||||
|
||||
val msg = new Message(header, body)
|
||||
|
|
|
@ -14,7 +14,7 @@ object Message {
|
|||
val Ordering = new Ordering[Message] {
|
||||
override def compare(m1: Message, m2: Message) = (m1.header, m2.header) match {
|
||||
case (h1: ContentHeader, h2: ContentHeader) =>
|
||||
h1.time.compareTo(h2.time)
|
||||
h1.time.get.compareTo(h2.time.get)
|
||||
case _ => 0
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.nutomic.ensichat.protocol.header
|
||||
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.Date
|
||||
|
||||
import com.nutomic.ensichat.protocol.Address
|
||||
import com.nutomic.ensichat.util.BufferUtils
|
||||
|
@ -18,6 +19,8 @@ object AbstractHeader {
|
|||
/**
|
||||
* Contains the header fields and functionality that are used both in [[MessageHeader]] and
|
||||
* [[ContentHeader]].
|
||||
*
|
||||
* The fields messageId and time are only set in [[ContentHeader]].
|
||||
*/
|
||||
trait AbstractHeader {
|
||||
|
||||
|
@ -27,6 +30,8 @@ trait AbstractHeader {
|
|||
def origin: Address
|
||||
def target: Address
|
||||
def seqNum: Int
|
||||
def messageId: Option[Long] = None
|
||||
def time: Option[Date] = None
|
||||
|
||||
/**
|
||||
* Writes the header to byte array.
|
||||
|
|
|
@ -24,8 +24,8 @@ object ContentHeader {
|
|||
val messageId = BufferUtils.getUnsignedInt(b)
|
||||
val time = BufferUtils.getUnsignedInt(b)
|
||||
|
||||
val ch = new ContentHeader(mh.origin, mh.target, mh.seqNum, contentType, messageId,
|
||||
new Date(time * 1000), mh.hopCount)
|
||||
val ch = new ContentHeader(mh.origin, mh.target, mh.seqNum, contentType, Some(messageId),
|
||||
Some(new Date(time * 1000)), mh.hopCount)
|
||||
|
||||
val remaining = new Array[Byte](b.remaining())
|
||||
b.get(remaining, 0, b.remaining())
|
||||
|
@ -37,14 +37,14 @@ object ContentHeader {
|
|||
/**
|
||||
* Header for user-sent messages.
|
||||
*
|
||||
* This is [[AbstractHeader]] with some extra data appended.
|
||||
* This is [[AbstractHeader]] with messageId and time fields set.
|
||||
*/
|
||||
case class ContentHeader(override val origin: Address,
|
||||
override val target: Address,
|
||||
override val seqNum: Int,
|
||||
contentType: Int,
|
||||
messageId: Long,
|
||||
time: Date,
|
||||
override val messageId: Some[Long],
|
||||
override val time: Some[Date],
|
||||
override val hopCount: Int = 0,
|
||||
override val hopLimit: Int = AbstractHeader.DefaultHopLimit)
|
||||
extends AbstractHeader {
|
||||
|
@ -60,8 +60,8 @@ case class ContentHeader(override val origin: Address,
|
|||
b.put(super.write(contentLength))
|
||||
|
||||
BufferUtils.putUnsignedShort(b, contentType)
|
||||
BufferUtils.putUnsignedInt(b, messageId)
|
||||
BufferUtils.putUnsignedInt(b, time.getTime / 1000)
|
||||
BufferUtils.putUnsignedInt(b, messageId.get)
|
||||
BufferUtils.putUnsignedInt(b, time.get.getTime / 1000)
|
||||
|
||||
b.array()
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ case class ContentHeader(override val origin: Address,
|
|||
super.equals(a) &&
|
||||
contentType == o.contentType &&
|
||||
messageId == o.messageId &&
|
||||
time.getTime / 1000 == o.time.getTime / 1000
|
||||
time.get.getTime / 1000 == o.time.get.getTime / 1000
|
||||
case _ => false
|
||||
}
|
||||
|
||||
|
|
|
@ -44,8 +44,8 @@ object Database {
|
|||
new Address(c.getString(c.getColumnIndex("target"))),
|
||||
-1,
|
||||
Text.Type,
|
||||
c.getLong(c.getColumnIndex("message_id")),
|
||||
new Date(c.getLong(c.getColumnIndex("date"))))
|
||||
Some(c.getLong(c.getColumnIndex("message_id"))),
|
||||
Some(new Date(c.getLong(c.getColumnIndex("date")))))
|
||||
val body = new Text(new String(c.getString(c.getColumnIndex ("text"))))
|
||||
new Message(header, body)
|
||||
}
|
||||
|
@ -89,12 +89,11 @@ class Database(context: Context)
|
|||
def onMessageReceived(msg: Message): Unit = msg.body match {
|
||||
case text: Text =>
|
||||
val cv = new ContentValues()
|
||||
val ch = msg.header.asInstanceOf[ContentHeader]
|
||||
cv.put("origin", ch.origin.toString)
|
||||
cv.put("target", ch.target.toString)
|
||||
cv.put("origin", msg.header.origin.toString)
|
||||
cv.put("target", msg.header.target.toString)
|
||||
// Need to use [[Long#toString]] because of https://issues.scala-lang.org/browse/SI-2991
|
||||
cv.put("message_id", ch.messageId.toString)
|
||||
cv.put("date", ch.time.getTime.toString)
|
||||
cv.put("message_id", msg.header.messageId.get.toString)
|
||||
cv.put("date", msg.header.time.get.getTime.toString)
|
||||
cv.put("text", text.text)
|
||||
getWritableDatabase.insert("messages", null, cv)
|
||||
case _: RequestAddContact | _: ResultAddContact =>
|
||||
|
|
|
@ -17,7 +17,7 @@ object DatesAdapter {
|
|||
override def getSectionTitleForItem(item: Cursor): String = {
|
||||
DateFormat
|
||||
.getDateInstance(DateFormat.MEDIUM)
|
||||
.format(Database.messageFromCursor(item).header.asInstanceOf[ContentHeader].time)
|
||||
.format(Database.messageFromCursor(item).header.time.get)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ class MessagesAdapter(context: Context, cursor: Cursor, remoteAddress: Address)
|
|||
text.setText(msg.body.asInstanceOf[Text].text)
|
||||
val formattedDate = DateFormat
|
||||
.getTimeInstance(DateFormat.SHORT)
|
||||
.format(msg.header.asInstanceOf[ContentHeader].time)
|
||||
.format(msg.header.time.get)
|
||||
time.setText(formattedDate)
|
||||
|
||||
val lp = new LinearLayout.LayoutParams(view.getLayoutParams)
|
||||
|
|
Reference in a new issue