-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from obermillerk/main
Add stream variants for download and upload in FsManager
- Loading branch information
Showing
9 changed files
with
443 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/StreamDownload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package io.runtime.mcumgr.transfer; | ||
|
||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
import io.runtime.mcumgr.McuMgrErrorCode; | ||
import io.runtime.mcumgr.exception.McuMgrErrorException; | ||
import io.runtime.mcumgr.exception.McuMgrException; | ||
import io.runtime.mcumgr.response.DownloadResponse; | ||
import io.runtime.mcumgr.response.McuMgrResponse; | ||
|
||
@SuppressWarnings("unused") | ||
public abstract class StreamDownload extends StreamTransfer { | ||
|
||
@NotNull | ||
private final OutputStream mDataOutput; | ||
|
||
@Nullable | ||
private final StreamDownloadCallback mCallback; | ||
|
||
protected StreamDownload(@NotNull OutputStream dataOutput) { | ||
this(dataOutput, null); | ||
} | ||
|
||
protected StreamDownload( | ||
@NotNull OutputStream dataOutput, | ||
@Nullable StreamDownloadCallback callback | ||
) { | ||
mDataOutput = dataOutput; | ||
mCallback = callback; | ||
} | ||
|
||
/** | ||
* Sends read request from given offset. | ||
* | ||
* @param offset the offset. | ||
* @return received response. | ||
* @throws McuMgrException a reason of a failure. | ||
*/ | ||
protected abstract DownloadResponse read(int offset) throws McuMgrException; | ||
|
||
@Override | ||
public McuMgrResponse send(int offset) throws McuMgrException { | ||
DownloadResponse response = read(offset); | ||
// Check for a McuManager error. | ||
if (response.rc != 0) { | ||
throw new McuMgrErrorException(McuMgrErrorCode.valueOf(response.rc)); | ||
} | ||
|
||
// The first packet contains the file length. | ||
if (response.off == 0) { | ||
mDataLength = response.len; | ||
} | ||
|
||
// Validate response body | ||
if (response.data == null) { | ||
throw new McuMgrException("Download response data is null."); | ||
} | ||
if (mDataLength < 0) { | ||
throw new McuMgrException("Download size not set."); | ||
} | ||
|
||
try { | ||
mDataOutput.write(response.data); | ||
} catch (IOException e) { | ||
throw new McuMgrException("Download data failed to write to stream.", e); | ||
} | ||
mOffset = response.off + response.data.length; | ||
|
||
return response; | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
mOffset = 0; | ||
mDataLength = -1; | ||
} | ||
|
||
@Override | ||
public void onProgressChanged(int current, int total, long timestamp) { | ||
if (mCallback != null) { | ||
mCallback.onDownloadProgressChanged(current, total, timestamp); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailed(@NotNull McuMgrException e) { | ||
if (mCallback != null) { | ||
mCallback.onDownloadFailed(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
if (mCallback != null) { | ||
mCallback.onDownloadCompleted(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCanceled() { | ||
if (mCallback != null) { | ||
mCallback.onDownloadCanceled(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
mcumgr-core/src/main/java/io/runtime/mcumgr/transfer/StreamDownloadCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.runtime.mcumgr.transfer; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import io.runtime.mcumgr.exception.McuMgrException; | ||
|
||
public interface StreamDownloadCallback { | ||
/** | ||
* Called when a response has been received successfully. | ||
* | ||
* @param current the number of bytes downloaded so far. | ||
* @param total the total size of the download in bytes. | ||
* @param timestamp the timestamp of when the response was received. | ||
*/ | ||
void onDownloadProgressChanged(int current, int total, long timestamp); | ||
|
||
/** | ||
* Called when the download has failed. | ||
* | ||
* @param error the error. See the cause for more info. | ||
*/ | ||
void onDownloadFailed(@NotNull McuMgrException error); | ||
|
||
/** | ||
* Called when the download has been canceled. | ||
*/ | ||
void onDownloadCanceled(); | ||
|
||
/** | ||
* Called when the download has finished successfully. | ||
*/ | ||
void onDownloadCompleted(); | ||
} |
Oops, something went wrong.