Use case classes, cleaned up code.
This commit is contained in:
parent
539d955737
commit
5cbf918d86
15 changed files with 29 additions and 75 deletions
|
@ -10,9 +10,9 @@ import com.nutomic.ensichat.bluetooth.{ChatService, ChatServiceBinder}
|
||||||
*/
|
*/
|
||||||
class EnsiChatActivity extends Activity with ServiceConnection {
|
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.
|
* Starts service and connects to it.
|
||||||
|
|
|
@ -26,8 +26,6 @@ object ChatService {
|
||||||
*/
|
*/
|
||||||
val appUuid: UUID = UUID.fromString("8ed52b7a-4501-5348-b054-3d94d004656e")
|
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
|
* Used with [[ChatService.registerConnectionListener]], called when a bluetooth device
|
||||||
* connects or disconnects
|
* connects or disconnects
|
||||||
|
|
|
@ -11,9 +11,9 @@ import android.util.Log
|
||||||
class ConnectThread(device: Device, onConnected: (Device, BluetoothSocket) => Unit)
|
class ConnectThread(device: Device, onConnected: (Device, BluetoothSocket) => Unit)
|
||||||
extends Thread {
|
extends Thread {
|
||||||
|
|
||||||
val Tag = "ConnectThread"
|
private val Tag = "ConnectThread"
|
||||||
|
|
||||||
val Socket: BluetoothSocket =
|
private val Socket: BluetoothSocket =
|
||||||
device.bluetoothDevice.createInsecureRfcommSocketToServiceRecord(ChatService.appUuid)
|
device.bluetoothDevice.createInsecureRfcommSocketToServiceRecord(ChatService.appUuid)
|
||||||
|
|
||||||
override def run(): Unit = {
|
override def run(): Unit = {
|
||||||
|
|
|
@ -9,16 +9,10 @@ private[bluetooth] object Device {
|
||||||
*
|
*
|
||||||
* @param Id A bluetooth device address.
|
* @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")
|
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
|
override def toString = Id
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -28,16 +22,13 @@ private[bluetooth] object Device {
|
||||||
/**
|
/**
|
||||||
* Holds information about a remote bluetooth 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) {
|
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))
|
this(new Device.ID(btDevice.getAddress), btDevice.getName, connected, Option(btDevice))
|
||||||
}
|
}
|
||||||
|
|
||||||
def bluetoothDevice = btDevice.get
|
def bluetoothDevice = btDevice.get
|
||||||
|
|
||||||
override def toString = "Device(Id=" + Id + ", Name=" + Name + ", Connected=" + Connected +
|
|
||||||
", btDevice=" + btDevice + ")"
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,9 @@ import android.util.Log
|
||||||
class ListenThread(name: String, adapter: BluetoothAdapter,
|
class ListenThread(name: String, adapter: BluetoothAdapter,
|
||||||
onConnected: (Device, BluetoothSocket) => Unit) extends Thread {
|
onConnected: (Device, BluetoothSocket) => Unit) extends Thread {
|
||||||
|
|
||||||
val Tag: String = "ListenThread"
|
private val Tag: String = "ListenThread"
|
||||||
|
|
||||||
val ServerSocket: BluetoothServerSocket =
|
private val ServerSocket: BluetoothServerSocket =
|
||||||
try {
|
try {
|
||||||
adapter.listenUsingInsecureRfcommWithServiceRecord(name, ChatService.appUuid)
|
adapter.listenUsingInsecureRfcommWithServiceRecord(name, ChatService.appUuid)
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
@ -23,7 +23,7 @@ object Address {
|
||||||
*
|
*
|
||||||
* @param Bytes SHA-256 hash of the node's public key.
|
* @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 + ")")
|
require(Bytes.length == Address.Length, "Invalid address length (was " + Bytes.length + ")")
|
||||||
|
|
||||||
|
|
|
@ -31,7 +31,7 @@ object ConnectionInfo {
|
||||||
/**
|
/**
|
||||||
* Holds a node's public key.
|
* 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
|
override def Type = ConnectionInfo.Type
|
||||||
|
|
||||||
|
@ -42,13 +42,6 @@ class ConnectionInfo(val key: PublicKey) extends MessageBody {
|
||||||
b.array()
|
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
|
override def length = 4 + key.getEncoded.length
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ object CryptoData {
|
||||||
/**
|
/**
|
||||||
* Holds the signature and (optional) key that are stored in a message.
|
* 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 {
|
override def equals(a: Any): Boolean = a match {
|
||||||
case o: CryptoData =>
|
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
|
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) + ")"
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -3,13 +3,11 @@ package com.nutomic.ensichat.protocol.messages
|
||||||
/**
|
/**
|
||||||
* Represents the data in an encrypted message body.
|
* 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
|
override def Type = -1
|
||||||
|
|
||||||
def write = Data
|
def write = Data
|
||||||
|
|
||||||
override def toString = "EncryptedBody(Data.length=" + Data.length + ")"
|
|
||||||
|
|
||||||
override def length = Data.length
|
override def length = Data.length
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) =
|
def this(header: MessageHeader, body: MessageBody) =
|
||||||
this(header, new CryptoData(None, None), body)
|
this(header, new CryptoData(None, None), body)
|
||||||
|
|
||||||
def write = Header.write(Body.length + Crypto.length) ++ Crypto.write ++ Body.write
|
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
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -46,15 +46,15 @@ object MessageHeader {
|
||||||
/**
|
/**
|
||||||
* First part of any message, used for routing.
|
* First part of any message, used for routing.
|
||||||
*/
|
*/
|
||||||
class MessageHeader(val MessageType: Int,
|
case class MessageHeader(MessageType: Int,
|
||||||
val HopLimit: Int,
|
HopLimit: Int,
|
||||||
val Origin: Address,
|
Origin: Address,
|
||||||
val Target: Address,
|
Target: Address,
|
||||||
val SequenceNumber: Int,
|
SequenceNumber: Int,
|
||||||
val Metric: Int,
|
Metric: Int,
|
||||||
val Time: Date = new Date(),
|
Time: Date = new Date(),
|
||||||
val Length: Long = -1,
|
Length: Long = -1,
|
||||||
val HopCount: Int = 0) {
|
HopCount: Int = 0) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes the header to byte array.
|
* Writes the header to byte array.
|
||||||
|
@ -93,9 +93,4 @@ class MessageHeader(val MessageType: Int,
|
||||||
case _ => false
|
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 + ")"
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ object RequestAddContact {
|
||||||
/**
|
/**
|
||||||
* Sent when the user initiates adding another device as a contact.
|
* 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
|
override def Type = RequestAddContact.Type
|
||||||
|
|
||||||
|
@ -27,8 +27,6 @@ class RequestAddContact extends MessageBody {
|
||||||
b.array()
|
b.array()
|
||||||
}
|
}
|
||||||
|
|
||||||
override def toString = "RequestAddContact()"
|
|
||||||
|
|
||||||
override def length = 4
|
override def length = 4
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ object ResultAddContact {
|
||||||
/**
|
/**
|
||||||
* Contains the result of a [[RequestAddContact]] message.
|
* 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
|
override def Type = ResultAddContact.Type
|
||||||
|
|
||||||
|
@ -34,8 +34,6 @@ class ResultAddContact(val Accepted: Boolean) extends MessageBody {
|
||||||
b.array()
|
b.array()
|
||||||
}
|
}
|
||||||
|
|
||||||
override def toString = "ResultAddContact(Accepted=" + Accepted + ")"
|
|
||||||
|
|
||||||
override def length = 4
|
override def length = 4
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ object Text {
|
||||||
/**
|
/**
|
||||||
* Holds a plain text message.
|
* Holds a plain text message.
|
||||||
*/
|
*/
|
||||||
class Text(val text: String) extends MessageBody {
|
case class Text(text: String) extends MessageBody {
|
||||||
|
|
||||||
override def Type = Text.Type
|
override def Type = Text.Type
|
||||||
|
|
||||||
|
@ -38,13 +38,6 @@ class Text(val text: String) extends MessageBody {
|
||||||
b.array()
|
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
|
override def length = write.length
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,9 @@ import com.nutomic.ensichat.protocol.Address
|
||||||
*/
|
*/
|
||||||
object IdenticonGenerator {
|
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.
|
* Generates an identicon for the key.
|
||||||
|
|
Reference in a new issue