Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f0b465f054 | |||
| 7bdcef8338 | |||
| c2823c1aad | |||
| 37b6a5492b | |||
| 9e632c2f58 | |||
| eaf948d3cd | |||
| 3242d3d742 | |||
| a90d724c06 | |||
| c257a730b0 | |||
| fcbd344491 | |||
| 773bb4259b | |||
| 3f6382e9c6 | |||
| 660346a856 | |||
| 93f2e0de57 | |||
| 1fbecc449a | |||
| c7d368dee6 | |||
| 5addeb8ea6 | |||
| 57364b4e14 | |||
| 4f9c44a4ad | |||
| 126bb507ba | |||
| f352303f6b | |||
| 53749eac5c | |||
| 34147063eb | |||
| edeb5ccb0d | |||
| 248ccc0606 | |||
| 726c9c974e |
+32
@@ -0,0 +1,32 @@
|
||||
sudo: required
|
||||
language: android
|
||||
jdk: oraclejdk8
|
||||
dist: trusty
|
||||
|
||||
# Install Android SDK
|
||||
android:
|
||||
components:
|
||||
- tools
|
||||
- platform-tools
|
||||
- build-tools-28.0.2
|
||||
- android-28
|
||||
- extra-android-m2repository
|
||||
|
||||
before_install:
|
||||
# Hack to accept Android licenses
|
||||
- yes | sdkmanager "platforms;android-27"
|
||||
- yes | sdkmanager "platforms;android-28"
|
||||
|
||||
# Cache gradle dependencies
|
||||
# https://docs.travis-ci.com/user/languages/android/#Caching
|
||||
before_cache:
|
||||
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
|
||||
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.gradle/caches/
|
||||
- $HOME/.gradle/wrapper/
|
||||
|
||||
script:
|
||||
- ./gradlew lint
|
||||
- ./gradlew assembleDebug
|
||||
@@ -1,5 +1,6 @@
|
||||
# Syncthing Lite
|
||||
|
||||
[](https://travis-ci.org/syncthing/syncthing-lite)
|
||||
[](https://www.mozilla.org/MPL/2.0/)
|
||||
|
||||
This project is an Android app, that works as a client for a [Syncthing][1] share (accessing
|
||||
@@ -13,13 +14,16 @@ example, mobile devices with limited storage available, wishing to access a sync
|
||||
|
||||
This project is based on [syncthing-java][3], a java implementation of Syncthing protocols.
|
||||
|
||||
Due to the behaviour of this App and the [behaviour of the Syncthing Server](https://github.com/syncthing/syncthing/issues/5224),
|
||||
you can't reconnct for some minutes if the App was killed (due to removing from the recent App list) or the connection was interrupted.
|
||||
This does not apply to connections over an WiFi, but to connections over the internet.
|
||||
|
||||
[<img alt="Get it on F-Droid" src="https://f-droid.org/badge/get-it-on.png" height="80">](https://f-droid.org/packages/net.syncthing.lite/)
|
||||
[<img alt="Get it on Google Play" src="https://play.google.com/intl/en_us/badges/images/generic/en_badge_web_generic.png" height="80">](https://play.google.com/store/apps/details?id=net.syncthing.lite)
|
||||
|
||||
## Translations
|
||||
|
||||
The project is translated on [Transifex](https://www.transifex.com/syncthing-android/syncthing-lite/).
|
||||
The project is translated on [Transifex](https://www.transifex.com/syncthing/syncthing-lite/).
|
||||
|
||||
## Building
|
||||
|
||||
|
||||
+33
-12
@@ -1,18 +1,18 @@
|
||||
apply plugin: 'com.android.application'
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-kapt'
|
||||
apply plugin: 'com.github.ben-manes.versions'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 27
|
||||
buildToolsVersion "27.0.2"
|
||||
buildToolsVersion "28.0.2"
|
||||
dataBinding.enabled = true
|
||||
defaultConfig {
|
||||
applicationId "net.syncthing.lite"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 25
|
||||
versionCode 9
|
||||
versionName "0.2.1"
|
||||
targetSdkVersion 26
|
||||
versionCode 10
|
||||
versionName "0.3.0"
|
||||
multiDexEnabled true
|
||||
}
|
||||
sourceSets {
|
||||
@@ -34,34 +34,55 @@ android {
|
||||
}
|
||||
}
|
||||
buildTypes {
|
||||
debug {
|
||||
minifyEnabled false
|
||||
}
|
||||
release {
|
||||
signingConfig signingConfigs.release
|
||||
minifyEnabled true
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
packagingOptions {
|
||||
exclude 'META-INF/*'
|
||||
}
|
||||
dataBinding {
|
||||
enabled = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation "org.jetbrains.anko:anko-commons:$anko_version"
|
||||
implementation "org.jetbrains.anko:anko-coroutines:$anko_version"
|
||||
kapt "com.android.databinding:compiler:$build_tools_version"
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.2'
|
||||
implementation "com.android.support:design:$support_version"
|
||||
implementation "com.android.support:preference-v14:$support_version"
|
||||
kapt "com.android.databinding:library:1.3.3"
|
||||
implementation ("com.github.Nutomic:syncthing-java:0.2.1") {
|
||||
exclude group: 'commons-logging', module:'commons-logging'
|
||||
exclude group: 'org.apache.httpcomponents', module:'httpclient'
|
||||
implementation "com.android.support:support-v4:$support_version"
|
||||
implementation 'android.arch.lifecycle:extensions:1.1.1'
|
||||
|
||||
/**
|
||||
* syncthing-java depends on the Apache HTTP Client
|
||||
* https://github.com/syncthing/syncthing-java/blob/dd020737ba5fc6a7c681a1d258025b8ddb2e8f67/core/build.gradle#L9
|
||||
*
|
||||
* Android itself contains an older version of this HTTP Client. Due to that, there is an
|
||||
* extra version of it which does not cause conflicts with the builtin client of Android.
|
||||
*
|
||||
* This extra implementation is included below. As this other version is used,
|
||||
* it's ignored as dependency of syncthing-java.
|
||||
*/
|
||||
implementation(project(':syncthing-client')) {
|
||||
exclude group: 'commons-logging', module: 'commons-logging'
|
||||
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
|
||||
exclude group: 'org.slf4j'
|
||||
exclude group: 'ch.qos.logback'
|
||||
}
|
||||
// NOTE: httpclient-android seems to be used via reflection somehow. Removing this dependency
|
||||
// silently breaks the app.
|
||||
implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
|
||||
|
||||
implementation 'sk.baka.slf4j:slf4j-handroid:1.7.26'
|
||||
implementation 'com.google.zxing:android-integration:3.3.0'
|
||||
implementation 'com.google.zxing:core:3.3.0'
|
||||
implementation 'com.github.apl-devs:appintro:v4.2.3'
|
||||
|
||||
implementation project(':syncthing-repository-android')
|
||||
}
|
||||
|
||||
Vendored
+89
@@ -0,0 +1,89 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# By default, the flags in this file are appended to flags specified
|
||||
# in /home/jonas/android-studio/sdk/tools/proguard/proguard-android.txt
|
||||
# You can edit the include path and order by changing the proguardFiles
|
||||
# directive in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# Add any project specific keep options here:
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# ensure that stack traces make sense
|
||||
-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# this library uses factories with reflection
|
||||
-keep class net.jpountz.lz4.** { *; }
|
||||
|
||||
# from https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/kotlinx-coroutines-android/example-app/app/proguard-rules.pro
|
||||
# kotlin coroutines crash without it
|
||||
-keepclassmembernames class kotlinx.** {
|
||||
volatile <fields>;
|
||||
}
|
||||
|
||||
# disable warnings
|
||||
-dontwarn com.google.protobuf.UnsafeUtil
|
||||
-dontwarn com.google.protobuf.UnsafeUtil$1
|
||||
-dontwarn net.jpountz.util.UnsafeUtils
|
||||
-dontwarn org.bouncycastle.cert.dane.fetcher.JndiDANEFetcherFactory
|
||||
-dontwarn org.bouncycastle.cert.dane.fetcher.JndiDANEFetcherFactory$1
|
||||
-dontwarn org.bouncycastle.jce.provider.X509LDAPCertStoreSpi
|
||||
-dontwarn org.bouncycastle.mail.smime.CMSProcessableBodyPart
|
||||
-dontwarn org.bouncycastle.mail.smime.CMSProcessableBodyPartInbound
|
||||
-dontwarn org.bouncycastle.mail.smime.CMSProcessableBodyPartOutbound
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.CreateCompressedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.CreateEncryptedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.CreateLargeCompressedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.CreateLargeEncryptedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.CreateLargeSignedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.CreateSignedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.CreateSignedMultipartMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ExampleUtils
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ReadCompressedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ReadEncryptedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ReadLargeCompressedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ReadLargeEncryptedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ReadLargeSignedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ReadSignedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.SendSignedAndEncryptedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.examples.ValidateSignedMail
|
||||
-dontwarn org.bouncycastle.mail.smime.handlers.multipart_signed
|
||||
-dontwarn org.bouncycastle.mail.smime.handlers.multipart_signed$LineOutputStream
|
||||
-dontwarn org.bouncycastle.mail.smime.handlers.PKCS7ContentHandler
|
||||
-dontwarn org.bouncycastle.mail.smime.handlers.pkcs7_mime
|
||||
-dontwarn org.bouncycastle.mail.smime.handlers.pkcs7_signature
|
||||
-dontwarn org.bouncycastle.mail.smime.handlers.x_pkcs7_mime
|
||||
-dontwarn org.bouncycastle.mail.smime.handlers.x_pkcs7_signature
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMECompressed
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMECompressedGenerator
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMECompressedGenerator$1
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMECompressedGenerator$ContentCompressor
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMECompressedParser
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEEnveloped
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator$1
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEEnvelopedGenerator$ContentEncryptor
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEEnvelopedParser
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEGenerator
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMESigned
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMESigned$1
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMESignedGenerator
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMESignedGenerator$1
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMESignedGenerator$ContentSigner
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMESignedParser
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMESignedParser$1
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEToolkit
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEUtil
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEUtil$LineOutputStream
|
||||
-dontwarn org.bouncycastle.mail.smime.SMIMEUtil$WriteOnceFileBackedMimeBodyPart
|
||||
-dontwarn org.bouncycastle.mail.smime.util.FileBackedMimeBodyPart
|
||||
-dontwarn org.bouncycastle.mail.smime.util.SharedFileInputStream
|
||||
-dontwarn org.bouncycastle.mail.smime.validator.SignedMailValidator
|
||||
-dontwarn org.bouncycastle.x509.util.LDAPStoreHelper
|
||||
@@ -5,16 +5,21 @@ import android.content.Intent
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.View
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.java.bep.IndexBrowser
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.java.core.utils.PathUtils
|
||||
import net.syncthing.lite.BuildConfig
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.adapters.FolderContentsAdapter
|
||||
import net.syncthing.lite.adapters.FolderContentsListener
|
||||
import net.syncthing.lite.databinding.ActivityFolderBrowserBinding
|
||||
import net.syncthing.lite.dialogs.FileDownloadDialog
|
||||
import net.syncthing.lite.dialogs.FileUploadDialog
|
||||
import net.syncthing.lite.dialogs.ReconnectIssueDialogFragment
|
||||
import net.syncthing.lite.dialogs.downloadfile.DownloadFileDialogFragment
|
||||
import org.jetbrains.anko.custom.async
|
||||
|
||||
class FolderBrowserActivity : SyncthingActivity() {
|
||||
|
||||
@@ -28,23 +33,25 @@ class FolderBrowserActivity : SyncthingActivity() {
|
||||
|
||||
private lateinit var binding: ActivityFolderBrowserBinding
|
||||
private lateinit var indexBrowser: IndexBrowser
|
||||
private lateinit var adapter: FolderContentsAdapter
|
||||
private val adapter = FolderContentsAdapter()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = DataBindingUtil.setContentView(this, R.layout.activity_folder_browser)
|
||||
binding.mainListViewUploadHereButton.setOnClickListener { showUploadHereDialog() }
|
||||
adapter = FolderContentsAdapter(this)
|
||||
binding.listView.adapter = adapter
|
||||
binding.listView.setOnItemClickListener { _, _, position, _ ->
|
||||
val fileInfo = binding.listView.getItemAtPosition(position) as FileInfo
|
||||
navigateToFolder(fileInfo)
|
||||
adapter.listener = object: FolderContentsListener {
|
||||
override fun onItemClicked(fileInfo: FileInfo) {
|
||||
navigateToFolder(fileInfo)
|
||||
}
|
||||
}
|
||||
val folder = intent.getStringExtra(EXTRA_FOLDER_NAME)
|
||||
libraryHandler?.syncthingClient {
|
||||
indexBrowser = it.indexHandler.newIndexBrowser(folder, true, true)
|
||||
indexBrowser.setOnFolderChangedListener(this::onFolderChanged)
|
||||
}
|
||||
|
||||
ReconnectIssueDialogFragment.showIfNeeded(this)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
@@ -56,17 +63,19 @@ class FolderBrowserActivity : SyncthingActivity() {
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
val listView = binding.listView
|
||||
//click item '0', ie '..' (go to parent)
|
||||
listView.performItemClick(adapter.getView(0, null, listView), 0, listView.getItemIdAtPosition(0))
|
||||
navigateToFolder(adapter.data[0])
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
|
||||
if (requestCode == REQUEST_SELECT_UPLOAD_FILE && resultCode == Activity.RESULT_OK) {
|
||||
libraryHandler?.syncthingClient { syncthingClient ->
|
||||
FileUploadDialog(this@FolderBrowserActivity, syncthingClient, intent!!.data,
|
||||
indexBrowser.folder, indexBrowser.currentPath,
|
||||
{ showFolderListView(indexBrowser.currentPath) }).show()
|
||||
async (UI) {
|
||||
// FIXME: it would be better if the dialog would use the library handler
|
||||
FileUploadDialog(this@FolderBrowserActivity, syncthingClient, intent!!.data,
|
||||
indexBrowser.folder, indexBrowser.currentPath,
|
||||
{ showFolderListView(indexBrowser.currentPath) }).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,35 +91,47 @@ class FolderBrowserActivity : SyncthingActivity() {
|
||||
finish()
|
||||
} else {
|
||||
if (fileInfo.isDirectory()) {
|
||||
indexBrowser.navigateTo(fileInfo)
|
||||
async {
|
||||
indexBrowser.navigateTo(fileInfo)
|
||||
}
|
||||
|
||||
Log.d(TAG, "load folder cache bg")
|
||||
binding.listView.visibility = View.GONE
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
binding.isLoading = true
|
||||
} else {
|
||||
Log.i(TAG, "pulling file = " + fileInfo)
|
||||
libraryHandler?.syncthingClient { FileDownloadDialog(this, it, fileInfo).show() }
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "pulling file = " + fileInfo)
|
||||
}
|
||||
|
||||
DownloadFileDialogFragment.newInstance(fileInfo).show(supportFragmentManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onFolderChanged() {
|
||||
runOnUiThread {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
binding.listView.visibility = View.VISIBLE
|
||||
val list = indexBrowser.listFiles()
|
||||
Log.i("navigateToFolder", "list for path = '" + indexBrowser.currentPath + "' list = " + list.size + " records")
|
||||
Log.d("navigateToFolder", "list for path = '" + indexBrowser.currentPath + "' list = " + list)
|
||||
assert(!list.isEmpty())//list must contain at least the 'parent' path
|
||||
adapter.clear()
|
||||
adapter.addAll(list)
|
||||
adapter.notifyDataSetChanged()
|
||||
binding.listView.setSelection(0)
|
||||
if (indexBrowser.isRoot())
|
||||
libraryHandler?.folderBrowser {
|
||||
supportActionBar?.title = it.getFolderInfo(indexBrowser.folder)?.label
|
||||
binding.isLoading = false
|
||||
|
||||
async {
|
||||
val list = indexBrowser.listFiles()
|
||||
|
||||
async (UI) {
|
||||
Log.i("navigateToFolder", "list for path = '" + indexBrowser.currentPath + "' list = " + list.size + " records")
|
||||
Log.d("navigateToFolder", "list for path = '" + indexBrowser.currentPath + "' list = " + list)
|
||||
assert(!list.isEmpty())//list must contain at least the 'parent' path
|
||||
adapter.data = list
|
||||
binding.listView.scrollToPosition(0)
|
||||
if (indexBrowser.isRoot())
|
||||
libraryHandler?.folderBrowser {
|
||||
val title = it.getFolderInfo(indexBrowser.folder)?.label
|
||||
|
||||
async(UI) {
|
||||
supportActionBar?.title = title
|
||||
}
|
||||
}
|
||||
else
|
||||
supportActionBar?.title = indexBrowser.currentPathInfo().fileName
|
||||
}
|
||||
else
|
||||
supportActionBar?.title = indexBrowser.currentPathInfo().fileName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
package net.syncthing.lite.activities
|
||||
|
||||
import android.arch.lifecycle.Observer
|
||||
import android.content.Intent
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.support.v4.app.Fragment
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.text.Html
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import com.github.paolorotolo.appintro.AppIntro
|
||||
import com.google.zxing.integration.android.IntentIntegrator
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.databinding.FragmentIntroOneBinding
|
||||
@@ -69,14 +75,17 @@ class IntroActivity : AppIntro() {
|
||||
class IntroFragmentOne : SyncthingFragment() {
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
return DataBindingUtil.inflate<FragmentIntroOneBinding>(
|
||||
inflater, R.layout.fragment_intro_one, container, false).root
|
||||
val binding = FragmentIntroOneBinding.inflate(inflater, container, false)
|
||||
|
||||
libraryHandler.isListeningPortTaken.observe(this, Observer { binding.listeningPortTaken = it })
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onLibraryLoaded() {
|
||||
super.onLibraryLoaded()
|
||||
context?.let { SyncthingActivity.checkLocalDiscoveryPort(it) }
|
||||
libraryHandler?.configuration { config ->
|
||||
|
||||
libraryHandler.configuration { config ->
|
||||
config.localDeviceName = Util.getDeviceName()
|
||||
config.persistLater()
|
||||
}
|
||||
@@ -121,6 +130,46 @@ class IntroActivity : AppIntro() {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private val addedDeviceIds = HashSet<DeviceId>()
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
binding.foundDevices.removeAllViews()
|
||||
addedDeviceIds.clear()
|
||||
|
||||
libraryHandler.registerMessageFromUnknownDeviceListener(onDeviceFound)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
|
||||
libraryHandler.unregisterMessageFromUnknownDeviceListener(onDeviceFound)
|
||||
}
|
||||
|
||||
private val onDeviceFound: (DeviceId) -> Unit = {
|
||||
deviceId ->
|
||||
|
||||
if (addedDeviceIds.add(deviceId)) {
|
||||
binding.foundDevices.addView(
|
||||
Button(context).apply {
|
||||
layoutParams = ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
text = deviceId.deviceId
|
||||
|
||||
setOnClickListener {
|
||||
binding.enterDeviceId.deviceId.setText(deviceId.deviceId)
|
||||
binding.enterDeviceId.deviceIdHolder.isErrorEnabled = false
|
||||
|
||||
binding.scroll.scrollTo(0, 0)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,26 +181,28 @@ class IntroActivity : AppIntro() {
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_intro_three, container, false)
|
||||
|
||||
libraryHandler.library { config, client, _ ->
|
||||
async(UI) {
|
||||
client.addOnConnectionChangedListener(this@IntroFragmentThree::onConnectionChanged)
|
||||
val deviceId = config.localDeviceId.deviceId
|
||||
val desc = activity?.getString(R.string.intro_page_three_description, "<b>$deviceId</b>")
|
||||
binding.description.text = Html.fromHtml(desc)
|
||||
}
|
||||
}
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onLibraryLoaded() {
|
||||
super.onLibraryLoaded()
|
||||
libraryHandler?.library { config, client, _ ->
|
||||
client.addOnConnectionChangedListener(this::onConnectionChanged)
|
||||
val deviceId = config.localDeviceId.deviceId
|
||||
val desc = activity?.getString(R.string.intro_page_three_description, "<b>$deviceId</b>")
|
||||
binding.description.text = Html.fromHtml(desc)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onConnectionChanged(deviceId: DeviceId) {
|
||||
libraryHandler?.library { config, client, _ ->
|
||||
if (config.folders.isNotEmpty()) {
|
||||
client.removeOnConnectionChangedListener(this::onConnectionChanged)
|
||||
(activity as IntroActivity?)?.onDonePressed(this)
|
||||
libraryHandler.library { config, client, _ ->
|
||||
async(UI) {
|
||||
if (config.folders.isNotEmpty()) {
|
||||
client.removeOnConnectionChangedListener(this@IntroFragmentThree::onConnectionChanged)
|
||||
(activity as IntroActivity?)?.onDonePressed(this@IntroFragmentThree)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.databinding.ActivityMainBinding
|
||||
import net.syncthing.lite.dialogs.DeviceIdDialog
|
||||
import net.syncthing.lite.dialogs.DeviceIdDialogFragment
|
||||
import net.syncthing.lite.fragments.DevicesFragment
|
||||
import net.syncthing.lite.fragments.FoldersFragment
|
||||
import net.syncthing.lite.fragments.SettingsFragment
|
||||
@@ -80,9 +80,7 @@ class MainActivity : SyncthingActivity() {
|
||||
R.id.folders -> setContentFragment(FoldersFragment())
|
||||
R.id.devices -> setContentFragment(DevicesFragment())
|
||||
R.id.settings -> setContentFragment(SettingsFragment())
|
||||
R.id.device_id -> libraryHandler?.configuration { config ->
|
||||
DeviceIdDialog(this, config.localDeviceId).show()
|
||||
}
|
||||
R.id.device_id -> DeviceIdDialogFragment().show(supportFragmentManager)
|
||||
R.id.clear_index -> AlertDialog.Builder(this)
|
||||
.setTitle(getString(R.string.clear_cache_and_index_title))
|
||||
.setMessage(getString(R.string.clear_cache_and_index_body))
|
||||
@@ -104,7 +102,7 @@ class MainActivity : SyncthingActivity() {
|
||||
|
||||
private fun cleanCacheAndIndex() {
|
||||
async(UI) {
|
||||
libraryHandler?.syncthingClient { it.clearCacheAndIndex() }
|
||||
libraryHandler.syncthingClient { it.clearCacheAndIndex() }
|
||||
recreate()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,51 +16,47 @@ import org.jetbrains.anko.contentView
|
||||
import org.slf4j.impl.HandroidLoggerAdapter
|
||||
|
||||
abstract class SyncthingActivity : AppCompatActivity() {
|
||||
|
||||
companion object {
|
||||
fun checkLocalDiscoveryPort(context: Context) {
|
||||
if (LibraryHandler.isListeningPortTaken) {
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(R.string.other_syncthing_instance_title)
|
||||
.setMessage(R.string.other_syncthing_instance_message)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
val libraryHandler: LibraryHandler by lazy {
|
||||
LibraryHandler(
|
||||
context = this@SyncthingActivity,
|
||||
onIndexUpdateProgressListener = this::onIndexUpdateProgress,
|
||||
onIndexUpdateCompleteListener = this::onIndexUpdateComplete
|
||||
)
|
||||
}
|
||||
|
||||
var libraryHandler: LibraryHandler? = null
|
||||
private set
|
||||
private var loadingDialog: AlertDialog? = null
|
||||
private var snackBar: Snackbar? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
HandroidLoggerAdapter.DEBUG = BuildConfig.DEBUG
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
val binding = DataBindingUtil.inflate<DialogLoadingBinding>(
|
||||
LayoutInflater.from(this), R.layout.dialog_loading, null, false)
|
||||
binding.loadingText.text = getString(R.string.loading_config_starting_syncthing_client)
|
||||
|
||||
loadingDialog = AlertDialog.Builder(this)
|
||||
.setCancelable(false)
|
||||
.setView(binding.root)
|
||||
.show()
|
||||
LibraryHandler(this, this::onLibraryLoadedInternal,
|
||||
this::onIndexUpdateProgress, this::onIndexUpdateComplete)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
libraryHandler?.close()
|
||||
loadingDialog?.dismiss()
|
||||
}
|
||||
libraryHandler.start {
|
||||
if (!isDestroyed) {
|
||||
loadingDialog?.dismiss()
|
||||
}
|
||||
|
||||
private fun onLibraryLoadedInternal(libraryHandler: LibraryHandler) {
|
||||
this.libraryHandler = libraryHandler
|
||||
if (!isDestroyed) {
|
||||
loadingDialog?.dismiss()
|
||||
onLibraryLoaded()
|
||||
}
|
||||
onLibraryLoaded()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
|
||||
libraryHandler.stop()
|
||||
loadingDialog?.dismiss()
|
||||
}
|
||||
|
||||
open fun onIndexUpdateProgress(folderInfo: FolderInfo, percentage: Int) {
|
||||
@@ -77,6 +73,6 @@ abstract class SyncthingActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
open fun onLibraryLoaded() {
|
||||
checkLocalDiscoveryPort(this)
|
||||
// nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,47 @@
|
||||
package net.syncthing.lite.adapters
|
||||
|
||||
import android.content.Context
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import net.syncthing.java.core.beans.DeviceInfo
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.databinding.ListviewDeviceBinding
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class DevicesAdapter(context: Context) :
|
||||
ArrayAdapter<DeviceInfo>(context, R.layout.listview_device, mutableListOf()) {
|
||||
class DevicesAdapter: RecyclerView.Adapter<DeviceViewHolder>() {
|
||||
var data: List<DeviceInfo> by Delegates.observable(listOf()) {
|
||||
_, _, _ -> notifyDataSetChanged()
|
||||
}
|
||||
|
||||
override fun getView(position: Int, v: View?, parent: ViewGroup): View {
|
||||
val binding: ListviewDeviceBinding
|
||||
= if (v == null) {
|
||||
DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.listview_device, parent, false)
|
||||
} else {
|
||||
DataBindingUtil.bind(v)
|
||||
}
|
||||
val deviceStats = getItem(position)
|
||||
binding.deviceName.text = deviceStats!!.name
|
||||
val icon =
|
||||
if (deviceStats.isConnected!!) {
|
||||
R.drawable.ic_laptop_green_24dp
|
||||
} else {
|
||||
R.drawable.ic_laptop_red_24dp
|
||||
}
|
||||
binding.deviceIcon.setImageResource(icon)
|
||||
return binding.root
|
||||
var listener: DeviceAdapterListener? = null
|
||||
|
||||
init {
|
||||
setHasStableIds(true)
|
||||
}
|
||||
|
||||
override fun getItemCount() = data.size
|
||||
override fun getItemId(position: Int) = data[position].deviceId.deviceId.hashCode().toLong()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = DeviceViewHolder(
|
||||
ListviewDeviceBinding.inflate(
|
||||
LayoutInflater.from(parent.context), parent, false
|
||||
)
|
||||
)
|
||||
|
||||
override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
|
||||
val deviceStats = data[position]
|
||||
val binding = holder.binding
|
||||
|
||||
binding.name = deviceStats.name
|
||||
binding.isConnected = deviceStats.isConnected
|
||||
|
||||
binding.root.setOnLongClickListener { listener?.onDeviceLongClicked(deviceStats) ?: false }
|
||||
|
||||
binding.executePendingBindings()
|
||||
}
|
||||
}
|
||||
|
||||
interface DeviceAdapterListener {
|
||||
fun onDeviceLongClicked(deviceInfo: DeviceInfo): Boolean
|
||||
}
|
||||
|
||||
class DeviceViewHolder(val binding: ListviewDeviceBinding): RecyclerView.ViewHolder(binding.root)
|
||||
@@ -1,39 +1,64 @@
|
||||
package net.syncthing.lite.adapters
|
||||
|
||||
import android.content.Context
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.text.format.DateUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.databinding.ListviewFileBinding
|
||||
import org.apache.commons.io.FileUtils
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class FolderContentsAdapter(context: Context) :
|
||||
ArrayAdapter<FileInfo>(context, R.layout.listview_file, mutableListOf()) {
|
||||
// TODO: enable setHasStableIds and add a good way to get an id
|
||||
class FolderContentsAdapter: RecyclerView.Adapter<FolderContentsViewHolder>() {
|
||||
var data: List<FileInfo> by Delegates.observable(listOf()) {
|
||||
_, _, _ -> notifyDataSetChanged()
|
||||
}
|
||||
|
||||
var listener: FolderContentsListener? = null
|
||||
|
||||
init {
|
||||
// setHasStableIds(true)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = FolderContentsViewHolder(
|
||||
ListviewFileBinding.inflate(
|
||||
LayoutInflater.from(parent.context),
|
||||
parent,
|
||||
false
|
||||
)
|
||||
)
|
||||
|
||||
override fun onBindViewHolder(holder: FolderContentsViewHolder, position: Int) {
|
||||
val binding = holder.binding
|
||||
val fileInfo = data[position]
|
||||
|
||||
binding.fileName = fileInfo.fileName
|
||||
|
||||
override fun getView(position: Int, v: View?, parent: ViewGroup): View {
|
||||
val binding: ListviewFileBinding =
|
||||
if (v == null) {
|
||||
DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.listview_file, parent, false)
|
||||
} else {
|
||||
DataBindingUtil.bind(v)
|
||||
}
|
||||
val fileInfo = getItem(position)
|
||||
binding.fileLabel.text = fileInfo!!.fileName
|
||||
if (fileInfo.isDirectory()) {
|
||||
binding.fileIcon.setImageResource(R.drawable.ic_folder_black_24dp)
|
||||
binding.fileSize.visibility = View.GONE
|
||||
binding.fileSize = null
|
||||
} else {
|
||||
binding.fileIcon.setImageResource(R.drawable.ic_image_black_24dp)
|
||||
binding.fileSize.visibility = View.VISIBLE
|
||||
binding.fileSize.text = context.getString(R.string.file_info,
|
||||
binding.fileSize = binding.root.context.getString(R.string.file_info,
|
||||
FileUtils.byteCountToDisplaySize(fileInfo.size!!),
|
||||
DateUtils.getRelativeDateTimeString(context, fileInfo.lastModified.time, DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0))
|
||||
DateUtils.getRelativeDateTimeString(binding.root.context, fileInfo.lastModified.time, DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0))
|
||||
}
|
||||
return binding.root
|
||||
|
||||
binding.root.setOnClickListener {
|
||||
listener?.onItemClicked(fileInfo)
|
||||
}
|
||||
|
||||
binding.executePendingBindings()
|
||||
}
|
||||
|
||||
override fun getItemCount() = data.size
|
||||
// override fun getItemId(position: Int) = data[position].fileName.hashCode().toLong()
|
||||
}
|
||||
|
||||
interface FolderContentsListener {
|
||||
fun onItemClicked(fileInfo: FileInfo)
|
||||
}
|
||||
|
||||
class FolderContentsViewHolder(val binding: ListviewFileBinding): RecyclerView.ViewHolder(binding.root)
|
||||
@@ -1,35 +1,55 @@
|
||||
package net.syncthing.lite.adapters
|
||||
|
||||
import android.content.Context
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.text.format.DateUtils
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.java.core.beans.FolderStats
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.databinding.ListviewFolderBinding
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
class FoldersListAdapter(context: Context?, list: List<Pair<FolderInfo, FolderStats>>) :
|
||||
ArrayAdapter<Pair<FolderInfo, FolderStats>>(context, R.layout.listview_folder, list) {
|
||||
|
||||
override fun getView(position: Int, v: View?, parent: ViewGroup): View {
|
||||
val binding: ListviewFolderBinding =
|
||||
if (v == null) {
|
||||
DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.listview_folder, parent, false)
|
||||
} else {
|
||||
DataBindingUtil.bind(v)
|
||||
}
|
||||
val folderInfo = getItem(position)!!.first
|
||||
val folderStats = getItem(position)!!.second
|
||||
binding.folderName.text = context.getString(R.string.folder_label_format, folderInfo.label, folderInfo.folderId)
|
||||
|
||||
binding.folderLastmodInfo.text = context.getString(R.string.last_modified_time,
|
||||
DateUtils.getRelativeDateTimeString(context, folderStats.lastUpdate.time, DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0))
|
||||
binding.folderContentInfo.text = context.getString(R.string.folder_content_info, folderStats.describeSize(), folderStats.fileCount, folderStats.dirCount)
|
||||
return binding.root
|
||||
class FoldersListAdapter: RecyclerView.Adapter<FolderListViewHolder>() {
|
||||
var data: List<Pair<FolderInfo, FolderStats>> by Delegates.observable(listOf()) {
|
||||
_, _, _ -> notifyDataSetChanged()
|
||||
}
|
||||
|
||||
var listener: FolderListAdapterListener? = null
|
||||
|
||||
init {
|
||||
setHasStableIds(true)
|
||||
}
|
||||
|
||||
override fun getItemCount() = data.size
|
||||
override fun getItemId(position: Int) = data[position].first.folderId.hashCode().toLong()
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = FolderListViewHolder (
|
||||
ListviewFolderBinding.inflate(
|
||||
LayoutInflater.from(parent.context), parent, false
|
||||
)
|
||||
)
|
||||
|
||||
override fun onBindViewHolder(holder: FolderListViewHolder, position: Int) {
|
||||
val binding = holder.binding
|
||||
val (folderInfo, folderStats) = data[position]
|
||||
val context = holder.itemView.context
|
||||
|
||||
binding.folderName = context.getString(R.string.folder_label_format, folderInfo.label, folderInfo.folderId)
|
||||
|
||||
binding.lastModification = context.getString(R.string.last_modified_time,
|
||||
DateUtils.getRelativeDateTimeString(context, folderStats.lastUpdate.time, DateUtils.MINUTE_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0))
|
||||
|
||||
binding.info = context.getString(R.string.folder_content_info, folderStats.describeSize(), folderStats.fileCount, folderStats.dirCount)
|
||||
|
||||
binding.root.setOnClickListener {
|
||||
listener?.onFolderClicked(folderInfo, folderStats)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FolderListViewHolder(val binding: ListviewFolderBinding): RecyclerView.ViewHolder(binding.root)
|
||||
|
||||
interface FolderListAdapterListener {
|
||||
fun onFolderClicked(folderInfo: FolderInfo, folderStats: FolderStats)
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package net.syncthing.lite.dialogs
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.WriterException
|
||||
import com.google.zxing.qrcode.QRCodeWriter
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.databinding.DialogDeviceIdBinding
|
||||
import org.jetbrains.anko.doAsync
|
||||
|
||||
|
||||
class DeviceIdDialog(private val context: Context, private val deviceId: DeviceId) {
|
||||
|
||||
private val Tag = "DeviceIdDialog"
|
||||
|
||||
private val binding = DataBindingUtil.inflate<DialogDeviceIdBinding>(
|
||||
LayoutInflater.from(context), R.layout.dialog_device_id, null, false)
|
||||
|
||||
fun show() {
|
||||
generateQrCode()
|
||||
binding.deviceId.text = deviceId.deviceId
|
||||
// Make QR code/progress bar views rectangular based on match_parent height.
|
||||
binding.progressBar.post {
|
||||
binding.progressBar.minimumHeight = binding.progressBar.width
|
||||
binding.qrCode.minimumHeight = binding.progressBar.width
|
||||
}
|
||||
binding.deviceId.setOnClickListener({ copyDeviceId() })
|
||||
binding.share.setOnClickListener { }
|
||||
binding.share.setOnClickListener({ shareDeviceId() })
|
||||
|
||||
val qrCodeDialog = AlertDialog.Builder(context)
|
||||
.setTitle(context.getString(R.string.device_id))
|
||||
.setView(binding.root)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.create()
|
||||
|
||||
qrCodeDialog.show()
|
||||
}
|
||||
|
||||
private fun generateQrCode() {
|
||||
doAsync {
|
||||
val writer = QRCodeWriter()
|
||||
try {
|
||||
val bitMatrix = writer.encode(deviceId.deviceId, BarcodeFormat.QR_CODE, 512, 512)
|
||||
val width = bitMatrix.width
|
||||
val height = bitMatrix.height
|
||||
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
|
||||
for (x in 0 until width) {
|
||||
for (y in 0 until height) {
|
||||
bmp.setPixel(x, y, if (bitMatrix.get(x, y)) Color.BLACK else Color.WHITE)
|
||||
}
|
||||
}
|
||||
async(UI) {
|
||||
binding.progressBar.visibility = View.GONE
|
||||
binding.qrCode.visibility = View.VISIBLE
|
||||
binding.qrCode.setImageBitmap(bmp)
|
||||
}
|
||||
} catch (e: WriterException) {
|
||||
Log.w(Tag, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun copyDeviceId() {
|
||||
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clip = ClipData.newPlainText(context.getString(R.string.device_id), deviceId.deviceId)
|
||||
clipboard.primaryClip = clip
|
||||
Toast.makeText(context, context.getString(R.string.device_id_copied), Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
|
||||
private fun shareDeviceId() {
|
||||
val shareIntent = Intent(Intent.ACTION_SEND)
|
||||
shareIntent.type = "text/plain"
|
||||
shareIntent.putExtra(Intent.EXTRA_TEXT, deviceId.deviceId)
|
||||
context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_device_id_chooser)))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package net.syncthing.lite.dialogs
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.app.Dialog
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.FragmentManager
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.WriterException
|
||||
import com.google.zxing.qrcode.QRCodeWriter
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.databinding.DialogDeviceIdBinding
|
||||
import net.syncthing.lite.fragments.SyncthingDialogFragment
|
||||
import org.jetbrains.anko.doAsync
|
||||
|
||||
class DeviceIdDialogFragment: SyncthingDialogFragment() {
|
||||
companion object {
|
||||
private const val QR_RESOLUTION = 512
|
||||
private const val TAG = "DeviceIdDialog"
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val binding = DialogDeviceIdBinding.inflate(LayoutInflater.from(context), null, false)
|
||||
|
||||
// use an placeholder to prevent size changes; this string is never shown
|
||||
binding.deviceId.text = "XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX"
|
||||
binding.deviceId.visibility = View.INVISIBLE
|
||||
|
||||
binding.qrCode.setImageBitmap(Bitmap.createBitmap(QR_RESOLUTION, QR_RESOLUTION, Bitmap.Config.RGB_565))
|
||||
|
||||
libraryHandler.library { configuration, _, _ ->
|
||||
val deviceId = configuration.localDeviceId
|
||||
|
||||
fun copyDeviceId() {
|
||||
val clipboard = context!!.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
val clip = ClipData.newPlainText(context!!.getString(R.string.device_id), deviceId.deviceId)
|
||||
|
||||
clipboard.primaryClip = clip
|
||||
|
||||
Toast.makeText(context, context!!.getString(R.string.device_id_copied), Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
|
||||
fun shareDeviceId() {
|
||||
context!!.startActivity(Intent.createChooser(
|
||||
Intent(Intent.ACTION_SEND).apply {
|
||||
type = "text/plain"
|
||||
putExtra(Intent.EXTRA_TEXT, deviceId.deviceId)
|
||||
},
|
||||
context!!.getString(R.string.share_device_id_chooser)
|
||||
))
|
||||
}
|
||||
|
||||
async (UI) {
|
||||
binding.deviceId.text = deviceId.deviceId
|
||||
binding.deviceId.visibility = View.VISIBLE
|
||||
|
||||
binding.deviceId.setOnClickListener { copyDeviceId() }
|
||||
binding.share.setOnClickListener { shareDeviceId() }
|
||||
}
|
||||
|
||||
doAsync {
|
||||
val writer = QRCodeWriter()
|
||||
try {
|
||||
val bitMatrix = writer.encode(deviceId.deviceId, BarcodeFormat.QR_CODE, QR_RESOLUTION, QR_RESOLUTION)
|
||||
val width = bitMatrix.width
|
||||
val height = bitMatrix.height
|
||||
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
|
||||
for (x in 0 until width) {
|
||||
for (y in 0 until height) {
|
||||
bmp.setPixel(x, y, if (bitMatrix.get(x, y)) Color.BLACK else Color.WHITE)
|
||||
}
|
||||
}
|
||||
|
||||
async(UI) {
|
||||
binding.flipper.displayedChild = 1
|
||||
binding.qrCode.setImageBitmap(bmp)
|
||||
}
|
||||
} catch (e: WriterException) {
|
||||
Log.w(TAG, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return AlertDialog.Builder(context!!, theme)
|
||||
.setTitle(context!!.getString(R.string.device_id))
|
||||
.setView(binding.root)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.create()
|
||||
}
|
||||
|
||||
fun show(manager: FragmentManager?) {
|
||||
super.show(manager, TAG)
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package net.syncthing.lite.dialogs
|
||||
|
||||
import android.app.ProgressDialog
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.support.v4.content.FileProvider
|
||||
import android.util.Log
|
||||
import android.webkit.MimeTypeMap
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.java.bep.BlockPuller
|
||||
import net.syncthing.java.client.SyncthingClient
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.library.DownloadFileTask
|
||||
import org.apache.commons.io.FilenameUtils
|
||||
import org.jetbrains.anko.doAsync
|
||||
import org.jetbrains.anko.newTask
|
||||
import org.jetbrains.anko.toast
|
||||
import java.io.File
|
||||
|
||||
class FileDownloadDialog(private val context: Context, private val syncthingClient: SyncthingClient,
|
||||
private val fileInfo: FileInfo) {
|
||||
|
||||
private val Tag = "FileDownloadDialog"
|
||||
private lateinit var progressDialog: ProgressDialog
|
||||
private var downloadFileTask: DownloadFileTask? = null
|
||||
|
||||
fun show() {
|
||||
showDialog()
|
||||
doAsync {
|
||||
downloadFileTask = DownloadFileTask(context, syncthingClient, fileInfo,
|
||||
this@FileDownloadDialog::onProgress, this@FileDownloadDialog::onComplete,
|
||||
this@FileDownloadDialog::onError)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showDialog() {
|
||||
progressDialog = ProgressDialog(context)
|
||||
progressDialog.setMessage(context.getString(R.string.dialog_downloading_file, fileInfo.fileName))
|
||||
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
|
||||
progressDialog.setCancelable(true)
|
||||
progressDialog.setOnCancelListener { downloadFileTask?.cancel() }
|
||||
progressDialog.isIndeterminate = true
|
||||
progressDialog.show()
|
||||
}
|
||||
|
||||
private fun onProgress(downloadFileTask: DownloadFileTask, fileDownloadObserver: BlockPuller.FileDownloadObserver) {
|
||||
async(UI) {
|
||||
progressDialog.isIndeterminate = false
|
||||
progressDialog.max = (fileInfo.size as Long).toInt()
|
||||
progressDialog.progress = (fileDownloadObserver.progress() * fileInfo.size!!).toInt()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onComplete(file: File) {
|
||||
async(UI) {
|
||||
progressDialog.dismiss()
|
||||
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(FilenameUtils.getExtension(file.name))
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
val uri = FileProvider.getUriForFile(this@FileDownloadDialog.context, "net.syncthing.lite.fileprovider", file)
|
||||
intent.setDataAndType(uri, mimeType)
|
||||
intent.newTask()
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
try {
|
||||
this@FileDownloadDialog.context.startActivity(intent)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
this@FileDownloadDialog.context.toast(R.string.toast_open_file_failed)
|
||||
Log.w(Tag, "No handler found for file " + file.name, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onError() {
|
||||
async(UI) {
|
||||
progressDialog.cancel()
|
||||
this@FileDownloadDialog.context.toast(R.string.toast_file_download_failed)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,25 +41,19 @@ class FileUploadDialog(private val context: Context, private val syncthingClient
|
||||
}
|
||||
|
||||
private fun onProgress(observer: BlockPusher.FileUploadObserver) {
|
||||
async(UI) {
|
||||
progressDialog.isIndeterminate = false
|
||||
progressDialog.progress = observer.progressPercentage()
|
||||
progressDialog.max = 100
|
||||
}
|
||||
progressDialog.isIndeterminate = false
|
||||
progressDialog.progress = observer.progressPercentage()
|
||||
progressDialog.max = 100
|
||||
}
|
||||
|
||||
private fun onComplete() {
|
||||
async(UI) {
|
||||
progressDialog.dismiss()
|
||||
this@FileUploadDialog.context.toast(R.string.toast_upload_complete)
|
||||
onUploadCompleteListener()
|
||||
}
|
||||
progressDialog.dismiss()
|
||||
this@FileUploadDialog.context.toast(R.string.toast_upload_complete)
|
||||
onUploadCompleteListener()
|
||||
}
|
||||
|
||||
private fun onError() {
|
||||
async(UI) {
|
||||
progressDialog.dismiss()
|
||||
this@FileUploadDialog.context.toast(R.string.toast_file_upload_failed)
|
||||
}
|
||||
progressDialog.dismiss()
|
||||
this@FileUploadDialog.context.toast(R.string.toast_file_upload_failed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package net.syncthing.lite.dialogs
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.support.v4.app.FragmentActivity
|
||||
import android.support.v7.app.AlertDialog
|
||||
import net.syncthing.lite.R
|
||||
import org.jetbrains.anko.defaultSharedPreferences
|
||||
|
||||
class ReconnectIssueDialogFragment: DialogFragment() {
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?) = AlertDialog.Builder(context!!, theme)
|
||||
.setMessage(R.string.dialog_warning_reconnect_problem)
|
||||
.setPositiveButton(android.R.string.ok) { _, _ ->
|
||||
context!!.defaultSharedPreferences.edit()
|
||||
.putBoolean(SETTINGS_PARAM, true)
|
||||
.apply()
|
||||
}
|
||||
.create()
|
||||
|
||||
companion object {
|
||||
private const val DIALOG_TAG = "ReconnectIssueDialog"
|
||||
private const val SETTINGS_PARAM = "has_educated_about_reconnect_issues"
|
||||
|
||||
fun showIfNeeded(activity: FragmentActivity) {
|
||||
if (!activity.defaultSharedPreferences.getBoolean(SETTINGS_PARAM, false)) {
|
||||
if (activity.supportFragmentManager.findFragmentByTag(DIALOG_TAG) == null) {
|
||||
ReconnectIssueDialogFragment().show(activity.supportFragmentManager, DIALOG_TAG)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package net.syncthing.lite.dialogs.downloadfile
|
||||
|
||||
import android.app.Dialog
|
||||
import android.app.ProgressDialog
|
||||
import android.arch.lifecycle.Observer
|
||||
import android.arch.lifecycle.ViewModelProviders
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.support.v4.app.FragmentManager
|
||||
import android.support.v4.content.FileProvider
|
||||
import android.util.Log
|
||||
import android.webkit.MimeTypeMap
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.lite.BuildConfig
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.library.LibraryHandler
|
||||
import org.apache.commons.io.FilenameUtils
|
||||
import org.jetbrains.anko.newTask
|
||||
import org.jetbrains.anko.toast
|
||||
|
||||
class DownloadFileDialogFragment : DialogFragment() {
|
||||
companion object {
|
||||
private const val ARG_FILE_SPEC = "file spec"
|
||||
private const val TAG = "DownloadFileDialog"
|
||||
|
||||
fun newInstance(fileInfo: FileInfo) = newInstance(DownloadFileSpec(
|
||||
folder = fileInfo.folder,
|
||||
path = fileInfo.path,
|
||||
fileName = fileInfo.fileName
|
||||
))
|
||||
|
||||
fun newInstance(fileSpec: DownloadFileSpec) = DownloadFileDialogFragment().apply {
|
||||
arguments = Bundle().apply {
|
||||
putSerializable(ARG_FILE_SPEC, fileSpec)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val model: DownloadFileDialogViewModel by lazy {
|
||||
ViewModelProviders.of(this).get(DownloadFileDialogViewModel::class.java)
|
||||
}
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
val fileSpec = arguments!!.getSerializable(ARG_FILE_SPEC) as DownloadFileSpec
|
||||
|
||||
model.init(
|
||||
libraryHandler = LibraryHandler(context!!),
|
||||
fileSpec = fileSpec,
|
||||
externalCacheDir = context!!.externalCacheDir
|
||||
)
|
||||
|
||||
val progressDialog = ProgressDialog(context).apply {
|
||||
setMessage(context!!.getString(R.string.dialog_downloading_file, fileSpec.fileName))
|
||||
setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
|
||||
isCancelable = true
|
||||
isIndeterminate = true
|
||||
max = DownloadFileStatusRunning.MAX_PROGRESS
|
||||
}
|
||||
|
||||
model.status.observe(this, Observer {
|
||||
status ->
|
||||
|
||||
when (status) {
|
||||
is DownloadFileStatusRunning -> {
|
||||
progressDialog.apply {
|
||||
isIndeterminate = false
|
||||
progress = status.progress
|
||||
}
|
||||
}
|
||||
is DownloadFileStatusDone -> {
|
||||
dismissAllowingStateLoss()
|
||||
|
||||
try {
|
||||
context!!.startActivity(
|
||||
Intent(Intent.ACTION_VIEW)
|
||||
.setDataAndType(
|
||||
FileProvider.getUriForFile(context!!, "net.syncthing.lite.fileprovider", status.file),
|
||||
MimeTypeMap.getSingleton().getMimeTypeFromExtension(FilenameUtils.getExtension(fileSpec.fileName))
|
||||
)
|
||||
.newTask()
|
||||
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.w(TAG, "No handler found for file " + status.file.name, e)
|
||||
}
|
||||
|
||||
context!!.toast(R.string.toast_open_file_failed)
|
||||
}
|
||||
}
|
||||
is DownloadFileStatusFailed -> {
|
||||
dismissAllowingStateLoss()
|
||||
|
||||
context!!.toast(R.string.toast_file_download_failed)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return progressDialog
|
||||
}
|
||||
|
||||
override fun onCancel(dialog: DialogInterface?) {
|
||||
super.onCancel(dialog)
|
||||
|
||||
model.cancel()
|
||||
}
|
||||
|
||||
fun show(fragmentManager: FragmentManager?) {
|
||||
show(fragmentManager, TAG)
|
||||
}
|
||||
}
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
package net.syncthing.lite.dialogs.downloadfile
|
||||
|
||||
import android.arch.lifecycle.LiveData
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import android.arch.lifecycle.ViewModel;
|
||||
import android.support.v4.os.CancellationSignal
|
||||
import android.util.Log
|
||||
import net.syncthing.lite.BuildConfig
|
||||
import net.syncthing.lite.library.DownloadFileTask
|
||||
import net.syncthing.lite.library.LibraryHandler
|
||||
import java.io.File
|
||||
|
||||
class DownloadFileDialogViewModel : ViewModel() {
|
||||
companion object {
|
||||
private const val TAG = "DownloadFileDialog"
|
||||
}
|
||||
|
||||
private var isInitialized = false
|
||||
private val statusInternal = MutableLiveData<DownloadFileStatus>()
|
||||
private val cancellationSignal = CancellationSignal()
|
||||
val status: LiveData<DownloadFileStatus> = statusInternal
|
||||
|
||||
fun init(libraryHandler: LibraryHandler, fileSpec: DownloadFileSpec, externalCacheDir: File) {
|
||||
if (isInitialized) {
|
||||
return
|
||||
}
|
||||
|
||||
isInitialized = true
|
||||
|
||||
libraryHandler.start()
|
||||
|
||||
// this keeps the client only active as long as the block is running
|
||||
// but the file downloading is not synchronous.
|
||||
// Due to that, the start and stop calls are used.
|
||||
libraryHandler.syncthingClient {
|
||||
syncthingClient ->
|
||||
|
||||
try {
|
||||
val fileInfo = syncthingClient.indexHandler.getFileInfoByPath(
|
||||
folder = fileSpec.folder,
|
||||
path = fileSpec.path
|
||||
)!!
|
||||
|
||||
val task = DownloadFileTask(
|
||||
fileStorageDirectory = externalCacheDir,
|
||||
syncthingClient = syncthingClient,
|
||||
fileInfo = fileInfo,
|
||||
onProgress = { status ->
|
||||
val newProgress = (status.downloadedBytes * DownloadFileStatusRunning.MAX_PROGRESS / status.totalTransferSize).toInt()
|
||||
val currentStatus = statusInternal.value
|
||||
|
||||
// only update if it changed
|
||||
if (!(currentStatus is DownloadFileStatusRunning) || currentStatus.progress != newProgress) {
|
||||
statusInternal.value = DownloadFileStatusRunning(newProgress)
|
||||
}
|
||||
},
|
||||
onComplete = {
|
||||
statusInternal.value = DownloadFileStatusDone(it)
|
||||
|
||||
libraryHandler.stop()
|
||||
},
|
||||
onError = {
|
||||
statusInternal.value = DownloadFileStatusFailed
|
||||
|
||||
libraryHandler.stop()
|
||||
}
|
||||
)
|
||||
|
||||
cancellationSignal.setOnCancelListener {
|
||||
task.cancel()
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.w(TAG, "downloading file failed", ex)
|
||||
}
|
||||
|
||||
statusInternal.postValue(DownloadFileStatusFailed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
|
||||
cancel()
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
cancellationSignal.cancel()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package net.syncthing.lite.dialogs.downloadfile
|
||||
|
||||
import java.io.Serializable
|
||||
|
||||
data class DownloadFileSpec(val folder: String, val path: String, val fileName: String): Serializable
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.syncthing.lite.dialogs.downloadfile
|
||||
|
||||
import java.io.File
|
||||
|
||||
sealed class DownloadFileStatus
|
||||
object DownloadFileStatusFailed: DownloadFileStatus()
|
||||
data class DownloadFileStatusDone(val file: File): DownloadFileStatus()
|
||||
data class DownloadFileStatusRunning(val progress: Int): DownloadFileStatus() {
|
||||
companion object {
|
||||
const val MAX_PROGRESS = 100
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,9 @@ import android.view.inputmethod.InputMethodManager
|
||||
import com.google.zxing.integration.android.IntentIntegrator
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.java.core.beans.DeviceInfo
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.lite.adapters.DeviceAdapterListener
|
||||
import net.syncthing.lite.adapters.DevicesAdapter
|
||||
import net.syncthing.lite.databinding.FragmentDevicesBinding
|
||||
import net.syncthing.lite.databinding.ViewEnterDeviceIdBinding
|
||||
@@ -23,26 +25,25 @@ import java.io.IOException
|
||||
class DevicesFragment : SyncthingFragment() {
|
||||
|
||||
private lateinit var binding: FragmentDevicesBinding
|
||||
private lateinit var adapter: DevicesAdapter
|
||||
private val adapter = DevicesAdapter()
|
||||
private var addDeviceDialog: AlertDialog? = null
|
||||
private var addDeviceDialogBinding: ViewEnterDeviceIdBinding? = null
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?): View? {
|
||||
binding = DataBindingUtil.inflate(layoutInflater, R.layout.fragment_devices, container, false)
|
||||
binding.list.emptyView = binding.empty
|
||||
binding.addDevice.setOnClickListener { showDialog() }
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
libraryHandler?.syncthingClient { it.addOnConnectionChangedListener { updateDeviceList() } }
|
||||
libraryHandler?.syncthingClient { it.addOnConnectionChangedListener { _ -> updateDeviceList() } }
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
libraryHandler?.syncthingClient { it.removeOnConnectionChangedListener{ updateDeviceList() } }
|
||||
libraryHandler?.syncthingClient { it.removeOnConnectionChangedListener{ _ -> updateDeviceList() } }
|
||||
}
|
||||
|
||||
override fun onLibraryLoaded() {
|
||||
@@ -51,32 +52,33 @@ class DevicesFragment : SyncthingFragment() {
|
||||
}
|
||||
|
||||
private fun initDeviceList() {
|
||||
adapter = DevicesAdapter(context!!)
|
||||
binding.list.adapter = adapter
|
||||
binding.list.setOnItemLongClickListener { _, _, position, _ ->
|
||||
val device = adapter.getItem(position)
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(getString(R.string.remove_device_title, device.name))
|
||||
.setMessage(getString(R.string.remove_device_message, device.deviceId.deviceId.substring(0, 7)))
|
||||
.setPositiveButton(android.R.string.yes) { _, _ ->
|
||||
libraryHandler?.configuration { config ->
|
||||
config.peers = config.peers.filterNot { it.deviceId == device.deviceId }.toSet()
|
||||
config.persistLater()
|
||||
updateDeviceList()
|
||||
|
||||
adapter.listener = object: DeviceAdapterListener {
|
||||
override fun onDeviceLongClicked(deviceInfo: DeviceInfo): Boolean {
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(getString(R.string.remove_device_title, deviceInfo.name))
|
||||
.setMessage(getString(R.string.remove_device_message, deviceInfo.deviceId.deviceId.substring(0, 7)))
|
||||
.setPositiveButton(android.R.string.yes) { _, _ ->
|
||||
libraryHandler?.configuration { config ->
|
||||
config.peers = config.peers.filterNot { it.deviceId == deviceInfo.deviceId }.toSet()
|
||||
config.persistLater()
|
||||
updateDeviceList()
|
||||
}
|
||||
}
|
||||
}
|
||||
.setNegativeButton(android.R.string.no, null)
|
||||
.show()
|
||||
false
|
||||
.setNegativeButton(android.R.string.no, null)
|
||||
.show()
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDeviceList() {
|
||||
async(UI) {
|
||||
libraryHandler?.syncthingClient { syncthingClient ->
|
||||
adapter.clear()
|
||||
adapter.addAll(syncthingClient.getPeerStatus())
|
||||
adapter.notifyDataSetChanged()
|
||||
libraryHandler.syncthingClient { syncthingClient ->
|
||||
async(UI) {
|
||||
adapter.data = syncthingClient.getPeerStatus()
|
||||
binding.isEmpty = adapter.data.isEmpty()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
package net.syncthing.lite.fragments
|
||||
|
||||
import android.databinding.DataBindingUtil
|
||||
import android.arch.lifecycle.Observer
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.lite.R
|
||||
import net.syncthing.java.core.beans.FolderStats
|
||||
import net.syncthing.lite.activities.FolderBrowserActivity
|
||||
import net.syncthing.lite.adapters.FolderListAdapterListener
|
||||
import net.syncthing.lite.adapters.FoldersListAdapter
|
||||
import net.syncthing.lite.databinding.FragmentFoldersBinding
|
||||
import org.jetbrains.anko.intentFor
|
||||
@@ -21,8 +24,10 @@ class FoldersFragment : SyncthingFragment() {
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?): View? {
|
||||
binding = DataBindingUtil.inflate(layoutInflater, R.layout.fragment_folders, container, false)
|
||||
binding.list.emptyView = binding.empty
|
||||
binding = FragmentFoldersBinding.inflate(layoutInflater, container, false)
|
||||
|
||||
libraryHandler.isListeningPortTaken.observe(this, Observer { binding.listeningPortTaken = it })
|
||||
|
||||
return binding.root
|
||||
}
|
||||
|
||||
@@ -31,15 +36,24 @@ class FoldersFragment : SyncthingFragment() {
|
||||
}
|
||||
|
||||
private fun showAllFoldersListView() {
|
||||
libraryHandler?.folderBrowser { folderBrowser ->
|
||||
libraryHandler.folderBrowser { folderBrowser ->
|
||||
val list = folderBrowser.folderInfoAndStatsList()
|
||||
Log.i(TAG, "list folders = " + list + " (" + list.size + " records)")
|
||||
val adapter = FoldersListAdapter(context, list)
|
||||
binding.list.adapter = adapter
|
||||
binding.list.setOnItemClickListener { _, _, position, _ ->
|
||||
val folder = adapter.getItem(position)!!.first.folderId
|
||||
val intent = context?.intentFor<FolderBrowserActivity>(FolderBrowserActivity.EXTRA_FOLDER_NAME to folder)
|
||||
startActivity(intent)
|
||||
|
||||
async (UI) {
|
||||
Log.i(TAG, "list folders = " + list + " (" + list.size + " records)")
|
||||
val adapter = FoldersListAdapter().apply { data = list }
|
||||
binding.list.adapter = adapter
|
||||
adapter.listener = object : FolderListAdapterListener {
|
||||
override fun onFolderClicked(folderInfo: FolderInfo, folderStats: FolderStats) {
|
||||
startActivity(
|
||||
activity!!.intentFor<FolderBrowserActivity>(
|
||||
FolderBrowserActivity.EXTRA_FOLDER_NAME to folderInfo.folderId
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
binding.isEmpty = list.isEmpty()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package net.syncthing.lite.fragments
|
||||
|
||||
import android.support.v4.app.DialogFragment
|
||||
import net.syncthing.lite.library.LibraryHandler
|
||||
|
||||
abstract class SyncthingDialogFragment : DialogFragment() {
|
||||
val libraryHandler: LibraryHandler by lazy { LibraryHandler(
|
||||
context = context!!
|
||||
)}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
libraryHandler.start()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
|
||||
libraryHandler.stop()
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,29 @@
|
||||
package net.syncthing.lite.fragments
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.Fragment
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.lite.library.LibraryHandler
|
||||
|
||||
abstract class SyncthingFragment : Fragment() {
|
||||
val libraryHandler: LibraryHandler by lazy { LibraryHandler(
|
||||
context = context!!,
|
||||
onIndexUpdateProgressListener = this::onIndexUpdateProgress,
|
||||
onIndexUpdateCompleteListener = this::onIndexUpdateComplete
|
||||
)}
|
||||
|
||||
var libraryHandler: LibraryHandler? = null
|
||||
private set
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||
super.onActivityCreated(savedInstanceState)
|
||||
LibraryHandler(context!!, this::onLibraryLoadedInternal, this::onIndexUpdateProgress,
|
||||
this::onIndexUpdateComplete)
|
||||
libraryHandler.start {
|
||||
// TODO: check if this is still useful
|
||||
onLibraryLoaded()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onLibraryLoadedInternal(libraryHandler: LibraryHandler) {
|
||||
this.libraryHandler = libraryHandler
|
||||
onLibraryLoaded()
|
||||
}
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
libraryHandler?.close()
|
||||
libraryHandler.stop()
|
||||
}
|
||||
|
||||
open fun onLibraryLoaded() {}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package net.syncthing.lite.library
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import net.syncthing.lite.BuildConfig
|
||||
import net.syncthing.lite.R
|
||||
import org.jetbrains.anko.defaultSharedPreferences
|
||||
|
||||
object DefaultLibraryManager {
|
||||
private const val LOG_TAG = "DefaultLibraryManager"
|
||||
|
||||
private var instance: LibraryManager? = null
|
||||
private val lock = Object()
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
fun with(context: Context) = withApplicationContext(context.applicationContext)
|
||||
|
||||
private fun withApplicationContext(context: Context): LibraryManager {
|
||||
if (instance == null) {
|
||||
synchronized(lock) {
|
||||
if (instance == null) {
|
||||
val shutdownRunnable = Runnable {
|
||||
instance!!.shutdownIfThereAreZeroUsers()
|
||||
}
|
||||
|
||||
fun scheduleShutdown() {
|
||||
val shutdownDelay = context.defaultSharedPreferences.getString(
|
||||
"shutdown_delay",
|
||||
context.getString(R.string.default_shutdown_delay)
|
||||
).toLong()
|
||||
|
||||
handler.postDelayed(shutdownRunnable, shutdownDelay)
|
||||
}
|
||||
|
||||
fun cancelShutdown() {
|
||||
handler.removeCallbacks(shutdownRunnable)
|
||||
}
|
||||
|
||||
instance = LibraryManager(
|
||||
synchronousInstanceCreator = { LibraryInstance(context) },
|
||||
userCounterListener = {
|
||||
newUserCounter ->
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.d(LOG_TAG, "user counter updated to $newUserCounter")
|
||||
}
|
||||
|
||||
val isUsed = newUserCounter > 0
|
||||
|
||||
if (isUsed) {
|
||||
cancelShutdown()
|
||||
} else {
|
||||
scheduleShutdown()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return instance!!
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.syncthing.lite.library
|
||||
|
||||
import java.io.File
|
||||
|
||||
data class DownloadFilePath (val baseDirectory: File, val fileHash: String) {
|
||||
val filesDirectory = File(baseDirectory, fileHash.substring(0, 2))
|
||||
val targetFile = File(filesDirectory, fileHash.substring(2))
|
||||
val tempFile = File(filesDirectory, fileHash.substring(2) + "_temp")
|
||||
}
|
||||
@@ -1,49 +1,148 @@
|
||||
package net.syncthing.lite.library
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.support.v4.os.CancellationSignal
|
||||
import android.util.Log
|
||||
import net.syncthing.java.bep.BlockPuller
|
||||
import kotlinx.coroutines.experimental.launch
|
||||
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
|
||||
import net.syncthing.java.bep.BlockPullerStatus
|
||||
import net.syncthing.java.client.SyncthingClient
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.lite.BuildConfig
|
||||
import org.apache.commons.io.FileUtils
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
|
||||
class DownloadFileTask(private val context: Context, syncthingClient: SyncthingClient,
|
||||
class DownloadFileTask(private val fileStorageDirectory: File,
|
||||
syncthingClient: SyncthingClient,
|
||||
private val fileInfo: FileInfo,
|
||||
private val onProgress: (DownloadFileTask, BlockPuller.FileDownloadObserver) -> Unit,
|
||||
private val onProgress: (status: BlockPullerStatus) -> Unit,
|
||||
private val onComplete: (File) -> Unit,
|
||||
private val onError: () -> Unit) {
|
||||
private val onError: (Exception) -> Unit) {
|
||||
|
||||
private val Tag = "DownloadFileTask"
|
||||
private var isCancelled = false
|
||||
companion object {
|
||||
private const val TAG = "DownloadFileTask"
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
|
||||
suspend fun downloadFileCoroutine(
|
||||
externalCacheDir: File,
|
||||
syncthingClient: SyncthingClient,
|
||||
fileInfo: FileInfo,
|
||||
onProgress: (status: BlockPullerStatus) -> Unit
|
||||
) = suspendCancellableCoroutine<File> {
|
||||
continuation ->
|
||||
|
||||
val task = DownloadFileTask(
|
||||
externalCacheDir,
|
||||
syncthingClient,
|
||||
fileInfo,
|
||||
onProgress,
|
||||
{
|
||||
continuation.resume(it)
|
||||
},
|
||||
{
|
||||
continuation.resumeWithException(it)
|
||||
}
|
||||
)
|
||||
|
||||
continuation.invokeOnCancellation {
|
||||
task.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val cancellationSignal = CancellationSignal()
|
||||
private var doneListenerCalled = false
|
||||
|
||||
init {
|
||||
syncthingClient.getBlockPuller(fileInfo.folder, { blockPuller ->
|
||||
val observer = blockPuller.pullFile(fileInfo)
|
||||
onProgress(this, observer)
|
||||
try {
|
||||
while (!observer.isCompleted()) {
|
||||
if (isCancelled)
|
||||
return@getBlockPuller
|
||||
val file = DownloadFilePath(fileStorageDirectory, fileInfo.hash!!)
|
||||
|
||||
observer.waitForProgressUpdate()
|
||||
Log.i("pullFile", "download progress = " + observer.progressMessage())
|
||||
onProgress(this, observer)
|
||||
launch {
|
||||
if (file.targetFile.exists()) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.d(TAG, "there is already a file")
|
||||
}
|
||||
|
||||
val outputFile = File("${context.externalCacheDir}/${fileInfo.folder}/${fileInfo.path}")
|
||||
FileUtils.copyInputStreamToFile(observer.inputStream(), outputFile)
|
||||
Log.i(Tag, "Downloaded file $fileInfo")
|
||||
onComplete(outputFile)
|
||||
} catch (e: IOException) {
|
||||
onError()
|
||||
Log.w(Tag, "Failed to download file $fileInfo", e)
|
||||
callComplete(file.targetFile)
|
||||
|
||||
return@launch
|
||||
}
|
||||
}, { onError() })
|
||||
|
||||
syncthingClient.getBlockPuller(fileInfo.folder, { blockPuller ->
|
||||
val job = launch {
|
||||
try {
|
||||
if (!file.filesDirectory.isDirectory) {
|
||||
if (!file.filesDirectory.mkdirs()) {
|
||||
throw IOException("could not create output directory")
|
||||
}
|
||||
}
|
||||
|
||||
// download the file to a temp location
|
||||
val inputStream = blockPuller.pullFileCoroutine(fileInfo, this@DownloadFileTask::callProgress)
|
||||
|
||||
try {
|
||||
FileUtils.copyInputStreamToFile(inputStream, file.tempFile)
|
||||
file.tempFile.renameTo(file.targetFile)
|
||||
} finally {
|
||||
file.tempFile.delete()
|
||||
}
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i(TAG, "Downloaded file $fileInfo")
|
||||
}
|
||||
|
||||
callComplete(file.targetFile)
|
||||
} catch (e: Exception) {
|
||||
callError(e)
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.w(TAG, "Failed to download file $fileInfo", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cancellationSignal.setOnCancelListener {
|
||||
job.cancel()
|
||||
}
|
||||
}, { callError(IOException("could not get block puller for file")) })
|
||||
}
|
||||
}
|
||||
|
||||
private fun callProgress(status: BlockPullerStatus) {
|
||||
handler.post {
|
||||
if (!doneListenerCalled) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
Log.i("pullFile", "download progress = $status")
|
||||
}
|
||||
|
||||
onProgress(status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun callComplete(file: File) {
|
||||
handler.post {
|
||||
if (!doneListenerCalled) {
|
||||
doneListenerCalled = true
|
||||
|
||||
onComplete(file)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun callError(exception: Exception) {
|
||||
handler.post {
|
||||
if (!doneListenerCalled) {
|
||||
doneListenerCalled = true
|
||||
|
||||
onError(exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
isCancelled = true
|
||||
cancellationSignal.cancel()
|
||||
callError(InterruptedException())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,88 @@
|
||||
package net.syncthing.lite.library
|
||||
|
||||
import android.arch.lifecycle.LiveData
|
||||
import android.arch.lifecycle.MutableLiveData
|
||||
import android.content.Context
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.experimental.android.UI
|
||||
import kotlinx.coroutines.experimental.async
|
||||
import net.syncthing.java.bep.FolderBrowser
|
||||
import net.syncthing.java.client.SyncthingClient
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.java.core.beans.IndexInfo
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import org.jetbrains.anko.doAsync
|
||||
import java.net.DatagramSocket
|
||||
import java.net.InetAddress
|
||||
import java.net.SocketException
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
class LibraryHandler(context: Context, onLibraryLoaded: (LibraryHandler) -> Unit,
|
||||
private val onIndexUpdateProgressListener: (FolderInfo, Int) -> Unit,
|
||||
private val onIndexUpdateCompleteListener: (FolderInfo) -> Unit) {
|
||||
/**
|
||||
* This class helps when using the library.
|
||||
* It's required to start and stop it to make the callbacks fire (or stop to fire).
|
||||
*
|
||||
* It's possible to do multiple start and stop cycles with one instance of this class.
|
||||
*/
|
||||
class LibraryHandler(context: Context,
|
||||
private val onIndexUpdateProgressListener: (FolderInfo, Int) -> Unit = {_, _ -> },
|
||||
private val onIndexUpdateCompleteListener: (FolderInfo) -> Unit = {}) {
|
||||
|
||||
companion object {
|
||||
private var instanceCount = 0
|
||||
private var configuration: Configuration? = null
|
||||
private var syncthingClient: SyncthingClient? = null
|
||||
private var folderBrowser: FolderBrowser? = null
|
||||
private val callbacks = ArrayList<(Configuration, SyncthingClient, FolderBrowser) -> Unit>()
|
||||
private var isLoading = false
|
||||
var isListeningPortTaken = false
|
||||
private const val TAG = "LibraryHandler"
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
}
|
||||
|
||||
private val TAG = "LibraryHandler"
|
||||
private val libraryManager = DefaultLibraryManager.with(context)
|
||||
private val isStarted = AtomicBoolean(false)
|
||||
private val isListeningPortTakenInternal = MutableLiveData<Boolean>().apply { value = false }
|
||||
|
||||
init {
|
||||
instanceCount++
|
||||
if (configuration == null && !isLoading) {
|
||||
isLoading = true
|
||||
doAsync {
|
||||
checkIsListeningPortTaken()
|
||||
init(context)
|
||||
async(UI) {
|
||||
onLibraryLoaded(this@LibraryHandler)
|
||||
}
|
||||
isLoading = false
|
||||
}
|
||||
} else {
|
||||
val isListeningPortTaken: LiveData<Boolean> = isListeningPortTakenInternal
|
||||
|
||||
private val messageFromUnknownDeviceListeners = HashSet<(DeviceId) -> Unit>()
|
||||
private val internalMessageFromUnknownDeviceListener: (DeviceId) -> Unit = {
|
||||
deviceId ->
|
||||
|
||||
handler.post {
|
||||
messageFromUnknownDeviceListeners.forEach { listener -> listener(deviceId) }
|
||||
}
|
||||
}
|
||||
|
||||
fun start(onLibraryLoaded: (LibraryHandler) -> Unit = {}) {
|
||||
if (isStarted.getAndSet(true) == true) {
|
||||
throw IllegalStateException("already started")
|
||||
}
|
||||
|
||||
libraryManager.startLibraryUsage {
|
||||
libraryInstance ->
|
||||
|
||||
isListeningPortTakenInternal.value = libraryInstance.isListeningPortTaken
|
||||
onLibraryLoaded(this)
|
||||
|
||||
val client = libraryInstance.syncthingClient
|
||||
|
||||
client.indexHandler.registerOnIndexRecordAcquiredListener(this::onIndexRecordAcquired)
|
||||
client.indexHandler.registerOnFullIndexAcquiredListenersListener(this::onRemoteIndexAcquired)
|
||||
client.discoveryHandler.registerMessageFromUnknownDeviceListener(internalMessageFromUnknownDeviceListener)
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
if (isStarted.getAndSet(false) == false) {
|
||||
throw IllegalStateException("already stopped")
|
||||
}
|
||||
|
||||
syncthingClient {
|
||||
it.indexHandler.registerOnIndexRecordAcquiredListener(this::onIndexRecordAcquired)
|
||||
it.indexHandler.registerOnFullIndexAcquiredListenersListener(this::onRemoteIndexAcquired)
|
||||
try {
|
||||
it.indexHandler.unregisterOnIndexRecordAcquiredListener(this::onIndexRecordAcquired)
|
||||
it.indexHandler.unregisterOnFullIndexAcquiredListenersListener(this::onRemoteIndexAcquired)
|
||||
it.discoveryHandler.unregisterMessageFromUnknownDeviceListener(internalMessageFromUnknownDeviceListener)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
// ignored, no idea why this is thrown
|
||||
}
|
||||
}
|
||||
|
||||
libraryManager.stopLibraryUsage()
|
||||
}
|
||||
|
||||
private fun onIndexRecordAcquired(folderInfo: FolderInfo, newRecords: List<FileInfo>, indexInfo: IndexInfo) {
|
||||
@@ -71,38 +101,18 @@ class LibraryHandler(context: Context, onLibraryLoaded: (LibraryHandler) -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
private fun init(context: Context) {
|
||||
val configuration = Configuration(configFolder = context.filesDir)
|
||||
val syncthingClient = SyncthingClient(configuration)
|
||||
val folderBrowser = syncthingClient.indexHandler.newFolderBrowser()
|
||||
|
||||
if (instanceCount == 0) {
|
||||
Log.d(TAG, "All LibraryHandler instances were closed during init")
|
||||
syncthingClient.close()
|
||||
folderBrowser.close()
|
||||
}
|
||||
|
||||
async(UI) {
|
||||
callbacks.forEach { it(configuration, syncthingClient, folderBrowser) }
|
||||
}
|
||||
LibraryHandler.configuration = configuration
|
||||
LibraryHandler.syncthingClient = syncthingClient
|
||||
LibraryHandler.folderBrowser = folderBrowser
|
||||
}
|
||||
|
||||
/*
|
||||
* The callback is executed asynchronously.
|
||||
* As soon as it returns, there is no guarantee about the availability of the library
|
||||
*/
|
||||
fun library(callback: (Configuration, SyncthingClient, FolderBrowser) -> Unit) {
|
||||
val nullCount = listOf(configuration, syncthingClient, folderBrowser).count { it == null }
|
||||
assert(nullCount == 0 || nullCount == 3, { "Inconsistent library state" })
|
||||
|
||||
// https://stackoverflow.com/a/35522422/1837158
|
||||
fun <T1: Any, T2: Any, T3: Any, R: Any> safeLet(p1: T1?, p2: T2?, p3: T3?, block: (T1, T2, T3)->R?): R? {
|
||||
return if (p1 != null && p2 != null && p3 != null) block(p1, p2, p3) else null
|
||||
}
|
||||
safeLet(configuration, syncthingClient, folderBrowser) { c, s, f ->
|
||||
callback(c, s, f)
|
||||
} ?: run {
|
||||
if (isLoading) {
|
||||
callbacks.add(callback)
|
||||
libraryManager.startLibraryUsage {
|
||||
doAsync {
|
||||
try {
|
||||
callback(it.configuration, it.syncthingClient, it.folderBrowser)
|
||||
} finally {
|
||||
libraryManager.stopLibraryUsage()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -119,47 +129,13 @@ class LibraryHandler(context: Context, onLibraryLoaded: (LibraryHandler) -> Unit
|
||||
library { _, _, f -> callback(f) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if listening port for local discovery is taken by another app. Do this check here to
|
||||
* avoid adding another callback.
|
||||
*/
|
||||
private fun checkIsListeningPortTaken() {
|
||||
try {
|
||||
DatagramSocket(21027, InetAddress.getByName("0.0.0.0")).close()
|
||||
} catch (e: SocketException) {
|
||||
Log.w(TAG, e)
|
||||
isListeningPortTaken = true
|
||||
}
|
||||
// these listeners are called at the UI Thread
|
||||
// there is no need to unregister because they removed from the library when close is called
|
||||
fun registerMessageFromUnknownDeviceListener(listener: (DeviceId) -> Unit) {
|
||||
messageFromUnknownDeviceListeners.add(listener)
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters index update listener and decreases instance count.
|
||||
*
|
||||
* We wait a bit before closing [[syncthingClient]] etc, in case LibraryHandler is opened again
|
||||
* soon (eg in case of device rotation).
|
||||
*/
|
||||
fun close() {
|
||||
syncthingClient {
|
||||
try {
|
||||
it.indexHandler.unregisterOnIndexRecordAcquiredListener(this::onIndexRecordAcquired)
|
||||
it.indexHandler.unregisterOnFullIndexAcquiredListenersListener(this::onRemoteIndexAcquired)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
// ignored, no idea why this is thrown
|
||||
}
|
||||
}
|
||||
|
||||
instanceCount--
|
||||
Handler().postDelayed({
|
||||
Thread {
|
||||
if (instanceCount == 0) {
|
||||
folderBrowser?.close()
|
||||
folderBrowser = null
|
||||
syncthingClient?.close()
|
||||
syncthingClient = null
|
||||
configuration = null
|
||||
}
|
||||
}.start()
|
||||
}, 60 * 1000)
|
||||
|
||||
fun unregisterMessageFromUnknownDeviceListener(listener: (DeviceId) -> Unit) {
|
||||
messageFromUnknownDeviceListeners.remove(listener)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package net.syncthing.lite.library
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import net.syncthing.java.client.SyncthingClient
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.repository.android.SqliteIndexRepository
|
||||
import net.syncthing.repository.android.TempDirectoryLocalRepository
|
||||
import net.syncthing.repository.android.database.RepositoryDatabase
|
||||
import java.io.File
|
||||
import java.net.DatagramSocket
|
||||
import java.net.InetAddress
|
||||
import java.net.SocketException
|
||||
|
||||
/**
|
||||
* This class is used internally to access the syncthing-java library
|
||||
* There should be never more than 1 instance of this class
|
||||
*
|
||||
* This class can not be recycled. This means that after doing a shutdown of it,
|
||||
* a new instance must be created
|
||||
*
|
||||
* The creation and the shutdown are synchronous, so keep them out of the UI Thread
|
||||
*/
|
||||
class LibraryInstance (context: Context) {
|
||||
companion object {
|
||||
private const val LOG_TAG = "LibraryInstance"
|
||||
|
||||
/**
|
||||
* Check if listening port for local discovery is taken by another app. Do this check here to
|
||||
* avoid adding another callback.
|
||||
*/
|
||||
private fun checkIsListeningPortTaken(): Boolean {
|
||||
try {
|
||||
DatagramSocket(21027, InetAddress.getByName("0.0.0.0")).close()
|
||||
|
||||
return false
|
||||
} catch (e: SocketException) {
|
||||
Log.w(LOG_TAG, e)
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val tempRepository = TempDirectoryLocalRepository(File(context.filesDir, "temp_repository"))
|
||||
|
||||
val isListeningPortTaken = checkIsListeningPortTaken() // this must come first to work correctly
|
||||
val configuration = Configuration(configFolder = context.filesDir)
|
||||
val syncthingClient = SyncthingClient(
|
||||
configuration = configuration,
|
||||
repository = SqliteIndexRepository(
|
||||
database = RepositoryDatabase.with(context),
|
||||
closeDatabaseOnClose = false,
|
||||
clearTempStorageHook = { tempRepository.deleteAllData() }
|
||||
),
|
||||
tempRepository = tempRepository
|
||||
)
|
||||
val folderBrowser = syncthingClient.indexHandler.newFolderBrowser()
|
||||
|
||||
fun shutdown() {
|
||||
folderBrowser.close()
|
||||
syncthingClient.close()
|
||||
configuration.persistNow()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package net.syncthing.lite.library
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import java.util.concurrent.Executors
|
||||
import kotlin.coroutines.experimental.suspendCoroutine
|
||||
|
||||
/**
|
||||
* This class manages the access to an LibraryInstance
|
||||
*
|
||||
* Users can get an instance with startLibraryUsage()
|
||||
* If they are done with it, the should call stopLibraryUsage()
|
||||
* After this, it's NOT safe to continue using the received LibraryInstance
|
||||
*
|
||||
* Every call to startLibraryUsage should be followed by an call to stopLibraryUsage,
|
||||
* even if the callback was not called yet. It can still be called, so users should watch out.
|
||||
*
|
||||
* All listeners are executed at the UI Thread (except the synchronousInstanceCreator)
|
||||
*
|
||||
* The userCounterListener is always called before the isRunningListener
|
||||
*
|
||||
* The listeners are called for all changes, nothing is skipped or batched
|
||||
*/
|
||||
class LibraryManager (
|
||||
val synchronousInstanceCreator: () -> LibraryInstance,
|
||||
val userCounterListener: (Int) -> Unit = {},
|
||||
val isRunningListener: (isRunning: Boolean) -> Unit = {}
|
||||
) {
|
||||
companion object {
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
}
|
||||
|
||||
// this must be a SingleThreadExecutor to avoid race conditions
|
||||
// only this Thread should access instance and userCounter
|
||||
private val startStopExecutor = Executors.newSingleThreadExecutor()
|
||||
|
||||
private var instance: LibraryInstance? = null
|
||||
private var userCounter = 0
|
||||
|
||||
fun startLibraryUsage(callback: (LibraryInstance) -> Unit) {
|
||||
startStopExecutor.submit {
|
||||
val newUserCounter = ++userCounter
|
||||
handler.post { userCounterListener(newUserCounter) }
|
||||
|
||||
if (instance == null) {
|
||||
instance = synchronousInstanceCreator()
|
||||
handler.post { isRunningListener(true) }
|
||||
}
|
||||
|
||||
handler.post { callback(instance!!) }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun startLibraryUsageCoroutine(): LibraryInstance {
|
||||
return suspendCoroutine { continuation ->
|
||||
startLibraryUsage { instance ->
|
||||
continuation.resume(instance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stopLibraryUsage() {
|
||||
startStopExecutor.submit {
|
||||
val newUserCounter = --userCounter
|
||||
|
||||
if (newUserCounter < 0) {
|
||||
userCounter = 0
|
||||
|
||||
throw IllegalStateException("can not stop library usage if there are 0 users")
|
||||
}
|
||||
|
||||
handler.post { userCounterListener(newUserCounter) }
|
||||
}
|
||||
}
|
||||
|
||||
fun shutdownIfThereAreZeroUsers(listener: (wasShutdownPerformed: Boolean) -> Unit = {}) {
|
||||
startStopExecutor.submit {
|
||||
if (userCounter == 0) {
|
||||
instance?.shutdown()
|
||||
instance = null
|
||||
|
||||
handler.post { isRunningListener(false) }
|
||||
handler.post { listener(true) }
|
||||
} else {
|
||||
handler.post { listener(false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,13 @@ import android.provider.DocumentsContract.Document
|
||||
import android.provider.DocumentsContract.Root
|
||||
import android.provider.DocumentsProvider
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.experimental.cancel
|
||||
import kotlinx.coroutines.experimental.runBlocking
|
||||
import net.syncthing.java.bep.IndexBrowser
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.java.core.beans.FolderStats
|
||||
import net.syncthing.lite.R
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.net.URLConnection
|
||||
import java.util.concurrent.CountDownLatch
|
||||
@@ -43,18 +44,15 @@ class SyncthingProvider : DocumentsProvider() {
|
||||
return true
|
||||
}
|
||||
|
||||
private fun getLibraryHandler(): LibraryHandler {
|
||||
val latch = CountDownLatch(1)
|
||||
val libraryHandler = LibraryHandler(context, { latch.countDown() }, { _, _ -> }, {})
|
||||
latch.await()
|
||||
return libraryHandler
|
||||
}
|
||||
// this instance is not started -> it connects and disconnects on demand
|
||||
private val libraryHandler: LibraryHandler by lazy { LibraryHandler(context) }
|
||||
private val libraryManager: LibraryManager by lazy { DefaultLibraryManager.with(context) }
|
||||
|
||||
override fun queryRoots(projection: Array<String>?): Cursor {
|
||||
Log.d(Tag, "queryRoots($projection)")
|
||||
val latch = CountDownLatch(1)
|
||||
var folders: List<Pair<FolderInfo, FolderStats>>? = null
|
||||
getLibraryHandler().folderBrowser { folderBrowser ->
|
||||
libraryHandler.folderBrowser { folderBrowser ->
|
||||
folders = folderBrowser.folderInfoAndStatsList()
|
||||
latch.countDown()
|
||||
}
|
||||
@@ -98,23 +96,32 @@ class SyncthingProvider : DocumentsProvider() {
|
||||
override fun openDocument(documentId: String, mode: String, signal: CancellationSignal?):
|
||||
ParcelFileDescriptor {
|
||||
Log.d(Tag, "openDocument($documentId, $mode, $signal)")
|
||||
val fileInfo = FileInfo(folder = getFolderIdForDocId(documentId),
|
||||
path = getPathForDocId(documentId), type = FileInfo.FileType.FILE)
|
||||
val fileInfo = getIndexBrowser(getFolderIdForDocId(documentId))
|
||||
.getFileInfoByAbsolutePath(getPathForDocId(documentId))
|
||||
val accessMode = ParcelFileDescriptor.parseMode(mode)
|
||||
if (accessMode != ParcelFileDescriptor.MODE_READ_ONLY) {
|
||||
throw NotImplementedError()
|
||||
}
|
||||
|
||||
val latch = CountDownLatch(1)
|
||||
var outputFile: File? = null
|
||||
getLibraryHandler().syncthingClient { syncthingClient ->
|
||||
DownloadFileTask(context, syncthingClient, fileInfo,
|
||||
{ t, _ -> if (signal?.isCanceled == true) t.cancel() }, {
|
||||
outputFile = it
|
||||
latch.countDown()
|
||||
}, {})
|
||||
val outputFile = runBlocking {
|
||||
signal?.setOnCancelListener {
|
||||
this.coroutineContext.cancel()
|
||||
}
|
||||
|
||||
val libraryInstance = libraryManager.startLibraryUsageCoroutine()
|
||||
|
||||
try {
|
||||
DownloadFileTask.downloadFileCoroutine(
|
||||
externalCacheDir = context.externalCacheDir,
|
||||
syncthingClient = libraryInstance.syncthingClient,
|
||||
fileInfo = fileInfo,
|
||||
onProgress = { /* ignore the progress */ }
|
||||
)
|
||||
} finally {
|
||||
libraryManager.stopLibraryUsage()
|
||||
}
|
||||
}
|
||||
latch.await()
|
||||
|
||||
return ParcelFileDescriptor.open(outputFile, ParcelFileDescriptor.MODE_READ_ONLY)
|
||||
}
|
||||
|
||||
@@ -124,7 +131,7 @@ class SyncthingProvider : DocumentsProvider() {
|
||||
row.add(Document.COLUMN_DISPLAY_NAME, fileInfo.fileName)
|
||||
row.add(Document.COLUMN_SIZE, fileInfo.size)
|
||||
val mime = if (fileInfo.isDirectory()) Document.MIME_TYPE_DIR
|
||||
else URLConnection.guessContentTypeFromName(fileInfo.fileName)
|
||||
else URLConnection.guessContentTypeFromName(fileInfo.fileName)
|
||||
row.add(Document.COLUMN_MIME_TYPE, mime)
|
||||
row.add(Document.COLUMN_LAST_MODIFIED, fileInfo.lastModified)
|
||||
row.add(Document.COLUMN_FLAGS, 0)
|
||||
@@ -141,11 +148,11 @@ class SyncthingProvider : DocumentsProvider() {
|
||||
private fun getIndexBrowser(folderId: String): IndexBrowser {
|
||||
val latch = CountDownLatch(1)
|
||||
var indexBrowser: IndexBrowser? = null
|
||||
getLibraryHandler().syncthingClient {
|
||||
libraryHandler.syncthingClient {
|
||||
indexBrowser = it.indexHandler.newIndexBrowser(folderId)
|
||||
latch.countDown()
|
||||
}
|
||||
latch.await()
|
||||
return indexBrowser!!
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package net.syncthing.lite.library
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.util.Log
|
||||
import net.syncthing.java.bep.BlockPusher
|
||||
import net.syncthing.java.client.SyncthingClient
|
||||
@@ -17,7 +19,10 @@ class UploadFileTask(context: Context, syncthingClient: SyncthingClient,
|
||||
private val onComplete: () -> Unit,
|
||||
private val onError: () -> Unit) {
|
||||
|
||||
private val TAG = "UploadFileTask"
|
||||
companion object {
|
||||
private const val TAG = "UploadFileTask"
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
}
|
||||
|
||||
private val syncthingPath = PathUtils.buildPath(syncthingSubFolder, Util.getContentFileName(context, localFile))
|
||||
private val uploadStream = context.contentResolver.openInputStream(localFile)
|
||||
@@ -28,18 +33,20 @@ class UploadFileTask(context: Context, syncthingClient: SyncthingClient,
|
||||
Log.i(TAG, "Uploading file $localFile to folder $syncthingFolder:$syncthingPath")
|
||||
syncthingClient.getBlockPusher(syncthingFolder, { blockPusher ->
|
||||
val observer = blockPusher.pushFile(uploadStream, syncthingFolder, syncthingPath)
|
||||
onProgress(observer)
|
||||
while (!observer.isCompleted()) {
|
||||
if (isCancelled)
|
||||
return@getBlockPusher
|
||||
|
||||
observer.waitForProgressUpdate()
|
||||
Log.i(TAG, "upload progress = ${observer.progressPercentage()}%")
|
||||
onProgress(observer)
|
||||
}
|
||||
IOUtils.closeQuietly(uploadStream)
|
||||
onComplete()
|
||||
}, { onError() })
|
||||
handler.post { onProgress(observer) }
|
||||
|
||||
while (!observer.isCompleted()) {
|
||||
if (isCancelled)
|
||||
return@getBlockPusher
|
||||
|
||||
observer.waitForProgressUpdate()
|
||||
Log.i(TAG, "upload progress = ${observer.progressPercentage()}%")
|
||||
handler.post { onProgress(observer) }
|
||||
}
|
||||
IOUtils.closeQuietly(uploadStream)
|
||||
handler.post { onComplete() }
|
||||
}, { handler.post { onError() } })
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
|
||||
</vector>
|
||||
@@ -0,0 +1,9 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M13,3h-2v10h2L13,3zM17.83,5.17l-1.42,1.42C17.99,7.86 19,9.81 19,12c0,3.87 -3.13,7 -7,7s-7,-3.13 -7,-7c0,-2.19 1.01,-4.14 2.58,-5.42L6.17,5.17C4.23,6.82 3,9.26 3,12c0,4.97 4.03,9 9,9s9,-4.03 9,-9c0,-2.74 -1.23,-5.18 -3.17,-6.83z"/>
|
||||
</vector>
|
||||
@@ -1,9 +0,0 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#7D000000"
|
||||
android:pathData="M3,11H5V13H3V11M11,5H13V9H11V5M9,11H13V15H11V13H9V11M15,11H17V13H19V11H21V13H19V15H21V19H19V21H17V19H13V21H11V17H15V15H17V13H15V11M19,19V15H17V19H19M15,3H21V9H15V3M17,5V7H19V5H17M3,3H9V9H3V3M5,5V7H7V5H5M3,15H9V21H3V15M5,17V19H7V17H5Z"/>
|
||||
</vector>
|
||||
@@ -2,56 +2,43 @@
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<data>
|
||||
<variable
|
||||
name="isLoading"
|
||||
type="Boolean" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
</data>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:divider="?android:listDivider"
|
||||
android:showDividers="middle">
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:visibility="@{safeUnbox(isLoading) ? View.GONE : View.VISIBLE}"
|
||||
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/list_view" />
|
||||
|
||||
<ListView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:id="@+id/list_view"
|
||||
android:divider="@color/divider"
|
||||
android:dividerHeight="2dp">
|
||||
</ListView>
|
||||
<ProgressBar
|
||||
android:visibility="@{safeUnbox(isLoading) ? View.VISIBLE : View.GONE}"
|
||||
android:layout_centerInParent="true"
|
||||
android:id="@+id/progress_bar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/empty_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:textAlignment="center"
|
||||
android:text="@string/folder_list_empty_message"
|
||||
android:textSize="20sp"
|
||||
android:visibility="gone" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_bar"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_gravity="center"
|
||||
android:visibility="gone"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/main_list_view_upload_here_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
app:elevation="6dp"
|
||||
app:pressedTranslationZ="12dp"
|
||||
android:src="@drawable/ic_file_upload_white_24dp"/>
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/main_list_view_upload_here_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="24dp"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
app:elevation="6dp"
|
||||
app:pressedTranslationZ="12dp"
|
||||
android:src="@drawable/ic_file_upload_white_24dp"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
|
||||
@@ -43,17 +43,29 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/qr_code"
|
||||
<ViewFlipper
|
||||
android:id="@+id/flipper"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:scaleType="centerCrop"
|
||||
android:visibility="gone"/>
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/progress_bar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"/>
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ProgressBar
|
||||
android:layout_centerInParent="true"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/qr_code"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:scaleType="fitCenter" />
|
||||
|
||||
</ViewFlipper>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -2,26 +2,35 @@
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<data>
|
||||
<variable
|
||||
name="isEmpty"
|
||||
type="Boolean" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
</data>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ListView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/list"
|
||||
android:divider="@color/divider"
|
||||
android:dividerHeight="2dp">
|
||||
</ListView>
|
||||
<android.support.v7.widget.RecyclerView
|
||||
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/list"
|
||||
android:divider="@color/divider"
|
||||
android:dividerHeight="2dp">
|
||||
</android.support.v7.widget.RecyclerView>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/empty"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:text="@string/devices_list_view_empty_message"
|
||||
android:textSize="20sp"
|
||||
android:visibility="gone" />
|
||||
<TextView
|
||||
android:id="@+id/empty"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:text="@string/devices_list_view_empty_message"
|
||||
android:textSize="20sp"
|
||||
android:visibility="@{safeUnbox(isEmpty) ? View.VISIBLE : View.GONE}" />
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/add_device"
|
||||
|
||||
@@ -2,26 +2,69 @@
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<FrameLayout
|
||||
<data>
|
||||
<variable
|
||||
name="isEmpty"
|
||||
type="Boolean" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
|
||||
<variable
|
||||
name="listeningPortTaken"
|
||||
type="Boolean" />
|
||||
</data>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<ListView
|
||||
<FrameLayout
|
||||
android:layout_weight="1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/list"
|
||||
android:divider="@color/divider"
|
||||
android:dividerHeight="2dp">
|
||||
</ListView>
|
||||
<TextView
|
||||
android:id="@+id/empty"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:text="@string/folder_list_empty_message"
|
||||
android:textSize="20sp"
|
||||
android:visibility="gone" />
|
||||
android:layout_height="0dp">
|
||||
|
||||
</FrameLayout>
|
||||
<android.support.v7.widget.RecyclerView
|
||||
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/list"
|
||||
android:visibility="@{safeUnbox(isEmpty) ? View.GONE : View.VISIBLE}" />
|
||||
|
||||
</layout>
|
||||
<TextView
|
||||
android:id="@+id/empty"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:text="@string/folder_list_empty_message"
|
||||
android:textSize="20sp"
|
||||
android:visibility="@{safeUnbox(isEmpty) ? View.VISIBLE : View.GONE}" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:theme="@style/ThemeOverlay.AppCompat.Dark"
|
||||
android:background="?colorPrimary"
|
||||
android:visibility="@{safeUnbox(listeningPortTaken) ? View.VISIBLE : View.GONE}"
|
||||
android:padding="8dp"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:textAppearance="?android:textAppearanceMedium"
|
||||
android:text="@string/other_syncthing_instance_title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:textAppearance="?android:textAppearanceSmall"
|
||||
android:text="@string/other_syncthing_instance_message"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</layout>
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<data>
|
||||
<variable
|
||||
name="listeningPortTaken"
|
||||
type="Boolean" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
</data>
|
||||
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
@@ -36,6 +43,23 @@
|
||||
android:textColor="#eee"
|
||||
android:text="@string/intro_page_one_description" />
|
||||
|
||||
<TextView
|
||||
android:visibility="@{safeUnbox(listeningPortTaken) ? View.VISIBLE : View.GONE}"
|
||||
android:textAppearance="?android:textAppearanceMedium"
|
||||
android:textColor="#eee"
|
||||
android:text="@string/other_syncthing_instance_title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:layout_marginBottom="48dp"
|
||||
android:visibility="@{safeUnbox(listeningPortTaken) ? View.VISIBLE : View.GONE}"
|
||||
android:textAppearance="?android:textAppearanceSmall"
|
||||
android:textColor="#eee"
|
||||
android:text="@string/other_syncthing_instance_message"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</layout>
|
||||
</layout>
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
@@ -10,40 +8,65 @@
|
||||
android:padding="28dp"
|
||||
android:gravity="center_horizontal">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:textColor="#eee"
|
||||
android:textSize="24sp"
|
||||
android:text="@string/intro_page_two_title"/>
|
||||
<TextView
|
||||
android:id="@+id/title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="24dp"
|
||||
android:textColor="#eee"
|
||||
android:textSize="24sp"
|
||||
android:text="@string/intro_page_two_title"/>
|
||||
|
||||
<FrameLayout
|
||||
<FrameLayout
|
||||
android:layout_weight="1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp">
|
||||
<ScrollView
|
||||
android:layout_gravity="center"
|
||||
android:id="@+id/scroll"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:layout_gravity="center">
|
||||
|
||||
<include
|
||||
android:layout_height="wrap_content">
|
||||
<LinearLayout
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:id="@+id/enter_device_id"
|
||||
layout="@layout/view_enter_device_id" />
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
</FrameLayout>
|
||||
<include
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:id="@+id/enter_device_id"
|
||||
layout="@layout/view_enter_device_id" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/description"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="24dp"
|
||||
android:layout_marginBottom="@dimen/appIntroBottomBarHeight"
|
||||
android:textSize="16sp"
|
||||
android:textAlignment="center"
|
||||
android:textColor="#eee"
|
||||
android:text="@string/intro_page_two_description" />
|
||||
<LinearLayout
|
||||
android:id="@+id/found_devices"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<!--
|
||||
Found device ids will be put here as buttons
|
||||
|
||||
This does not use an ListView or RecyclerView because this allows using
|
||||
wrap_content as height and because it's expected to be an small list
|
||||
-->
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
</FrameLayout>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/description"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="24dp"
|
||||
android:layout_marginBottom="@dimen/appIntroBottomBarHeight"
|
||||
android:textSize="16sp"
|
||||
android:textAlignment="center"
|
||||
android:textColor="#eee"
|
||||
android:text="@string/intro_page_two_description" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
<variable
|
||||
name="name"
|
||||
type="String" />
|
||||
|
||||
<variable
|
||||
name="isConnected"
|
||||
type="Boolean" />
|
||||
</data>
|
||||
|
||||
<RelativeLayout
|
||||
android:background="?selectableItemBackground"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingBottom="12dp"
|
||||
@@ -13,11 +25,14 @@
|
||||
android:id="@+id/device_icon"
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp"
|
||||
android:src="@drawable/ic_laptop_green_24dp"
|
||||
tools:src="@drawable/ic_laptop_green_24dp"
|
||||
android:src="@{safeUnbox(isConnected) ? @drawable/ic_laptop_green_24dp : @drawable/ic_laptop_red_24dp}"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_alignParentTop="true"/>
|
||||
|
||||
<TextView
|
||||
tools:text="Computer"
|
||||
android:text="@{name}"
|
||||
android:id="@+id/device_name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_alignParentEnd="true"
|
||||
|
||||
@@ -2,7 +2,21 @@
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<data>
|
||||
<variable
|
||||
name="fileName"
|
||||
type="String" />
|
||||
|
||||
<variable
|
||||
name="fileSize"
|
||||
type="String" />
|
||||
|
||||
<import type="android.view.View" />
|
||||
<import type="android.text.TextUtils" />
|
||||
</data>
|
||||
|
||||
<RelativeLayout
|
||||
android:background="?selectableItemBackground"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="8dp"
|
||||
@@ -19,6 +33,8 @@
|
||||
android:layout_alignParentTop="true" />
|
||||
|
||||
<TextView
|
||||
tools:text="Test Directory"
|
||||
android:text="@{fileName}"
|
||||
android:id="@+id/file_label"
|
||||
android:maxLines="1"
|
||||
android:layout_width="wrap_content"
|
||||
@@ -29,7 +45,9 @@
|
||||
android:textSize="22sp" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/file_size"
|
||||
android:visibility="@{TextUtils.isEmpty(fileSize) ? View.GONE : View.VISIBLE}"
|
||||
tools:text="250 MB"
|
||||
android:text="@{fileSize}"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:ellipsize="end"
|
||||
|
||||
@@ -1,7 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<data>
|
||||
<variable
|
||||
name="folderName"
|
||||
type="String" />
|
||||
|
||||
<variable
|
||||
name="lastModification"
|
||||
type="String" />
|
||||
|
||||
<variable
|
||||
name="info"
|
||||
type="String" />
|
||||
</data>
|
||||
|
||||
<RelativeLayout
|
||||
android:background="?selectableItemBackground"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingBottom="12dp"
|
||||
@@ -10,7 +25,9 @@
|
||||
android:paddingTop="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/folder_name"
|
||||
tools:text="Music"
|
||||
android:text="@{folderName}"
|
||||
android:id="@+id/folder_name_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -23,16 +40,20 @@
|
||||
android:textStyle="bold"/>
|
||||
|
||||
<TextView
|
||||
tools:text="Last modified: two minutes ago"
|
||||
android:text="@{lastModification}"
|
||||
android:id="@+id/folder_lastmod_info"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:ellipsize="end"
|
||||
android:maxLines="1"
|
||||
android:layout_below="@id/folder_name"
|
||||
android:layout_below="@id/folder_name_view"
|
||||
android:textSize="14sp"
|
||||
android:layout_alignParentStart="true" />
|
||||
|
||||
<TextView
|
||||
tools:text="Additional information"
|
||||
android:text="@{info}"
|
||||
android:id="@+id/folder_content_info"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
<string name="folder_list_empty_message">Keine Ordner verfügbar</string>
|
||||
<string name="clear_local_cache_index_label">Lokalen Index und Cache löschen</string>
|
||||
<string name="devices_list_view_empty_message">Keine Geräte verfügbar</string>
|
||||
<string name="scan_qr_code">QR code scannen</string>
|
||||
<string name="enter_device_id">Geräte ID eingeben</string>
|
||||
<string name="device_id_dialog_title">Geräte ID eingeben</string>
|
||||
<string name="dialog_downloading_file">Datei %1$s wird heruntergeladen</string>
|
||||
<string name="toast_file_download_failed">Datei konnte nicht heruntergeladen werden</string>
|
||||
@@ -15,7 +13,7 @@
|
||||
<string name="clear_cache_and_index_title">Lokalen Cache und Index löschen?</string>
|
||||
<string name="clear_cache_and_index_body">Gesamten lokalen Cache und Index löschen?</string>
|
||||
<string name="last_modified_time">Zuletzt modifiziert: %1$s</string>
|
||||
<string name="remove_device_title">Gerät entfernen:</string>
|
||||
<string name="remove_device_title">Gerät entfernen: %1$s</string>
|
||||
<string name="device_import_success">Gerät %1$s erfolgreich importiert</string>
|
||||
<string name="device_already_known">Gerät ist bereits bekannt $1%s</string>
|
||||
<string name="folders_label">Ordner</string>
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
<string name="folder_list_empty_message">Aucun dossier disponible</string>
|
||||
<string name="clear_local_cache_index_label">Effacer le cache et l\'index local</string>
|
||||
<string name="devices_list_view_empty_message">Aucun appareil disponible</string>
|
||||
<string name="scan_qr_code">Scanner le QR Code</string>
|
||||
<string name="enter_device_id">Entrer l\'ID de l\'appareil</string>
|
||||
<string name="device_id_dialog_title">Entrer l\'ID de l\'appareil</string>
|
||||
<string name="dialog_downloading_file">Téléchargement du fichier %1$s</string>
|
||||
<string name="toast_file_download_failed">Le téléchargement du fichier a échoué</string>
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
<string name="folder_list_empty_message">Nessuna cartella disponibile</string>
|
||||
<string name="clear_local_cache_index_label">Cancella cache/indice</string>
|
||||
<string name="devices_list_view_empty_message">Nessun dispositivo disponibile</string>
|
||||
<string name="scan_qr_code">Scansiona codice QR</string>
|
||||
<string name="enter_device_id">Inserisci ID dispositivo</string>
|
||||
<string name="device_id_dialog_title">Inserisci ID Dispositivo</string>
|
||||
<string name="dialog_downloading_file">Download del file %1$s</string>
|
||||
<string name="toast_file_download_failed">Impossibile scaricare il file</string>
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
<string name="folder_list_empty_message">フォルダーがありません</string>
|
||||
<string name="clear_local_cache_index_label">ローカルキャッシュ/索引をクリア</string>
|
||||
<string name="devices_list_view_empty_message">デバイスがありません</string>
|
||||
<string name="scan_qr_code">QR コードをスキャン</string>
|
||||
<string name="enter_device_id">デバイス ID を入力</string>
|
||||
<string name="device_id_dialog_title">デバイス ID を入力</string>
|
||||
<string name="dialog_downloading_file">ファイル %1$s のダウンロード中</string>
|
||||
<string name="toast_file_download_failed">ファイルのダウンロードに失敗しました</string>
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
<string name="folder_list_empty_message">Nici un director disponibil</string>
|
||||
<string name="clear_local_cache_index_label">Curăță indexul/memoria locală</string>
|
||||
<string name="devices_list_view_empty_message">Nici un dispozitiv disponibil</string>
|
||||
<string name="scan_qr_code">Scanează cod QR</string>
|
||||
<string name="enter_device_id">Introduceți ID dispozitiv</string>
|
||||
<string name="device_id_dialog_title">Introduceți ID dispozitiv</string>
|
||||
<string name="dialog_downloading_file">Se descarcă fișierul %1$s</string>
|
||||
<string name="toast_file_download_failed">Descărcarea fișierului a eșuat</string>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="primary">#f43703</color>
|
||||
<color name="primary_light">#ff6f39</color>
|
||||
<color name="primary_dark">#b90000</color>
|
||||
<color name="accent">#FFC107</color>
|
||||
<color name="divider">#1F000000</color>
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string-array name="shutdown_delay_labels">
|
||||
<item>@string/settings_shutdown_delay_10_seconds</item>
|
||||
<item>@string/settings_shutdown_delay_30_seconds</item>
|
||||
<item>@string/settings_shutdown_delay_1_minute</item>
|
||||
<item>@string/settings_shutdown_delay_5_minutes</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="shutdown_delay_values">
|
||||
<item>10000</item>
|
||||
<item>30000</item>
|
||||
<item>60000</item>
|
||||
<item>300000</item>
|
||||
</string-array>
|
||||
|
||||
<string translatable="false" name="default_shutdown_delay">60000</string>
|
||||
</resources>
|
||||
@@ -3,10 +3,7 @@
|
||||
<string name="folder_list_empty_message">No folder available</string>
|
||||
<string name="clear_local_cache_index_label">Clear local cache/index</string>
|
||||
<string name="devices_list_view_empty_message">No devices available</string>
|
||||
<string name="scan_qr_code">Scan QR code</string>
|
||||
<string name="enter_device_id">Enter device ID</string>
|
||||
<string name="invalid_device_id">Error: Invalid device ID</string>
|
||||
<string name="device_id_dialog_title">Enter Device ID</string>
|
||||
<string name="dialog_downloading_file">Downloading file %1$s</string>
|
||||
<string name="toast_file_download_failed">Failed to download file</string>
|
||||
<string name="toast_open_file_failed">No compatible app found</string>
|
||||
@@ -43,4 +40,17 @@
|
||||
<string name="settings_app_version_title">App version</string>
|
||||
<string name="settings_local_device_name">Local device name</string>
|
||||
<string name="settings_local_device_summary">The name that other devices will see for this device</string>
|
||||
<string name="settings_shutdown_delay_title">Shutdown delay</string>
|
||||
<string name="settings_shutdown_delay_summary">Time before shuting down the Syncthing client after its last usage</string>
|
||||
<string name="device_id_dialog_title">Enter Device ID</string>
|
||||
<string name="settings_shutdown_delay_10_seconds">10 seconds</string>
|
||||
<string name="settings_shutdown_delay_30_seconds">30 seconds</string>
|
||||
<string name="settings_shutdown_delay_1_minute">1 minute</string>
|
||||
<string name="settings_shutdown_delay_5_minutes">5 minutes</string>
|
||||
<string name="dialog_warning_reconnect_problem">
|
||||
Due to the behaviour of this App and the behaviour of the Syncthing Server,
|
||||
you can\'t reconnct for some minutes if the App was killed (due to removing from the recent App list)
|
||||
or the connection was interrupted.
|
||||
This does not apply to local discovery connections.
|
||||
</string>
|
||||
</resources>
|
||||
|
||||
@@ -10,10 +10,24 @@
|
||||
android:summary="@string/settings_local_device_summary"
|
||||
android:persistent="false"/>
|
||||
|
||||
<!--
|
||||
|
||||
This is something for the advanced preferences (later), but it still has got an effect
|
||||
|
||||
<ListPreference
|
||||
android:key="shutdown_delay"
|
||||
android:title="@string/settings_shutdown_delay_title"
|
||||
android:summary="@string/settings_shutdown_delay_summary"
|
||||
android:entries="@array/shutdown_delay_labels"
|
||||
android:entryValues="@array/shutdown_delay_values"
|
||||
android:defaultValue="@string/default_shutdown_delay" />
|
||||
|
||||
-->
|
||||
|
||||
<Preference
|
||||
android:key="app_version"
|
||||
android:title="@string/settings_app_version_title"/>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
</PreferenceScreen>
|
||||
|
||||
+5
-4
@@ -1,10 +1,11 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.2.20'
|
||||
ext.kotlin_version = '1.2.61'
|
||||
ext.support_version = '27.0.2'
|
||||
ext.build_tools_version = '3.0.1'
|
||||
ext.anko_version = '0.10.4'
|
||||
ext.build_tools_version = '3.2.0'
|
||||
ext.anko_version = '0.10.7'
|
||||
ext.protobuf_lite_version = '3.0.1'
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
@@ -15,12 +16,12 @@ buildscript {
|
||||
classpath "com.android.tools.build:gradle:$build_tools_version"
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
|
||||
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
maven {
|
||||
url "https://jitpack.io"
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
#Mon Dec 28 10:00:20 PST 2015
|
||||
#Fri Sep 14 08:50:38 CEST 2018
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3.1-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
include ':app'
|
||||
include ':app', ':syncthing-repository-android', ':syncthing-repository-default', ':syncthing-relay-client', ':syncthing-bep', ':syncthing-core', ':syncthing-client', ':syncthing-discovery', ':syncthing-client-cli', ':syncthing-http-relay-client'
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'com.google.protobuf'
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile project(':syncthing-core')
|
||||
compile project(':syncthing-relay-client')
|
||||
compile project(':syncthing-http-relay-client')
|
||||
compile "net.jpountz.lz4:lz4:1.3.0"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.30.2'
|
||||
implementation "com.google.protobuf:protobuf-lite:$protobuf_lite_version"
|
||||
}
|
||||
|
||||
protobuf {
|
||||
protoc {
|
||||
artifact = "com.google.protobuf:protoc:3.5.1-1"
|
||||
}
|
||||
plugins {
|
||||
javalite {
|
||||
// The codegen for lite comes as a separate artifact
|
||||
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
|
||||
}
|
||||
}
|
||||
generateProtoTasks {
|
||||
all().each { task ->
|
||||
task.builtins {
|
||||
// In most cases you don't need the full Java output
|
||||
// if you use the lite output.
|
||||
remove java
|
||||
}
|
||||
task.plugins {
|
||||
javalite { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/google/protobuf-gradle-plugin/issues/100
|
||||
compileKotlin.dependsOn('generateProto')
|
||||
sourceSets.main.kotlin.srcDirs += file("${protobuf.generatedFilesBaseDir}/main/javalite")
|
||||
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
* Copyright (C) 2018 Jonas Lochmann
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
import com.google.protobuf.ByteString
|
||||
import kotlinx.coroutines.experimental.*
|
||||
import kotlinx.coroutines.experimental.channels.Channel
|
||||
import net.syncthing.java.bep.BlockExchangeProtos.ErrorCode
|
||||
import net.syncthing.java.bep.BlockExchangeProtos.Request
|
||||
import net.syncthing.java.bep.utils.longSumBy
|
||||
import net.syncthing.java.core.beans.BlockInfo
|
||||
import net.syncthing.java.core.beans.FileBlocks
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.java.core.interfaces.TempRepository
|
||||
import net.syncthing.java.core.utils.NetworkUtils
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.bouncycastle.util.encoders.Hex
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.*
|
||||
import java.lang.Exception
|
||||
import java.security.MessageDigest
|
||||
import java.util.*
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
class BlockPuller internal constructor(private val connectionHandler: ConnectionHandler,
|
||||
private val indexHandler: IndexHandler,
|
||||
private val responseHandler: ResponseHandler,
|
||||
private val tempRepository: TempRepository) {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
fun pullFileSync(
|
||||
fileInfo: FileInfo,
|
||||
progressListener: (status: BlockPullerStatus) -> Unit = { }
|
||||
): InputStream {
|
||||
return runBlocking {
|
||||
pullFileCoroutine(fileInfo, progressListener)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun pullFileCoroutine(
|
||||
fileInfo: FileInfo,
|
||||
progressListener: (status: BlockPullerStatus) -> Unit = { }
|
||||
): InputStream {
|
||||
val fileBlocks = indexHandler.waitForRemoteIndexAcquired(connectionHandler)
|
||||
.getFileInfoAndBlocksByPath(fileInfo.folder, fileInfo.path)
|
||||
?.value
|
||||
?: throw IOException("file not found in local index for folder = ${fileInfo.folder} path = ${fileInfo.path}")
|
||||
logger.info("pulling file = {}", fileBlocks)
|
||||
NetworkUtils.assertProtocol(connectionHandler.hasFolder(fileBlocks.folder), { "supplied connection handler $connectionHandler will not share folder ${fileBlocks.folder}" })
|
||||
|
||||
// the file could have changed since the caller read it
|
||||
// this would save the file using a wrong name, so throw here
|
||||
if (fileBlocks.hash != fileInfo.hash) {
|
||||
throw IllegalStateException("the current file entry hash does not match the hash of the provided one")
|
||||
}
|
||||
|
||||
val blockTempIdByHash = Collections.synchronizedMap(HashMap<String, String>())
|
||||
|
||||
var status = BlockPullerStatus(
|
||||
downloadedBytes = 0,
|
||||
totalTransferSize = fileBlocks.blocks.distinctBy { it.hash }.longSumBy { it.size.toLong() },
|
||||
totalFileSize = fileBlocks.size
|
||||
)
|
||||
|
||||
try {
|
||||
val reportProgressLock = Object()
|
||||
|
||||
fun updateProgress(additionalDownloadedBytes: Long) {
|
||||
synchronized(reportProgressLock) {
|
||||
status = status.copy(
|
||||
downloadedBytes = status.downloadedBytes + additionalDownloadedBytes
|
||||
)
|
||||
|
||||
progressListener(status)
|
||||
}
|
||||
}
|
||||
|
||||
coroutineScope {
|
||||
val pipe = Channel<BlockInfo>()
|
||||
|
||||
repeat(4 /* 4 blocks per time */) { workerNumber ->
|
||||
async {
|
||||
for (block in pipe) {
|
||||
logger.debug("request block with hash = {} from worker {}", block.hash, workerNumber)
|
||||
|
||||
val blockContent = pullBlock(fileBlocks, block, 1000 * 60 /* 60 seconds timeout per block */)
|
||||
|
||||
blockTempIdByHash[block.hash] = tempRepository.pushTempData(blockContent)
|
||||
|
||||
updateProgress(blockContent.size.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileBlocks.blocks.distinctBy { it.hash }.forEach { block ->
|
||||
pipe.send(block)
|
||||
}
|
||||
|
||||
pipe.close()
|
||||
}
|
||||
|
||||
// the sequence is evaluated lazy -> only one block per time is loaded
|
||||
val fileBlocksIterator = fileBlocks.blocks
|
||||
.asSequence()
|
||||
.map { tempRepository.popTempData(blockTempIdByHash[it.hash]!!) }
|
||||
.map { ByteArrayInputStream(it) }
|
||||
.iterator()
|
||||
|
||||
return object : SequenceInputStream(object : Enumeration<InputStream> {
|
||||
override fun hasMoreElements() = fileBlocksIterator.hasNext()
|
||||
override fun nextElement() = fileBlocksIterator.next()
|
||||
}) {
|
||||
override fun close() {
|
||||
super.close()
|
||||
|
||||
// delete all temp blocks now
|
||||
// they are deleted after reading, but the consumer could stop before reading the whole stream
|
||||
tempRepository.deleteTempData(blockTempIdByHash.values.toList())
|
||||
}
|
||||
}
|
||||
} catch (ex: Exception) {
|
||||
// delete all temp blocks now
|
||||
tempRepository.deleteTempData(blockTempIdByHash.values.toList())
|
||||
|
||||
throw ex
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun pullBlock(fileBlocks: FileBlocks, block: BlockInfo, timeoutInMillis: Long): ByteArray {
|
||||
logger.debug("sent request for block, hash = {}", block.hash)
|
||||
|
||||
val response =
|
||||
withTimeout(timeoutInMillis) {
|
||||
try {
|
||||
doRequest(
|
||||
Request.newBuilder()
|
||||
.setFolder(fileBlocks.folder)
|
||||
.setName(fileBlocks.path)
|
||||
.setOffset(block.offset)
|
||||
.setSize(block.size)
|
||||
.setHash(ByteString.copyFrom(Hex.decode(block.hash)))
|
||||
)
|
||||
} catch (ex: TimeoutCancellationException) {
|
||||
// It seems like the TimeoutCancellationException
|
||||
// is handled differently so that the timeout is ignored.
|
||||
// Due to that, it's converted to an IOException.
|
||||
|
||||
throw IOException("timeout during requesting block")
|
||||
}
|
||||
}
|
||||
|
||||
NetworkUtils.assertProtocol(response.code == ErrorCode.NO_ERROR) {
|
||||
"received error response, code = ${response.code}"
|
||||
}
|
||||
|
||||
val data = response.data.toByteArray()
|
||||
val hash = Hex.toHexString(MessageDigest.getInstance("SHA-256").digest(data))
|
||||
|
||||
if (hash != block.hash) {
|
||||
throw IllegalStateException("expected block with hash ${block.hash}, but got block with hash $hash")
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
private suspend fun doRequest(request: Request.Builder): BlockExchangeProtos.Response {
|
||||
return suspendCancellableCoroutine { continuation ->
|
||||
val requestId = responseHandler.registerListener { response ->
|
||||
continuation.resume(response)
|
||||
}
|
||||
|
||||
connectionHandler.sendMessage(
|
||||
request
|
||||
.setId(requestId)
|
||||
.build()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class BlockPullerStatus(
|
||||
val downloadedBytes: Long,
|
||||
val totalTransferSize: Long,
|
||||
val totalFileSize: Long
|
||||
)
|
||||
@@ -0,0 +1,308 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
import com.google.protobuf.ByteString
|
||||
import net.syncthing.java.bep.BlockExchangeProtos.Vector
|
||||
import net.syncthing.java.core.beans.*
|
||||
import net.syncthing.java.core.beans.FileInfo.Version
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.core.utils.BlockUtils
|
||||
import net.syncthing.java.core.utils.NetworkUtils
|
||||
import net.syncthing.java.core.utils.submitLogging
|
||||
import org.apache.commons.io.IOUtils
|
||||
import org.apache.commons.lang3.tuple.Pair
|
||||
import org.bouncycastle.util.encoders.Hex
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.Closeable
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.security.MessageDigest
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.ExecutionException
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.Future
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
|
||||
class BlockPusher internal constructor(private val localDeviceId: DeviceId,
|
||||
private val connectionHandler: ConnectionHandler,
|
||||
private val indexHandler: IndexHandler) {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
|
||||
fun pushDelete(folderId: String, targetPath: String): IndexEditObserver {
|
||||
val fileInfo = indexHandler.waitForRemoteIndexAcquired(connectionHandler).getFileInfoByPath(folderId, targetPath)!!
|
||||
NetworkUtils.assertProtocol(connectionHandler.hasFolder(fileInfo.folder), {"supplied connection handler $connectionHandler will not share folder ${fileInfo.folder}"})
|
||||
return IndexEditObserver(sendIndexUpdate(folderId, BlockExchangeProtos.FileInfo.newBuilder()
|
||||
.setName(targetPath)
|
||||
.setType(BlockExchangeProtos.FileInfoType.valueOf(fileInfo.type.name))
|
||||
.setDeleted(true), fileInfo.versionList))
|
||||
}
|
||||
|
||||
fun pushDir(folder: String, path: String): IndexEditObserver {
|
||||
NetworkUtils.assertProtocol(connectionHandler.hasFolder(folder), {"supplied connection handler $connectionHandler will not share folder $folder"})
|
||||
return IndexEditObserver(sendIndexUpdate(folder, BlockExchangeProtos.FileInfo.newBuilder()
|
||||
.setName(path)
|
||||
.setType(BlockExchangeProtos.FileInfoType.DIRECTORY), null))
|
||||
}
|
||||
|
||||
fun pushFile(inputStream: InputStream, folderId: String, targetPath: String): FileUploadObserver {
|
||||
val fileInfo = indexHandler.waitForRemoteIndexAcquired(connectionHandler).getFileInfoByPath(folderId, targetPath)
|
||||
NetworkUtils.assertProtocol(connectionHandler.hasFolder(folderId), {"supplied connection handler $connectionHandler will not share folder $folderId"})
|
||||
assert(fileInfo == null || fileInfo.folder == folderId)
|
||||
assert(fileInfo == null || fileInfo.path == targetPath)
|
||||
val monitoringProcessExecutorService = Executors.newCachedThreadPool()
|
||||
val dataSource = DataSource(inputStream)
|
||||
val fileSize = dataSource.size
|
||||
val sentBlocks = Collections.newSetFromMap(ConcurrentHashMap<String, Boolean>())
|
||||
val uploadError = AtomicReference<Exception>()
|
||||
val isCompleted = AtomicBoolean(false)
|
||||
val updateLock = Object()
|
||||
val listener = {request: BlockExchangeProtos.Request ->
|
||||
if (request.folder == folderId && request.name == targetPath) {
|
||||
val hash = Hex.toHexString(request.hash.toByteArray())
|
||||
logger.debug("handling block request = {}:{}-{} ({})", request.name, request.offset, request.size, hash)
|
||||
val data = dataSource.getBlock(request.offset, request.size, hash)
|
||||
val future = connectionHandler.sendMessage(BlockExchangeProtos.Response.newBuilder()
|
||||
.setCode(BlockExchangeProtos.ErrorCode.NO_ERROR)
|
||||
.setData(ByteString.copyFrom(data))
|
||||
.setId(request.id)
|
||||
.build())
|
||||
monitoringProcessExecutorService.submitLogging {
|
||||
try {
|
||||
future.get()
|
||||
sentBlocks.add(hash)
|
||||
synchronized(updateLock) {
|
||||
updateLock.notifyAll()
|
||||
}
|
||||
//TODO retry on error, register error and throw on watcher
|
||||
} catch (ex: InterruptedException) {
|
||||
//return and do nothing
|
||||
} catch (ex: ExecutionException) {
|
||||
uploadError.set(ex)
|
||||
synchronized(updateLock) {
|
||||
updateLock.notifyAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
connectionHandler.registerOnRequestMessageReceivedListeners(listener)
|
||||
logger.debug("send index update for file = {}", targetPath)
|
||||
val indexListener = { folderInfo: FolderInfo, newRecords: List<FileInfo>, indexInfo: IndexInfo ->
|
||||
if (folderInfo.folderId == folderId) {
|
||||
for (fileInfo2 in newRecords) {
|
||||
if (fileInfo2.path == targetPath && fileInfo2.hash == dataSource.getHash()) { //TODO check not invalid
|
||||
// sentBlocks.addAll(dataSource.getHashes());
|
||||
isCompleted.set(true)
|
||||
synchronized(updateLock) {
|
||||
updateLock.notifyAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
indexHandler.registerOnIndexRecordAcquiredListener(indexListener)
|
||||
val indexUpdate = sendIndexUpdate(folderId, BlockExchangeProtos.FileInfo.newBuilder()
|
||||
.setName(targetPath)
|
||||
.setSize(fileSize)
|
||||
.setType(BlockExchangeProtos.FileInfoType.FILE)
|
||||
.addAllBlocks(dataSource.blocks), fileInfo?.versionList).right
|
||||
return object : FileUploadObserver() {
|
||||
|
||||
override fun progressPercentage() = if (isCompleted.get()) 100 else (sentBlocks.size.toFloat() / dataSource.getHashes().size).toInt()
|
||||
|
||||
// return sentBlocks.size() == dataSource.getHashes().size();
|
||||
override fun isCompleted() = isCompleted.get()
|
||||
|
||||
override fun close() {
|
||||
logger.debug("closing upload process")
|
||||
monitoringProcessExecutorService.shutdown()
|
||||
indexHandler.unregisterOnIndexRecordAcquiredListener(indexListener)
|
||||
connectionHandler.unregisterOnRequestMessageReceivedListeners(listener)
|
||||
val fileInfo1 = indexHandler.pushRecord(indexUpdate.folder, indexUpdate.filesList.single())
|
||||
logger.info("sent file info record = {}", fileInfo1)
|
||||
}
|
||||
|
||||
@Throws(InterruptedException::class, IOException::class)
|
||||
override fun waitForProgressUpdate(): Int {
|
||||
synchronized(updateLock) {
|
||||
updateLock.wait()
|
||||
}
|
||||
if (uploadError.get() != null) {
|
||||
throw IOException(uploadError.get())
|
||||
}
|
||||
return progressPercentage()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendIndexUpdate(folderId: String, fileInfoBuilder: BlockExchangeProtos.FileInfo.Builder,
|
||||
oldVersions: Iterable<Version>?): Pair<Future<*>, BlockExchangeProtos.IndexUpdate> {
|
||||
run {
|
||||
val nextSequence = indexHandler.sequencer().nextSequence()
|
||||
val list = oldVersions ?: emptyList()
|
||||
logger.debug("version list = {}", list)
|
||||
val id = ByteBuffer.wrap(localDeviceId.toHashData()).long
|
||||
val version = BlockExchangeProtos.Counter.newBuilder()
|
||||
.setId(id)
|
||||
.setValue(nextSequence)
|
||||
.build()
|
||||
logger.debug("append new version = {}", version)
|
||||
fileInfoBuilder
|
||||
.setSequence(nextSequence)
|
||||
.setVersion(Vector.newBuilder().addAllCounters(list.map { record ->
|
||||
BlockExchangeProtos.Counter.newBuilder().setId(record.id).setValue(record.value).build()
|
||||
})
|
||||
.addCounters(version))
|
||||
}
|
||||
val lastModified = Date()
|
||||
val fileInfo = fileInfoBuilder
|
||||
.setModifiedS(lastModified.time / 1000)
|
||||
.setModifiedNs((lastModified.time % 1000 * 1000000).toInt())
|
||||
.setNoPermissions(true)
|
||||
.build()
|
||||
val indexUpdate = BlockExchangeProtos.IndexUpdate.newBuilder()
|
||||
.setFolder(folderId)
|
||||
.addFiles(fileInfo)
|
||||
.build()
|
||||
logger.debug("index update = {}", fileInfo)
|
||||
return Pair.of(connectionHandler.sendMessage(indexUpdate), indexUpdate)
|
||||
}
|
||||
|
||||
abstract inner class FileUploadObserver : Closeable {
|
||||
|
||||
abstract fun progressPercentage(): Int
|
||||
|
||||
abstract fun isCompleted(): Boolean
|
||||
|
||||
@Throws(InterruptedException::class)
|
||||
abstract fun waitForProgressUpdate(): Int
|
||||
|
||||
@Throws(InterruptedException::class)
|
||||
fun waitForComplete(): FileUploadObserver {
|
||||
while (!isCompleted()) {
|
||||
waitForProgressUpdate()
|
||||
}
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
||||
inner class IndexEditObserver(private val future: Future<*>, private val indexUpdate: BlockExchangeProtos.IndexUpdate) : Closeable {
|
||||
|
||||
//throw exception if job has errors
|
||||
@Throws(InterruptedException::class, ExecutionException::class)
|
||||
fun isCompleted(): Boolean {
|
||||
return if (future.isDone) {
|
||||
future.get()
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
constructor(pair: Pair<Future<*>, BlockExchangeProtos.IndexUpdate>) : this(pair.left, pair.right)
|
||||
|
||||
@Throws(InterruptedException::class, ExecutionException::class)
|
||||
fun waitForComplete() {
|
||||
future.get()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
override fun close() {
|
||||
indexHandler.pushRecord(indexUpdate.folder, indexUpdate.filesList.single())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class DataSource @Throws(IOException::class) constructor(private val inputStream: InputStream) {
|
||||
|
||||
var size: Long = 0
|
||||
private set
|
||||
lateinit var blocks: List<BlockExchangeProtos.BlockInfo>
|
||||
private set
|
||||
private var hashes: Set<String>? = null
|
||||
|
||||
private var hash: String? = null
|
||||
|
||||
init {
|
||||
inputStream.use { it ->
|
||||
val list = mutableListOf<BlockExchangeProtos.BlockInfo>()
|
||||
var offset: Long = 0
|
||||
while (true) {
|
||||
var block = ByteArray(BLOCK_SIZE)
|
||||
val blockSize = it.read(block)
|
||||
if (blockSize <= 0) {
|
||||
break
|
||||
}
|
||||
if (blockSize < block.size) {
|
||||
block = Arrays.copyOf(block, blockSize)
|
||||
}
|
||||
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(block)
|
||||
list.add(BlockExchangeProtos.BlockInfo.newBuilder()
|
||||
.setHash(ByteString.copyFrom(hash))
|
||||
.setOffset(offset)
|
||||
.setSize(blockSize)
|
||||
.build())
|
||||
offset += blockSize.toLong()
|
||||
}
|
||||
size = offset
|
||||
blocks = list
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun getBlock(offset: Long, size: Int, hash: String): ByteArray {
|
||||
val buffer = ByteArray(size)
|
||||
inputStream.use { it ->
|
||||
IOUtils.skipFully(it, offset)
|
||||
IOUtils.readFully(it, buffer)
|
||||
NetworkUtils.assertProtocol(Hex.toHexString(MessageDigest.getInstance("SHA-256").digest(buffer)) == hash, {"block hash mismatch!"})
|
||||
return buffer
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun getHashes(): Set<String> {
|
||||
return hashes ?: let {
|
||||
val hashes2 = blocks.map { input -> Hex.toHexString(input.hash.toByteArray()) }.toSet()
|
||||
hashes = hashes2
|
||||
return hashes2
|
||||
}
|
||||
}
|
||||
|
||||
fun getHash(): String {
|
||||
return hash ?: let {
|
||||
val blockInfo = blocks.map { input ->
|
||||
BlockInfo(input.offset, input.size, Hex.toHexString(input.hash.toByteArray()))
|
||||
}
|
||||
val hash2 = BlockUtils.hashBlocks(blockInfo)
|
||||
hash = hash2
|
||||
hash2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
const val BLOCK_SIZE = 128 * 1024
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
internal data class ClusterConfigFolderInfo(val folderId: String, var label: String = folderId,
|
||||
var isAnnounced: Boolean = false, var isShared: Boolean = false) {
|
||||
|
||||
init {
|
||||
assert(folderId.isNotEmpty())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,517 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
* Copyright (C) 2018 Jonas Lochmann
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
import com.google.protobuf.ByteString
|
||||
import com.google.protobuf.MessageLite
|
||||
import net.jpountz.lz4.LZ4Factory
|
||||
import net.syncthing.java.bep.BlockExchangeProtos.*
|
||||
import net.syncthing.java.client.protocol.rp.RelayClient
|
||||
import net.syncthing.java.core.beans.DeviceAddress
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.beans.DeviceInfo
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.core.interfaces.TempRepository
|
||||
import net.syncthing.java.core.security.KeystoreHandler
|
||||
import net.syncthing.java.core.utils.NetworkUtils
|
||||
import net.syncthing.java.core.utils.submitLogging
|
||||
import net.syncthing.java.httprelay.HttpRelayClient
|
||||
import org.apache.commons.io.IOUtils
|
||||
import org.apache.commons.lang3.tuple.Pair
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.Closeable
|
||||
import java.io.DataInputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.io.IOException
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.nio.ByteBuffer
|
||||
import java.security.cert.CertificateException
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.Future
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.net.ssl.SSLSocket
|
||||
|
||||
class ConnectionHandler(private val configuration: Configuration, val address: DeviceAddress,
|
||||
private val indexHandler: IndexHandler,
|
||||
private val tempRepository: TempRepository,
|
||||
private val onNewFolderSharedListener: (ConnectionHandler, FolderInfo) -> Unit,
|
||||
private val onConnectionChangedListener: (ConnectionHandler) -> Unit) : Closeable {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
private val outExecutorService = Executors.newSingleThreadExecutor()
|
||||
private val inExecutorService = Executors.newSingleThreadExecutor()
|
||||
private val messageProcessingService = Executors.newCachedThreadPool()
|
||||
private val periodicExecutorService = Executors.newSingleThreadScheduledExecutor()
|
||||
private lateinit var socket: SSLSocket
|
||||
private var inputStream: DataInputStream? = null
|
||||
private var outputStream: DataOutputStream? = null
|
||||
private var lastActive = Long.MIN_VALUE
|
||||
internal var clusterConfigInfo: ClusterConfigInfo? = null
|
||||
private set
|
||||
private val clusterConfigWaitingLock = Object()
|
||||
private val responseHandler = ResponseHandler()
|
||||
private val blockPuller = BlockPuller(this, indexHandler, responseHandler, tempRepository)
|
||||
private val blockPusher = BlockPusher(configuration.localDeviceId, this, indexHandler)
|
||||
private val onRequestMessageReceivedListeners = mutableSetOf<(Request) -> Unit>()
|
||||
private var isClosed = false
|
||||
var isConnected = false
|
||||
private set
|
||||
|
||||
fun deviceId(): DeviceId = address.deviceId()
|
||||
|
||||
private fun checkNotClosed() {
|
||||
NetworkUtils.assertProtocol(!isClosed, {"connection $this closed"})
|
||||
}
|
||||
|
||||
internal fun registerOnRequestMessageReceivedListeners(listener: (Request) -> Unit) {
|
||||
onRequestMessageReceivedListeners.add(listener)
|
||||
}
|
||||
|
||||
internal fun unregisterOnRequestMessageReceivedListeners(listener: (Request) -> Unit) {
|
||||
assert(onRequestMessageReceivedListeners.contains(listener))
|
||||
onRequestMessageReceivedListeners.remove(listener)
|
||||
}
|
||||
|
||||
@Throws(IOException::class, KeystoreHandler.CryptoException::class)
|
||||
fun connect(): ConnectionHandler {
|
||||
checkNotClosed()
|
||||
assert(!isConnected, {"already connected!"})
|
||||
logger.info("connecting to {}", address.address)
|
||||
|
||||
val keystoreHandler = KeystoreHandler.Loader().loadKeystore(configuration)
|
||||
|
||||
socket = when (address.getType()) {
|
||||
DeviceAddress.AddressType.TCP -> {
|
||||
logger.debug("opening tcp ssl connection")
|
||||
keystoreHandler.createSocket(address.getSocketAddress(), KeystoreHandler.BEP)
|
||||
}
|
||||
DeviceAddress.AddressType.RELAY -> {
|
||||
logger.debug("opening relay connection")
|
||||
keystoreHandler.wrapSocket(RelayClient(configuration).openRelayConnection(address), KeystoreHandler.BEP)
|
||||
}
|
||||
DeviceAddress.AddressType.HTTP_RELAY, DeviceAddress.AddressType.HTTPS_RELAY -> {
|
||||
logger.debug("opening http relay connection")
|
||||
keystoreHandler.wrapSocket(HttpRelayClient().openRelayConnection(address), KeystoreHandler.BEP)
|
||||
}
|
||||
else -> throw UnsupportedOperationException("unsupported address type = " + address.getType())
|
||||
}
|
||||
inputStream = DataInputStream(socket.inputStream)
|
||||
outputStream = DataOutputStream(socket.outputStream)
|
||||
|
||||
sendHelloMessage(BlockExchangeProtos.Hello.newBuilder()
|
||||
.setClientName(configuration.clientName)
|
||||
.setClientVersion(configuration.clientVersion)
|
||||
.setDeviceName(configuration.localDeviceName)
|
||||
.build().toByteArray())
|
||||
markActivityOnSocket()
|
||||
|
||||
receiveHelloMessage()
|
||||
try {
|
||||
keystoreHandler.checkSocketCertificate(socket, address.deviceId())
|
||||
} catch (e: CertificateException) {
|
||||
throw IOException(e)
|
||||
}
|
||||
|
||||
run {
|
||||
val clusterConfigBuilder = ClusterConfig.newBuilder()
|
||||
for (folder in configuration.folders) {
|
||||
val folderBuilder = Folder.newBuilder()
|
||||
.setId(folder.folderId)
|
||||
.setLabel(folder.label)
|
||||
run {
|
||||
//our device
|
||||
val deviceBuilder = Device.newBuilder()
|
||||
.setId(ByteString.copyFrom(configuration.localDeviceId.toHashData()))
|
||||
.setIndexId(indexHandler.sequencer().indexId())
|
||||
.setMaxSequence(indexHandler.sequencer().currentSequence())
|
||||
folderBuilder.addDevices(deviceBuilder)
|
||||
}
|
||||
run {
|
||||
//other device
|
||||
val deviceBuilder = Device.newBuilder()
|
||||
.setId(ByteString.copyFrom(DeviceId(address.deviceId).toHashData()))
|
||||
val indexSequenceInfo = indexHandler.indexRepository.findIndexInfoByDeviceAndFolder(address.deviceId(), folder.folderId)
|
||||
indexSequenceInfo?.let {
|
||||
deviceBuilder
|
||||
.setIndexId(indexSequenceInfo.indexId)
|
||||
.setMaxSequence(indexSequenceInfo.localSequence)
|
||||
logger.info("send delta index info device = {} index = {} max (local) sequence = {}",
|
||||
indexSequenceInfo.deviceId,
|
||||
indexSequenceInfo.indexId,
|
||||
indexSequenceInfo.localSequence)
|
||||
}
|
||||
folderBuilder.addDevices(deviceBuilder)
|
||||
}
|
||||
clusterConfigBuilder.addFolders(folderBuilder)
|
||||
//TODO other devices??
|
||||
}
|
||||
sendMessage(clusterConfigBuilder.build())
|
||||
}
|
||||
synchronized(clusterConfigWaitingLock) {
|
||||
startMessageListenerService()
|
||||
while (clusterConfigInfo == null && !isClosed) {
|
||||
logger.debug("wait for cluster config")
|
||||
try {
|
||||
clusterConfigWaitingLock.wait()
|
||||
} catch (e: InterruptedException) {
|
||||
throw IOException(e)
|
||||
}
|
||||
}
|
||||
if (clusterConfigInfo == null) {
|
||||
throw IOException("unable to retrieve cluster config from peer!")
|
||||
}
|
||||
}
|
||||
for (folder in configuration.folders) {
|
||||
if (hasFolder(folder.folderId)) {
|
||||
sendIndexMessage(folder.folderId)
|
||||
}
|
||||
}
|
||||
periodicExecutorService.scheduleWithFixedDelay({ this.sendPing() }, 90, 90, TimeUnit.SECONDS)
|
||||
isConnected = true
|
||||
onConnectionChangedListener(this)
|
||||
return this
|
||||
}
|
||||
|
||||
fun getBlockPuller(): BlockPuller {
|
||||
return blockPuller
|
||||
}
|
||||
|
||||
fun getBlockPusher(): BlockPusher {
|
||||
return blockPusher
|
||||
}
|
||||
|
||||
private fun sendIndexMessage(folderId: String) {
|
||||
sendMessage(Index.newBuilder()
|
||||
.setFolder(folderId)
|
||||
.build())
|
||||
}
|
||||
|
||||
fun closeBg() {
|
||||
Thread { close() }.start()
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive hello message and save device name to configuration.
|
||||
*/
|
||||
@Throws(IOException::class)
|
||||
private fun receiveHelloMessage() {
|
||||
val magic = inputStream!!.readInt()
|
||||
NetworkUtils.assertProtocol(magic == MAGIC, {"magic mismatch, expected $MAGIC, got $magic"})
|
||||
val length = inputStream!!.readShort().toInt()
|
||||
NetworkUtils.assertProtocol(length > 0, {"invalid lenght, must be >0, got $length"})
|
||||
val buffer = ByteArray(length)
|
||||
inputStream!!.readFully(buffer)
|
||||
val hello = BlockExchangeProtos.Hello.parseFrom(buffer)
|
||||
logger.info("Received hello message, deviceName=${hello.deviceName}, clientName=${hello.clientName}, clientVersion=${hello.clientVersion}")
|
||||
configuration.peers = configuration.peers.map { peer ->
|
||||
if (peer.deviceId == deviceId()) {
|
||||
DeviceInfo(deviceId(), hello.deviceName)
|
||||
} else {
|
||||
peer
|
||||
}
|
||||
}.toSet()
|
||||
configuration.persistLater()
|
||||
}
|
||||
|
||||
private fun sendHelloMessage(payload: ByteArray): Future<*> {
|
||||
return outExecutorService.submitLogging {
|
||||
try {
|
||||
logger.debug("Sending hello message")
|
||||
val header = ByteBuffer.allocate(6)
|
||||
header.putInt(MAGIC)
|
||||
header.putShort(payload.size.toShort())
|
||||
outputStream!!.write(header.array())
|
||||
outputStream!!.write(payload)
|
||||
outputStream!!.flush()
|
||||
} catch (ex: IOException) {
|
||||
if (outExecutorService.isShutdown) {
|
||||
return@submitLogging
|
||||
}
|
||||
logger.error("error writing to output stream", ex)
|
||||
closeBg()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendPing(): Future<*> {
|
||||
return sendMessage(Ping.newBuilder().build())
|
||||
}
|
||||
|
||||
private fun markActivityOnSocket() {
|
||||
lastActive = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
@Throws(IOException::class)
|
||||
private fun receiveMessage(): Pair<BlockExchangeProtos.MessageType, MessageLite> {
|
||||
var headerLength = inputStream!!.readShort().toInt()
|
||||
while (headerLength == 0) {
|
||||
logger.warn("got headerLength == 0, skipping short")
|
||||
headerLength = inputStream!!.readShort().toInt()
|
||||
}
|
||||
markActivityOnSocket()
|
||||
NetworkUtils.assertProtocol(headerLength > 0, {"invalid lenght, must be >0, got $headerLength"})
|
||||
val headerBuffer = ByteArray(headerLength)
|
||||
inputStream!!.readFully(headerBuffer)
|
||||
val header = BlockExchangeProtos.Header.parseFrom(headerBuffer)
|
||||
var messageLength = 0
|
||||
while (messageLength == 0) {
|
||||
logger.warn("received readInt() == 0, expecting 'bep message header length' (int >0), ignoring (keepalive?)")
|
||||
messageLength = inputStream!!.readInt()
|
||||
}
|
||||
NetworkUtils.assertProtocol(messageLength >= 0, {"invalid lenght, must be >=0, got $messageLength"})
|
||||
var messageBuffer = ByteArray(messageLength)
|
||||
inputStream!!.readFully(messageBuffer)
|
||||
markActivityOnSocket()
|
||||
if (header.compression == BlockExchangeProtos.MessageCompression.LZ4) {
|
||||
val uncompressedLength = ByteBuffer.wrap(messageBuffer).int
|
||||
messageBuffer = LZ4Factory.fastestInstance().fastDecompressor().decompress(messageBuffer, 4, uncompressedLength)
|
||||
}
|
||||
val messageTypeInfo = messageTypesByProtoMessageType[header.type]
|
||||
NetworkUtils.assertProtocol(messageTypeInfo != null, {"unsupported message type = ${header.type}"})
|
||||
try {
|
||||
val message = messageTypeInfo!!.parseFrom(messageBuffer)
|
||||
return Pair.of(header.type, message)
|
||||
} catch (e: Exception) {
|
||||
when (e) {
|
||||
is IllegalAccessException, is IllegalArgumentException, is InvocationTargetException, is NoSuchMethodException, is SecurityException ->
|
||||
throw IOException(e)
|
||||
else -> throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun sendMessage(message: MessageLite): Future<*> {
|
||||
checkNotClosed()
|
||||
val messageTypeInfo = messageTypesByJavaClass[message.javaClass]
|
||||
messageTypeInfo!!
|
||||
val header = BlockExchangeProtos.Header.newBuilder()
|
||||
.setCompression(BlockExchangeProtos.MessageCompression.NONE)
|
||||
// invert map
|
||||
.setType(messageTypeInfo.protoMessageType)
|
||||
.build()
|
||||
val headerData = header.toByteArray()
|
||||
val messageData = message.toByteArray() //TODO compression
|
||||
return outExecutorService.submit<Any> {
|
||||
try {
|
||||
logger.debug("sending message type = {} {}", header.type, getIdForMessage(message))
|
||||
markActivityOnSocket()
|
||||
outputStream!!.writeShort(headerData.size)
|
||||
outputStream!!.write(headerData)
|
||||
outputStream!!.writeInt(messageData.size)//with compression, check this
|
||||
outputStream!!.write(messageData)
|
||||
outputStream!!.flush()
|
||||
markActivityOnSocket()
|
||||
} catch (ex: IOException) {
|
||||
if (!outExecutorService.isShutdown) {
|
||||
logger.error("error writing to output stream", ex)
|
||||
closeBg()
|
||||
}
|
||||
throw ex
|
||||
}
|
||||
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
if (!isClosed) {
|
||||
sendMessage(Close.getDefaultInstance())
|
||||
isClosed = true
|
||||
isConnected = false
|
||||
periodicExecutorService.shutdown()
|
||||
outExecutorService.shutdown()
|
||||
inExecutorService.shutdown()
|
||||
messageProcessingService.shutdown()
|
||||
assert(onRequestMessageReceivedListeners.isEmpty())
|
||||
if (outputStream != null) {
|
||||
IOUtils.closeQuietly(outputStream)
|
||||
outputStream = null
|
||||
}
|
||||
if (inputStream != null) {
|
||||
IOUtils.closeQuietly(inputStream)
|
||||
inputStream = null
|
||||
}
|
||||
try {
|
||||
IOUtils.closeQuietly(socket)
|
||||
} catch (ex: Exception) {
|
||||
// ignore this
|
||||
// this can throw an exception if socket was not yet initialized/ set
|
||||
// as Kotlin does an check about this, the closeQuietly does not catch it
|
||||
}
|
||||
logger.info("closed connection {}", address)
|
||||
synchronized(clusterConfigWaitingLock) {
|
||||
clusterConfigWaitingLock.notifyAll()
|
||||
}
|
||||
onConnectionChangedListener(this)
|
||||
try {
|
||||
periodicExecutorService.awaitTermination(2, TimeUnit.SECONDS)
|
||||
outExecutorService.awaitTermination(2, TimeUnit.SECONDS)
|
||||
inExecutorService.awaitTermination(2, TimeUnit.SECONDS)
|
||||
messageProcessingService.awaitTermination(2, TimeUnit.SECONDS)
|
||||
} catch (ex: InterruptedException) {
|
||||
logger.warn("", ex)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return time elapsed since last activity on socket, inputStream millis
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
fun getLastActive(): Long {
|
||||
return System.currentTimeMillis() - lastActive
|
||||
}
|
||||
|
||||
private fun startMessageListenerService() {
|
||||
inExecutorService.submitLogging {
|
||||
try {
|
||||
while (!Thread.interrupted()) {
|
||||
val message = receiveMessage()
|
||||
messageProcessingService.submitLogging {
|
||||
logger.debug("received message type = {} {}", message.left, getIdForMessage(message.right))
|
||||
when (message.left) {
|
||||
BlockExchangeProtos.MessageType.INDEX -> {
|
||||
val index = message.value as Index
|
||||
indexHandler.handleIndexMessageReceivedEvent(index.folder, index.filesList, this)
|
||||
}
|
||||
BlockExchangeProtos.MessageType.INDEX_UPDATE -> {
|
||||
val update = message.value as IndexUpdate
|
||||
indexHandler.handleIndexMessageReceivedEvent(update.folder, update.filesList, this)
|
||||
}
|
||||
BlockExchangeProtos.MessageType.REQUEST -> {
|
||||
onRequestMessageReceivedListeners.forEach { it(message.value as Request) }
|
||||
}
|
||||
BlockExchangeProtos.MessageType.RESPONSE -> {
|
||||
responseHandler.handleResponse(message.value as Response)
|
||||
}
|
||||
BlockExchangeProtos.MessageType.PING -> logger.debug("ping message received")
|
||||
BlockExchangeProtos.MessageType.CLOSE -> {
|
||||
val close = message.value as BlockExchangeProtos.Close
|
||||
logger.info("received close message, reason=${close.reason}")
|
||||
closeBg()
|
||||
}
|
||||
BlockExchangeProtos.MessageType.CLUSTER_CONFIG -> {
|
||||
NetworkUtils.assertProtocol(clusterConfigInfo == null, {"received cluster config message twice!"})
|
||||
clusterConfigInfo = ClusterConfigInfo()
|
||||
val clusterConfig = message.value as ClusterConfig
|
||||
for (folder in clusterConfig.foldersList ?: emptyList()) {
|
||||
val folderInfo = ClusterConfigFolderInfo(folder.id, folder.label)
|
||||
val devicesById = (folder.devicesList ?: emptyList())
|
||||
.associateBy { input ->
|
||||
DeviceId.fromHashData(input.id!!.toByteArray())
|
||||
}
|
||||
val otherDevice = devicesById[address.deviceId()]
|
||||
val ourDevice = devicesById[configuration.localDeviceId]
|
||||
if (otherDevice != null) {
|
||||
folderInfo.isAnnounced = true
|
||||
}
|
||||
if (ourDevice != null) {
|
||||
folderInfo.isShared = true
|
||||
logger.info("folder shared from device = {} folder = {}", address.deviceId, folderInfo)
|
||||
val folderIds = configuration.folders.map { it.folderId }
|
||||
if (!folderIds.contains(folderInfo.folderId)) {
|
||||
val fi = FolderInfo(folderInfo.folderId, folderInfo.label)
|
||||
configuration.folders = configuration.folders + fi
|
||||
onNewFolderSharedListener(this, fi)
|
||||
logger.info("new folder shared = {}", folderInfo)
|
||||
}
|
||||
} else {
|
||||
logger.info("folder not shared from device = {} folder = {}", address.deviceId, folderInfo)
|
||||
}
|
||||
clusterConfigInfo!!.putFolderInfo(folderInfo)
|
||||
}
|
||||
configuration.persistLater()
|
||||
indexHandler.handleClusterConfigMessageProcessedEvent(clusterConfig)
|
||||
synchronized(clusterConfigWaitingLock) {
|
||||
clusterConfigWaitingLock.notifyAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (ex: IOException) {
|
||||
if (inExecutorService.isShutdown) {
|
||||
return@submitLogging
|
||||
}
|
||||
logger.error("error receiving message", ex)
|
||||
closeBg()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "ConnectionHandler{" + "address=" + address + ", lastActive=" + getLastActive() / 1000.0 + "secs ago}"
|
||||
}
|
||||
|
||||
internal inner class ClusterConfigInfo {
|
||||
|
||||
private val folderInfoById = ConcurrentHashMap<String, ClusterConfigFolderInfo>()
|
||||
|
||||
fun getSharedFolders(): Set<String> = folderInfoById.values.filter { it.isShared }.map { it.folderId }.toSet()
|
||||
|
||||
fun putFolderInfo(folderInfo: ClusterConfigFolderInfo) {
|
||||
folderInfoById[folderInfo.folderId] = folderInfo
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun hasFolder(folder: String): Boolean {
|
||||
return clusterConfigInfo!!.getSharedFolders().contains(folder)
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val MAGIC = 0x2EA7D90B
|
||||
|
||||
private val messageTypes = listOf(
|
||||
MessageTypeInfo(MessageType.CLOSE, Close::class.java) { Close.parseFrom(it) },
|
||||
MessageTypeInfo(MessageType.CLUSTER_CONFIG, ClusterConfig::class.java) { ClusterConfig.parseFrom(it) },
|
||||
MessageTypeInfo(MessageType.DOWNLOAD_PROGRESS, DownloadProgress::class.java) { DownloadProgress.parseFrom(it) },
|
||||
MessageTypeInfo(MessageType.INDEX, Index::class.java) { Index.parseFrom(it) },
|
||||
MessageTypeInfo(MessageType.INDEX_UPDATE, IndexUpdate::class.java) { IndexUpdate.parseFrom(it) },
|
||||
MessageTypeInfo(MessageType.PING, Ping::class.java) { Ping.parseFrom(it) },
|
||||
MessageTypeInfo(MessageType.REQUEST, Request::class.java) { Request.parseFrom(it) },
|
||||
MessageTypeInfo(MessageType.RESPONSE, Response::class.java) { Response.parseFrom(it) }
|
||||
)
|
||||
|
||||
private val messageTypesByProtoMessageType = messageTypes.map { it.protoMessageType to it }.toMap()
|
||||
private val messageTypesByJavaClass = messageTypes.map { it.javaClass to it }.toMap()
|
||||
|
||||
/**
|
||||
* get id for message bean/instance, for log tracking
|
||||
*
|
||||
* @param message
|
||||
* @return id for message bean
|
||||
*/
|
||||
private fun getIdForMessage(message: MessageLite): String {
|
||||
return when (message) {
|
||||
is Request -> Integer.toString(message.id)
|
||||
is Response -> Integer.toString(message.id)
|
||||
else -> Integer.toString(Math.abs(message.hashCode()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
data class MessageTypeInfo(
|
||||
val protoMessageType: MessageType,
|
||||
val javaClass: Class<out MessageLite>,
|
||||
val parseFrom: (data: ByteArray) -> MessageLite
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.java.core.beans.FolderStats
|
||||
import net.syncthing.java.core.interfaces.IndexRepository
|
||||
import java.io.Closeable
|
||||
|
||||
class FolderBrowser internal constructor(private val indexHandler: IndexHandler) : Closeable {
|
||||
private val folderStatsCache = mutableMapOf<String, FolderStats>()
|
||||
private val indexRepositoryEventListener = { event: IndexRepository.FolderStatsUpdatedEvent ->
|
||||
addFolderStats(event.getFolderStats())
|
||||
}
|
||||
|
||||
fun folderInfoAndStatsList(): List<Pair<FolderInfo, FolderStats>> =
|
||||
indexHandler.folderInfoList()
|
||||
.map { folderInfo -> Pair(folderInfo, getFolderStats(folderInfo.folderId)) }
|
||||
.sortedBy { it.first.label }
|
||||
|
||||
init {
|
||||
indexHandler.indexRepository.setOnFolderStatsUpdatedListener(indexRepositoryEventListener)
|
||||
addFolderStats(indexHandler.indexRepository.findAllFolderStats())
|
||||
}
|
||||
|
||||
private fun addFolderStats(folderStatsList: List<FolderStats>) {
|
||||
for (folderStats in folderStatsList) {
|
||||
folderStatsCache.put(folderStats.folderId, folderStats)
|
||||
}
|
||||
}
|
||||
|
||||
fun getFolderStats(folder: String): FolderStats {
|
||||
return folderStatsCache[folder] ?: let {
|
||||
FolderStats.Builder()
|
||||
.setFolder(folder)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
fun getFolderInfo(folder: String): FolderInfo? {
|
||||
return indexHandler.getFolderInfo(folder)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
indexHandler.indexRepository.setOnFolderStatsUpdatedListener(null)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.java.core.interfaces.IndexRepository
|
||||
import net.syncthing.java.core.utils.PathUtils
|
||||
import net.syncthing.java.core.utils.awaitTerminationSafe
|
||||
import net.syncthing.java.core.utils.submitLogging
|
||||
import org.apache.commons.lang3.StringUtils
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.Closeable
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class IndexBrowser internal constructor(private val indexRepository: IndexRepository, private val indexHandler: IndexHandler,
|
||||
val folder: String, private val includeParentInList: Boolean = false,
|
||||
private val allowParentInRoot: Boolean = false, ordering: Comparator<FileInfo>?) : Closeable {
|
||||
|
||||
private fun isParent(fileInfo: FileInfo) = PathUtils.isParent(fileInfo.path)
|
||||
|
||||
val ALPHA_ASC_DIR_FIRST: Comparator<FileInfo> =
|
||||
compareBy<FileInfo>({!isParent(it)}, {!it.isDirectory()})
|
||||
.thenBy { it.fileName.toLowerCase() }
|
||||
val LAST_MOD_DESC: Comparator<FileInfo> =
|
||||
compareBy<FileInfo>({!isParent(it)}, {it.lastModified})
|
||||
.thenBy { it.fileName.toLowerCase() }
|
||||
|
||||
private val ordering = ordering ?: ALPHA_ASC_DIR_FIRST
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
var currentPath: String = PathUtils.ROOT_PATH
|
||||
private set
|
||||
private val PARENT_FILE_INFO: FileInfo
|
||||
private val ROOT_FILE_INFO: FileInfo
|
||||
private val executorService = Executors.newSingleThreadScheduledExecutor()
|
||||
private val preloadJobs = mutableSetOf<String>()
|
||||
private val preloadJobsLock = Any()
|
||||
private var mOnPathChangedListener: (() -> Unit)? = null
|
||||
|
||||
private fun isCacheReady(): Boolean {
|
||||
synchronized(preloadJobsLock) {
|
||||
return preloadJobs.isEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun onIndexChangedevent(folder: String, newRecord: FileInfo) {
|
||||
if (folder == this.folder) {
|
||||
preloadFileInfoForCurrentPath()
|
||||
}
|
||||
}
|
||||
|
||||
fun currentPathInfo(): FileInfo = getFileInfoByAbsolutePath(currentPath)
|
||||
|
||||
fun currentPathFileName(): String? = PathUtils.getFileName(currentPath)
|
||||
|
||||
fun isRoot(): Boolean = PathUtils.isRoot(currentPath)
|
||||
|
||||
init {
|
||||
assert(folder.isNotEmpty())
|
||||
PARENT_FILE_INFO = FileInfo(folder = folder, type = FileInfo.FileType.DIRECTORY, path = PathUtils.PARENT_PATH)
|
||||
ROOT_FILE_INFO = FileInfo(folder = folder, type = FileInfo.FileType.DIRECTORY, path = PathUtils.ROOT_PATH)
|
||||
navigateToAbsolutePath(PathUtils.ROOT_PATH)
|
||||
}
|
||||
|
||||
fun setOnFolderChangedListener(onPathChangedListener: (() -> Unit)?) {
|
||||
mOnPathChangedListener = onPathChangedListener
|
||||
}
|
||||
|
||||
private fun preloadFileInfoForCurrentPath() {
|
||||
logger.debug("trigger preload for folder = '{}'", folder)
|
||||
synchronized(preloadJobsLock) {
|
||||
currentPath.let<String, Any> { currentPath ->
|
||||
if (preloadJobs.contains(currentPath)) {
|
||||
preloadJobs.remove(currentPath)
|
||||
preloadJobs.add(currentPath) ///add last
|
||||
} else {
|
||||
preloadJobs.add(currentPath)
|
||||
executorService.submitLogging(object : Runnable {
|
||||
|
||||
override fun run() {
|
||||
|
||||
val preloadPath =
|
||||
synchronized(preloadJobsLock) {
|
||||
assert(!preloadJobs.isEmpty())
|
||||
preloadJobs.last() //pop last job
|
||||
}
|
||||
|
||||
logger.info("folder preload BEGIN for folder = '{}' path = '{}'", folder, preloadPath)
|
||||
getFileInfoByAbsolutePath(preloadPath)
|
||||
if (!PathUtils.isRoot(preloadPath)) {
|
||||
val parent = PathUtils.getParentPath(preloadPath)
|
||||
getFileInfoByAbsolutePath(parent)
|
||||
listFiles(parent)
|
||||
}
|
||||
for (record in listFiles(preloadPath)) {
|
||||
if (record.path == PARENT_FILE_INFO.path && record.isDirectory()) {
|
||||
listFiles(record.path)
|
||||
}
|
||||
}
|
||||
logger.info("folder preload END for folder = '{}' path = '{}'", folder, preloadPath)
|
||||
synchronized(preloadJobsLock) {
|
||||
preloadJobs.remove(preloadPath)
|
||||
if (isCacheReady()) {
|
||||
logger.info("cache ready, notify listeners")
|
||||
mOnPathChangedListener?.invoke()
|
||||
} else {
|
||||
logger.info("still {} job[s] left in cache loader", preloadJobs.size)
|
||||
executorService.submitLogging(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun listFiles(path: String = currentPath): List<FileInfo> {
|
||||
logger.debug("doListFiles for path = '{}' BEGIN", path)
|
||||
val list = ArrayList(indexRepository.findNotDeletedFilesByFolderAndParent(folder, path))
|
||||
logger.debug("doListFiles for path = '{}' : {} records loaded)", path, list.size)
|
||||
if (includeParentInList && (!PathUtils.isRoot(path) || allowParentInRoot)) {
|
||||
list.add(0, PARENT_FILE_INFO)
|
||||
}
|
||||
return list.sortedWith(ordering)
|
||||
}
|
||||
|
||||
fun getFileInfoByAbsolutePath(path: String): FileInfo {
|
||||
return if (PathUtils.isRoot(path)) {
|
||||
ROOT_FILE_INFO
|
||||
} else {
|
||||
logger.debug("doGetFileInfoByAbsolutePath for path = '{}' BEGIN", path)
|
||||
val fileInfo = indexRepository.findNotDeletedFileInfo(folder, path) ?: error("file not found for path = $path")
|
||||
logger.debug("doGetFileInfoByAbsolutePath for path = '{}' END", path)
|
||||
fileInfo
|
||||
}
|
||||
}
|
||||
|
||||
fun navigateTo(fileInfo: FileInfo) {
|
||||
assert(fileInfo.isDirectory())
|
||||
assert(fileInfo.folder == folder)
|
||||
return if (fileInfo.path == PARENT_FILE_INFO.path)
|
||||
navigateToAbsolutePath(PathUtils.getParentPath(currentPath))
|
||||
else
|
||||
navigateToAbsolutePath(fileInfo.path)
|
||||
}
|
||||
|
||||
fun navigateToNearestPath(oldPath: String) {
|
||||
if (!StringUtils.isBlank(oldPath)) {
|
||||
navigateToAbsolutePath(oldPath)
|
||||
}
|
||||
}
|
||||
|
||||
private fun navigateToAbsolutePath(newPath: String) {
|
||||
if (PathUtils.isRoot(newPath)) {
|
||||
currentPath = PathUtils.ROOT_PATH
|
||||
} else {
|
||||
val fileInfo = getFileInfoByAbsolutePath(newPath)
|
||||
assert(fileInfo.isDirectory(), {"cannot navigate to path ${fileInfo.path}: not a directory"})
|
||||
currentPath = fileInfo.path
|
||||
}
|
||||
logger.info("navigate to path = '{}'", currentPath)
|
||||
preloadFileInfoForCurrentPath()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
logger.info("closing")
|
||||
indexHandler.unregisterIndexBrowser(this)
|
||||
executorService.shutdown()
|
||||
executorService.awaitTerminationSafe()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,453 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
import net.syncthing.java.core.beans.*
|
||||
import net.syncthing.java.core.beans.FileInfo.Version
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.core.interfaces.IndexRepository
|
||||
import net.syncthing.java.core.interfaces.Sequencer
|
||||
import net.syncthing.java.core.interfaces.TempRepository
|
||||
import net.syncthing.java.core.utils.BlockUtils
|
||||
import net.syncthing.java.core.utils.NetworkUtils
|
||||
import net.syncthing.java.core.utils.awaitTerminationSafe
|
||||
import net.syncthing.java.core.utils.submitLogging
|
||||
import org.apache.commons.lang3.tuple.Pair
|
||||
import org.apache.http.util.TextUtils
|
||||
import org.bouncycastle.util.encoders.Hex
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.Closeable
|
||||
import java.io.IOException
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class IndexHandler(private val configuration: Configuration, val indexRepository: IndexRepository,
|
||||
private val tempRepository: TempRepository) : Closeable {
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
private val folderInfoByFolder = mutableMapOf<String, FolderInfo>()
|
||||
private val indexMessageProcessor = IndexMessageProcessor()
|
||||
private var lastIndexActivity: Long = 0
|
||||
private val writeAccessLock = Object()
|
||||
private val indexWaitLock = Object()
|
||||
private val indexBrowsers = mutableSetOf<IndexBrowser>()
|
||||
private val onIndexRecordAcquiredListeners = mutableSetOf<(FolderInfo, List<FileInfo>, IndexInfo) -> Unit>()
|
||||
private val onFullIndexAcquiredListeners = mutableSetOf<(FolderInfo) -> Unit>()
|
||||
|
||||
private fun lastActive(): Long = System.currentTimeMillis() - lastIndexActivity
|
||||
|
||||
fun sequencer(): Sequencer = indexRepository.getSequencer()
|
||||
|
||||
fun folderList(): List<String> = folderInfoByFolder.keys.toList()
|
||||
|
||||
fun folderInfoList(): List<FolderInfo> = folderInfoByFolder.values.toList()
|
||||
|
||||
private fun markActive() {
|
||||
lastIndexActivity = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
fun registerOnIndexRecordAcquiredListener(listener: (FolderInfo, List<FileInfo>, IndexInfo) -> Unit) {
|
||||
onIndexRecordAcquiredListeners.add(listener)
|
||||
}
|
||||
|
||||
fun unregisterOnIndexRecordAcquiredListener(listener: (FolderInfo, List<FileInfo>, IndexInfo) -> Unit) {
|
||||
assert(onIndexRecordAcquiredListeners.contains(listener))
|
||||
onIndexRecordAcquiredListeners.remove(listener)
|
||||
}
|
||||
|
||||
fun registerOnFullIndexAcquiredListenersListener(listener: (FolderInfo) -> Unit) {
|
||||
onFullIndexAcquiredListeners.add(listener)
|
||||
}
|
||||
|
||||
fun unregisterOnFullIndexAcquiredListenersListener(listener: (FolderInfo) -> Unit) {
|
||||
assert(onFullIndexAcquiredListeners.contains(listener))
|
||||
onFullIndexAcquiredListeners.remove(listener)
|
||||
}
|
||||
|
||||
init {
|
||||
loadFolderInfoFromConfig()
|
||||
}
|
||||
|
||||
private fun loadFolderInfoFromConfig() {
|
||||
synchronized(writeAccessLock) {
|
||||
for (folderInfo in configuration.folders) {
|
||||
folderInfoByFolder.put(folderInfo.folderId, folderInfo) //TODO reference 'folder info' repository
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun clearIndex() {
|
||||
synchronized(writeAccessLock) {
|
||||
indexRepository.clearIndex()
|
||||
folderInfoByFolder.clear()
|
||||
loadFolderInfoFromConfig()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun isRemoteIndexAcquired(clusterConfigInfo: ConnectionHandler.ClusterConfigInfo, peerDeviceId: DeviceId): Boolean {
|
||||
var ready = true
|
||||
for (folder in clusterConfigInfo.getSharedFolders()) {
|
||||
val indexSequenceInfo = indexRepository.findIndexInfoByDeviceAndFolder(peerDeviceId, folder)
|
||||
if (indexSequenceInfo == null || indexSequenceInfo.localSequence < indexSequenceInfo.maxSequence) {
|
||||
logger.debug("waiting for index on folder = {} sequenceInfo = {}", folder, indexSequenceInfo)
|
||||
ready = false
|
||||
}
|
||||
}
|
||||
return ready
|
||||
}
|
||||
|
||||
@Throws(InterruptedException::class)
|
||||
fun waitForRemoteIndexAcquired(connectionHandler: ConnectionHandler, timeoutSecs: Long? = null): IndexHandler {
|
||||
val timeoutMillis = (timeoutSecs ?: DEFAULT_INDEX_TIMEOUT) * 1000
|
||||
synchronized(indexWaitLock) {
|
||||
while (!isRemoteIndexAcquired(connectionHandler.clusterConfigInfo!!, connectionHandler.deviceId())) {
|
||||
indexWaitLock.wait(timeoutMillis)
|
||||
NetworkUtils.assertProtocol(connectionHandler.getLastActive() < timeoutMillis || lastActive() < timeoutMillis,
|
||||
{"unable to acquire index from connection $connectionHandler, timeout reached!"})
|
||||
}
|
||||
}
|
||||
logger.debug("acquired all indexes on connection {}", connectionHandler)
|
||||
return this
|
||||
}
|
||||
|
||||
fun handleClusterConfigMessageProcessedEvent(clusterConfig: BlockExchangeProtos.ClusterConfig) {
|
||||
synchronized(writeAccessLock) {
|
||||
for (folderRecord in clusterConfig.foldersList) {
|
||||
val folder = folderRecord.id
|
||||
val folderInfo = updateFolderInfo(folder, folderRecord.label)
|
||||
logger.debug("acquired folder info from cluster config = {}", folderInfo)
|
||||
for (deviceRecord in folderRecord.devicesList) {
|
||||
val deviceId = DeviceId.fromHashData(deviceRecord.id.toByteArray())
|
||||
if (deviceRecord.hasIndexId() && deviceRecord.hasMaxSequence()) {
|
||||
val folderIndexInfo = updateIndexInfo(folder, deviceId, deviceRecord.indexId, deviceRecord.maxSequence, null)
|
||||
logger.debug("acquired folder index info from cluster config = {}", folderIndexInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun handleIndexMessageReceivedEvent(folderId: String, filesList: List<BlockExchangeProtos.FileInfo>, connectionHandler: ConnectionHandler) {
|
||||
indexMessageProcessor.handleIndexMessageReceivedEvent(folderId, filesList, connectionHandler)
|
||||
}
|
||||
|
||||
fun pushRecord(folder: String, bepFileInfo: BlockExchangeProtos.FileInfo): FileInfo? {
|
||||
var fileBlocks: FileBlocks? = null
|
||||
val builder = FileInfo.Builder()
|
||||
.setFolder(folder)
|
||||
.setPath(bepFileInfo.name)
|
||||
.setLastModified(Date(bepFileInfo.modifiedS * 1000 + bepFileInfo.modifiedNs / 1000000))
|
||||
.setVersionList((if (bepFileInfo.hasVersion()) bepFileInfo.version.countersList else null ?: emptyList()).map { record -> Version(record.id, record.value) })
|
||||
.setDeleted(bepFileInfo.deleted)
|
||||
when (bepFileInfo.type) {
|
||||
BlockExchangeProtos.FileInfoType.FILE -> {
|
||||
fileBlocks = FileBlocks(folder, builder.getPath()!!, ((bepFileInfo.blocksList ?: emptyList())).map { record ->
|
||||
BlockInfo(record.offset, record.size, Hex.toHexString(record.hash.toByteArray()))
|
||||
})
|
||||
builder
|
||||
.setTypeFile()
|
||||
.setHash(fileBlocks.hash)
|
||||
.setSize(bepFileInfo.size)
|
||||
}
|
||||
BlockExchangeProtos.FileInfoType.DIRECTORY -> builder.setTypeDir()
|
||||
else -> {
|
||||
logger.warn("unsupported file type = {}, discarding file info", bepFileInfo.type)
|
||||
return null
|
||||
}
|
||||
}
|
||||
return addRecord(builder.build(), fileBlocks)
|
||||
}
|
||||
|
||||
private fun updateIndexInfo(folder: String, deviceId: DeviceId, indexId: Long?, maxSequence: Long?, localSequence: Long?): IndexInfo {
|
||||
synchronized(writeAccessLock) {
|
||||
var indexSequenceInfo = indexRepository.findIndexInfoByDeviceAndFolder(deviceId, folder)
|
||||
var shouldUpdate = false
|
||||
val builder: IndexInfo.Builder
|
||||
if (indexSequenceInfo == null) {
|
||||
shouldUpdate = true
|
||||
assert(indexId != null, {"index sequence info not found, and supplied null index id (folder = $folder, device = $deviceId)"})
|
||||
builder = IndexInfo.newBuilder()
|
||||
.setFolder(folder)
|
||||
.setDeviceId(deviceId.deviceId)
|
||||
.setIndexId(indexId!!)
|
||||
.setLocalSequence(0)
|
||||
.setMaxSequence(-1)
|
||||
} else {
|
||||
builder = indexSequenceInfo.copyBuilder()
|
||||
}
|
||||
if (indexId != null && indexId != builder.getIndexId()) {
|
||||
shouldUpdate = true
|
||||
builder.setIndexId(indexId)
|
||||
}
|
||||
if (maxSequence != null && maxSequence > builder.getMaxSequence()) {
|
||||
shouldUpdate = true
|
||||
builder.setMaxSequence(maxSequence)
|
||||
}
|
||||
if (localSequence != null && localSequence > builder.getLocalSequence()) {
|
||||
shouldUpdate = true
|
||||
builder.setLocalSequence(localSequence)
|
||||
}
|
||||
if (shouldUpdate) {
|
||||
indexSequenceInfo = builder.build()
|
||||
indexRepository.updateIndexInfo(indexSequenceInfo)
|
||||
}
|
||||
return indexSequenceInfo!!
|
||||
}
|
||||
}
|
||||
|
||||
private fun addRecord(record: FileInfo, fileBlocks: FileBlocks?): FileInfo? {
|
||||
synchronized(writeAccessLock) {
|
||||
val lastModified = indexRepository.findFileInfoLastModified(record.folder, record.path)
|
||||
return if (lastModified != null && !record.lastModified.after(lastModified)) {
|
||||
logger.trace("discarding record = {}, modified before local record", record)
|
||||
null
|
||||
} else {
|
||||
indexRepository.updateFileInfo(record, fileBlocks)
|
||||
logger.trace("loaded new record = {}", record)
|
||||
indexBrowsers.forEach {
|
||||
it.onIndexChangedevent(record.folder, record)
|
||||
}
|
||||
record
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getFileInfoByPath(folder: String, path: String): FileInfo? {
|
||||
return indexRepository.findFileInfo(folder, path)
|
||||
}
|
||||
|
||||
fun getFileInfoAndBlocksByPath(folder: String, path: String): Pair<FileInfo, FileBlocks>? {
|
||||
val fileInfo = getFileInfoByPath(folder, path)
|
||||
return if (fileInfo == null) {
|
||||
null
|
||||
} else {
|
||||
assert(fileInfo.isFile())
|
||||
val fileBlocks = indexRepository.findFileBlocks(folder, path)
|
||||
checkNotNull(fileBlocks, {"file blocks not found for file info = $fileInfo"})
|
||||
|
||||
FileInfo.checkBlocks(fileInfo, fileBlocks!!)
|
||||
|
||||
Pair.of(fileInfo, fileBlocks)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateFolderInfo(folder: String, label: String?): FolderInfo {
|
||||
var folderInfo: FolderInfo? = folderInfoByFolder[folder]
|
||||
if (folderInfo == null || !TextUtils.isEmpty(label)) {
|
||||
folderInfo = FolderInfo(folder, label)
|
||||
folderInfoByFolder.put(folderInfo.folderId, folderInfo)
|
||||
}
|
||||
return folderInfo
|
||||
}
|
||||
|
||||
fun getFolderInfo(folder: String): FolderInfo? {
|
||||
return folderInfoByFolder[folder]
|
||||
}
|
||||
|
||||
fun getIndexInfo(device: DeviceId, folder: String): IndexInfo? {
|
||||
return indexRepository.findIndexInfoByDeviceAndFolder(device, folder)
|
||||
}
|
||||
|
||||
fun newFolderBrowser(): FolderBrowser {
|
||||
return FolderBrowser(this)
|
||||
}
|
||||
|
||||
fun newIndexBrowser(folder: String, includeParentInList: Boolean = false, allowParentInRoot: Boolean = false,
|
||||
ordering: Comparator<FileInfo>? = null): IndexBrowser {
|
||||
val indexBrowser = IndexBrowser(indexRepository, this, folder, includeParentInList, allowParentInRoot, ordering)
|
||||
indexBrowsers.add(indexBrowser)
|
||||
return indexBrowser
|
||||
}
|
||||
|
||||
internal fun unregisterIndexBrowser(indexBrowser: IndexBrowser) {
|
||||
assert(indexBrowsers.contains(indexBrowser))
|
||||
indexBrowsers.remove(indexBrowser)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
assert(indexBrowsers.isEmpty())
|
||||
assert(onIndexRecordAcquiredListeners.isEmpty())
|
||||
assert(onFullIndexAcquiredListeners.isEmpty())
|
||||
indexMessageProcessor.stop()
|
||||
}
|
||||
|
||||
private inner class IndexMessageProcessor {
|
||||
|
||||
private val executorService = Executors.newSingleThreadExecutor()
|
||||
private var queuedMessages = 0
|
||||
private var queuedRecords: Long = 0
|
||||
// private long lastRecordProcessingTime = 0;
|
||||
// , delay = 0;
|
||||
// private boolean addProcessingDelayForInterface = true;
|
||||
// private final int MIN_DELAY = 0, MAX_DELAY = 5000, MAX_RECORD_PER_PROCESS = 16, DELAY_FACTOR = 1;
|
||||
private var startTime: Long? = null
|
||||
|
||||
fun handleIndexMessageReceivedEvent(folderId: String, filesList: List<BlockExchangeProtos.FileInfo>, connectionHandler: ConnectionHandler) {
|
||||
logger.info("received index message event, preparing (queued records = {} event record count = {})", queuedRecords, filesList.size)
|
||||
markActive()
|
||||
val clusterConfigInfo = connectionHandler.clusterConfigInfo
|
||||
val peerDeviceId = connectionHandler.deviceId()
|
||||
// List<BlockExchangeProtos.FileInfo> fileList = event.getFilesList();
|
||||
// for (int index = 0; index < fileList.size(); index += MAX_RECORD_PER_PROCESS) {
|
||||
// BlockExchangeProtos.IndexUpdate data = BlockExchangeProtos.IndexUpdate.newBuilder()
|
||||
// .addAllFiles(Iterables.limit(Iterables.skip(fileList, index), MAX_RECORD_PER_PROCESS))
|
||||
// .setFolder(event.getFolder())
|
||||
// .build();
|
||||
// if (queuedMessages > 0) {
|
||||
// storeAndProcessBg(data, clusterConfigInfo, peerDeviceId);
|
||||
// } else {
|
||||
// processBg(data, clusterConfigInfo, peerDeviceId);
|
||||
// }
|
||||
// }
|
||||
val data = BlockExchangeProtos.IndexUpdate.newBuilder()
|
||||
.addAllFiles(filesList)
|
||||
.setFolder(folderId)
|
||||
.build()
|
||||
if (queuedMessages > 0) {
|
||||
storeAndProcessBg(data, clusterConfigInfo, peerDeviceId)
|
||||
} else {
|
||||
processBg(data, clusterConfigInfo, peerDeviceId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun processBg(data: BlockExchangeProtos.IndexUpdate, clusterConfigInfo: ConnectionHandler.ClusterConfigInfo?, peerDeviceId: DeviceId) {
|
||||
logger.debug("received index message event, queuing for processing")
|
||||
queuedMessages++
|
||||
queuedRecords += data.filesCount.toLong()
|
||||
executorService.submitLogging(object : ProcessingRunnable() {
|
||||
override fun runProcess() {
|
||||
doHandleIndexMessageReceivedEvent(data, clusterConfigInfo, peerDeviceId)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun storeAndProcessBg(data: BlockExchangeProtos.IndexUpdate, clusterConfigInfo: ConnectionHandler.ClusterConfigInfo?, peerDeviceId: DeviceId) {
|
||||
val key = tempRepository.pushTempData(data.toByteArray())
|
||||
logger.debug("received index message event, stored to temp record {}, queuing for processing", key)
|
||||
queuedMessages++
|
||||
queuedRecords += data.filesCount.toLong()
|
||||
executorService.submitLogging(object : ProcessingRunnable() {
|
||||
override fun runProcess() {
|
||||
try {
|
||||
doHandleIndexMessageReceivedEvent(key, clusterConfigInfo, peerDeviceId)
|
||||
} catch (ex: IOException) {
|
||||
logger.error("error processing index message", ex)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
private abstract inner class ProcessingRunnable : Runnable {
|
||||
|
||||
override fun run() {
|
||||
startTime = System.currentTimeMillis()
|
||||
runProcess()
|
||||
queuedMessages--
|
||||
// lastRecordProcessingTime = stopwatch.elapsed(TimeUnit.MILLISECONDS) - delay;
|
||||
// logger.info("processed a bunch of records, {}*{} remaining", queuedMessages, MAX_RECORD_PER_PROCESS);
|
||||
// logger.debug("processed index message in {} secs", lastRecordProcessingTime / 1000d);
|
||||
startTime = null
|
||||
}
|
||||
|
||||
protected abstract fun runProcess()
|
||||
|
||||
// private boolean isVersionOlderThanSequence(BlockExchangeProtos.FileInfo fileInfo, long localSequence) {
|
||||
// long fileSequence = fileInfo.getSequence();
|
||||
// //TODO should we check last version instead of sequence? verify
|
||||
// return fileSequence < localSequence;
|
||||
// }
|
||||
@Throws(IOException::class)
|
||||
protected fun doHandleIndexMessageReceivedEvent(key: String, clusterConfigInfo: ConnectionHandler.ClusterConfigInfo?, peerDeviceId: DeviceId) {
|
||||
logger.debug("processing index message event from temp record {}", key)
|
||||
markActive()
|
||||
val data = tempRepository.popTempData(key)
|
||||
val message = BlockExchangeProtos.IndexUpdate.parseFrom(data)
|
||||
doHandleIndexMessageReceivedEvent(message, clusterConfigInfo, peerDeviceId)
|
||||
}
|
||||
|
||||
protected fun doHandleIndexMessageReceivedEvent(message: BlockExchangeProtos.IndexUpdate, clusterConfigInfo: ConnectionHandler.ClusterConfigInfo?, peerDeviceId: DeviceId) {
|
||||
// synchronized (writeAccessLock) {
|
||||
// if (addProcessingDelayForInterface) {
|
||||
// delay = Math.min(MAX_DELAY, Math.max(MIN_DELAY, lastRecordProcessingTime * DELAY_FACTOR));
|
||||
// logger.info("add delay of {} secs before processing index message (to allow UI to process)", delay / 1000d);
|
||||
// try {
|
||||
// Thread.sleep(delay);
|
||||
// } catch (InterruptedException ex) {
|
||||
// logger.warn("interrupted", ex);
|
||||
// }
|
||||
// } else {
|
||||
// delay = 0;
|
||||
// }
|
||||
logger.info("processing index message with {} records (queue size: messages = {} records = {})", message.filesCount, queuedMessages, queuedRecords)
|
||||
// String deviceId = connectionHandler.getDeviceId();
|
||||
val folderId = message.folder
|
||||
var sequence: Long = -1
|
||||
val newRecords = mutableListOf<FileInfo>()
|
||||
// IndexInfo oldIndexInfo = indexRepository.findIndexInfoByDeviceAndFolder(deviceId, folder);
|
||||
// Stopwatch stopwatch = Stopwatch.createStarted();
|
||||
logger.debug("processing {} index records for folder {}", message.filesList.size, folderId)
|
||||
for (fileInfo in message.filesList) {
|
||||
markActive()
|
||||
// if (oldIndexInfo != null && isVersionOlderThanSequence(fileInfo, oldIndexInfo.getLocalSequence())) {
|
||||
// logger.trace("skipping file {}, version older than sequence {}", fileInfo, oldIndexInfo.getLocalSequence());
|
||||
// } else {
|
||||
val newRecord = pushRecord(folderId, fileInfo)
|
||||
if (newRecord != null) {
|
||||
newRecords.add(newRecord)
|
||||
}
|
||||
sequence = Math.max(fileInfo.sequence, sequence)
|
||||
markActive()
|
||||
// }
|
||||
}
|
||||
val newIndexInfo = updateIndexInfo(folderId, peerDeviceId, null, null, sequence)
|
||||
val elap = System.currentTimeMillis() - startTime!!
|
||||
queuedRecords -= message.filesCount.toLong()
|
||||
logger.info("processed {} index records, acquired {} ({} secs, {} record/sec)", message.filesCount, newRecords.size, elap / 1000.0, Math.round(message.filesCount / (elap / 1000.0) * 100) / 100.0)
|
||||
if (logger.isInfoEnabled && newRecords.size <= 10) {
|
||||
for (fileInfo in newRecords) {
|
||||
logger.info("acquired record = {}", fileInfo)
|
||||
}
|
||||
}
|
||||
val folderInfo = folderInfoByFolder[folderId]
|
||||
if (!newRecords.isEmpty()) {
|
||||
onIndexRecordAcquiredListeners.forEach { it(folderInfo!!, newRecords, newIndexInfo) }
|
||||
}
|
||||
logger.debug("index info = {}", newIndexInfo)
|
||||
if (isRemoteIndexAcquired(clusterConfigInfo!!, peerDeviceId)) {
|
||||
logger.debug("index acquired")
|
||||
onFullIndexAcquiredListeners.forEach { it(folderInfo!!)}
|
||||
}
|
||||
// IndexHandler.this.notifyAll();
|
||||
markActive()
|
||||
synchronized(indexWaitLock) {
|
||||
indexWaitLock.notifyAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
logger.info("stopping index record processor")
|
||||
executorService.shutdown()
|
||||
executorService.awaitTerminationSafe()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val DEFAULT_INDEX_TIMEOUT: Long = 30
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Jonas Lochmann
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.bep
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.collections.HashMap
|
||||
|
||||
class ResponseHandler {
|
||||
companion object {
|
||||
private val logger = LoggerFactory.getLogger(ResponseHandler::class.java)
|
||||
}
|
||||
|
||||
private val responseListeners = Collections.synchronizedMap(HashMap<Int, (BlockExchangeProtos.Response) -> Unit>())
|
||||
private val nextRequestId = AtomicInteger(0)
|
||||
|
||||
fun registerListener(listener: (BlockExchangeProtos.Response) -> Unit): Int {
|
||||
val requestId = nextRequestId.getAndIncrement()
|
||||
|
||||
responseListeners[requestId] = listener
|
||||
|
||||
return requestId
|
||||
}
|
||||
|
||||
fun unregisterListener(requestId: Int) {
|
||||
responseListeners.remove(requestId)
|
||||
}
|
||||
|
||||
fun handleResponse(response: BlockExchangeProtos.Response) {
|
||||
val listener = responseListeners.remove(response.id)
|
||||
|
||||
if (listener != null) {
|
||||
listener(response)
|
||||
} else {
|
||||
logger.warn("received response for {} without associated handler", response.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package net.syncthing.java.bep.utils
|
||||
|
||||
inline fun <T> Iterable<T>.longSumBy(selector: (T) -> Long): Long {
|
||||
var sum = 0L
|
||||
|
||||
this.forEach {
|
||||
sum += selector(it)
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package net.syncthing.java.bep;
|
||||
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
import "blockExchangeProtos.proto";
|
||||
|
||||
message Blocks {
|
||||
repeated BlockInfo blocks = 1;
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package net.syncthing.java.bep;
|
||||
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
message Hello {
|
||||
optional string device_name = 1;
|
||||
optional string client_name = 2;
|
||||
optional string client_version = 3;
|
||||
}
|
||||
|
||||
message Header {
|
||||
optional MessageType type = 1;
|
||||
optional MessageCompression compression = 2;
|
||||
}
|
||||
|
||||
enum MessageType {
|
||||
CLUSTER_CONFIG = 0;
|
||||
INDEX = 1;
|
||||
INDEX_UPDATE = 2;
|
||||
REQUEST = 3;
|
||||
RESPONSE = 4;
|
||||
DOWNLOAD_PROGRESS = 5;
|
||||
PING = 6;
|
||||
CLOSE = 7;
|
||||
}
|
||||
|
||||
enum MessageCompression {
|
||||
NONE = 0;
|
||||
LZ4 = 1;
|
||||
}
|
||||
|
||||
message ClusterConfig {
|
||||
repeated Folder folders = 1;
|
||||
}
|
||||
|
||||
message Folder {
|
||||
optional string id = 1;
|
||||
optional string label = 2;
|
||||
optional bool read_only = 3;
|
||||
optional bool ignore_permissions = 4;
|
||||
optional bool ignore_delete = 5;
|
||||
optional bool disable_temp_indexes = 6;
|
||||
|
||||
repeated Device devices = 16;
|
||||
}
|
||||
|
||||
message Device {
|
||||
optional bytes id = 1;
|
||||
optional string name = 2;
|
||||
repeated string addresses = 3;
|
||||
optional Compression compression = 4;
|
||||
optional string cert_name = 5;
|
||||
optional int64 max_sequence = 6;
|
||||
optional bool introducer = 7;
|
||||
optional uint64 index_id = 8;
|
||||
}
|
||||
|
||||
enum Compression {
|
||||
METADATA = 0;
|
||||
NEVER = 1;
|
||||
ALWAYS = 2;
|
||||
}
|
||||
|
||||
message Index {
|
||||
optional string folder = 1;
|
||||
repeated FileInfo files = 2;
|
||||
}
|
||||
|
||||
message IndexUpdate {
|
||||
optional string folder = 1;
|
||||
repeated FileInfo files = 2;
|
||||
}
|
||||
|
||||
message FileInfo {
|
||||
optional string name = 1;
|
||||
optional FileInfoType type = 2;
|
||||
optional int64 size = 3;
|
||||
optional uint32 permissions = 4;
|
||||
optional int64 modified_s = 5;
|
||||
optional int32 modified_ns = 11;
|
||||
optional uint64 modified_by = 12;
|
||||
optional bool deleted = 6;
|
||||
optional bool invalid = 7;
|
||||
optional bool no_permissions = 8;
|
||||
optional Vector version = 9;
|
||||
optional int64 sequence = 10;
|
||||
|
||||
repeated BlockInfo Blocks = 16;
|
||||
optional string symlink_target = 17;
|
||||
}
|
||||
|
||||
enum FileInfoType {
|
||||
FILE = 0;
|
||||
DIRECTORY = 1;
|
||||
SYMLINK_FILE = 2;
|
||||
SYMLINK_DIRECTORY = 3;
|
||||
SYMLINK = 4;
|
||||
}
|
||||
|
||||
message BlockInfo {
|
||||
optional int64 offset = 1;
|
||||
optional int32 size = 2;
|
||||
optional bytes hash = 3;
|
||||
optional uint32 weak_hash = 4;
|
||||
}
|
||||
|
||||
message Vector {
|
||||
repeated Counter counters = 1;
|
||||
}
|
||||
|
||||
message Counter {
|
||||
optional uint64 id = 1;
|
||||
optional uint64 value = 2;
|
||||
}
|
||||
|
||||
message Request {
|
||||
optional int32 id = 1;
|
||||
optional string folder = 2;
|
||||
optional string name = 3;
|
||||
optional int64 offset = 4;
|
||||
optional int32 size = 5;
|
||||
optional bytes hash = 6;
|
||||
optional bool from_temporary = 7;
|
||||
}
|
||||
|
||||
message Response {
|
||||
optional int32 id = 1;
|
||||
optional bytes data = 2;
|
||||
optional ErrorCode code = 3;
|
||||
}
|
||||
|
||||
enum ErrorCode {
|
||||
NO_ERROR = 0;
|
||||
GENERIC = 1;
|
||||
NO_SUCH_FILE = 2;
|
||||
INVALID_FILE = 3;
|
||||
}
|
||||
|
||||
message DownloadProgress {
|
||||
optional string folder = 1;
|
||||
repeated FileDownloadProgressUpdate updates = 2;
|
||||
}
|
||||
|
||||
message FileDownloadProgressUpdate {
|
||||
optional FileDownloadProgressUpdateType update_type = 1;
|
||||
optional string name = 2;
|
||||
optional Vector version = 3;
|
||||
repeated int32 block_indexes = 4;
|
||||
}
|
||||
|
||||
enum FileDownloadProgressUpdateType {
|
||||
APPEND = 0;
|
||||
FORGET = 1;
|
||||
}
|
||||
|
||||
message Ping {
|
||||
}
|
||||
|
||||
message Close {
|
||||
optional string reason = 1;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
apply plugin: 'application'
|
||||
apply plugin: 'kotlin'
|
||||
mainClassName = 'net.syncthing.java.client.cli.Main'
|
||||
|
||||
dependencies {
|
||||
compile project(':syncthing-client')
|
||||
compile project(':syncthing-repository-default')
|
||||
compile "commons-cli:commons-cli:1.4"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
}
|
||||
|
||||
run {
|
||||
if (project.hasProperty('args')) {
|
||||
args project.args.split('\\s+')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.client.cli
|
||||
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.beans.DeviceInfo
|
||||
import net.syncthing.java.core.beans.FileInfo
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.repository.repo.SqlRepository
|
||||
import net.syncthing.java.client.SyncthingClient
|
||||
import org.apache.commons.cli.*
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
class Main(private val commandLine: CommandLine) {
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val options = generateOptions()
|
||||
val parser = DefaultParser()
|
||||
val cmd = parser.parse(options, args)
|
||||
if (cmd.hasOption("h")) {
|
||||
val formatter = HelpFormatter()
|
||||
formatter.printHelp("s-client", options)
|
||||
return
|
||||
}
|
||||
val configuration = if (cmd.hasOption("C")) Configuration(File(cmd.getOptionValue("C")))
|
||||
else Configuration()
|
||||
|
||||
val repository = SqlRepository(configuration.databaseFolder)
|
||||
|
||||
SyncthingClient(configuration, repository, repository).use { syncthingClient ->
|
||||
val main = Main(cmd)
|
||||
cmd.options.forEach { main.handleOption(it, configuration, syncthingClient) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateOptions(): Options {
|
||||
val options = Options()
|
||||
options.addOption("C", "set-config", true, "set config file for s-client")
|
||||
options.addOption("c", "config", false, "dump config")
|
||||
options.addOption("S", "set-peers", true, "set peer, or comma-separated list of peers")
|
||||
options.addOption("p", "pull", true, "pull file from network")
|
||||
options.addOption("P", "push", true, "push file to network")
|
||||
options.addOption("o", "output", true, "set output file/directory")
|
||||
options.addOption("i", "input", true, "set input file/directory")
|
||||
options.addOption("a", "list-peers", false, "list peer addresses")
|
||||
options.addOption("a", "address", true, "use this peer addresses")
|
||||
options.addOption("L", "list-remote", false, "list folder (root) content from network")
|
||||
options.addOption("I", "list-info", false, "dump folder info from network")
|
||||
options.addOption("l", "list-info", false, "list folder info from local db")
|
||||
options.addOption("D", "delete", true, "push delete to network")
|
||||
options.addOption("M", "mkdir", true, "push directory create to network")
|
||||
options.addOption("h", "help", false, "print help")
|
||||
return options
|
||||
}
|
||||
}
|
||||
|
||||
private val logger = LoggerFactory.getLogger(Main::class.java)
|
||||
|
||||
private fun handleOption(option: Option, configuration: Configuration, syncthingClient: SyncthingClient) {
|
||||
when (option.opt) {
|
||||
"S" -> {
|
||||
val peers = option.value
|
||||
.split(",")
|
||||
.filterNot { it.isEmpty() }
|
||||
.map { DeviceId(it.trim()) }
|
||||
.toList()
|
||||
System.out.println("set peers = $peers")
|
||||
configuration.peers = peers.map { DeviceInfo(it, null) }.toSet()
|
||||
configuration.persistNow()
|
||||
}
|
||||
"p" -> {
|
||||
val folderAndPath = option.value
|
||||
System.out.println("file path = $folderAndPath")
|
||||
val folder = folderAndPath.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0]
|
||||
val path = folderAndPath.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1]
|
||||
val latch = CountDownLatch(1)
|
||||
val fileInfo = FileInfo(folder = folder, path = path, type = FileInfo.FileType.FILE)
|
||||
syncthingClient.getBlockPuller(folder, { blockPuller ->
|
||||
try {
|
||||
val inputStream = blockPuller.pullFileSync(fileInfo)
|
||||
val fileName = syncthingClient.indexHandler.getFileInfoByPath(folder, path)!!.fileName
|
||||
val file =
|
||||
if (commandLine.hasOption("o")) {
|
||||
val param = File(commandLine.getOptionValue("o"))
|
||||
if (param.isDirectory) File(param, fileName) else param
|
||||
} else {
|
||||
File(fileName)
|
||||
}
|
||||
FileUtils.copyInputStreamToFile(inputStream, file)
|
||||
System.out.println("saved file to = $file.absolutePath")
|
||||
} catch (e: InterruptedException) {
|
||||
logger.warn("", e)
|
||||
} catch (e: IOException) {
|
||||
logger.warn("", e)
|
||||
}
|
||||
}, { logger.warn("Failed to pull file") })
|
||||
latch.await()
|
||||
}
|
||||
"P" -> {
|
||||
var path = option.value
|
||||
val file = File(commandLine.getOptionValue("i"))
|
||||
assert(!path.startsWith("/")) //TODO check path syntax
|
||||
System.out.println("file path = $path")
|
||||
val folder = path.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0]
|
||||
path = path.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1]
|
||||
val latch = CountDownLatch(1)
|
||||
syncthingClient.getBlockPusher(folder, { blockPusher ->
|
||||
val observer = blockPusher.pushFile(FileInputStream(file), folder, path)
|
||||
while (!observer.isCompleted()) {
|
||||
try {
|
||||
observer.waitForProgressUpdate()
|
||||
} catch (e: InterruptedException) {
|
||||
logger.warn("", e)
|
||||
}
|
||||
|
||||
System.out.println("upload progress ${observer.progressPercentage()}%")
|
||||
}
|
||||
latch.countDown()
|
||||
}, { logger.warn("Failed to upload file") })
|
||||
latch.await()
|
||||
System.out.println("uploaded file to network")
|
||||
}
|
||||
"D" -> {
|
||||
var path = option.value
|
||||
val folder = path.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0]
|
||||
path = path.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1]
|
||||
System.out.println("delete path = $path")
|
||||
val latch = CountDownLatch(1)
|
||||
syncthingClient.getBlockPusher(folder, { blockPusher ->
|
||||
try {
|
||||
blockPusher.pushDelete(folder, path).waitForComplete()
|
||||
} catch (e: InterruptedException) {
|
||||
logger.warn("", e)
|
||||
}
|
||||
|
||||
latch.countDown()
|
||||
}, { System.out.println("Failed to delete path") })
|
||||
latch.await()
|
||||
System.out.println("deleted path")
|
||||
}
|
||||
"M" -> {
|
||||
var path = option.value
|
||||
val folder = path.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0]
|
||||
path = path.split(":".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[1]
|
||||
System.out.println("dir path = $path")
|
||||
val latch = CountDownLatch(1)
|
||||
syncthingClient.getBlockPusher(folder, { blockPusher ->
|
||||
try {
|
||||
blockPusher.pushDir(folder, path).waitForComplete()
|
||||
} catch (e: InterruptedException) {
|
||||
logger.warn("", e)
|
||||
}
|
||||
|
||||
latch.countDown()
|
||||
}, { System.out.println("Failed to push directory") })
|
||||
latch.await()
|
||||
System.out.println("uploaded dir to network")
|
||||
}
|
||||
"L" -> {
|
||||
waitForIndexUpdate(syncthingClient, configuration)
|
||||
for (folder in syncthingClient.indexHandler.folderList()) {
|
||||
syncthingClient.indexHandler.newIndexBrowser(folder).use { indexBrowser ->
|
||||
System.out.println("list folder = ${indexBrowser.folder}")
|
||||
for (fileInfo in indexBrowser.listFiles()) {
|
||||
System.out.println("${fileInfo.type.name.substring(0, 1)}\t${fileInfo.describeSize()}\t${fileInfo.path}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"I" -> {
|
||||
waitForIndexUpdate(syncthingClient, configuration)
|
||||
val folderInfo = StringBuilder()
|
||||
for (folder in syncthingClient.indexHandler.folderList()) {
|
||||
folderInfo.append("\nfolder info: ")
|
||||
.append(syncthingClient.indexHandler.getFolderInfo(folder))
|
||||
folderInfo.append("\nfolder stats: ")
|
||||
.append(syncthingClient.indexHandler.newFolderBrowser().getFolderStats(folder).dumpInfo())
|
||||
.append("\n")
|
||||
}
|
||||
System.out.println("folders:\n$folderInfo\n")
|
||||
}
|
||||
"l" -> {
|
||||
var folderInfo = ""
|
||||
for (folder in syncthingClient.indexHandler.folderList()) {
|
||||
folderInfo += "\nfolder info: " + syncthingClient.indexHandler.getFolderInfo(folder)
|
||||
folderInfo += "\nfolder stats: " + syncthingClient.indexHandler.newFolderBrowser().getFolderStats(folder).dumpInfo() + "\n"
|
||||
}
|
||||
System.out.println("folders:\n$folderInfo\n")
|
||||
}
|
||||
"a" -> {
|
||||
val deviceAddressSupplier = syncthingClient.discoveryHandler.newDeviceAddressSupplier()
|
||||
var deviceAddressesStr = ""
|
||||
for (deviceAddress in deviceAddressSupplier.toList()) {
|
||||
deviceAddressesStr += "\n" + deviceAddress?.deviceId + " : " + deviceAddress?.address
|
||||
}
|
||||
System.out.println("device addresses:\n$deviceAddressesStr\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(InterruptedException::class)
|
||||
private fun waitForIndexUpdate(client: SyncthingClient, configuration: Configuration) {
|
||||
val latch = CountDownLatch(configuration.peers.size)
|
||||
client.indexHandler.registerOnFullIndexAcquiredListenersListener {
|
||||
latch.countDown()
|
||||
}
|
||||
latch.await()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'kotlin'
|
||||
|
||||
dependencies {
|
||||
compile project(':syncthing-core')
|
||||
compile project(':syncthing-bep')
|
||||
compile project(':syncthing-discovery')
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.client
|
||||
|
||||
import net.syncthing.java.bep.BlockPuller
|
||||
import net.syncthing.java.bep.BlockPusher
|
||||
import net.syncthing.java.bep.ConnectionHandler
|
||||
import net.syncthing.java.bep.IndexHandler
|
||||
import net.syncthing.java.core.beans.DeviceAddress
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.beans.DeviceInfo
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.core.interfaces.IndexRepository
|
||||
import net.syncthing.java.core.interfaces.TempRepository
|
||||
import net.syncthing.java.core.security.KeystoreHandler
|
||||
import net.syncthing.java.core.utils.awaitTerminationSafe
|
||||
import net.syncthing.java.discovery.DiscoveryHandler
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.Closeable
|
||||
import java.io.IOException
|
||||
import java.util.Collections
|
||||
import java.util.TreeSet
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.TimeUnit
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class SyncthingClient(
|
||||
private val configuration: Configuration,
|
||||
private val repository: IndexRepository,
|
||||
private val tempRepository: TempRepository
|
||||
) : Closeable {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
val discoveryHandler: DiscoveryHandler
|
||||
val indexHandler: IndexHandler
|
||||
private val connections = Collections.synchronizedSet(createConnectionsSet())
|
||||
private val connectByDeviceIdLocks = Collections.synchronizedMap(HashMap<DeviceId, Object>())
|
||||
private val onConnectionChangedListeners = Collections.synchronizedList(mutableListOf<(DeviceId) -> Unit>())
|
||||
private var connectDevicesScheduler = Executors.newSingleThreadScheduledExecutor()
|
||||
|
||||
private fun createConnectionsSet() = TreeSet<ConnectionHandler>(compareBy { it.address.score })
|
||||
|
||||
init {
|
||||
indexHandler = IndexHandler(configuration, repository, tempRepository)
|
||||
discoveryHandler = DiscoveryHandler(configuration)
|
||||
connectDevicesScheduler.scheduleAtFixedRate(this::updateIndexFromPeers, 0, 15, TimeUnit.SECONDS)
|
||||
}
|
||||
|
||||
fun clearCacheAndIndex() {
|
||||
indexHandler.clearIndex()
|
||||
configuration.folders = emptySet()
|
||||
configuration.persistLater()
|
||||
updateIndexFromPeers()
|
||||
}
|
||||
|
||||
fun addOnConnectionChangedListener(listener: (DeviceId) -> Unit) {
|
||||
onConnectionChangedListeners.add(listener)
|
||||
}
|
||||
|
||||
fun removeOnConnectionChangedListener(listener: (DeviceId) -> Unit) {
|
||||
assert(onConnectionChangedListeners.contains(listener))
|
||||
onConnectionChangedListeners.remove(listener)
|
||||
}
|
||||
|
||||
@Throws(IOException::class, KeystoreHandler.CryptoException::class)
|
||||
private fun openConnection(deviceAddress: DeviceAddress): ConnectionHandler {
|
||||
logger.debug("Connecting to ${deviceAddress.deviceId}, active connections: ${connections.map { it.deviceId().deviceId }}")
|
||||
val connectionHandler = ConnectionHandler(
|
||||
configuration, deviceAddress, indexHandler, tempRepository, { connectionHandler, _ ->
|
||||
connectionHandler.close()
|
||||
openConnection(deviceAddress)
|
||||
},
|
||||
{connection ->
|
||||
if (!connection.isConnected) {
|
||||
connections.remove(connection)
|
||||
}
|
||||
onConnectionChangedListeners.forEach { it(connection.deviceId()) }
|
||||
})
|
||||
|
||||
try {
|
||||
connectionHandler.connect()
|
||||
} catch (ex: Exception) {
|
||||
connectionHandler.closeBg()
|
||||
|
||||
throw ex
|
||||
}
|
||||
|
||||
connections.add(connectionHandler)
|
||||
|
||||
return connectionHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes discovered addresses from [[DiscoveryHandler]] and connects to devices.
|
||||
*
|
||||
* We need to make sure that we are only connecting once to each device.
|
||||
*/
|
||||
private fun getPeerConnections(listener: (connection: ConnectionHandler) -> Unit, completeListener: () -> Unit) {
|
||||
// create an copy to prevent dispatching an action two times
|
||||
val connectionsWhichWereDispatched = createConnectionsSet()
|
||||
|
||||
synchronized (connections) {
|
||||
connectionsWhichWereDispatched.addAll(connections)
|
||||
}
|
||||
|
||||
connectionsWhichWereDispatched.forEach { listener(it) }
|
||||
|
||||
discoveryHandler.newDeviceAddressSupplier()
|
||||
.takeWhile { it != null }
|
||||
.filterNotNull()
|
||||
.groupBy { it.deviceId() }
|
||||
.filterNot { it.value.isEmpty() }
|
||||
.forEach { (deviceId, addresses) ->
|
||||
// create an lock per device id to prevent multiple connections to one device
|
||||
|
||||
synchronized (connectByDeviceIdLocks) {
|
||||
if (connectByDeviceIdLocks[deviceId] == null) {
|
||||
connectByDeviceIdLocks[deviceId] = Object()
|
||||
}
|
||||
}
|
||||
|
||||
synchronized (connectByDeviceIdLocks[deviceId]!!) {
|
||||
val existingConnection = connections.find { it.deviceId() == deviceId && it.isConnected }
|
||||
|
||||
if (existingConnection != null) {
|
||||
connectionsWhichWereDispatched.add(existingConnection)
|
||||
listener(existingConnection)
|
||||
|
||||
return@synchronized
|
||||
}
|
||||
|
||||
// try to use all addresses
|
||||
for (address in addresses.distinctBy { it.address }) {
|
||||
try {
|
||||
val newConnection = openConnection(address)
|
||||
|
||||
connectionsWhichWereDispatched.add(newConnection)
|
||||
listener(newConnection)
|
||||
|
||||
break // it worked, no need to try more
|
||||
} catch (e: IOException) {
|
||||
logger.warn("error connecting to device = $address", e)
|
||||
} catch (e: KeystoreHandler.CryptoException) {
|
||||
logger.warn("error connecting to device = $address", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// use all connections which were added in the time between and were not added by this function call
|
||||
val newConnectionsBackup = createConnectionsSet()
|
||||
|
||||
synchronized (connections) {
|
||||
newConnectionsBackup.addAll(connections)
|
||||
}
|
||||
|
||||
connectionsWhichWereDispatched.forEach { newConnectionsBackup.remove(it) }
|
||||
|
||||
newConnectionsBackup.forEach { listener(it) }
|
||||
|
||||
completeListener()
|
||||
}
|
||||
|
||||
private fun updateIndexFromPeers() {
|
||||
getPeerConnections({ connection ->
|
||||
try {
|
||||
indexHandler.waitForRemoteIndexAcquired(connection)
|
||||
} catch (ex: InterruptedException) {
|
||||
logger.warn("exception while waiting for index", ex)
|
||||
}
|
||||
}, {})
|
||||
}
|
||||
|
||||
private fun getConnectionForFolder(folder: String, listener: (connection: ConnectionHandler) -> Unit,
|
||||
errorListener: () -> Unit) {
|
||||
val isConnected = AtomicBoolean(false)
|
||||
getPeerConnections({ connection ->
|
||||
if (connection.hasFolder(folder) && !isConnected.get()) {
|
||||
listener(connection)
|
||||
isConnected.set(true)
|
||||
}
|
||||
}, {
|
||||
if (!isConnected.get()) {
|
||||
errorListener()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun getBlockPuller(folderId: String, listener: (BlockPuller) -> Unit, errorListener: () -> Unit) {
|
||||
getConnectionForFolder(folderId, { connection ->
|
||||
listener(connection.getBlockPuller())
|
||||
}, errorListener)
|
||||
}
|
||||
|
||||
fun getBlockPusher(folderId: String, listener: (BlockPusher) -> Unit, errorListener: () -> Unit) {
|
||||
getConnectionForFolder(folderId, { connection ->
|
||||
listener(connection.getBlockPusher())
|
||||
}, errorListener)
|
||||
}
|
||||
|
||||
fun getPeerStatus(): List<DeviceInfo> {
|
||||
return configuration.peers.map { device ->
|
||||
val isConnected = connections.find { it.deviceId() == device.deviceId }?.isConnected ?: false
|
||||
device.copy(isConnected = isConnected)
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
connectDevicesScheduler.awaitTerminationSafe()
|
||||
discoveryHandler.close()
|
||||
// Create copy of list, because it will be modified by handleConnectionClosedEvent(), causing ConcurrentModificationException.
|
||||
ArrayList(connections).forEach{it.close()}
|
||||
indexHandler.close()
|
||||
repository.close()
|
||||
tempRepository.close()
|
||||
assert(onConnectionChangedListeners.isEmpty())
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'kotlin'
|
||||
|
||||
dependencies {
|
||||
compile "org.apache.commons:commons-lang3:3.7"
|
||||
compile 'commons-codec:commons-codec:1.11'
|
||||
// Can't upgrade to 2.6 because it crashes on Android
|
||||
compile "commons-io:commons-io:2.5"
|
||||
compile "org.slf4j:slf4j-api:1.7.25"
|
||||
compile "ch.qos.logback:logback-classic:1.2.3"
|
||||
compile "com.google.code.gson:gson:2.8.2"
|
||||
compile "org.apache.httpcomponents:httpclient:4.5.4"
|
||||
compile "org.bouncycastle:bcmail-jdk15on:1.59"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
data class BlockInfo(val offset: Long, val size: Int, val hash: String)
|
||||
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.net.InetAddress
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.UnknownHostException
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
*
|
||||
* TODO: this class cant use [[DeviceId]] because [[GlobalDiscoveryHandler.pickAnnounceServers]] uses that field for discovery server URLs.
|
||||
*/
|
||||
class DeviceAddress private constructor(val deviceId: String, private val instanceId: Long?, val address: String, producer: AddressProducer?, score: Int?, lastModified: Date?) {
|
||||
private val producer = producer ?: AddressProducer.UNKNOWN
|
||||
val score = score ?: Integer.MAX_VALUE
|
||||
private val lastModified = lastModified ?: Date()
|
||||
|
||||
fun deviceId() = DeviceId(deviceId)
|
||||
|
||||
@Throws(UnknownHostException::class)
|
||||
private fun getInetAddress(): InetAddress = InetAddress.getByName(address.replaceFirst("^[^:]+://".toRegex(), "").replaceFirst("(:[0-9]+)?(/.*)?$".toRegex(), ""))
|
||||
|
||||
private fun getPort(): Int = if (address.matches("^[a-z]+://[^:]+:([0-9]+).*".toRegex())) {
|
||||
Integer.parseInt(address.replaceFirst("^[a-z]+://[^:]+:([0-9]+).*".toRegex(), "$1"))
|
||||
} else {
|
||||
DEFAULT_PORT_BY_PROTOCOL[getType()]!!
|
||||
}
|
||||
|
||||
fun getType(): AddressType = when {
|
||||
address.isEmpty() -> AddressType.NULL
|
||||
address.startsWith("tcp://") -> AddressType.TCP
|
||||
address.startsWith("relay://") -> AddressType.RELAY
|
||||
address.startsWith("relay-http://") -> AddressType.HTTP_RELAY
|
||||
address.startsWith("relay-https://") -> AddressType.HTTPS_RELAY
|
||||
else -> AddressType.OTHER
|
||||
}
|
||||
|
||||
@Throws(UnknownHostException::class)
|
||||
fun getSocketAddress(): InetSocketAddress = InetSocketAddress(getInetAddress(), getPort())
|
||||
|
||||
fun isWorking(): Boolean = score < Integer.MAX_VALUE
|
||||
|
||||
constructor(deviceId: String, address: String) : this(deviceId, null, address, null, null, null)
|
||||
|
||||
fun containsUriParamValue(key: String): Boolean {
|
||||
return !getUriParam(key).isNullOrEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns value for the specified URL parameter key.
|
||||
*
|
||||
* We need to parse the URL manually, as it is not URL encoded and may contain invalid key/values
|
||||
* like "key=a b" (with an unencoded space).
|
||||
*/
|
||||
fun getUriParam(key: String): String? {
|
||||
assert(!key.isEmpty())
|
||||
return address
|
||||
.split("?", limit = 2).first()
|
||||
.splitToSequence("&")
|
||||
.map { it.split("=", limit = 2) }
|
||||
.map { it[0] to (it.getOrNull(1) ?: "") }
|
||||
.find { it.first == key }
|
||||
?.second
|
||||
}
|
||||
|
||||
enum class AddressType {
|
||||
TCP, RELAY, OTHER, NULL, HTTP_RELAY, HTTPS_RELAY
|
||||
}
|
||||
|
||||
enum class AddressProducer {
|
||||
LOCAL_DISCOVERY, GLOBAL_DISCOVERY, UNKNOWN
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "DeviceAddress(deviceId=$deviceId, instanceId=$instanceId, address=$address, producer=$producer, score=$score, lastModified=$lastModified)"
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var hash = 3
|
||||
hash = 29 * hash + Objects.hashCode(this.deviceId)
|
||||
hash = 29 * hash + Objects.hashCode(this.address)
|
||||
return hash
|
||||
}
|
||||
|
||||
override fun equals(obj: Any?): Boolean {
|
||||
if (this === obj) {
|
||||
return true
|
||||
}
|
||||
if (obj == null) {
|
||||
return false
|
||||
}
|
||||
if (javaClass != obj.javaClass) {
|
||||
return false
|
||||
}
|
||||
val other = obj as DeviceAddress?
|
||||
if (this.deviceId != other!!.deviceId) {
|
||||
return false
|
||||
}
|
||||
return this.address == other.address
|
||||
}
|
||||
|
||||
fun copyBuilder(): Builder {
|
||||
return Builder(deviceId, instanceId, address, producer, score, lastModified)
|
||||
}
|
||||
|
||||
class Builder {
|
||||
|
||||
private var deviceId: String? = null
|
||||
private var instanceId: Long? = null
|
||||
private var address: String? = null
|
||||
private var producer: AddressProducer? = null
|
||||
private var score: Int? = null
|
||||
private var lastModified: Date? = null
|
||||
|
||||
constructor()
|
||||
|
||||
internal constructor(deviceId: String, instanceId: Long?, address: String, producer: AddressProducer, score: Int?, lastModified: Date) {
|
||||
this.deviceId = deviceId
|
||||
this.instanceId = instanceId
|
||||
this.address = address
|
||||
this.producer = producer
|
||||
this.score = score
|
||||
this.lastModified = lastModified
|
||||
}
|
||||
|
||||
fun getLastModified(): Date? {
|
||||
return lastModified
|
||||
}
|
||||
|
||||
fun setLastModified(lastModified: Date): Builder {
|
||||
this.lastModified = lastModified
|
||||
return this
|
||||
}
|
||||
|
||||
fun getDeviceId(): String? {
|
||||
return deviceId
|
||||
}
|
||||
|
||||
fun setDeviceId(deviceId: String): Builder {
|
||||
this.deviceId = deviceId
|
||||
return this
|
||||
}
|
||||
|
||||
fun getInstanceId(): Long? {
|
||||
return instanceId
|
||||
}
|
||||
|
||||
fun setInstanceId(instanceId: Long?): Builder {
|
||||
this.instanceId = instanceId
|
||||
return this
|
||||
}
|
||||
|
||||
fun getAddress(): String? {
|
||||
return address
|
||||
}
|
||||
|
||||
fun setAddress(address: String): Builder {
|
||||
this.address = address
|
||||
return this
|
||||
}
|
||||
|
||||
fun getProducer(): AddressProducer? {
|
||||
return producer
|
||||
}
|
||||
|
||||
fun setProducer(producer: AddressProducer): Builder {
|
||||
this.producer = producer
|
||||
return this
|
||||
}
|
||||
|
||||
fun getScore(): Int? {
|
||||
return score
|
||||
}
|
||||
|
||||
fun setScore(score: Int?): Builder {
|
||||
this.score = score
|
||||
return this
|
||||
}
|
||||
|
||||
fun build(): DeviceAddress {
|
||||
return DeviceAddress(deviceId!!, instanceId, address!!, producer, score, lastModified)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val DEFAULT_PORT_BY_PROTOCOL = mapOf(
|
||||
AddressType.TCP to 22000,
|
||||
AddressType.RELAY to 22067,
|
||||
AddressType.HTTP_RELAY to 80,
|
||||
AddressType.HTTPS_RELAY to 443)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import net.syncthing.java.core.utils.NetworkUtils
|
||||
import org.apache.commons.codec.binary.Base32
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.IOException
|
||||
|
||||
data class DeviceId @Throws(IOException::class) constructor(val deviceId: String) {
|
||||
|
||||
init {
|
||||
val withoutDashes = this.deviceId.replace("-", "")
|
||||
NetworkUtils.assertProtocol(DeviceId.fromHashDataToString(toHashData()) == withoutDashes)
|
||||
}
|
||||
|
||||
val shortId
|
||||
get() = deviceId.substring(0, 7)
|
||||
|
||||
fun toHashData(): ByteArray {
|
||||
NetworkUtils.assertProtocol(deviceId.matches("^[A-Z0-9]{7}-[A-Z0-9]{7}-[A-Z0-9]{7}-[A-Z0-9]{7}-[A-Z0-9]{7}-[A-Z0-9]{7}-[A-Z0-9]{7}-[A-Z0-9]{7}$".toRegex()), {"device id syntax error for deviceId = $deviceId"})
|
||||
val base32data = deviceId.replaceFirst("(.{7})-(.{6}).-(.{7})-(.{6}).-(.{7})-(.{6}).-(.{7})-(.{6}).".toRegex(), "$1$2$3$4$5$6$7$8") + "==="
|
||||
val binaryData = Base32().decode(base32data)
|
||||
NetworkUtils.assertProtocol(binaryData.size == SHA256_BYTES)
|
||||
return binaryData
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val DEVICE_ID = "deviceId"
|
||||
|
||||
private const val SHA256_BYTES = 256 / 8
|
||||
|
||||
private fun fromHashDataToString(hashData: ByteArray): String {
|
||||
NetworkUtils.assertProtocol(hashData.size == SHA256_BYTES)
|
||||
val string = Base32().encodeAsString(hashData).replace("=", "")
|
||||
return string.chunked(13).joinToString("") { part -> part + generateLuhn32Checksum(part) }
|
||||
}
|
||||
|
||||
fun fromHashData(hashData: ByteArray): DeviceId {
|
||||
return DeviceId(fromHashDataToString(hashData).chunked(7).joinToString("-"))
|
||||
}
|
||||
|
||||
private fun generateLuhn32Checksum(string: String): Char {
|
||||
val alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
|
||||
var factor = 1
|
||||
var sum = 0
|
||||
val n = alphabet.length
|
||||
for (character in string.toCharArray()) {
|
||||
val index = alphabet.indexOf(character)
|
||||
NetworkUtils.assertProtocol(index >= 0)
|
||||
var add = factor * index
|
||||
factor = if (factor == 2) 1 else 2
|
||||
add = add / n + add % n
|
||||
sum += add
|
||||
}
|
||||
val remainder = sum % n
|
||||
val check = (n - remainder) % n
|
||||
return alphabet[check]
|
||||
}
|
||||
|
||||
fun parse(reader: JsonReader): DeviceId {
|
||||
var deviceId: String? = null
|
||||
|
||||
reader.beginObject()
|
||||
while (reader.hasNext()) {
|
||||
when (reader.nextName()) {
|
||||
DEVICE_ID -> deviceId = reader.nextString()
|
||||
else -> reader.skipValue()
|
||||
}
|
||||
}
|
||||
reader.endObject()
|
||||
|
||||
return DeviceId(deviceId!!)
|
||||
}
|
||||
}
|
||||
|
||||
fun serialize(writer: JsonWriter) {
|
||||
writer.beginObject()
|
||||
writer.name(DEVICE_ID).value(deviceId)
|
||||
writer.endObject()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2016 Davide Imbriaco <davide.imbriaco@gmail.com>
|
||||
* Copyright 2018 Jonas Lochmann
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
|
||||
data class DeviceInfo(val deviceId: DeviceId, val name: String, val isConnected: Boolean? = null) {
|
||||
|
||||
companion object {
|
||||
private const val DEVICE_ID = "deviceId"
|
||||
private const val NAME = "name"
|
||||
|
||||
fun parse(reader: JsonReader): DeviceInfo {
|
||||
var deviceId: DeviceId? = null
|
||||
var name: String? = null
|
||||
|
||||
reader.beginObject()
|
||||
while (reader.hasNext()) {
|
||||
when (reader.nextName()) {
|
||||
DEVICE_ID -> deviceId = DeviceId.parse(reader)
|
||||
NAME -> name = reader.nextString()
|
||||
else -> reader.skipValue()
|
||||
}
|
||||
}
|
||||
reader.endObject()
|
||||
|
||||
return DeviceInfo(
|
||||
deviceId = deviceId!!,
|
||||
name = name!!
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
constructor(deviceId: DeviceId, name: String?) :
|
||||
this(deviceId, if (name != null && !name.isBlank()) name else deviceId.shortId, null)
|
||||
|
||||
fun serialize(writer: JsonWriter) {
|
||||
writer.beginObject()
|
||||
|
||||
writer.name(DEVICE_ID)
|
||||
deviceId.serialize(writer)
|
||||
|
||||
writer.name(NAME).value(name)
|
||||
|
||||
writer.endObject()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
import net.syncthing.java.core.utils.BlockUtils
|
||||
|
||||
class FileBlocks(val folder: String, val path: String, blocks: List<BlockInfo>) {
|
||||
|
||||
val blocks: List<BlockInfo>
|
||||
val hash: String
|
||||
val size: Long
|
||||
|
||||
init {
|
||||
assert(!folder.isEmpty())
|
||||
assert(!path.isEmpty())
|
||||
this.blocks = blocks.toList()
|
||||
var num: Long = 0
|
||||
for (block in blocks) {
|
||||
num += block.size.toLong()
|
||||
}
|
||||
this.size = num
|
||||
this.hash = BlockUtils.hashBlocks(this.blocks)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "FileBlocks(" + "blocks=" + blocks.size + ", hash=" + hash + ", folder=" + folder + ", path=" + path + ", size=" + size + ")"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
import net.syncthing.java.core.utils.PathUtils
|
||||
import org.apache.commons.io.FileUtils
|
||||
import java.util.*
|
||||
|
||||
class FileInfo(val folder: String, val type: FileType, val path: String, size: Long? = null,
|
||||
lastModified: Date? = Date(0), hash: String? = null, versionList: List<Version>? = null,
|
||||
val isDeleted: Boolean = false) {
|
||||
val fileName: String
|
||||
val parent: String
|
||||
val hash: String?
|
||||
val size: Long?
|
||||
val lastModified: Date
|
||||
val versionList: List<Version>
|
||||
|
||||
fun isDirectory(): Boolean = type == FileType.DIRECTORY
|
||||
|
||||
fun isFile(): Boolean = type == FileType.FILE
|
||||
|
||||
init {
|
||||
assert(!folder.isEmpty())
|
||||
if (PathUtils.isParent(path)) {
|
||||
this.fileName = PathUtils.PARENT_PATH
|
||||
this.parent = PathUtils.ROOT_PATH
|
||||
} else {
|
||||
this.fileName = PathUtils.getFileName(path)
|
||||
this.parent = if (PathUtils.isRoot(path)) PathUtils.ROOT_PATH else PathUtils.getParentPath(path)
|
||||
}
|
||||
this.lastModified = lastModified ?: Date(0)
|
||||
if (type == FileType.DIRECTORY) {
|
||||
this.size = null
|
||||
this.hash = null
|
||||
} else {
|
||||
assert(size != null)
|
||||
assert(!hash.isNullOrEmpty())
|
||||
this.size = size
|
||||
this.hash = hash
|
||||
}
|
||||
this.versionList = versionList ?: emptyList()
|
||||
}
|
||||
|
||||
enum class FileType {
|
||||
FILE, DIRECTORY
|
||||
}
|
||||
|
||||
fun describeSize(): String = if (isFile()) FileUtils.byteCountToDisplaySize(size!!) else ""
|
||||
|
||||
override fun toString(): String {
|
||||
return "FileRecord{" + "folder=" + folder + ", path=" + path + ", size=" + size + ", lastModified=" + lastModified + ", type=" + type + ", last version = " + versionList.lastOrNull() + '}'
|
||||
}
|
||||
|
||||
class Version(val id: Long, val value: Long) {
|
||||
|
||||
override fun toString(): String {
|
||||
return "Version{id=$id, value=$value}"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Builder {
|
||||
|
||||
private var folder: String? = null
|
||||
private var path: String? = null
|
||||
private var hash: String? = null
|
||||
private var size: Long? = null
|
||||
private var lastModified = Date(0)
|
||||
private var type: FileType? = null
|
||||
var versionList: List<Version>? = null
|
||||
private set
|
||||
private var deleted = false
|
||||
|
||||
fun getFolder(): String? {
|
||||
return folder
|
||||
}
|
||||
|
||||
fun setFolder(folder: String): Builder {
|
||||
this.folder = folder
|
||||
return this
|
||||
}
|
||||
|
||||
fun getPath(): String? {
|
||||
return path
|
||||
}
|
||||
|
||||
fun setPath(path: String): Builder {
|
||||
this.path = path
|
||||
return this
|
||||
}
|
||||
|
||||
fun getSize(): Long? {
|
||||
return size
|
||||
}
|
||||
|
||||
fun setSize(size: Long?): Builder {
|
||||
this.size = size
|
||||
return this
|
||||
}
|
||||
|
||||
fun getLastModified(): Date {
|
||||
return lastModified
|
||||
}
|
||||
|
||||
fun setLastModified(lastModified: Date): Builder {
|
||||
this.lastModified = lastModified
|
||||
return this
|
||||
}
|
||||
|
||||
fun getType(): FileType? {
|
||||
return type
|
||||
}
|
||||
|
||||
fun setType(type: FileType): Builder {
|
||||
this.type = type
|
||||
return this
|
||||
}
|
||||
|
||||
fun setTypeFile(): Builder {
|
||||
return setType(FileType.FILE)
|
||||
}
|
||||
|
||||
fun setTypeDir(): Builder {
|
||||
return setType(FileType.DIRECTORY)
|
||||
}
|
||||
|
||||
fun setVersionList(versionList: Iterable<Version>?): Builder {
|
||||
this.versionList = versionList?.toList()
|
||||
return this
|
||||
}
|
||||
|
||||
fun isDeleted(): Boolean {
|
||||
return deleted
|
||||
}
|
||||
|
||||
fun setDeleted(deleted: Boolean): Builder {
|
||||
this.deleted = deleted
|
||||
return this
|
||||
}
|
||||
|
||||
fun getHash(): String? {
|
||||
return hash
|
||||
}
|
||||
|
||||
fun setHash(hash: String): Builder {
|
||||
this.hash = hash
|
||||
return this
|
||||
}
|
||||
|
||||
fun build(): FileInfo {
|
||||
return FileInfo(folder!!, type!!, path!!, size, lastModified, hash, versionList, deleted)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun checkBlocks(fileInfo: FileInfo, fileBlocks: FileBlocks) {
|
||||
assert(fileBlocks.folder == fileInfo.folder, {"file info folder not match file block folder"})
|
||||
assert(fileBlocks.path == fileInfo.path, {"file info path does not match file block path"})
|
||||
assert(fileInfo.isFile(), {"file info must be of type 'FILE' to have blocks"})
|
||||
assert(fileBlocks.size == fileInfo.size, {"file info size does not match file block size"})
|
||||
assert(fileBlocks.hash == fileInfo.hash, {"file info hash does not match file block hash"})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
* Copyright (C) 2018 Jonas Lochmann
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
|
||||
open class FolderInfo(val folderId: String, label: String? = null) {
|
||||
companion object {
|
||||
private const val FOLDER_ID = "folderId"
|
||||
private const val LABEL = "label"
|
||||
|
||||
fun parse(reader: JsonReader): FolderInfo {
|
||||
var folderId: String? = null
|
||||
var label: String? = null
|
||||
|
||||
reader.beginObject()
|
||||
while (reader.hasNext()) {
|
||||
when (reader.nextName()) {
|
||||
FOLDER_ID -> folderId = reader.nextString()
|
||||
LABEL -> label = reader.nextString()
|
||||
else -> reader.skipValue()
|
||||
}
|
||||
}
|
||||
reader.endObject()
|
||||
|
||||
return FolderInfo(
|
||||
folderId = folderId!!,
|
||||
label = label!!
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val label: String
|
||||
|
||||
init {
|
||||
assert(!folderId.isEmpty())
|
||||
this.label = if (label != null && !label.isEmpty()) label else folderId
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "FolderInfo(folderId=$folderId, label=$label)"
|
||||
}
|
||||
|
||||
fun serialize(writer: JsonWriter) {
|
||||
writer.beginObject()
|
||||
|
||||
writer.name(FOLDER_ID).value(folderId)
|
||||
writer.name(LABEL).value(label)
|
||||
|
||||
writer.endObject()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
import org.apache.commons.io.FileUtils
|
||||
|
||||
import java.util.Date
|
||||
|
||||
class FolderStats private constructor(val fileCount: Long, val dirCount: Long, val size: Long, val lastUpdate: Date, folder: String, label: String?) : FolderInfo(folder, label) {
|
||||
|
||||
fun getRecordCount(): Long = dirCount + fileCount
|
||||
|
||||
fun describeSize(): String = FileUtils.byteCountToDisplaySize(size)
|
||||
|
||||
fun dumpInfo(): String {
|
||||
return ("folder " + label + " (" + folderId + ") file count = " + fileCount
|
||||
+ " dir count = " + dirCount + " folder size = " + describeSize() + " last update = " + lastUpdate)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "FolderStats{folder=$folderId, fileCount=$fileCount, dirCount=$dirCount, size=$size, lastUpdate=$lastUpdate}"
|
||||
}
|
||||
|
||||
fun copyBuilder(): Builder = Builder(fileCount, dirCount, size, folderId, label)
|
||||
|
||||
class Builder {
|
||||
|
||||
private var fileCount: Long = 0
|
||||
private var dirCount: Long = 0
|
||||
private var size: Long = 0
|
||||
private var lastUpdate = Date(0)
|
||||
private var folder: String? = null
|
||||
private var label: String? = null
|
||||
|
||||
constructor()
|
||||
|
||||
constructor(fileCount: Long, dirCount: Long, size: Long, folder: String, label: String) {
|
||||
this.fileCount = fileCount
|
||||
this.dirCount = dirCount
|
||||
this.size = size
|
||||
this.folder = folder
|
||||
this.label = label
|
||||
}
|
||||
|
||||
fun getFileCount(): Long = fileCount
|
||||
|
||||
fun setFileCount(fileCount: Long): Builder {
|
||||
this.fileCount = fileCount
|
||||
return this
|
||||
}
|
||||
|
||||
fun getDirCount(): Long = dirCount
|
||||
|
||||
fun setDirCount(dirCount: Long): Builder {
|
||||
this.dirCount = dirCount
|
||||
return this
|
||||
}
|
||||
|
||||
fun getSize(): Long = size
|
||||
|
||||
fun setSize(size: Long): Builder {
|
||||
this.size = size
|
||||
return this
|
||||
}
|
||||
|
||||
fun getLastUpdate(): Date = lastUpdate
|
||||
|
||||
fun setLastUpdate(lastUpdate: Date): Builder {
|
||||
this.lastUpdate = lastUpdate
|
||||
return this
|
||||
}
|
||||
|
||||
fun getFolder(): String? = folder
|
||||
|
||||
fun setFolder(folder: String): Builder {
|
||||
this.folder = folder
|
||||
return this
|
||||
}
|
||||
|
||||
fun getLabel(): String? = label
|
||||
|
||||
fun setLabel(label: String): Builder {
|
||||
this.label = label
|
||||
return this
|
||||
}
|
||||
|
||||
fun build(): FolderStats {
|
||||
return FolderStats(fileCount, dirCount, size, lastUpdate, folder!!, label)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.beans
|
||||
|
||||
|
||||
class IndexInfo private constructor(folder: String, val deviceId: String, val indexId: Long, val localSequence: Long, val maxSequence: Long) : FolderInfo(folder) {
|
||||
|
||||
fun getCompleted(): Double = if (maxSequence > 0) localSequence.toDouble() / maxSequence else 0.0
|
||||
|
||||
init {
|
||||
assert(!deviceId.isEmpty())
|
||||
}
|
||||
|
||||
fun copyBuilder(): Builder {
|
||||
return Builder(folderId, indexId, deviceId, localSequence, maxSequence)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "FolderIndexInfo{indexId=$indexId, folder=$folderId, deviceId=$deviceId, localSequence=$localSequence, maxSequence=$maxSequence}"
|
||||
}
|
||||
|
||||
class Builder {
|
||||
|
||||
private var indexId: Long = 0
|
||||
private var deviceId: String? = null
|
||||
private var folder: String? = null
|
||||
private var localSequence: Long = 0
|
||||
private var maxSequence: Long = 0
|
||||
|
||||
internal constructor()
|
||||
|
||||
internal constructor(folder: String, indexId: Long, deviceId: String, localSequence: Long, maxSequence: Long) {
|
||||
assert(!folder.isEmpty())
|
||||
assert(!deviceId.isEmpty())
|
||||
this.folder = folder
|
||||
this.indexId = indexId
|
||||
this.deviceId = deviceId
|
||||
this.localSequence = localSequence
|
||||
this.maxSequence = maxSequence
|
||||
}
|
||||
|
||||
fun getIndexId(): Long {
|
||||
return indexId
|
||||
}
|
||||
|
||||
fun getDeviceId(): String? {
|
||||
return deviceId
|
||||
}
|
||||
|
||||
fun getFolder(): String? {
|
||||
return folder
|
||||
}
|
||||
|
||||
fun getLocalSequence(): Long {
|
||||
return localSequence
|
||||
}
|
||||
|
||||
fun getMaxSequence(): Long {
|
||||
return maxSequence
|
||||
}
|
||||
|
||||
fun setIndexId(indexId: Long): Builder {
|
||||
this.indexId = indexId
|
||||
return this
|
||||
}
|
||||
|
||||
fun setDeviceId(deviceId: String): Builder {
|
||||
this.deviceId = deviceId
|
||||
return this
|
||||
}
|
||||
|
||||
fun setFolder(folder: String): Builder {
|
||||
this.folder = folder
|
||||
return this
|
||||
}
|
||||
|
||||
fun setLocalSequence(localSequence: Long): Builder {
|
||||
this.localSequence = localSequence
|
||||
return this
|
||||
}
|
||||
|
||||
fun setMaxSequence(maxSequence: Long): Builder {
|
||||
this.maxSequence = maxSequence
|
||||
return this
|
||||
}
|
||||
|
||||
fun build(): IndexInfo {
|
||||
return IndexInfo(folder!!, deviceId!!, indexId, localSequence, maxSequence)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
fun newBuilder(): Builder {
|
||||
return Builder()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package net.syncthing.java.core.configuration
|
||||
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import net.syncthing.java.core.beans.DeviceInfo
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import java.util.*
|
||||
import kotlin.collections.HashSet
|
||||
|
||||
data class Config(
|
||||
val peers: Set<DeviceInfo>,
|
||||
val folders: Set<FolderInfo>,
|
||||
val localDeviceName: String,
|
||||
val localDeviceId: String,
|
||||
val discoveryServers: Set<String>,
|
||||
val keystoreAlgorithm: String,
|
||||
val keystoreData: String
|
||||
) {
|
||||
companion object {
|
||||
private const val PEERS = "peers"
|
||||
private const val FOLDERS = "folders"
|
||||
private const val LOCAL_DEVICE_NAME = "localDeviceName"
|
||||
private const val LOCAL_DEVICE_ID = "localDeviceId"
|
||||
private const val DISCOVERY_SERVERS = "discoveryServers"
|
||||
private const val KEYSTORE_ALGORITHM = "keystoreAlgorithm"
|
||||
private const val KEYSTORE_DATA = "keystoreData"
|
||||
|
||||
fun parse(reader: JsonReader): Config {
|
||||
var peers: Set<DeviceInfo>? = null
|
||||
var folders: Set<FolderInfo>? = null
|
||||
var localDeviceName: String? = null
|
||||
var localDeviceId: String? = null
|
||||
var discoveryServers: Set<String>? = null
|
||||
var keystoreAlgorithm: String? = null
|
||||
var keystoreData: String? = null
|
||||
|
||||
reader.beginObject()
|
||||
while (reader.hasNext()) {
|
||||
when (reader.nextName()) {
|
||||
PEERS -> {
|
||||
val newPeers = HashSet<DeviceInfo>()
|
||||
|
||||
reader.beginArray()
|
||||
while (reader.hasNext()) {
|
||||
newPeers.add(DeviceInfo.parse(reader))
|
||||
}
|
||||
reader.endArray()
|
||||
|
||||
peers = Collections.unmodifiableSet(newPeers)
|
||||
}
|
||||
FOLDERS -> {
|
||||
val newFolders = HashSet<FolderInfo>()
|
||||
|
||||
reader.beginArray()
|
||||
while (reader.hasNext()) {
|
||||
newFolders.add(FolderInfo.parse(reader))
|
||||
}
|
||||
reader.endArray()
|
||||
|
||||
folders = Collections.unmodifiableSet(newFolders)
|
||||
}
|
||||
LOCAL_DEVICE_NAME -> localDeviceName = reader.nextString()
|
||||
LOCAL_DEVICE_ID -> localDeviceId = reader.nextString()
|
||||
DISCOVERY_SERVERS -> {
|
||||
val newDiscoveryServers = HashSet<String>()
|
||||
|
||||
reader.beginArray()
|
||||
while (reader.hasNext()) {
|
||||
newDiscoveryServers.add(reader.nextString())
|
||||
}
|
||||
reader.endArray()
|
||||
|
||||
discoveryServers = Collections.unmodifiableSet(newDiscoveryServers)
|
||||
}
|
||||
KEYSTORE_ALGORITHM -> keystoreAlgorithm = reader.nextString()
|
||||
KEYSTORE_DATA -> keystoreData = reader.nextString()
|
||||
else -> reader.skipValue()
|
||||
}
|
||||
}
|
||||
reader.endObject()
|
||||
|
||||
return Config(
|
||||
peers = peers!!,
|
||||
folders = folders!!,
|
||||
localDeviceName = localDeviceName!!,
|
||||
localDeviceId = localDeviceId!!,
|
||||
discoveryServers = discoveryServers!!,
|
||||
keystoreAlgorithm = keystoreAlgorithm!!,
|
||||
keystoreData = keystoreData!!
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun serialize(writer: JsonWriter) {
|
||||
writer.beginObject()
|
||||
|
||||
writer.name(PEERS).beginArray()
|
||||
peers.forEach { it.serialize(writer) }
|
||||
writer.endArray()
|
||||
|
||||
writer.name(FOLDERS).beginArray()
|
||||
folders.forEach { it.serialize(writer) }
|
||||
writer.endArray()
|
||||
|
||||
writer.name(LOCAL_DEVICE_NAME).value(localDeviceName)
|
||||
writer.name(LOCAL_DEVICE_ID).value(localDeviceId)
|
||||
|
||||
writer.name(DISCOVERY_SERVERS).beginArray()
|
||||
discoveryServers.forEach { writer.value(it) }
|
||||
writer.endArray()
|
||||
|
||||
writer.name(KEYSTORE_ALGORITHM).value(keystoreAlgorithm)
|
||||
writer.name(KEYSTORE_DATA).value(keystoreData)
|
||||
|
||||
writer.endObject()
|
||||
}
|
||||
|
||||
// Exclude keystoreData from toString()
|
||||
override fun toString() = "Config(peers=$peers, folders=$folders, localDeviceName=$localDeviceName, " +
|
||||
"localDeviceId=$localDeviceId, discoveryServers=$discoveryServers, keystoreAlgorithm=$keystoreAlgorithm)"
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package net.syncthing.java.core.configuration
|
||||
|
||||
import com.google.gson.stream.JsonReader
|
||||
import com.google.gson.stream.JsonWriter
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.beans.DeviceInfo
|
||||
import net.syncthing.java.core.beans.FolderInfo
|
||||
import net.syncthing.java.core.security.KeystoreHandler
|
||||
import org.bouncycastle.util.encoders.Base64
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.File
|
||||
import java.io.StringReader
|
||||
import java.io.StringWriter
|
||||
import java.net.InetAddress
|
||||
import java.util.*
|
||||
|
||||
class Configuration(configFolder: File = DefaultConfigFolder) {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
private val configFile = File(configFolder, ConfigFileName)
|
||||
val databaseFolder = File(configFolder, DatabaseFolderName)
|
||||
|
||||
private var isSaved = true
|
||||
private var config: Config
|
||||
|
||||
init {
|
||||
configFolder.mkdirs()
|
||||
databaseFolder.mkdirs()
|
||||
assert(configFolder.isDirectory && configFile.canWrite(), { "Invalid config folder $configFolder" })
|
||||
|
||||
if (!configFile.exists()) {
|
||||
var localDeviceName = InetAddress.getLocalHost().hostName
|
||||
if (localDeviceName.isEmpty() || localDeviceName == "localhost") {
|
||||
localDeviceName = "syncthing-lite"
|
||||
}
|
||||
val keystoreData = KeystoreHandler.Loader().generateKeystore()
|
||||
isSaved = false
|
||||
config = Config(peers = setOf(), folders = setOf(),
|
||||
localDeviceName = localDeviceName,
|
||||
discoveryServers = Companion.DiscoveryServers,
|
||||
localDeviceId = keystoreData.first.deviceId,
|
||||
keystoreData = Base64.toBase64String(keystoreData.second),
|
||||
keystoreAlgorithm = keystoreData.third)
|
||||
persistNow()
|
||||
} else {
|
||||
config = Config.parse(JsonReader(StringReader(configFile.readText())))
|
||||
|
||||
// automatic migration if the old config was used
|
||||
if (config.discoveryServers == OldDiscoveryServers) {
|
||||
config = Config(
|
||||
peers = config.peers,
|
||||
folders = config.folders,
|
||||
localDeviceName = config.localDeviceName,
|
||||
localDeviceId = config.localDeviceId,
|
||||
discoveryServers = Companion.DiscoveryServers,
|
||||
keystoreAlgorithm = config.keystoreAlgorithm,
|
||||
keystoreData = config.keystoreData
|
||||
)
|
||||
}
|
||||
}
|
||||
logger.debug("Loaded config = $config")
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val DefaultConfigFolder = File(System.getProperty("user.home"), ".config/syncthing-java/")
|
||||
private const val ConfigFileName = "config.json"
|
||||
private const val DatabaseFolderName = "database"
|
||||
private val DiscoveryServers = setOf(
|
||||
"discovery.syncthing.net", "discovery-v4.syncthing.net", "discovery-v6.syncthing.net")
|
||||
private val OldDiscoveryServers = setOf(
|
||||
"discovery-v4-1.syncthing.net", "discovery-v4-2.syncthing.net", "discovery-v4-3.syncthing.net",
|
||||
"discovery-v6-1.syncthing.net", "discovery-v6-2.syncthing.net", "discovery-v6-3.syncthing.net")
|
||||
}
|
||||
|
||||
val instanceId = Math.abs(Random().nextLong())
|
||||
|
||||
val localDeviceId: DeviceId
|
||||
get() = DeviceId(config.localDeviceId)
|
||||
|
||||
val discoveryServers: Set<String>
|
||||
get() = config.discoveryServers
|
||||
|
||||
val keystoreData: ByteArray
|
||||
get() = Base64.decode(config.keystoreData)
|
||||
|
||||
val keystoreAlgorithm: String
|
||||
get() = config.keystoreAlgorithm
|
||||
|
||||
val clientName = "syncthing-java"
|
||||
|
||||
val clientVersion = javaClass.`package`.implementationVersion ?: "0.0.0"
|
||||
|
||||
val peerIds: Set<DeviceId>
|
||||
get() = config.peers.map { it.deviceId }.toSet()
|
||||
|
||||
var localDeviceName: String
|
||||
get() = config.localDeviceName
|
||||
set(localDeviceName) {
|
||||
config = config.copy(localDeviceName = localDeviceName)
|
||||
isSaved = false
|
||||
}
|
||||
|
||||
var folders: Set<FolderInfo>
|
||||
get() = config.folders
|
||||
set(folders) {
|
||||
config = config.copy(folders = folders)
|
||||
isSaved = false
|
||||
}
|
||||
|
||||
var peers: Set<DeviceInfo>
|
||||
get() = config.peers
|
||||
set(peers) {
|
||||
config = config.copy(peers = peers)
|
||||
isSaved = false
|
||||
}
|
||||
|
||||
fun persistNow() {
|
||||
persist()
|
||||
}
|
||||
|
||||
fun persistLater() {
|
||||
Thread { persist() }.start()
|
||||
}
|
||||
|
||||
private fun persist() {
|
||||
if (isSaved)
|
||||
return
|
||||
|
||||
config.let {
|
||||
System.out.println("writing config to $configFile")
|
||||
configFile.writeText(
|
||||
StringWriter().apply {
|
||||
JsonWriter(this).apply {
|
||||
setIndent(" ")
|
||||
|
||||
config.serialize(this)
|
||||
}
|
||||
}.toString()
|
||||
)
|
||||
isSaved = true
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "Configuration(peers=$peers, folders=$folders, localDeviceName=$localDeviceName, " +
|
||||
"localDeviceId=${localDeviceId.deviceId}, discoveryServers=$discoveryServers, instanceId=$instanceId, " +
|
||||
"configFile=$configFile, databaseFolder=$databaseFolder)"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2016 Davide Imbriaco <davide.imbriaco@gmail.com>.
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.events
|
||||
|
||||
import net.syncthing.java.core.beans.DeviceAddress
|
||||
|
||||
interface DeviceAddressActiveEvent {
|
||||
|
||||
fun getDeviceAddress(): DeviceAddress
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2016 Davide Imbriaco <davide.imbriaco@gmail.com>.
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.events
|
||||
|
||||
import net.syncthing.java.core.beans.DeviceAddress
|
||||
|
||||
interface DeviceAddressReceivedEvent {
|
||||
|
||||
fun getDeviceAddresses(): List<DeviceAddress>
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.interfaces
|
||||
|
||||
import net.syncthing.java.core.beans.*
|
||||
import java.io.Closeable
|
||||
import java.util.*
|
||||
|
||||
interface IndexRepository: Closeable {
|
||||
|
||||
fun setOnFolderStatsUpdatedListener(listener: ((IndexRepository.FolderStatsUpdatedEvent) -> Unit)?)
|
||||
|
||||
fun getSequencer(): Sequencer
|
||||
|
||||
fun updateIndexInfo(indexInfo: IndexInfo)
|
||||
|
||||
fun findIndexInfoByDeviceAndFolder(deviceId: DeviceId, folder: String): IndexInfo?
|
||||
|
||||
fun findFileInfo(folder: String, path: String): FileInfo?
|
||||
|
||||
fun findFileInfoLastModified(folder: String, path: String): Date?
|
||||
|
||||
fun findNotDeletedFileInfo(folder: String, path: String): FileInfo?
|
||||
|
||||
fun findFileBlocks(folder: String, path: String): FileBlocks?
|
||||
|
||||
fun updateFileInfo(fileInfo: FileInfo, fileBlocks: FileBlocks?)
|
||||
|
||||
fun findNotDeletedFilesByFolderAndParent(folder: String, parentPath: String): List<FileInfo>
|
||||
|
||||
fun clearIndex()
|
||||
|
||||
fun findFolderStats(folder: String): FolderStats?
|
||||
|
||||
fun findAllFolderStats(): List<FolderStats>
|
||||
|
||||
fun findFileInfoBySearchTerm(query: String): List<FileInfo>
|
||||
|
||||
fun countFileInfoBySearchTerm(query: String): Long
|
||||
|
||||
abstract class FolderStatsUpdatedEvent {
|
||||
|
||||
abstract fun getFolderStats(): List<FolderStats>
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.interfaces
|
||||
|
||||
import java.net.Socket
|
||||
|
||||
interface RelayConnection {
|
||||
|
||||
fun getSocket(): Socket
|
||||
|
||||
fun isServerSocket(): Boolean
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.interfaces
|
||||
|
||||
interface Sequencer {
|
||||
|
||||
fun indexId(): Long
|
||||
|
||||
fun nextSequence(): Long
|
||||
|
||||
fun currentSequence(): Long
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.interfaces
|
||||
|
||||
import java.io.Closeable
|
||||
|
||||
interface TempRepository: Closeable {
|
||||
|
||||
fun pushTempData(data: ByteArray): String
|
||||
|
||||
fun popTempData(key: String): ByteArray
|
||||
|
||||
fun deleteTempData(keys: List<String>)
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.security
|
||||
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.core.interfaces.RelayConnection
|
||||
import net.syncthing.java.core.utils.NetworkUtils
|
||||
import org.apache.commons.codec.binary.Base32
|
||||
import org.apache.commons.lang3.tuple.Pair
|
||||
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter
|
||||
import org.bouncycastle.cert.jcajce.JcaX509v1CertificateBuilder
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider
|
||||
import org.bouncycastle.operator.OperatorCreationException
|
||||
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder
|
||||
import org.bouncycastle.util.encoders.Base64
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.IOException
|
||||
import java.math.BigInteger
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.Socket
|
||||
import java.security.*
|
||||
import java.security.cert.Certificate
|
||||
import java.security.cert.CertificateException
|
||||
import java.security.cert.CertificateFactory
|
||||
import java.security.cert.X509Certificate
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.net.ssl.*
|
||||
import javax.security.auth.x500.X500Principal
|
||||
|
||||
class KeystoreHandler private constructor(private val keyStore: KeyStore) {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
class CryptoException internal constructor(t: Throwable) : GeneralSecurityException(t)
|
||||
|
||||
private val socketFactory: SSLSocketFactory
|
||||
|
||||
init {
|
||||
val sslContext = SSLContext.getInstance(TLS_VERSION)
|
||||
val keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm())
|
||||
keyManagerFactory.init(keyStore, KEY_PASSWORD.toCharArray())
|
||||
|
||||
sslContext.init(keyManagerFactory.keyManagers, arrayOf(object : X509TrustManager {
|
||||
@Throws(CertificateException::class)
|
||||
override fun checkClientTrusted(xcs: Array<X509Certificate>, string: String) {}
|
||||
@Throws(CertificateException::class)
|
||||
override fun checkServerTrusted(xcs: Array<X509Certificate>, string: String) {}
|
||||
override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
|
||||
}), null)
|
||||
socketFactory = sslContext.socketFactory
|
||||
}
|
||||
|
||||
@Throws(CryptoException::class, IOException::class)
|
||||
private fun exportKeystoreToData(): ByteArray {
|
||||
val out = ByteArrayOutputStream()
|
||||
try {
|
||||
keyStore.store(out, JKS_PASSWORD.toCharArray())
|
||||
} catch (ex: NoSuchAlgorithmException) {
|
||||
throw CryptoException(ex)
|
||||
} catch (ex: CertificateException) {
|
||||
throw CryptoException(ex)
|
||||
}
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
@Throws(CryptoException::class, IOException::class)
|
||||
private fun wrapSocket(socket: Socket, isServerSocket: Boolean, protocol: String): SSLSocket {
|
||||
try {
|
||||
logger.debug("wrapping plain socket, server mode = {}", isServerSocket)
|
||||
val sslSocket = socketFactory.createSocket(socket, null, socket.port, true) as SSLSocket
|
||||
if (isServerSocket) {
|
||||
sslSocket.useClientMode = false
|
||||
}
|
||||
return sslSocket
|
||||
} catch (e: KeyManagementException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: KeyStoreException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: UnrecoverableKeyException) {
|
||||
throw CryptoException(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Throws(CryptoException::class, IOException::class)
|
||||
fun createSocket(relaySocketAddress: InetSocketAddress, protocol: String): SSLSocket {
|
||||
try {
|
||||
val socket = socketFactory.createSocket() as SSLSocket
|
||||
socket.connect(relaySocketAddress, SOCKET_TIMEOUT)
|
||||
return socket
|
||||
} catch (e: KeyManagementException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: KeyStoreException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: UnrecoverableKeyException) {
|
||||
throw CryptoException(e)
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(SSLPeerUnverifiedException::class, CertificateException::class)
|
||||
fun checkSocketCertificate(socket: SSLSocket, deviceId: DeviceId) {
|
||||
val session = socket.session
|
||||
val certs = session.peerCertificates.toList()
|
||||
val certificateFactory = CertificateFactory.getInstance("X.509")
|
||||
val certPath = certificateFactory.generateCertPath(certs)
|
||||
val certificate = certPath.certificates[0]
|
||||
NetworkUtils.assertProtocol(certificate is X509Certificate)
|
||||
val derData = certificate.encoded
|
||||
val deviceIdFromCertificate = derDataToDeviceId(derData)
|
||||
logger.trace("remote pem certificate =\n{}", derToPem(derData))
|
||||
NetworkUtils.assertProtocol(deviceIdFromCertificate == deviceId, {"device id mismatch! expected = $deviceId, got = $deviceIdFromCertificate"})
|
||||
logger.debug("remote ssl certificate match deviceId = {}", deviceId)
|
||||
}
|
||||
|
||||
@Throws(CryptoException::class, IOException::class)
|
||||
fun wrapSocket(relayConnection: RelayConnection, protocol: String): SSLSocket {
|
||||
return wrapSocket(relayConnection.getSocket(), relayConnection.isServerSocket(), protocol)
|
||||
}
|
||||
|
||||
class Loader {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
|
||||
private fun getKeystoreAlgorithm(keystoreAlgorithm: String?): String {
|
||||
return keystoreAlgorithm?.let { algo ->
|
||||
if (!algo.isBlank()) algo else null
|
||||
} ?: {
|
||||
val defaultAlgo = KeyStore.getDefaultType()!!
|
||||
logger.debug("keystore algo set to {}", defaultAlgo)
|
||||
defaultAlgo
|
||||
}()
|
||||
}
|
||||
|
||||
@Throws(CryptoException::class, IOException::class)
|
||||
fun generateKeystore(): Triple<DeviceId, ByteArray, String> {
|
||||
val keystoreAlgorithm = getKeystoreAlgorithm(null)
|
||||
val keystore = generateKeystore(keystoreAlgorithm)
|
||||
val keystoreHandler = KeystoreHandler(keystore.left)
|
||||
val keystoreData = keystoreHandler.exportKeystoreToData()
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(keystoreData)
|
||||
keystoreHandlersCacheByHash[Base32().encodeAsString(hash)] = keystoreHandler
|
||||
logger.info("keystore ready, device id = {}", keystore.right)
|
||||
return Triple(keystore.right, keystoreData, keystoreAlgorithm)
|
||||
}
|
||||
|
||||
fun loadKeystore(configuration: Configuration): KeystoreHandler {
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(configuration.keystoreData)
|
||||
val keystoreHandlerFromCache = keystoreHandlersCacheByHash[Base32().encodeAsString(hash)]
|
||||
if (keystoreHandlerFromCache != null) {
|
||||
return keystoreHandlerFromCache
|
||||
}
|
||||
val keystoreAlgo = getKeystoreAlgorithm(configuration.keystoreAlgorithm)
|
||||
val keystore = importKeystore(configuration.keystoreData, keystoreAlgo)
|
||||
val keystoreHandler = KeystoreHandler(keystore.left)
|
||||
keystoreHandlersCacheByHash[Base32().encodeAsString(hash)] = keystoreHandler
|
||||
logger.info("keystore ready, device id = {}", keystore.right)
|
||||
return keystoreHandler
|
||||
}
|
||||
|
||||
@Throws(CryptoException::class, IOException::class)
|
||||
private fun generateKeystore(keystoreAlgorithm: String): Pair<KeyStore, DeviceId> {
|
||||
try {
|
||||
logger.debug("generating key")
|
||||
val keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGO)
|
||||
keyPairGenerator.initialize(KEY_SIZE)
|
||||
val keyPair = keyPairGenerator.genKeyPair()
|
||||
|
||||
val contentSigner = JcaContentSignerBuilder(SIGNATURE_ALGO).build(keyPair.private)
|
||||
|
||||
val startDate = Date(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1))
|
||||
val endDate = Date(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(10 * 365))
|
||||
|
||||
val certificateBuilder = JcaX509v1CertificateBuilder(X500Principal(CERTIFICATE_CN), BigInteger.ZERO,
|
||||
startDate, endDate, X500Principal(CERTIFICATE_CN), keyPair.public)
|
||||
|
||||
val certificateHolder = certificateBuilder.build(contentSigner)
|
||||
|
||||
val certificateDerData = certificateHolder.encoded
|
||||
logger.info("generated cert =\n{}", derToPem(certificateDerData))
|
||||
val deviceId = derDataToDeviceId(certificateDerData)
|
||||
logger.info("device id from cert = {}", deviceId)
|
||||
|
||||
val keyStore = KeyStore.getInstance(keystoreAlgorithm)
|
||||
keyStore.load(null, null)
|
||||
val certChain = arrayOfNulls<Certificate>(1)
|
||||
certChain[0] = JcaX509CertificateConverter().getCertificate(certificateHolder)
|
||||
keyStore.setKeyEntry("key", keyPair.private, KEY_PASSWORD.toCharArray(), certChain)
|
||||
return Pair.of(keyStore, deviceId)
|
||||
} catch (e: OperatorCreationException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: CertificateException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: KeyStoreException) {
|
||||
throw CryptoException(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Throws(CryptoException::class, IOException::class)
|
||||
private fun importKeystore(keystoreData: ByteArray, keystoreAlgorithm: String): Pair<KeyStore, DeviceId> {
|
||||
try {
|
||||
val keyStore = KeyStore.getInstance(keystoreAlgorithm)
|
||||
keyStore.load(ByteArrayInputStream(keystoreData), JKS_PASSWORD.toCharArray())
|
||||
val alias = keyStore.aliases().nextElement()
|
||||
val certificate = keyStore.getCertificate(alias)
|
||||
NetworkUtils.assertProtocol(certificate is X509Certificate)
|
||||
val derData = certificate.encoded
|
||||
val deviceId = derDataToDeviceId(derData)
|
||||
logger.debug("loaded device id from cert = {}", deviceId)
|
||||
return Pair.of(keyStore, deviceId)
|
||||
} catch (e: NoSuchAlgorithmException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: KeyStoreException) {
|
||||
throw CryptoException(e)
|
||||
} catch (e: CertificateException) {
|
||||
throw CryptoException(e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val keystoreHandlersCacheByHash = mutableMapOf<String, KeystoreHandler>()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private const val JKS_PASSWORD = "password"
|
||||
private const val KEY_PASSWORD = "password"
|
||||
private const val KEY_ALGO = "RSA"
|
||||
private const val SIGNATURE_ALGO = "SHA1withRSA"
|
||||
private const val CERTIFICATE_CN = "CN=syncthing"
|
||||
private const val KEY_SIZE = 3072
|
||||
private const val SOCKET_TIMEOUT = 2000
|
||||
private const val TLS_VERSION = "TLSv1.2"
|
||||
|
||||
init {
|
||||
Security.addProvider(BouncyCastleProvider())
|
||||
}
|
||||
|
||||
private fun derToPem(der: ByteArray): String {
|
||||
return "-----BEGIN CERTIFICATE-----\n" + Base64.toBase64String(der).chunked(76).joinToString("\n") + "\n-----END CERTIFICATE-----"
|
||||
}
|
||||
|
||||
fun derDataToDeviceId(certificateDerData: ByteArray): DeviceId {
|
||||
return DeviceId.fromHashData(MessageDigest.getInstance("SHA-256").digest(certificateDerData))
|
||||
}
|
||||
|
||||
const val BEP = "bep/1.0"
|
||||
const val RELAY = "bep-relay"
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.utils
|
||||
|
||||
import net.syncthing.java.core.beans.BlockInfo
|
||||
import org.bouncycastle.util.encoders.Hex
|
||||
import java.security.MessageDigest
|
||||
|
||||
object BlockUtils {
|
||||
|
||||
fun hashBlocks(blocks: List<BlockInfo>): String {
|
||||
val string = blocks.joinToString(",") { it.hash }.toByteArray()
|
||||
val hash = MessageDigest.getInstance("SHA-256").digest(string)
|
||||
return Hex.toHexString(hash)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.utils
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Future
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
private val logger = LoggerFactory.getLogger(ExecutorService::class.java)
|
||||
|
||||
fun ExecutorService.awaitTerminationSafe() {
|
||||
try {
|
||||
awaitTermination(2, TimeUnit.SECONDS)
|
||||
} catch (ex: InterruptedException) {
|
||||
logger.warn("", ex)
|
||||
}
|
||||
}
|
||||
|
||||
fun ExecutorService.submitLogging(runnable: Runnable) = submitLogging { runnable.run() }
|
||||
|
||||
/**
|
||||
* Wrapper method for [[ExecutorService.submit]], which silently swallows exceptions. If an exception is thrown in
|
||||
* [[runnable]], logs the exception and force crashes
|
||||
*/
|
||||
fun <T> ExecutorService.submitLogging(runnable: () -> T): Future<T> {
|
||||
return submit<T>({
|
||||
try {
|
||||
runnable()
|
||||
} catch (e: Exception) {
|
||||
logger.error("", e)
|
||||
System.exit(1)
|
||||
null
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.syncthing.java.core.utils
|
||||
|
||||
import java.io.IOException
|
||||
|
||||
object NetworkUtils {
|
||||
|
||||
@Throws(IOException::class)
|
||||
fun assertProtocol(value: Boolean, lazyMessage: (() -> String)? = null) {
|
||||
if (!value) {
|
||||
if (lazyMessage != null)
|
||||
throw IOException(lazyMessage())
|
||||
else
|
||||
throw IOException()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.core.utils
|
||||
|
||||
import org.apache.commons.io.FilenameUtils
|
||||
|
||||
object PathUtils {
|
||||
|
||||
val ROOT_PATH = ""
|
||||
val PATH_SEPARATOR = "/"
|
||||
val PARENT_PATH = ".."
|
||||
|
||||
private fun normalizePath(path: String): String {
|
||||
return FilenameUtils.normalizeNoEndSeparator(path, true).replaceFirst(("^" + PATH_SEPARATOR).toRegex(), "")
|
||||
}
|
||||
|
||||
fun isRoot(path: String): Boolean {
|
||||
return path.isEmpty()
|
||||
}
|
||||
|
||||
fun isParent(path: String): Boolean {
|
||||
return path == PARENT_PATH
|
||||
}
|
||||
|
||||
fun getParentPath(path: String): String {
|
||||
assert(!isRoot(path), {"cannot get parent of root path"})
|
||||
return normalizePath(path + PATH_SEPARATOR + PARENT_PATH)
|
||||
}
|
||||
|
||||
fun getFileName(path: String): String {
|
||||
return FilenameUtils.getName(path)
|
||||
}
|
||||
|
||||
fun buildPath(dir: String, file: String): String {
|
||||
return normalizePath(dir + PATH_SEPARATOR + file)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{0} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,45 @@
|
||||
apply plugin: 'application'
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'com.google.protobuf'
|
||||
mainClassName = 'net.syncthing.java.discovery.Main'
|
||||
|
||||
dependencies {
|
||||
compile project(':syncthing-core')
|
||||
compile "commons-cli:commons-cli:1.4"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation "com.google.protobuf:protobuf-lite:$protobuf_lite_version"
|
||||
}
|
||||
|
||||
run {
|
||||
if (project.hasProperty('args')) {
|
||||
args project.args.split('\\s+')
|
||||
}
|
||||
}
|
||||
|
||||
protobuf {
|
||||
protoc {
|
||||
artifact = "com.google.protobuf:protoc:3.5.1-1"
|
||||
}
|
||||
plugins {
|
||||
javalite {
|
||||
// The codegen for lite comes as a separate artifact
|
||||
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
|
||||
}
|
||||
}
|
||||
generateProtoTasks {
|
||||
all().each { task ->
|
||||
task.builtins {
|
||||
// In most cases you don't need the full Java output
|
||||
// if you use the lite output.
|
||||
remove java
|
||||
}
|
||||
task.plugins {
|
||||
javalite { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/google/protobuf-gradle-plugin/issues/100
|
||||
compileKotlin.dependsOn('generateProto')
|
||||
sourceSets.main.kotlin.srcDirs += file("${protobuf.generatedFilesBaseDir}/main/javalite")
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.discovery
|
||||
|
||||
import net.syncthing.java.core.beans.DeviceAddress
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.*
|
||||
|
||||
class DeviceAddressSupplier(private val discoveryHandler: DiscoveryHandler) : Iterable<DeviceAddress?> {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
private val deviceAddressQueue = PriorityQueue<DeviceAddress>(11, compareBy { it.score })
|
||||
private val queueLock = Object()
|
||||
|
||||
private fun getDeviceAddress(): DeviceAddress? {
|
||||
synchronized(queueLock) {
|
||||
return deviceAddressQueue.poll()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun onNewDeviceAddressAcquired(address: DeviceAddress) {
|
||||
if (address.isWorking()) {
|
||||
synchronized(queueLock) {
|
||||
deviceAddressQueue.add(address)
|
||||
queueLock.notify()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(InterruptedException::class)
|
||||
fun getDeviceAddressOrWait(): DeviceAddress? = getDeviceAddressOrWait(5000)
|
||||
|
||||
init {
|
||||
synchronized(queueLock) {
|
||||
deviceAddressQueue.addAll(discoveryHandler.getAllWorkingDeviceAddresses())// note: slight risk of duplicate address loading
|
||||
}
|
||||
}
|
||||
|
||||
@Throws(InterruptedException::class)
|
||||
private fun getDeviceAddressOrWait(timeout: Long): DeviceAddress? {
|
||||
synchronized(queueLock) {
|
||||
if (deviceAddressQueue.isEmpty()) {
|
||||
queueLock.wait(timeout)
|
||||
}
|
||||
return getDeviceAddress()
|
||||
}
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<DeviceAddress?> {
|
||||
return object : Iterator<DeviceAddress?> {
|
||||
|
||||
private var hasNext: Boolean? = null
|
||||
private var next: DeviceAddress? = null
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
if (hasNext == null) {
|
||||
try {
|
||||
next = getDeviceAddressOrWait()
|
||||
} catch (ex: InterruptedException) {
|
||||
logger.warn("", ex)
|
||||
}
|
||||
|
||||
hasNext = next != null
|
||||
}
|
||||
return hasNext!!
|
||||
}
|
||||
|
||||
override fun next(): DeviceAddress? {
|
||||
assert(hasNext())
|
||||
val res = next
|
||||
hasNext = null
|
||||
next = null
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
* Copyright (C) 2018 Jonas Lochmann
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.discovery
|
||||
|
||||
import net.syncthing.java.core.beans.DeviceAddress
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.core.utils.awaitTerminationSafe
|
||||
import net.syncthing.java.core.utils.submitLogging
|
||||
import net.syncthing.java.discovery.protocol.GlobalDiscoveryHandler
|
||||
import net.syncthing.java.discovery.protocol.LocalDiscoveryHandler
|
||||
import net.syncthing.java.discovery.utils.AddressRanker
|
||||
import org.apache.commons.lang3.tuple.Pair
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.Closeable
|
||||
import java.util.*
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
class DiscoveryHandler(private val configuration: Configuration) : Closeable {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(javaClass)
|
||||
private val globalDiscoveryHandler = GlobalDiscoveryHandler(configuration)
|
||||
private val localDiscoveryHandler = LocalDiscoveryHandler(configuration, { _, deviceAddresses ->
|
||||
logger.info("received device address list from local discovery")
|
||||
processDeviceAddressBg(deviceAddresses)
|
||||
}, { deviceId ->
|
||||
onMessageFromUnknownDeviceListeners.forEach { listener -> listener(deviceId) }
|
||||
})
|
||||
private val executorService = Executors.newCachedThreadPool()
|
||||
private val deviceAddressMap = Collections.synchronizedMap(hashMapOf<Pair<DeviceId, String>, DeviceAddress>())
|
||||
private val deviceAddressSupplier = DeviceAddressSupplier(this)
|
||||
private var isClosed = false
|
||||
private val onMessageFromUnknownDeviceListeners = Collections.synchronizedSet(HashSet<(DeviceId) -> Unit>())
|
||||
|
||||
private var shouldLoadFromGlobal = true
|
||||
private var shouldStartLocalDiscovery = true
|
||||
|
||||
fun getAllWorkingDeviceAddresses() = deviceAddressMap.values.filter { it.isWorking() }
|
||||
|
||||
private fun updateAddressesBg() {
|
||||
if (shouldStartLocalDiscovery) {
|
||||
shouldStartLocalDiscovery = false
|
||||
localDiscoveryHandler.startListener()
|
||||
localDiscoveryHandler.sendAnnounceMessage()
|
||||
}
|
||||
if (shouldLoadFromGlobal) {
|
||||
shouldLoadFromGlobal = false //TODO timeout for reload
|
||||
executorService.submitLogging {
|
||||
for (deviceId in configuration.peerIds) {
|
||||
globalDiscoveryHandler.query(deviceId, this::processDeviceAddressBg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun processDeviceAddressBg(deviceAddresses: Iterable<DeviceAddress>) {
|
||||
if (isClosed) {
|
||||
logger.debug("discarding device addresses, discovery handler already closed")
|
||||
} else {
|
||||
executorService.submitLogging {
|
||||
val list = deviceAddresses.toList()
|
||||
val peers = configuration.peerIds
|
||||
//do not process address already processed
|
||||
list.filter { deviceAddress ->
|
||||
!peers.contains(deviceAddress.deviceId()) || deviceAddressMap.containsKey(Pair.of(DeviceId(deviceAddress.deviceId), deviceAddress.address))
|
||||
}
|
||||
AddressRanker.pingAddresses(list)
|
||||
.forEach { putDeviceAddress(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun putDeviceAddress(deviceAddress: DeviceAddress) {
|
||||
deviceAddressMap[Pair.of(DeviceId(deviceAddress.deviceId), deviceAddress.address)] = deviceAddress
|
||||
deviceAddressSupplier.onNewDeviceAddressAcquired(deviceAddress)
|
||||
}
|
||||
|
||||
fun newDeviceAddressSupplier(): DeviceAddressSupplier {
|
||||
updateAddressesBg()
|
||||
return deviceAddressSupplier
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
if (!isClosed) {
|
||||
isClosed = true
|
||||
localDiscoveryHandler.close()
|
||||
globalDiscoveryHandler.close()
|
||||
executorService.shutdown()
|
||||
executorService.awaitTerminationSafe()
|
||||
}
|
||||
}
|
||||
|
||||
fun registerMessageFromUnknownDeviceListener(listener: (DeviceId) -> Unit) {
|
||||
onMessageFromUnknownDeviceListeners.add(listener)
|
||||
}
|
||||
|
||||
fun unregisterMessageFromUnknownDeviceListener(listener: (DeviceId) -> Unit) {
|
||||
onMessageFromUnknownDeviceListeners.remove(listener)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Davide Imbriaco
|
||||
*
|
||||
* This Java file is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package net.syncthing.java.discovery
|
||||
|
||||
import net.syncthing.java.core.beans.DeviceAddress
|
||||
import net.syncthing.java.core.beans.DeviceId
|
||||
import net.syncthing.java.core.configuration.Configuration
|
||||
import net.syncthing.java.core.security.KeystoreHandler
|
||||
import net.syncthing.java.discovery.protocol.GlobalDiscoveryHandler
|
||||
import net.syncthing.java.discovery.protocol.LocalDiscoveryHandler
|
||||
import org.apache.commons.cli.DefaultParser
|
||||
import org.apache.commons.cli.HelpFormatter
|
||||
import org.apache.commons.cli.Option
|
||||
import org.apache.commons.cli.Options
|
||||
import org.apache.commons.io.FileUtils
|
||||
import java.io.File
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
class Main {
|
||||
|
||||
companion object {
|
||||
|
||||
private const val MAX_WAIT = 60 * 1000
|
||||
|
||||
@JvmStatic
|
||||
fun main(args: Array<String>) {
|
||||
val options = generateOptions()
|
||||
val parser = DefaultParser()
|
||||
val cmd = parser.parse(options, args)
|
||||
if (cmd.hasOption("h")) {
|
||||
val formatter = HelpFormatter()
|
||||
formatter.printHelp("s-client", options)
|
||||
return
|
||||
}
|
||||
val configuration = if (cmd.hasOption("C")) Configuration(File(cmd.getOptionValue("C")))
|
||||
else Configuration()
|
||||
|
||||
val main = Main()
|
||||
cmd.options.forEach { main.handleOption(it, configuration) }
|
||||
}
|
||||
|
||||
private fun generateOptions(): Options {
|
||||
val options = Options()
|
||||
options.addOption("C", "set-config", true, "set config file for s-client")
|
||||
options.addOption("q", "query", true, "query directory server for device id")
|
||||
options.addOption("d", "discovery", true, "discovery local network for device id")
|
||||
options.addOption("h", "help", false, "print help")
|
||||
return options
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleOption(option: Option, configuration: Configuration) {
|
||||
when (option.opt) {
|
||||
"q" -> {
|
||||
val deviceId = DeviceId(option.value)
|
||||
System.out.println("query device id = $deviceId")
|
||||
val latch = CountDownLatch(1)
|
||||
GlobalDiscoveryHandler(configuration).query(deviceId, { it ->
|
||||
val addresses = it.map { it.address }.fold("", { l, r -> "$l\n$r"})
|
||||
System.out.println("server response: $addresses")
|
||||
latch.countDown()
|
||||
})
|
||||
latch.await()
|
||||
}
|
||||
"d" -> {
|
||||
val deviceId = DeviceId(option.value)
|
||||
System.out.println("discovery device id = $deviceId")
|
||||
val deviceAddresses = queryLocalDiscovery(configuration, deviceId)
|
||||
System.out.println("local response = $deviceAddresses")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun queryLocalDiscovery(configuration: Configuration, deviceId: DeviceId): Collection<DeviceAddress> {
|
||||
val lock = Object()
|
||||
val discoveredAddresses = mutableListOf<DeviceAddress>()
|
||||
val handler = LocalDiscoveryHandler(configuration, { discoveredDeviceId, deviceAddresses ->
|
||||
synchronized(lock) {
|
||||
if (discoveredDeviceId == deviceId) {
|
||||
discoveredAddresses.addAll(deviceAddresses)
|
||||
lock.notify()
|
||||
}
|
||||
}
|
||||
})
|
||||
handler.startListener()
|
||||
handler.sendAnnounceMessage()
|
||||
synchronized(lock) {
|
||||
try {
|
||||
lock.wait(MAX_WAIT.toLong())
|
||||
} catch (ex: InterruptedException) {
|
||||
System.out.println(ex)
|
||||
}
|
||||
handler.close()
|
||||
return discoveredAddresses
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user