Skip to content

Commit

Permalink
file server refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Jun 6, 2018
1 parent 0364aa3 commit 95e5d3d
Show file tree
Hide file tree
Showing 23 changed files with 395 additions and 210 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonyodev.fetch2fileserver

import com.tonyodev.fetch2.util.InterruptMonitor
import com.tonyodev.fetch2fileserver.transporter.FileRequest
import com.tonyodev.fetch2fileserver.transporter.FileResourceTransporterWriter
import java.io.InputStream

Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,90 @@
package com.tonyodev.fetch2fileserver

import android.content.Context
import com.tonyodev.fetch2.Func
import java.net.ServerSocket

/** A lightweight TCP File Server that acts like an HTTP file server
* designed specifically for Android to distribute files from on device to another.
* The Fetch File Server works in hand with Fetch to download file resources.*/
interface FetchFileServer {

/** File Server Unique id.*/
val id: String

/** The port the File server is listening to file requests on.
* The port id is available after the server has started.
* */
val port: Int

/** The file server IP address. The IP address is available after the server has started. */
val address: String

/** Indicates if the File Server is shutdown.*/
val isShutDown: Boolean

/** Starts the file server. File Server beings to listen for requests on the designated port.*/
fun start()

/** Shuts down the file server. The File server will no longer accept request or allow
* file resources to be attached. Once called any method call to the instance will throw
* an exception indicating that the instance is no longer available for use.
* @param forced forces the file server to cancel active requests that are being served to
* any connected clients and terminates all operations and connections.
* */
fun shutDown(forced: Boolean = false)

fun setAuthenticator(fetchFileServerAuthenticator: FetchFileServerAuthenticator?)

fun setDelegate(fetchFileServerDelegate: FetchFileServerDelegate?)

fun setTransferProgressListener(fetchTransferProgressListener: FetchTransferProgressListener?)

/** Adds a FileResource that can be served to requesting clients.
* @param fileResource a file resource.
* */
fun addFileResource(fileResource: FileResource)

/** Adds a list of FileResource that can be served to requesting clients.
* @param fileResources a list file resource.
* */
fun addFileResources(fileResources: Collection<FileResource>)

/** Removes a fileResource from the instance of the File Server.
*@param fileResource file resource
* */
fun removeFileResource(fileResource: FileResource)

/** Removes a list of FileResource from the File Server instance.
* @param fileResources a list file resource.
* */
fun removeFileResources(fileResources: Collection<FileResource>)

/** Removes all FileResources managed by this instance.*/
fun removeAllFileResources()

fun containsFileResource(fileResourceId: Int, callback: (Boolean) -> Unit)

fun getFileResources(callback: (List<FileResource>) -> Unit)

fun getFullCatalog(callback: (String) -> Unit)

fun getFileResource(fileResourceId: Int, callback: (FileResource?) -> Unit)

class Builder(private val context: Context) {
/** Checks if a File Resource is managed by this instance.
* @param fileResourceId file resource id
* @param func callback the result will be returned on. True if the file resource
* is being managed. False otherwise.
* */
fun containsFileResource(fileResourceId: Long, func: Func<Boolean>)

/** Gets a list of all File Resources managed by this File Server instance.
* @param func callback the result is returned on.
* */
fun getFileResources(func: Func<List<FileResource>>)

/** Gets the Catalog(All File Resources) managed by this File Server instances
* as JSON. The FileResources `file` field is excluded.
* @param func callback the result will be returned on.
* */
fun getCatalog(func: Func<String>)

/** Queries the File Server instance for a managed file resource if it exist.
* @param fileResourceId file resource id
* @param func callback the result will be returned on. Result maybe null.
* */
fun getFileResource(fileResourceId: Long, func: Func<FileResource?>)

/** Creates an instance of FetchFileServer.*/
class Builder(
/** context*/
private val context: Context) {

private var serverSocket = ServerSocket(0)
private var clearDatabaseOnShutdown = false
Expand All @@ -51,53 +94,86 @@ interface FetchFileServer {
private var transferProgressListener: FetchTransferProgressListener? = null
private var fileResourceDatabaseName = "LibFetchFileServerDatabaseLib.db"

/** Set Custom Server Socket
* @param serverSocket
* @return builder
* */
fun setServerSocket(serverSocket: ServerSocket): Builder {
this.serverSocket = serverSocket
return this
}

/** Clears the File Server FileResources database on shut down
* @param clear Default is false
* @return builder
* */
fun setClearDatabaseOnShutdown(clear: Boolean): Builder {
this.clearDatabaseOnShutdown = clear
return this
}

/** Set File Server Logger.
* @param logger Default. Uses the default logger which is enabled
* @return builder
* */
fun setLogger(logger: FetchFileServerLogger): Builder {
this.logger = logger
return this
}

/** Set authenticator object.
*@param fetchFileServerAuthenticator Default is null. If Null, the file server will
* accept all requests.
* @return builder
* */
fun setAuthenticator(fetchFileServerAuthenticator: FetchFileServerAuthenticator): Builder {
this.fileServerAuthenticator = fetchFileServerAuthenticator
return this
}

/** Set Delegate
* @param fetchFileServerDelegate Default is null. If Null, the file server will
* attempt to handle custom requests and actions.
* @return builder
* */
fun setDelegate(fetchFileServerDelegate: FetchFileServerDelegate): Builder {
this.fileServerDelegate = fetchFileServerDelegate
return this
}

/** Set Transfer Progress Listener
* @param fetchTransferProgressListener Default is null.
* @return builder
* */
fun setTransferProgressListener(fetchTransferProgressListener: FetchTransferProgressListener): Builder {
this.transferProgressListener = fetchTransferProgressListener
return this
}

/** Set default Database name the File Server instance will use. This is used to store
* FileResource information that can later be retrieved by the File Server.
* @param databaseName Default is LibFetchFileServerDatabaseLib.db
* @return builder
* */
fun setFileServerDatabaseName(databaseName: String): Builder {
if (databaseName.isNotEmpty()) {
this.fileResourceDatabaseName = databaseName
}
return this
}

/** Build the FetchFileServer Instance.
* @return new Fetch File Server instance.
* */
fun build(): FetchFileServer {
val fetchFileServer = FetchFileServerImpl(context = context.applicationContext,
return FetchFileServerImpl(context = context.applicationContext,
serverSocket = serverSocket,
clearFileResourcesDatabaseOnShutdown = clearDatabaseOnShutdown,
logger = logger,
databaseName = fileResourceDatabaseName)
fetchFileServer.setAuthenticator(fileServerAuthenticator)
fetchFileServer.setDelegate(fileServerDelegate)
fetchFileServer.setTransferProgressListener(transferProgressListener)
return fetchFileServer
databaseName = fileResourceDatabaseName,
fetchFileServerAuthenticator = fileServerAuthenticator,
fetchFileServerDelegate = fileServerDelegate,
fetchTransferProgressListener = transferProgressListener)
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tonyodev.fetch2fileserver

import com.tonyodev.fetch2fileserver.transporter.FileRequest

/** Used to authenticate clients trying to connect to the Fetch File Server
* instance this authenticator instance is attached to.*/
interface FetchFileServerAuthenticator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonyodev.fetch2fileserver

import com.tonyodev.fetch2.util.InterruptMonitor
import com.tonyodev.fetch2fileserver.transporter.FileRequest
import com.tonyodev.fetch2fileserver.transporter.FileResourceTransporterWriter
import java.io.InputStream

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.tonyodev.fetch2fileserver
import com.tonyodev.fetch2.Downloader
import com.tonyodev.fetch2.FileServerDownloader
import com.tonyodev.fetch2.util.*
import com.tonyodev.fetch2fileserver.FileRequest.Companion.TYPE_FILE
import com.tonyodev.fetch2fileserver.transporter.FileRequest.Companion.TYPE_FILE
import com.tonyodev.fetch2fileserver.transporter.FetchFileResourceTransporter
import com.tonyodev.fetch2fileserver.transporter.FileRequest
import com.tonyodev.fetch2fileserver.transporter.FileResponse

import java.io.OutputStream
import java.net.HttpURLConnection
Expand Down Expand Up @@ -46,10 +48,14 @@ open class FetchFileServerDownloader @JvmOverloads constructor(
rangeStart = range.first,
rangeEnd = range.second,
authorization = authorization,
client = headers[FileRequest.FIELD_CLIENT] ?: UUID.randomUUID().toString(),
customData = headers[FileRequest.FIELD_CUSTOM_DATA] ?: "",
page = headers[FileRequest.FIELD_PAGE]?.toIntOrNull() ?: 0,
size = headers[FileRequest.FIELD_SIZE]?.toIntOrNull() ?: 0,
client = headers[FileRequest.FIELD_CLIENT]
?: UUID.randomUUID().toString(),
customData = headers[FileRequest.FIELD_CUSTOM_DATA]
?: "",
page = headers[FileRequest.FIELD_PAGE]?.toIntOrNull()
?: 0,
size = headers[FileRequest.FIELD_SIZE]?.toIntOrNull()
?: 0,
persistConnection = false)
transporter.sendFileRequest(fileRequest)
while (interruptMonitor?.isInterrupted == false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.tonyodev.fetch2fileserver

import com.tonyodev.fetch2.Func

/** Checks if a File Resource is managed by this instance.
* @param fileResourceId file resource id
* @param callback callback the result will be returned on. True if the file resource
* is being managed. False otherwise.
* */
fun FetchFileServer.containsFileResource(fileResourceId: Long, callback: (Boolean) -> Unit) {
containsFileResource(fileResourceId, object : Func<Boolean> {
override fun call(t: Boolean) {
callback(t)
}
})
}

/** Gets a list of all File Resources managed by this File Server instance.
* @param callback callback the result is returned on.
* */
fun FetchFileServer.getFileResources(callback: (List<FileResource>) -> Unit) {
getFileResources(object : Func<List<FileResource>> {
override fun call(t: List<FileResource>) {
callback(t)
}
})
}

/** Gets the Catalog(All File Resources) managed by this File Server instances
* as JSON. The FileResources `file` field is excluded.
* @param callback callback the result will be returned on.
* */
fun FetchFileServer.getCatalog(callback: (String) -> Unit) {
getCatalog(object : Func<String> {
override fun call(t: String) {
callback(t)
}
})
}

/** Queries the File Server instance for a managed file resource if it exist.
* @param fileResourceId file resource id
* @param callback callback the result will be returned on. Result maybe null.
* */
fun FetchFileServer.getFileResource(fileResourceId: Long, callback: (FileResource?) -> Unit) {
getFileResource(fileResourceId, object : Func<FileResource?> {
override fun call(t: FileResource?) {
callback(t)
}
})
}
Loading

0 comments on commit 95e5d3d

Please sign in to comment.