1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 02:39:23 +02:00

use server 'all.hashes' method

This commit is contained in:
Nikolay Pultsin 2014-06-29 16:54:51 +01:00
parent 307f6a9abd
commit a5c4efbcc0

View file

@ -42,6 +42,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
private final List<Book> myQueue = Collections.synchronizedList(new LinkedList<Book>());
private final Set<Book> myProcessed = new HashSet<Book>();
private final Set<String> myHashesFromServer = new HashSet<String>();
@Override
public IBinder onBind(Intent intent) {
@ -63,12 +64,23 @@ public class SynchroniserService extends Service implements IBookCollection.List
if (ourSynchronizationThread == null) {
ourSynchronizationThread = new Thread() {
public void run() {
System.err.println("HELLO THREAD");
try {
ourSynchronizationThread.sleep(5000);
} catch (InterruptedException e) {
System.err.println("HELLO THREAD");
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()) {
final List<Book> books = myCollection.books(q);
if (books.isEmpty()) {
@ -87,9 +99,11 @@ public class SynchroniserService extends Service implements IBookCollection.List
System.err.println("Processing " + book.getTitle() + " [" + book.File.getPath() + "]");
uploadBookToServer(book.File.getPhysicalFile());
}
} finally {
System.err.println("BYE-BYE THREAD");
ourSynchronizationThread = null;
}
}
};
ourSynchronizationThread.setPriority(Thread.MIN_PRIORITY);
ourSynchronizationThread.start();
@ -128,7 +142,7 @@ public class SynchroniserService extends Service implements IBookCollection.List
protected abstract void processResponse(Object response);
}
private static final class UploadRequest extends ZLNetworkRequest.FileUpload {
private final class UploadRequest extends ZLNetworkRequest.FileUpload {
UploadRequest(File file) {
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());
String id = null;
List<String> hashes = null;
String error = null;
try {
if (response instanceof Map) {
id = (String)((Map)response).get("id");
hashes = (List<String>)((Map)response).get("hashes");
error = (String)((Map)response).get("error");
}
} catch (Exception e) {
@ -154,8 +170,9 @@ public class SynchroniserService extends Service implements IBookCollection.List
}
if (error != null) {
System.err.println("UPLOAD FAILURE: " + error);
} else if (id != null) {
} else if (id != null && hashes != null) {
System.err.println("UPLOADED SUCCESSFULLY: " + id);
myHashesFromServer.addAll(hashes);
} else {
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");
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 JsonRequest verificationRequest =
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 Object response = result.get("result");
try {
final List responseList = (List)response;
final List<Map<String,Object>> responseList = (List<Map<String,Object>>)response;
if (responseList.isEmpty()) {
try {
final UploadRequest uploadRequest = new UploadRequest(file.javaFile());
@ -195,8 +216,10 @@ public class SynchroniserService extends Service implements IBookCollection.List
e.printStackTrace();
}
} else {
final Map<String,String> firstBookInfo = (Map<String,String>)responseList.get(0);
System.err.println("BOOK ALREADY UPLOADED: " + firstBookInfo.get("id"));
for (Map<String,Object> bookInfo : responseList) {
System.err.println("BOOK ALREADY UPLOADED: " + bookInfo.get("id"));
myHashesFromServer.addAll((List<String>)bookInfo.get("hashes"));
}
}
} catch (Exception e) {
System.err.println("UNEXPECTED RESPONSE: " + response);