Skip to content

Commit

Permalink
Made extandable protocol. At least backwards compatibility will be a …
Browse files Browse the repository at this point in the history
…thing.
  • Loading branch information
Revertron committed Jan 16, 2023
1 parent 28c9456 commit 96433af
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 24 deletions.
4 changes: 2 additions & 2 deletions apps/Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId "com.revertron.mimir"
minSdk 21
targetSdk 31
versionCode 14
versionName "1.7.1"
versionCode 15
versionName "1.8.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
69 changes: 51 additions & 18 deletions apps/Android/app/src/main/java/com/revertron/mimir/net/Messages.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.revertron.mimir.net

import android.util.Log
import org.json.JSONException
import org.json.JSONObject
import java.io.ByteArrayOutputStream
import java.io.DataInputStream
import java.io.DataOutputStream
Expand Down Expand Up @@ -150,25 +152,43 @@ fun writeChallengeAnswer(dos: DataOutputStream, challenge: ChallengeAnswer, stre
* Reads message from socket
*/
fun readMessage(dis: DataInputStream): Message? {
val guid = dis.readLong()
val replyTo = dis.readLong()
val sendTime = dis.readLong()
val editTime = dis.readLong()
val type = dis.readInt()
val size = dis.readInt()
//TODO check for too big sizes
val data = ByteArray(size)
var size = dis.readInt()
var data = ByteArray(size)
var count = 0
Log.d(TAG, "Guid $guid, size is $size, reading bytes...")
while (count < size) {
val read = dis.read(data, count, size - count)
Log.d(TAG, "Read $read bytes")
if (read < 0) {
return null
}
count += read
}
return Message(guid, replyTo, sendTime, editTime, type, data)

try {
val json = JSONObject(String(data))
val guid = json.getLong("guid")
val replyTo = json.optLong("replyTo", 0)
val sendTime = json.optLong("sendTime", 0)
val editTime = json.optLong("editTime", 0)
val type = json.optInt("type", 0)
size = json.optInt("payloadSize", 0)
if (size > 0) {
data = ByteArray(size)
count = 0
while (count < size) {
val read = dis.read(data, count, size - count)
if (read < 0) {
return null
}
count += read
}
} else {
data = ByteArray(0)
}
return Message(guid, replyTo, sendTime, editTime, type, data)
} catch (e: JSONException) {
Log.e(TAG, "Error parsing JSON: $e")
return null
}
}

/**
Expand All @@ -178,13 +198,26 @@ fun writeMessage(dos: DataOutputStream, message: Message, stream: Int = 0, type:
val size = 8 + 4 + 4 + message.data.size
writeHeader(dos, stream, type, size)

dos.writeLong(message.guid)
dos.writeLong(message.replyTo)
dos.writeLong(message.sendTime)
dos.writeLong(message.editTime)
dos.writeInt(message.type)
dos.writeInt(message.data.size)
dos.write(message.data)
val json = JSONObject()
json.put("guid", message.guid)
if (message.replyTo != 0L) {
json.put("replyTo", message.replyTo)
}
json.put("sendTime", message.sendTime)
if (message.editTime != 0L) {
json.put("editTime", message.editTime)
}
json.put("type", message.type)
if (message.data.isNotEmpty()) {
json.put("payloadSize", message.data.size)
}
val jsonData = json.toString().toByteArray()

dos.writeInt(jsonData.size)
dos.write(jsonData)
if (message.data.isNotEmpty()) {
dos.write(message.data)
}
dos.flush()
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class SqlStorage(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, nul
}
if (this.writableDatabase.update("messages", values, "guid = ? AND contact = ?", arrayOf("$guid", "$contact")) > 0) {
val id = getMessageIdByGuid(guid)
Log.i(TAG, "Message $id - $guid delivered = $delivered")
Log.i(TAG, "Message $id with guid $guid delivered = $delivered")
for (listener in listeners) {
listener.onMessageDelivered(id, delivered)
}
Expand Down Expand Up @@ -472,7 +472,7 @@ class SqlStorage(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, nul
return null
}

fun getMessageIdByGuid(guid: Long): Long {
private fun getMessageIdByGuid(guid: Long): Long {
val db = this.readableDatabase
val statement = db.compileStatement("SELECT id FROM messages WHERE guid=? LIMIT 1")
statement.bindLong(1, guid)
Expand Down Expand Up @@ -642,8 +642,8 @@ class SqlStorage(context: Context): SQLiteOpenHelper(context, DATABASE_NAME, nul
writableDatabase.delete("messages", "id=?", arrayOf("$messageId"))
}

fun generateGuid(time: Long, data: ByteArray): Long {
return (time.hashCode().toLong() shl 32) or data.contentHashCode().toLong()
private fun generateGuid(time: Long, data: ByteArray): Long {
return (data.contentHashCode().toLong() shl 32) xor time
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class ContactsAdapter(private var dataSet: List<Contact>, private val onclick: V
} else {
holder.deliveredIcon.setImageResource(R.drawable.ic_message_not_sent)
}
holder.deliveredIcon.visibility = View.VISIBLE
} else {
holder.deliveredIcon.visibility = View.GONE
}
Expand Down

0 comments on commit 96433af

Please sign in to comment.