From 5cbf918d86772e46b712a7846a4b82144e5d76d8 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 12 Dec 2014 01:14:04 +0200 Subject: [PATCH] Use case classes, cleaned up code. --- .../activities/EnsiChatActivity.scala | 4 ++-- .../ensichat/bluetooth/ChatService.scala | 2 -- .../ensichat/bluetooth/ConnectThread.scala | 4 ++-- .../nutomic/ensichat/bluetooth/Device.scala | 17 ++++---------- .../ensichat/bluetooth/ListenThread.scala | 4 ++-- .../nutomic/ensichat/protocol/Address.scala | 2 +- .../protocol/messages/ConnectionInfo.scala | 9 +------- .../protocol/messages/CryptoData.scala | 5 +--- .../protocol/messages/EncryptedBody.scala | 4 +--- .../ensichat/protocol/messages/Message.scala | 9 +------- .../protocol/messages/MessageHeader.scala | 23 ++++++++----------- .../protocol/messages/RequestAddContact.scala | 4 +--- .../protocol/messages/ResultAddContact.scala | 4 +--- .../ensichat/protocol/messages/Text.scala | 9 +------- .../ensichat/util/IdenticonGenerator.scala | 4 ++-- 15 files changed, 29 insertions(+), 75 deletions(-) diff --git a/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala b/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala index d9dbfc7..499051f 100644 --- a/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala +++ b/app/src/main/scala/com/nutomic/ensichat/activities/EnsiChatActivity.scala @@ -10,9 +10,9 @@ import com.nutomic.ensichat.bluetooth.{ChatService, ChatServiceBinder} */ class EnsiChatActivity extends Activity with ServiceConnection { - var chatService: Option[ChatService] = None + private var chatService: Option[ChatService] = None - var listeners = Set[() => Unit]() + private var listeners = Set[() => Unit]() /** * Starts service and connects to it. diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala index 94c6832..6a47a30 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ChatService.scala @@ -26,8 +26,6 @@ object ChatService { */ val appUuid: UUID = UUID.fromString("8ed52b7a-4501-5348-b054-3d94d004656e") - val KEY_GENERATION_FINISHED = "com.nutomic.ensichat.messages.KEY_GENERATION_FINISHED" - /** * Used with [[ChatService.registerConnectionListener]], called when a bluetooth device * connects or disconnects diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ConnectThread.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ConnectThread.scala index 967574d..2727e5e 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ConnectThread.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ConnectThread.scala @@ -11,9 +11,9 @@ import android.util.Log class ConnectThread(device: Device, onConnected: (Device, BluetoothSocket) => Unit) extends Thread { - val Tag = "ConnectThread" + private val Tag = "ConnectThread" - val Socket: BluetoothSocket = + private val Socket: BluetoothSocket = device.bluetoothDevice.createInsecureRfcommSocketToServiceRecord(ChatService.appUuid) override def run(): Unit = { diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala index f751eda..60ac5b8 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/Device.scala @@ -9,16 +9,10 @@ private[bluetooth] object Device { * * @param Id A bluetooth device address. */ - class ID(private val Id: String) { + case class ID(private val Id: String) { + require(Id.matches("([A-Z0-9][A-Z0-9]:){5}[A-Z0-9][A-Z0-9]"), "Invalid device ID format") - override def hashCode = Id.hashCode - - override def equals(a: Any) = a match { - case o: Device.ID => Id == o.Id - case _ => false - } - override def toString = Id } @@ -28,16 +22,13 @@ private[bluetooth] object Device { /** * Holds information about a remote bluetooth device. */ -private[bluetooth] class Device(val Id: Device.ID, val Name: String, val Connected: Boolean, +private[bluetooth] case class Device(Id: Device.ID, Name: String, Connected: Boolean, btDevice: Option[BluetoothDevice] = None) { - def this(btDevice: BluetoothDevice, connected: Boolean) { + def this(btDevice: BluetoothDevice, connected: Boolean) = { this(new Device.ID(btDevice.getAddress), btDevice.getName, connected, Option(btDevice)) } def bluetoothDevice = btDevice.get - override def toString = "Device(Id=" + Id + ", Name=" + Name + ", Connected=" + Connected + - ", btDevice=" + btDevice + ")" - } diff --git a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ListenThread.scala b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ListenThread.scala index 750a134..d155f48 100644 --- a/app/src/main/scala/com/nutomic/ensichat/bluetooth/ListenThread.scala +++ b/app/src/main/scala/com/nutomic/ensichat/bluetooth/ListenThread.scala @@ -13,9 +13,9 @@ import android.util.Log class ListenThread(name: String, adapter: BluetoothAdapter, onConnected: (Device, BluetoothSocket) => Unit) extends Thread { - val Tag: String = "ListenThread" + private val Tag: String = "ListenThread" - val ServerSocket: BluetoothServerSocket = + private val ServerSocket: BluetoothServerSocket = try { adapter.listenUsingInsecureRfcommWithServiceRecord(name, ChatService.appUuid) } catch { diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/Address.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/Address.scala index 398a59c..672e5d2 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/Address.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/Address.scala @@ -23,7 +23,7 @@ object Address { * * @param Bytes SHA-256 hash of the node's public key. */ -class Address(val Bytes: Array[Byte]) { +case class Address(Bytes: Array[Byte]) { require(Bytes.length == Address.Length, "Invalid address length (was " + Bytes.length + ")") diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ConnectionInfo.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ConnectionInfo.scala index 7c4454e..19760a6 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ConnectionInfo.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ConnectionInfo.scala @@ -31,7 +31,7 @@ object ConnectionInfo { /** * Holds a node's public key. */ -class ConnectionInfo(val key: PublicKey) extends MessageBody { +case class ConnectionInfo(key: PublicKey) extends MessageBody { override def Type = ConnectionInfo.Type @@ -42,13 +42,6 @@ class ConnectionInfo(val key: PublicKey) extends MessageBody { b.array() } - override def equals(a: Any): Boolean = a match { - case o: ConnectionInfo => key == o.key - case _ => false - } - - override def toString = "ConnectionInfo(key=" + key + ")" - override def length = 4 + key.getEncoded.length } diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala index fde2a93..b809570 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/CryptoData.scala @@ -36,7 +36,7 @@ object CryptoData { /** * Holds the signature and (optional) key that are stored in a message. */ -class CryptoData(val Signature: Option[Array[Byte]], val Key: Option[Array[Byte]]) { +case class CryptoData(Signature: Option[Array[Byte]], Key: Option[Array[Byte]]) { override def equals(a: Any): Boolean = a match { case o: CryptoData => @@ -61,7 +61,4 @@ class CryptoData(val Signature: Option[Array[Byte]], val Key: Option[Array[Byte] private def keyLength = if (Key.isDefined) Key.get.length else 0 - override def toString = "CryptoData(Signature.length=" + Signature.foreach(_.length) + - ", Key.length=" + Key.foreach(_.length) + ")" - } \ No newline at end of file diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/EncryptedBody.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/EncryptedBody.scala index b498de5..86ff2d7 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/EncryptedBody.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/EncryptedBody.scala @@ -3,13 +3,11 @@ package com.nutomic.ensichat.protocol.messages /** * Represents the data in an encrypted message body. */ -class EncryptedBody(val Data: Array[Byte]) extends MessageBody { +case class EncryptedBody(Data: Array[Byte]) extends MessageBody { override def Type = -1 def write = Data - override def toString = "EncryptedBody(Data.length=" + Data.length + ")" - override def length = Data.length } diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala index 7035b83..fa278d6 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Message.scala @@ -33,18 +33,11 @@ object Message { } -class Message(val Header: MessageHeader, val Crypto: CryptoData, val Body: MessageBody) { +case class Message(Header: MessageHeader, Crypto: CryptoData, Body: MessageBody) { def this(header: MessageHeader, body: MessageBody) = this(header, new CryptoData(None, None), body) def write = Header.write(Body.length + Crypto.length) ++ Crypto.write ++ Body.write - override def toString = "Message(Header=" + Header + ", Body=" + Body + ", Crypto=" + Crypto + ")" - - override def equals(a: Any): Boolean = a match { - case o: Message => Header == o.Header && Body == o.Body && Crypto == o.Crypto - case _ => false - } - } \ No newline at end of file diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/MessageHeader.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/MessageHeader.scala index b2891cd..455d25b 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/MessageHeader.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/MessageHeader.scala @@ -46,15 +46,15 @@ object MessageHeader { /** * First part of any message, used for routing. */ -class MessageHeader(val MessageType: Int, - val HopLimit: Int, - val Origin: Address, - val Target: Address, - val SequenceNumber: Int, - val Metric: Int, - val Time: Date = new Date(), - val Length: Long = -1, - val HopCount: Int = 0) { +case class MessageHeader(MessageType: Int, + HopLimit: Int, + Origin: Address, + Target: Address, + SequenceNumber: Int, + Metric: Int, + Time: Date = new Date(), + Length: Long = -1, + HopCount: Int = 0) { /** * Writes the header to byte array. @@ -93,9 +93,4 @@ class MessageHeader(val MessageType: Int, case _ => false } - override def toString = "MessageHeader(Version=" + MessageHeader.Version + - ", Type=" + MessageType + ", HopLimit=" + HopLimit + ", HopCount=" + HopCount + - ", Time=" + Time + ", Origin=" + Origin + ", Target=" + Target + ", SeqNum=" + - ", Metric=" + Metric + ", Length=" + Length + ", HopCount=" + HopCount + ")" - } diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/RequestAddContact.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/RequestAddContact.scala index a42fb3a..e4d2e01 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/RequestAddContact.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/RequestAddContact.scala @@ -18,7 +18,7 @@ object RequestAddContact { /** * Sent when the user initiates adding another device as a contact. */ -class RequestAddContact extends MessageBody { +case class RequestAddContact() extends MessageBody { override def Type = RequestAddContact.Type @@ -27,8 +27,6 @@ class RequestAddContact extends MessageBody { b.array() } - override def toString = "RequestAddContact()" - override def length = 4 } diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ResultAddContact.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ResultAddContact.scala index cd5e80a..8904fd5 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ResultAddContact.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/ResultAddContact.scala @@ -23,7 +23,7 @@ object ResultAddContact { /** * Contains the result of a [[RequestAddContact]] message. */ -class ResultAddContact(val Accepted: Boolean) extends MessageBody { +case class ResultAddContact(Accepted: Boolean) extends MessageBody { override def Type = ResultAddContact.Type @@ -34,8 +34,6 @@ class ResultAddContact(val Accepted: Boolean) extends MessageBody { b.array() } - override def toString = "ResultAddContact(Accepted=" + Accepted + ")" - override def length = 4 } diff --git a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Text.scala b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Text.scala index 68abfea..777bc18 100644 --- a/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Text.scala +++ b/app/src/main/scala/com/nutomic/ensichat/protocol/messages/Text.scala @@ -26,7 +26,7 @@ object Text { /** * Holds a plain text message. */ -class Text(val text: String) extends MessageBody { +case class Text(text: String) extends MessageBody { override def Type = Text.Type @@ -38,13 +38,6 @@ class Text(val text: String) extends MessageBody { b.array() } - override def equals(a: Any): Boolean = a match { - case o: Text => text == o.text - case _ => false - } - - override def toString = "Text(" + text + ")" - override def length = write.length } diff --git a/app/src/main/scala/com/nutomic/ensichat/util/IdenticonGenerator.scala b/app/src/main/scala/com/nutomic/ensichat/util/IdenticonGenerator.scala index 3071236..1a7d58b 100644 --- a/app/src/main/scala/com/nutomic/ensichat/util/IdenticonGenerator.scala +++ b/app/src/main/scala/com/nutomic/ensichat/util/IdenticonGenerator.scala @@ -13,9 +13,9 @@ import com.nutomic.ensichat.protocol.Address */ object IdenticonGenerator { - val Height: Int = 5 + private val Height: Int = 5 - val Width: Int = 5 + private val Width: Int = 5 /** * Generates an identicon for the key.