@@ -43,6 +43,7 @@ import org.threeten.bp.ZoneId
4343import java.net.InetAddress
4444import java.nio.file.Files
4545import java.nio.file.Paths
46+ import java.util.concurrent.TimeUnit
4647import kotlin.runCatching
4748import kotlin.streams.toList
4849
@@ -106,130 +107,137 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
106107 } else {
107108 connectToFileExploreServer(remoteAddress)
108109 }
109- val handshakeModel = fileConnection.observeConnected().await()
110-
111- updateState { oldState ->
112- oldState.copy(
113- connectionStatus = ConnectionStatus .Connected (
114- localAddress = localAddress,
115- remoteAddress = remoteAddress,
116- handshakeModel = handshakeModel,
117- fileExploreConnection = fileConnection
110+ val handshakeModel = try {
111+ fileConnection.observeConnected().timeout(30 * 1000 , TimeUnit .MILLISECONDS ).await()
112+ } catch (t: Throwable ) {
113+ t.printStackTrace()
114+ null
115+ }
116+
117+ if (handshakeModel != null ) {
118+ updateState { oldState ->
119+ oldState.copy(
120+ connectionStatus = ConnectionStatus .Connected (
121+ localAddress = localAddress,
122+ remoteAddress = remoteAddress,
123+ handshakeModel = handshakeModel,
124+ fileExploreConnection = fileConnection
125+ )
118126 )
119- )
120- }.await()
121-
122-
123- fileConnection.observeRemoteFileExploreContent()
124- .doOnNext {
125- when (it) {
126- is MessageModel -> {
127- val lastMessages = fileTransportScopeData.messagesEvent.firstOrError().blockingGet()
128- val message = FileTransportScopeData .Companion .Message (
129- isRemote = true ,
130- timeMilli = SystemClock .uptimeMillis(),
131- message = it.message
132- )
133- fileTransportScopeData.messagesEvent.onNext(lastMessages + message)
134- }
135- is RequestFilesModel -> {
136- runBlocking(context = this .coroutineContext) {
137- val dialog = withContext(Dispatchers .Main ) {
138- showLoadingDialog()
139- }
140- fileConnection.sendFileExploreContentToRemote(
141- fileExploreContent = ShareFilesModel (shareFiles = it.requestFiles),
142- waitReplay = true
127+ }.await()
128+
129+
130+ fileConnection.observeRemoteFileExploreContent()
131+ .doOnNext {
132+ when (it) {
133+ is MessageModel -> {
134+ val lastMessages = fileTransportScopeData.messagesEvent.firstOrError().blockingGet()
135+ val message = FileTransportScopeData .Companion .Message (
136+ isRemote = true ,
137+ timeMilli = SystemClock .uptimeMillis(),
138+ message = it.message
143139 )
144- withContext(Dispatchers .Main ) {
145- dialog.cancel()
146- val result = kotlin.runCatching {
147- startSendingFiles(
148- files = it.requestFiles,
149- localAddress = localAddress,
150- pathConverter = defaultPathConverter
151- ).await()
140+ fileTransportScopeData.messagesEvent.onNext(lastMessages + message)
141+ }
142+ is RequestFilesModel -> {
143+ runBlocking(context = this .coroutineContext) {
144+ val dialog = withContext(Dispatchers .Main ) {
145+ showLoadingDialog()
152146 }
153- if (result.isFailure) {
154- Log .e(" SendingFileError" , " SendingFileError" , result.exceptionOrNull())
147+ fileConnection.sendFileExploreContentToRemote(
148+ fileExploreContent = ShareFilesModel (shareFiles = it.requestFiles),
149+ waitReplay = true
150+ )
151+ withContext(Dispatchers .Main ) {
152+ dialog.cancel()
153+ val result = kotlin.runCatching {
154+ startSendingFiles(
155+ files = it.requestFiles,
156+ localAddress = localAddress,
157+ pathConverter = defaultPathConverter
158+ ).await()
159+ }
160+ if (result.isFailure) {
161+ Log .e(" SendingFileError" , " SendingFileError" , result.exceptionOrNull())
162+ }
155163 }
156164 }
157- }
158-
159- }
160- is RequestFolderModel -> {
161- val shareFolder = bindState().firstOrError().blockingGet().shareMyDir
162- val parentPath = it.requestPath
163- val path = Paths .get(FileConstants .homePathString + parentPath)
164- val children = if (shareFolder && Files .isReadable(path)) {
165- Files .list(path)
166- .filter { Files .isReadable(it) }
167- .map { p ->
168- val name = p.fileName.toString()
169- val lastModify = OffsetDateTime .ofInstant(
170- Instant .ofEpochMilli(
171- Files .getLastModifiedTime(p).toMillis()
172- ), ZoneId .systemDefault()
173- )
174- val pathString =
175- if (parentPath.endsWith(FileConstants .FILE_SEPARATOR )) parentPath + name else parentPath + FileConstants .FILE_SEPARATOR + name
176- if (Files .isDirectory(p)) {
177- Folder (
178- name = name,
179- path = pathString,
180- childCount = p.let {
181- val s = Files .list(it)
182- val size = s.count()
183- s.close()
184- size
185- },
186- lastModify = lastModify
187- )
188- } else {
189- File (
190- name = name,
191- path = pathString,
192- size = Files .size(p),
193- lastModify = lastModify
194- )
195- }
196- }.toList()
197165
198- } else {
199- emptyList()
200166 }
201- fileConnection.sendFileExploreContentToRemote(
202- fileExploreContent = ShareFolderModel (
203- path = parentPath,
204- childrenFolders = children.filterIsInstance<Folder >(),
205- childrenFiles = children.filterIsInstance<File >()
167+ is RequestFolderModel -> {
168+ val shareFolder = bindState().firstOrError().blockingGet().shareMyDir
169+ val parentPath = it.requestPath
170+ val path = Paths .get(FileConstants .homePathString + parentPath)
171+ val children = if (shareFolder && Files .isReadable(path)) {
172+ Files .list(path)
173+ .filter { Files .isReadable(it) }
174+ .map { p ->
175+ val name = p.fileName.toString()
176+ val lastModify = OffsetDateTime .ofInstant(
177+ Instant .ofEpochMilli(
178+ Files .getLastModifiedTime(p).toMillis()
179+ ), ZoneId .systemDefault()
180+ )
181+ val pathString =
182+ if (parentPath.endsWith(FileConstants .FILE_SEPARATOR )) parentPath + name else parentPath + FileConstants .FILE_SEPARATOR + name
183+ if (Files .isDirectory(p)) {
184+ Folder (
185+ name = name,
186+ path = pathString,
187+ childCount = p.let {
188+ val s = Files .list(it)
189+ val size = s.count()
190+ s.close()
191+ size
192+ },
193+ lastModify = lastModify
194+ )
195+ } else {
196+ File (
197+ name = name,
198+ path = pathString,
199+ size = Files .size(p),
200+ lastModify = lastModify
201+ )
202+ }
203+ }.toList()
204+
205+ } else {
206+ emptyList()
207+ }
208+ fileConnection.sendFileExploreContentToRemote(
209+ fileExploreContent = ShareFolderModel (
210+ path = parentPath,
211+ childrenFolders = children.filterIsInstance<Folder >(),
212+ childrenFiles = children.filterIsInstance<File >()
213+ )
206214 )
207- )
208- }
209- is ShareFilesModel -> {
210- val result = runCatching {
211- val unit = startDownloadingFiles(it.shareFiles, remoteAddress).blockingGet()
212215 }
213- if (result.isFailure) {
214- Log .e(
215- " Download Files Fail" ,
216- " Download Files Fail" ,
217- result.exceptionOrNull()
218- )
216+ is ShareFilesModel -> {
217+ val result = runCatching {
218+ val unit = startDownloadingFiles(it.shareFiles, remoteAddress).blockingGet()
219+ }
220+ if (result.isFailure) {
221+ Log .e(
222+ " Download Files Fail" ,
223+ " Download Files Fail" ,
224+ result.exceptionOrNull()
225+ )
226+ }
219227 }
228+ is ShareFolderModel -> {
229+ fileTransportScopeData.remoteFolderModelEvent.onNext(ResponseFolderModel (
230+ path = it.path,
231+ childrenFolders = it.childrenFolders,
232+ childrenFiles = it.childrenFiles
233+ ))
234+ }
235+ else -> {}
220236 }
221- is ShareFolderModel -> {
222- fileTransportScopeData.remoteFolderModelEvent.onNext(ResponseFolderModel (
223- path = it.path,
224- childrenFolders = it.childrenFolders,
225- childrenFiles = it.childrenFiles
226- ))
227- }
228- else -> {}
229237 }
230- }
231- .ignoreElements ()
232- .await()
238+ .ignoreElements()
239+ .await ()
240+ }
233241
234242 withContext(Dispatchers .Main ) {
235243 showNoOptionalDialog(
0 commit comments