Skip to content

Commit

Permalink
file renaming and doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyofrancis committed Jun 6, 2018
1 parent 5accbe6 commit 0364aa3
Show file tree
Hide file tree
Showing 27 changed files with 540 additions and 463 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fun getFetchFileServerHostAddress(url: String): String {
return url.substring(firstIndexOfDoubleSep + 2, colonIndex)
}

fun getContentFileIdFromUrl(url: String): String {
fun getFileResourceIdFromUrl(url: String): String {
return Uri.parse(url).lastPathSegment
}

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

/** Object used to monitor work interruption. */
interface InterruptMonitor {

val isInterrupted: Boolean
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
package com.tonyodev.fetch2fileserver

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

/**
* Delegate that can be attached to a Fetch File Server instance to take certain actions
* on a FileRequested by the client.
* */
abstract class AbstractFetchFileServerDelegate : FetchFileServerDelegate {

/** Called when a client is successfully connected to the Fetch File Server and
* the request has been authorized.
* @param client Client identifier
* @param fileRequest File request by the client
* */
override fun onClientConnected(client: String, fileRequest: FileRequest) {

}

/**
* Called when a client provides custom data that the file server device can used or act on.
* @param client Client identifier
* @param customData Custom data
* @param fileRequest File request by the client
* */
override fun onClientDidProvideCustomData(client: String, customData: String, fileRequest: FileRequest) {

}

/** Called when a client disconnects from the Fetch File Server.
* @param client Client identifier
* */
override fun onClientDisconnected(client: String) {

}

override fun getFileInputStream(contentFile: ContentFile, fileOffset: Long): InputStream? {
/** Called when the Fetch File Server needs to provide the client the requested file input stream.
* If null is returned, Fetch File Server will provide the InputSteam. Use this method if you need
* to provide a custom InputStream. For example an encrypted input stream.
* @param fileResource Resource File that Fetch file Server will provide the client.
* @param fileOffset The offset reading will begin from. Use this value to seek to the right
* offset position. Note: Not seeking to the right position will cause the server to send
* invalid data to the client.
* @return file input stream. Can be null.
* */
override fun getFileInputStream(fileResource: FileResource, fileOffset: Long): InputStream? {
return null
}

/** Called if the client requested a custom request that the Fetch File Server cannot
* serve. Use this callback to provide a custom response.
* @param client Client identifier
* @param fileRequest File request by the client
* @param fileResourceTransporterWriter Writer used to transport byte data to the requesting client.
* @param interruptMonitor used this object to monitor interruption.
* A request may have been cancelled by the server because it is shutting down or the client
* has closed the connection.
* **/
override fun onCustomRequest(client: String, fileRequest: FileRequest,
contentFileTransporterWriter: ContentFileTransporterWriter, interruptMonitor: InterruptMonitor) {
fileResourceTransporterWriter: FileResourceTransporterWriter, interruptMonitor: InterruptMonitor) {

}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ interface FetchFileServer {

fun setTransferProgressListener(fetchTransferProgressListener: FetchTransferProgressListener?)

fun addContentFile(contentFile: ContentFile)
fun addFileResource(fileResource: FileResource)

fun addContentFiles(contentFiles: Collection<ContentFile>)
fun addFileResources(fileResources: Collection<FileResource>)

fun removeContentFile(contentFile: ContentFile)
fun removeFileResource(fileResource: FileResource)

fun removeContentFiles(contentFiles: Collection<ContentFile>)
fun removeFileResources(fileResources: Collection<FileResource>)

fun removeAllContentFiles()
fun removeAllFileResources()

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

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

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

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

class Builder(private val context: Context) {

Expand All @@ -49,7 +49,7 @@ interface FetchFileServer {
private var fileServerAuthenticator: FetchFileServerAuthenticator? = null
private var fileServerDelegate: FetchFileServerDelegate? = null
private var transferProgressListener: FetchTransferProgressListener? = null
private var contentFileDatabaseName = "LibFetchFileServerDatabaseLib.db"
private var fileResourceDatabaseName = "LibFetchFileServerDatabaseLib.db"

fun setServerSocket(serverSocket: ServerSocket): Builder {
this.serverSocket = serverSocket
Expand Down Expand Up @@ -83,21 +83,21 @@ interface FetchFileServer {

fun setFileServerDatabaseName(databaseName: String): Builder {
if (databaseName.isNotEmpty()) {
this.contentFileDatabaseName = databaseName
this.fileResourceDatabaseName = databaseName
}
return this
}

fun build(): FetchFileServer {
val fetchContentFileServer = FetchFileServerImpl(context = context.applicationContext,
val fetchFileServer = FetchFileServerImpl(context = context.applicationContext,
serverSocket = serverSocket,
clearContentFileDatabaseOnShutdown = clearDatabaseOnShutdown,
clearFileResourcesDatabaseOnShutdown = clearDatabaseOnShutdown,
logger = logger,
databaseName = contentFileDatabaseName)
fetchContentFileServer.setAuthenticator(fileServerAuthenticator)
fetchContentFileServer.setDelegate(fileServerDelegate)
fetchContentFileServer.setTransferProgressListener(transferProgressListener)
return fetchContentFileServer
databaseName = fileResourceDatabaseName)
fetchFileServer.setAuthenticator(fileServerAuthenticator)
fetchFileServer.setDelegate(fileServerDelegate)
fetchFileServer.setTransferProgressListener(transferProgressListener)
return fetchFileServer
}

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

/** Used to authenticate clients trying to connect to the Fetch File Server
* instance this authenticator instance is attached to.*/
interface FetchFileServerAuthenticator {

/** Method called when a client is attempting to connect to the Fetch File Server.
* @param authorization the authorization token
* @param fileRequest the fileRequest the client sent.
* @return true if the authorize token is accepted. False otherwise.
* */
fun accept(authorization: String, fileRequest: FileRequest): Boolean

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,56 @@
package com.tonyodev.fetch2fileserver

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

/**
* Delegate that can be attached to a Fetch File Server instance to take certain actions
* on a FileRequested by the client.
* */
interface FetchFileServerDelegate {

/** Called when a client is successfully connected to the Fetch File Server and
* the request has been authorized.
* @param client Client identifier
* @param fileRequest File request by the client
* */
fun onClientConnected(client: String, fileRequest: FileRequest)

/**
* Called when a client provides custom data that the file server device can used or act on.
* @param client Client identifier
* @param customData Custom data
* @param fileRequest File request by the client
* */
fun onClientDidProvideCustomData(client: String, customData: String, fileRequest: FileRequest)

/** Called when a client disconnects from the Fetch File Server.
* @param client Client identifier
* */
fun onClientDisconnected(client: String)

fun getFileInputStream(contentFile: ContentFile, fileOffset: Long): InputStream?

/** Called when the Fetch File Server needs to provide the client the requested file input stream.
* If null is returned, Fetch File Server will provide the InputSteam. Use this method if you need
* to provide a custom InputStream. For example an encrypted input stream.
* @param fileResource Resource File that Fetch file Server will provide the client.
* @param fileOffset The offset reading will begin from. Use this value to seek to the right
* offset position. Note: Not seeking to the right position will cause the server to send
* invalid data to the client.
* @return file input stream. Can be null.
* */
fun getFileInputStream(fileResource: FileResource, fileOffset: Long): InputStream?

/** Called if the client requested a custom request that the Fetch File Server cannot
* serve. Use this callback to provide a custom response.
* @param client Client identifier
* @param fileRequest File request by the client
* @param fileResourceTransporterWriter Writer used to transport byte data to the requesting client.
* @param interruptMonitor used this object to monitor interruption.
* A request may have been cancelled by the server because it is shutting down or the client
* has closed the connection.
* **/
fun onCustomRequest(client: String, fileRequest: FileRequest,
contentFileTransporterWriter: ContentFileTransporterWriter, interruptMonitor: InterruptMonitor)
fileResourceTransporterWriter: FileResourceTransporterWriter, interruptMonitor: InterruptMonitor)

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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.FetchContentFileTransporter
import com.tonyodev.fetch2fileserver.transporter.FetchFileResourceTransporter

import java.io.OutputStream
import java.net.HttpURLConnection
Expand All @@ -27,7 +27,7 @@ open class FetchFileServerDownloader @JvmOverloads constructor(
/** The timeout value in milliseconds when trying to connect to the server. Default is 20_000 milliseconds. */
private val timeout: Long = 20_000) : FileServerDownloader {

protected val connections: MutableMap<Downloader.Response, FetchContentFileTransporter> = Collections.synchronizedMap(HashMap<Downloader.Response, FetchContentFileTransporter>())
protected val connections: MutableMap<Downloader.Response, FetchFileResourceTransporter> = Collections.synchronizedMap(HashMap<Downloader.Response, FetchFileResourceTransporter>())

override fun execute(request: Downloader.ServerRequest, interruptMonitor: InterruptMonitor?): Downloader.Response? {
val headers = request.headers
Expand All @@ -36,13 +36,13 @@ open class FetchFileServerDownloader @JvmOverloads constructor(
val port = getFetchFileServerPort(request.url)
val address = getFetchFileServerHostAddress(request.url)
val inetSocketAddress = InetSocketAddress(address, port)
val transporter = FetchContentFileTransporter()
val transporter = FetchFileResourceTransporter()
var timeoutStop: Long
val timeoutStart = System.nanoTime()
transporter.connect(inetSocketAddress)
val contentFileRequest = FileRequest(
val fileRequest = FileRequest(
type = TYPE_FILE,
contentFileId = getContentFileIdFromUrl(request.url),
fileResourceId = getFileResourceIdFromUrl(request.url),
rangeStart = range.first,
rangeEnd = range.second,
authorization = authorization,
Expand All @@ -51,9 +51,9 @@ open class FetchFileServerDownloader @JvmOverloads constructor(
page = headers[FileRequest.FIELD_PAGE]?.toIntOrNull() ?: 0,
size = headers[FileRequest.FIELD_SIZE]?.toIntOrNull() ?: 0,
persistConnection = false)
transporter.sendContentFileRequest(contentFileRequest)
transporter.sendFileRequest(fileRequest)
while (interruptMonitor?.isInterrupted == false) {
val serverResponse = transporter.receiveContentFileResponse()
val serverResponse = transporter.receiveFileResponse()
if (serverResponse != null) {
val code = serverResponse.status
val isSuccessful = serverResponse.connection == FileResponse.OPEN_CONNECTION &&
Expand All @@ -80,7 +80,7 @@ open class FetchFileServerDownloader @JvmOverloads constructor(

override fun disconnect(response: Downloader.Response) {
if (connections.contains(response)) {
val transporter = connections[response] as FetchContentFileTransporter
val transporter = connections[response] as FetchFileResourceTransporter
connections.remove(response)
transporter.close()
}
Expand Down
Loading

0 comments on commit 0364aa3

Please sign in to comment.