mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-16 02:48:08 -04:00
#1351 moved peerHandShake to each file
This commit is contained in:
parent
9891c19b67
commit
6853e82b76
@ -30,7 +30,6 @@ import java.net.InetAddress
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
import java.util.ArrayList
|
||||
|
||||
/**
|
||||
* Helper class for the local file sharing module.
|
||||
@ -105,43 +104,9 @@ internal class PeerGroupHandshake(private val wifiDirectManager: WifiDirectManag
|
||||
|
||||
private fun exchangeFileTransferMetadata(outputStream: OutputStream, inputStream: InputStream) {
|
||||
if (wifiDirectManager.isFileSender) {
|
||||
try {
|
||||
ObjectOutputStream(outputStream).use { objectOutputStream ->
|
||||
// Send total number of files which will be transferred
|
||||
objectOutputStream.writeObject(wifiDirectManager.totalFilesForTransfer)
|
||||
// Send the names of each of those files, in order
|
||||
wifiDirectManager.getFilesForTransfer().forEach { fileItem ->
|
||||
objectOutputStream.writeObject(fileItem.fileName)
|
||||
Log.d(TAG, "Sending " + fileItem.fileUri.toString())
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
SenderHandShake().handShake(wifiDirectManager, outputStream)
|
||||
} else { // Device is not the file sender
|
||||
try {
|
||||
ObjectInputStream(inputStream).use { objectInputStream ->
|
||||
// Read the number of files
|
||||
val totalFilesObject = objectInputStream.readObject().toString()
|
||||
if (totalFilesObject.javaClass == String::class.java) {
|
||||
val total: Int = totalFilesObject.toInt()
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Metadata: $total files")
|
||||
// Read names of each of those files, in order
|
||||
val fileItems = sequence {
|
||||
yieldAll(generateSequence(1) { it + 1 }.map {
|
||||
(objectInputStream.readObject() as? String)?.let { fileName ->
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Expecting $fileName")
|
||||
FileItem(fileName = fileName)
|
||||
}
|
||||
})
|
||||
}.take(total)
|
||||
val arrayListOfFileItems = ArrayList(fileItems.toList().filterNotNull())
|
||||
wifiDirectManager.setFilesForTransfer(arrayListOfFileItems)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
ReceiverHandShake().handShake(wifiDirectManager, inputStream)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2020 Kiwix <android.kiwix.org>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.kiwix.kiwixmobile.localFileTransfer
|
||||
|
||||
import android.util.Log
|
||||
import org.kiwix.kiwixmobile.core.BuildConfig
|
||||
import java.io.InputStream
|
||||
import java.io.ObjectInputStream
|
||||
import java.util.ArrayList
|
||||
|
||||
class ReceiverHandShake {
|
||||
fun handShake(wifiDirectManager: WifiDirectManager, inputStream: InputStream) {
|
||||
try {
|
||||
ObjectInputStream(inputStream).use { objectInputStream ->
|
||||
// Read the number of files
|
||||
val totalFilesObject = objectInputStream.readObject().toString()
|
||||
if (totalFilesObject.javaClass == String::class.java) {
|
||||
val total: Int = totalFilesObject.toInt()
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Metadata: $total files")
|
||||
// Read names of each of those files, in order
|
||||
val fileItems = sequence {
|
||||
yieldAll(generateSequence(1) { it + 1 }.map {
|
||||
(objectInputStream.readObject() as? String)?.let { fileName ->
|
||||
if (BuildConfig.DEBUG) Log.d(TAG, "Expecting $fileName")
|
||||
FileItem(fileName = fileName)
|
||||
}
|
||||
})
|
||||
}.take(total)
|
||||
val arrayListOfFileItems = ArrayList(fileItems.toList().filterNotNull())
|
||||
wifiDirectManager.setFilesForTransfer(arrayListOfFileItems)
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "ReceiverHandshake"
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Kiwix Android
|
||||
* Copyright (c) 2020 Kiwix <android.kiwix.org>
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.kiwix.kiwixmobile.localFileTransfer
|
||||
|
||||
import android.util.Log
|
||||
import java.io.ObjectOutputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
class SenderHandShake {
|
||||
fun handShake(wifiDirectManager: WifiDirectManager, outputStream: OutputStream) {
|
||||
try {
|
||||
ObjectOutputStream(outputStream).use { objectOutputStream ->
|
||||
// Send total number of files which will be transferred
|
||||
objectOutputStream.writeObject(wifiDirectManager.totalFilesForTransfer)
|
||||
// Send the names of each of those files, in order
|
||||
wifiDirectManager.getFilesForTransfer().forEach { fileItem ->
|
||||
objectOutputStream.writeObject(fileItem.fileName)
|
||||
Log.d(TAG, "Sending " + fileItem.fileUri.toString())
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "SenderHandShake"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user