mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
use server 'all.hashes' method
This commit is contained in:
parent
307f6a9abd
commit
a5c4efbcc0
1 changed files with 49 additions and 26 deletions
|
@ -42,6 +42,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
|
|
||||||
private final List<Book> myQueue = Collections.synchronizedList(new LinkedList<Book>());
|
private final List<Book> myQueue = Collections.synchronizedList(new LinkedList<Book>());
|
||||||
private final Set<Book> myProcessed = new HashSet<Book>();
|
private final Set<Book> myProcessed = new HashSet<Book>();
|
||||||
|
private final Set<String> myHashesFromServer = new HashSet<String>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
|
@ -63,12 +64,23 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
if (ourSynchronizationThread == null) {
|
if (ourSynchronizationThread == null) {
|
||||||
ourSynchronizationThread = new Thread() {
|
ourSynchronizationThread = new Thread() {
|
||||||
public void run() {
|
public void run() {
|
||||||
System.err.println("HELLO THREAD");
|
|
||||||
try {
|
try {
|
||||||
ourSynchronizationThread.sleep(5000);
|
System.err.println("HELLO THREAD");
|
||||||
} catch (InterruptedException e) {
|
myHashesFromServer.clear();
|
||||||
|
try {
|
||||||
|
myNetworkContext.perform(new JsonRequest("all.hashes", null) {
|
||||||
|
@Override
|
||||||
|
public void processResponse(Object response) {
|
||||||
|
myHashesFromServer.addAll((List)response);
|
||||||
}
|
}
|
||||||
System.err.println("START SYNCRONIZING");
|
});
|
||||||
|
System.err.println("RECEIVED: " + myHashesFromServer.size() + " HASHES");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.err.println("DO NOT SYNCHRONIZE: ALL HASHES REQUEST FAILED");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.err.println("START SYNCRONIZATION");
|
||||||
for (BookQuery q = new BookQuery(new Filter.Empty(), 20);; q = q.next()) {
|
for (BookQuery q = new BookQuery(new Filter.Empty(), 20);; q = q.next()) {
|
||||||
final List<Book> books = myCollection.books(q);
|
final List<Book> books = myCollection.books(q);
|
||||||
if (books.isEmpty()) {
|
if (books.isEmpty()) {
|
||||||
|
@ -87,9 +99,11 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
|
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
|
||||||
uploadBookToServer(book.File.getPhysicalFile());
|
uploadBookToServer(book.File.getPhysicalFile());
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
System.err.println("BYE-BYE THREAD");
|
System.err.println("BYE-BYE THREAD");
|
||||||
ourSynchronizationThread = null;
|
ourSynchronizationThread = null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
ourSynchronizationThread.setPriority(Thread.MIN_PRIORITY);
|
ourSynchronizationThread.setPriority(Thread.MIN_PRIORITY);
|
||||||
ourSynchronizationThread.start();
|
ourSynchronizationThread.start();
|
||||||
|
@ -128,7 +142,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
protected abstract void processResponse(Object response);
|
protected abstract void processResponse(Object response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class UploadRequest extends ZLNetworkRequest.FileUpload {
|
private final class UploadRequest extends ZLNetworkRequest.FileUpload {
|
||||||
UploadRequest(File file) {
|
UploadRequest(File file) {
|
||||||
super(BASE_URL + "book.upload", file, false);
|
super(BASE_URL + "book.upload", file, false);
|
||||||
}
|
}
|
||||||
|
@ -143,10 +157,12 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
}
|
}
|
||||||
final Object response = JSONValue.parse(buffer.toString());
|
final Object response = JSONValue.parse(buffer.toString());
|
||||||
String id = null;
|
String id = null;
|
||||||
|
List<String> hashes = null;
|
||||||
String error = null;
|
String error = null;
|
||||||
try {
|
try {
|
||||||
if (response instanceof Map) {
|
if (response instanceof Map) {
|
||||||
id = (String)((Map)response).get("id");
|
id = (String)((Map)response).get("id");
|
||||||
|
hashes = (List<String>)((Map)response).get("hashes");
|
||||||
error = (String)((Map)response).get("error");
|
error = (String)((Map)response).get("error");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -154,8 +170,9 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
}
|
}
|
||||||
if (error != null) {
|
if (error != null) {
|
||||||
System.err.println("UPLOAD FAILURE: " + error);
|
System.err.println("UPLOAD FAILURE: " + error);
|
||||||
} else if (id != null) {
|
} else if (id != null && hashes != null) {
|
||||||
System.err.println("UPLOADED SUCCESSFULLY: " + id);
|
System.err.println("UPLOADED SUCCESSFULLY: " + id);
|
||||||
|
myHashesFromServer.addAll(hashes);
|
||||||
} else {
|
} else {
|
||||||
System.err.println("UNEXPECED RESPONSE: " + response);
|
System.err.println("UNEXPECED RESPONSE: " + response);
|
||||||
}
|
}
|
||||||
|
@ -168,6 +185,10 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
System.err.println("Failed: SHA-1 checksum not computed");
|
System.err.println("Failed: SHA-1 checksum not computed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (myHashesFromServer.contains(uid.Id.toLowerCase())) {
|
||||||
|
System.err.println("HASH ALREADY IN THE TABLE");
|
||||||
|
return;
|
||||||
|
}
|
||||||
final Map<String,Object> result = new HashMap<String,Object>();
|
final Map<String,Object> result = new HashMap<String,Object>();
|
||||||
final JsonRequest verificationRequest =
|
final JsonRequest verificationRequest =
|
||||||
new JsonRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) {
|
new JsonRequest("books.by.hash", Collections.singletonMap("sha1", uid.Id)) {
|
||||||
|
@ -184,7 +205,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken");
|
final String csrfToken = myNetworkContext.getCookieValue(DOMAIN, "csrftoken");
|
||||||
final Object response = result.get("result");
|
final Object response = result.get("result");
|
||||||
try {
|
try {
|
||||||
final List responseList = (List)response;
|
final List<Map<String,Object>> responseList = (List<Map<String,Object>>)response;
|
||||||
if (responseList.isEmpty()) {
|
if (responseList.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
final UploadRequest uploadRequest = new UploadRequest(file.javaFile());
|
final UploadRequest uploadRequest = new UploadRequest(file.javaFile());
|
||||||
|
@ -195,8 +216,10 @@ public class SynchroniserService extends Service implements IBookCollection.List
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final Map<String,String> firstBookInfo = (Map<String,String>)responseList.get(0);
|
for (Map<String,Object> bookInfo : responseList) {
|
||||||
System.err.println("BOOK ALREADY UPLOADED: " + firstBookInfo.get("id"));
|
System.err.println("BOOK ALREADY UPLOADED: " + bookInfo.get("id"));
|
||||||
|
myHashesFromServer.addAll((List<String>)bookInfo.get("hashes"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("UNEXPECTED RESPONSE: " + response);
|
System.err.println("UNEXPECTED RESPONSE: " + response);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue