Skip to content

add error cache listener #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.danikula.videocache;

import java.io.File;

/**
* Listener for cache error.
*
* @author Sasha Karamyshev ([email protected])
*/
public interface ErrorCacheListener {

void onCacheUnavailable(Throwable throwable);
}
21 changes: 17 additions & 4 deletions library/src/main/java/com/danikula/videocache/HttpProxyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class HttpProxyCache extends ProxyCache {

private final HttpUrlSource source;
private final FileCache cache;
private CacheListener listener;
private CacheListener cacheListener;
private ErrorCacheListener errorListener;

public HttpProxyCache(HttpUrlSource source, FileCache cache) {
super(source, cache);
Expand All @@ -32,7 +33,11 @@ public HttpProxyCache(HttpUrlSource source, FileCache cache) {
}

public void registerCacheListener(CacheListener cacheListener) {
this.listener = cacheListener;
this.cacheListener = cacheListener;
}

public void registerErrorListener(ErrorCacheListener errorCacheListener) {
this.errorListener = errorCacheListener;
}

public void processRequest(GetRequest request, Socket socket) throws IOException, ProxyCacheException {
Expand Down Expand Up @@ -105,8 +110,16 @@ private String format(String pattern, Object... args) {

@Override
protected void onCachePercentsAvailableChanged(int percents) {
if (listener != null) {
listener.onCacheAvailable(cache.file, source.getUrl(), percents);
if (cacheListener != null) {
cacheListener.onCacheAvailable(cache.file, source.getUrl(), percents);
}
}

@Override
protected void onErrorCache(Throwable throwable) {
super.onErrorCache(throwable);
if (errorListener != null) {
errorListener.onCacheUnavailable(throwable);
}
}
}
13 changes: 12 additions & 1 deletion library/src/main/java/com/danikula/videocache/ProxyCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailab
}
}

private void notifyCacheError(Throwable throwable){
onErrorCache(throwable);
}

protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
boolean zeroLengthSource = sourceLength == 0;
int percents = zeroLengthSource ? 100 : (int) ((float) cacheAvailable / sourceLength * 100);
Expand All @@ -115,6 +119,10 @@ protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
protected void onCachePercentsAvailableChanged(int percentsAvailable) {
}

protected void onErrorCache(Throwable throwable) {
// do nothing
}

private void readSource() {
long sourceAvailable = -1;
long offset = 0;
Expand All @@ -139,9 +147,12 @@ private void readSource() {
} catch (Throwable e) {
readSourceErrorsCount.incrementAndGet();
onError(e);
notifyCacheError(e);
} finally {
closeSource();
notifyNewCacheDataAvailable(offset, sourceAvailable);
synchronized (wc) {
wc.notifyAll();
}
}
}

Expand Down